]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
fscrypt: new helper function - fscrypt_prepare_link()
authorEric Biggers <ebiggers@google.com>
Mon, 9 Oct 2017 19:15:41 +0000 (12:15 -0700)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 18 Oct 2017 23:52:38 +0000 (19:52 -0400)
Introduce a helper function which prepares to link an inode into a
possibly-encrypted directory.  It handles setting up the target
directory's encryption key, then verifying that the link won't violate
the constraint that all files in an encrypted directory tree use the
same encryption policy.

Acked-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/crypto/hooks.c
include/linux/fscrypt.h
include/linux/fscrypt_notsupp.h
include/linux/fscrypt_supp.h

index 069088e91ea991d3ef3e8d0143e598d9ff7ae047..8b90217320dd121edaf7feb6042d013538062778 100644 (file)
@@ -47,3 +47,18 @@ int fscrypt_file_open(struct inode *inode, struct file *filp)
        return err;
 }
 EXPORT_SYMBOL_GPL(fscrypt_file_open);
+
+int __fscrypt_prepare_link(struct inode *inode, struct inode *dir)
+{
+       int err;
+
+       err = fscrypt_require_key(dir);
+       if (err)
+               return err;
+
+       if (!fscrypt_has_permitted_context(dir, inode))
+               return -EPERM;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
index b3e2a5f93415ab47424e3b7daca38737a6e67459..9c9a53f99327f2c9387942d56defb38625bb2b78 100644 (file)
@@ -177,4 +177,31 @@ static inline int fscrypt_require_key(struct inode *inode)
        return 0;
 }
 
+/**
+ * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
+ * @old_dentry: an existing dentry for the inode being linked
+ * @dir: the target directory
+ * @dentry: negative dentry for the target filename
+ *
+ * A new link can only be added to an encrypted directory if the directory's
+ * encryption key is available --- since otherwise we'd have no way to encrypt
+ * the filename.  Therefore, we first set up the directory's encryption key (if
+ * not already done) and return an error if it's unavailable.
+ *
+ * We also verify that the link will not violate the constraint that all files
+ * in an encrypted directory tree use the same encryption policy.
+ *
+ * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
+ * -EPERM if the link would result in an inconsistent encryption policy, or
+ * another -errno code.
+ */
+static inline int fscrypt_prepare_link(struct dentry *old_dentry,
+                                      struct inode *dir,
+                                      struct dentry *dentry)
+{
+       if (IS_ENCRYPTED(dir))
+               return __fscrypt_prepare_link(d_inode(old_dentry), dir);
+       return 0;
+}
+
 #endif /* _LINUX_FSCRYPT_H */
index 162da6517ac49ff15a757dd05fa577ebb6b3c7c9..d7d1039eb6b5499212e4b7310f0e67b81050aa37 100644 (file)
@@ -186,4 +186,10 @@ static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
        return 0;
 }
 
+static inline int __fscrypt_prepare_link(struct inode *inode,
+                                        struct inode *dir)
+{
+       return -EOPNOTSUPP;
+}
+
 #endif /* _LINUX_FSCRYPT_NOTSUPP_H */
index fd2f6decaee4f496193da94884afe2fe0d0b1a62..80706283da757f9976d52cbea9ec26fe6a201646 100644 (file)
@@ -145,5 +145,6 @@ extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
 
 /* hooks.c */
 extern int fscrypt_file_open(struct inode *inode, struct file *filp);
+extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir);
 
 #endif /* _LINUX_FSCRYPT_SUPP_H */