VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
457c899653991 (Thomas Gleixner  2019-05-19 13:08:55 +0100   1) // SPDX-License-Identifier: GPL-2.0-only
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700   2) /*
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700   3)  * fs/crypto/hooks.c
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700   4)  *
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700   5)  * Encryption hooks for higher-level filesystem operations.
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700   6)  */
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700   7) 
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800   8) #include <linux/key.h>
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800   9) 
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  10) #include "fscrypt_private.h"
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  11) 
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  12) /**
d2fe97545a1e2 (Eric Biggers     2020-05-11 12:13:56 -0700  13)  * fscrypt_file_open() - prepare to open a possibly-encrypted regular file
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  14)  * @inode: the inode being opened
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  15)  * @filp: the struct file being set up
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  16)  *
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  17)  * Currently, an encrypted regular file can only be opened if its encryption key
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  18)  * is available; access to the raw encrypted contents is not supported.
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  19)  * Therefore, we first set up the inode's encryption key (if not already done)
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  20)  * and return an error if it's unavailable.
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  21)  *
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  22)  * We also verify that if the parent directory (from the path via which the file
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  23)  * is being opened) is encrypted, then the inode being opened uses the same
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  24)  * encryption policy.  This is needed as part of the enforcement that all files
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  25)  * in an encrypted directory tree use the same encryption policy, as a
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  26)  * protection against certain types of offline attacks.  Note that this check is
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  27)  * needed even when opening an *unencrypted* file, since it's forbidden to have
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  28)  * an unencrypted file in an encrypted directory.
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  29)  *
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  30)  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  31)  */
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  32) int fscrypt_file_open(struct inode *inode, struct file *filp)
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  33) {
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  34) 	int err;
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  35) 	struct dentry *dir;
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  36) 
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  37) 	err = fscrypt_require_key(inode);
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  38) 	if (err)
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  39) 		return err;
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  40) 
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  41) 	dir = dget_parent(file_dentry(filp));
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  42) 	if (IS_ENCRYPTED(d_inode(dir)) &&
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  43) 	    !fscrypt_has_permitted_context(d_inode(dir), inode)) {
886da8b39cf27 (Eric Biggers     2019-07-24 11:07:58 -0700  44) 		fscrypt_warn(inode,
886da8b39cf27 (Eric Biggers     2019-07-24 11:07:58 -0700  45) 			     "Inconsistent encryption context (parent directory: %lu)",
886da8b39cf27 (Eric Biggers     2019-07-24 11:07:58 -0700  46) 			     d_inode(dir)->i_ino);
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  47) 		err = -EPERM;
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  48) 	}
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  49) 	dput(dir);
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  50) 	return err;
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  51) }
efcc7ae2c9172 (Eric Biggers     2017-10-09 12:15:40 -0700  52) EXPORT_SYMBOL_GPL(fscrypt_file_open);
0ea87a9644ebb (Eric Biggers     2017-10-09 12:15:41 -0700  53) 
968dd6d0c6d6b (Eric Biggers     2019-03-20 11:39:10 -0700  54) int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
968dd6d0c6d6b (Eric Biggers     2019-03-20 11:39:10 -0700  55) 			   struct dentry *dentry)
0ea87a9644ebb (Eric Biggers     2017-10-09 12:15:41 -0700  56) {
159e1de201b6f (Eric Biggers     2020-11-17 23:56:05 -0800  57) 	if (fscrypt_is_nokey_name(dentry))
968dd6d0c6d6b (Eric Biggers     2019-03-20 11:39:10 -0700  58) 		return -ENOKEY;
234f1b7f8daf1 (Eric Biggers     2020-11-17 23:56:09 -0800  59) 	/*
234f1b7f8daf1 (Eric Biggers     2020-11-17 23:56:09 -0800  60) 	 * We don't need to separately check that the directory inode's key is
234f1b7f8daf1 (Eric Biggers     2020-11-17 23:56:09 -0800  61) 	 * available, as it's implied by the dentry not being a no-key name.
234f1b7f8daf1 (Eric Biggers     2020-11-17 23:56:09 -0800  62) 	 */
968dd6d0c6d6b (Eric Biggers     2019-03-20 11:39:10 -0700  63) 
0ea87a9644ebb (Eric Biggers     2017-10-09 12:15:41 -0700  64) 	if (!fscrypt_has_permitted_context(dir, inode))
f5e55e777cc93 (Eric Biggers     2019-01-22 16:20:21 -0800  65) 		return -EXDEV;
0ea87a9644ebb (Eric Biggers     2017-10-09 12:15:41 -0700  66) 
0ea87a9644ebb (Eric Biggers     2017-10-09 12:15:41 -0700  67) 	return 0;
0ea87a9644ebb (Eric Biggers     2017-10-09 12:15:41 -0700  68) }
0ea87a9644ebb (Eric Biggers     2017-10-09 12:15:41 -0700  69) EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  70) 
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  71) int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  72) 			     struct inode *new_dir, struct dentry *new_dentry,
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  73) 			     unsigned int flags)
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  74) {
159e1de201b6f (Eric Biggers     2020-11-17 23:56:05 -0800  75) 	if (fscrypt_is_nokey_name(old_dentry) ||
159e1de201b6f (Eric Biggers     2020-11-17 23:56:05 -0800  76) 	    fscrypt_is_nokey_name(new_dentry))
968dd6d0c6d6b (Eric Biggers     2019-03-20 11:39:10 -0700  77) 		return -ENOKEY;
234f1b7f8daf1 (Eric Biggers     2020-11-17 23:56:09 -0800  78) 	/*
234f1b7f8daf1 (Eric Biggers     2020-11-17 23:56:09 -0800  79) 	 * We don't need to separately check that the directory inodes' keys are
234f1b7f8daf1 (Eric Biggers     2020-11-17 23:56:09 -0800  80) 	 * available, as it's implied by the dentries not being no-key names.
234f1b7f8daf1 (Eric Biggers     2020-11-17 23:56:09 -0800  81) 	 */
968dd6d0c6d6b (Eric Biggers     2019-03-20 11:39:10 -0700  82) 
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  83) 	if (old_dir != new_dir) {
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  84) 		if (IS_ENCRYPTED(new_dir) &&
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  85) 		    !fscrypt_has_permitted_context(new_dir,
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  86) 						   d_inode(old_dentry)))
f5e55e777cc93 (Eric Biggers     2019-01-22 16:20:21 -0800  87) 			return -EXDEV;
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  88) 
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  89) 		if ((flags & RENAME_EXCHANGE) &&
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  90) 		    IS_ENCRYPTED(old_dir) &&
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  91) 		    !fscrypt_has_permitted_context(old_dir,
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  92) 						   d_inode(new_dentry)))
f5e55e777cc93 (Eric Biggers     2019-01-22 16:20:21 -0800  93) 			return -EXDEV;
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  94) 	}
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  95) 	return 0;
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  96) }
94b26f3672a0e (Eric Biggers     2017-10-09 12:15:42 -0700  97) EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700  98) 
b01531db6cec2 (Eric Biggers     2019-03-20 11:39:13 -0700  99) int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
b01531db6cec2 (Eric Biggers     2019-03-20 11:39:13 -0700 100) 			     struct fscrypt_name *fname)
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 101) {
b01531db6cec2 (Eric Biggers     2019-03-20 11:39:13 -0700 102) 	int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 103) 
b01531db6cec2 (Eric Biggers     2019-03-20 11:39:13 -0700 104) 	if (err && err != -ENOENT)
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 105) 		return err;
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 106) 
70fb2612aab62 (Eric Biggers     2020-09-23 21:26:23 -0700 107) 	if (fname->is_nokey_name) {
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 108) 		spin_lock(&dentry->d_lock);
501e43fbea468 (Eric Biggers     2020-09-23 21:26:24 -0700 109) 		dentry->d_flags |= DCACHE_NOKEY_NAME;
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 110) 		spin_unlock(&dentry->d_lock);
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 111) 	}
b01531db6cec2 (Eric Biggers     2019-03-20 11:39:13 -0700 112) 	return err;
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 113) }
32c3cf028e747 (Eric Biggers     2017-10-09 12:15:43 -0700 114) EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 115) 
ec0caa974cd09 (Eric Biggers     2020-12-02 18:20:37 -0800 116) int __fscrypt_prepare_readdir(struct inode *dir)
ec0caa974cd09 (Eric Biggers     2020-12-02 18:20:37 -0800 117) {
a14d0b6764917 (Eric Biggers     2020-12-02 18:20:41 -0800 118) 	return fscrypt_get_encryption_info(dir, true);
ec0caa974cd09 (Eric Biggers     2020-12-02 18:20:37 -0800 119) }
ec0caa974cd09 (Eric Biggers     2020-12-02 18:20:37 -0800 120) EXPORT_SYMBOL_GPL(__fscrypt_prepare_readdir);
ec0caa974cd09 (Eric Biggers     2020-12-02 18:20:37 -0800 121) 
7622350e5eda2 (Eric Biggers     2020-12-02 18:20:38 -0800 122) int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr)
7622350e5eda2 (Eric Biggers     2020-12-02 18:20:38 -0800 123) {
7622350e5eda2 (Eric Biggers     2020-12-02 18:20:38 -0800 124) 	if (attr->ia_valid & ATTR_SIZE)
7622350e5eda2 (Eric Biggers     2020-12-02 18:20:38 -0800 125) 		return fscrypt_require_key(d_inode(dentry));
7622350e5eda2 (Eric Biggers     2020-12-02 18:20:38 -0800 126) 	return 0;
7622350e5eda2 (Eric Biggers     2020-12-02 18:20:38 -0800 127) }
7622350e5eda2 (Eric Biggers     2020-12-02 18:20:38 -0800 128) EXPORT_SYMBOL_GPL(__fscrypt_prepare_setattr);
7622350e5eda2 (Eric Biggers     2020-12-02 18:20:38 -0800 129) 
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 130) /**
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 131)  * fscrypt_prepare_setflags() - prepare to change flags with FS_IOC_SETFLAGS
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 132)  * @inode: the inode on which flags are being changed
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 133)  * @oldflags: the old flags
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 134)  * @flags: the new flags
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 135)  *
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 136)  * The caller should be holding i_rwsem for write.
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 137)  *
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 138)  * Return: 0 on success; -errno if the flags change isn't allowed or if
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 139)  *	   another error occurs.
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 140)  */
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 141) int fscrypt_prepare_setflags(struct inode *inode,
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 142) 			     unsigned int oldflags, unsigned int flags)
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 143) {
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 144) 	struct fscrypt_info *ci;
4a4b8721f1a5e (Eric Biggers     2020-11-16 19:26:26 -0800 145) 	struct key *key;
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 146) 	struct fscrypt_master_key *mk;
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 147) 	int err;
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 148) 
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 149) 	/*
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 150) 	 * When the CASEFOLD flag is set on an encrypted directory, we must
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 151) 	 * derive the secret key needed for the dirhash.  This is only possible
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 152) 	 * if the directory uses a v2 encryption policy.
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 153) 	 */
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 154) 	if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 155) 		err = fscrypt_require_key(inode);
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 156) 		if (err)
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 157) 			return err;
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 158) 		ci = inode->i_crypt_info;
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 159) 		if (ci->ci_policy.version != FSCRYPT_POLICY_V2)
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 160) 			return -EINVAL;
4a4b8721f1a5e (Eric Biggers     2020-11-16 19:26:26 -0800 161) 		key = ci->ci_master_key;
4a4b8721f1a5e (Eric Biggers     2020-11-16 19:26:26 -0800 162) 		mk = key->payload.data[0];
4a4b8721f1a5e (Eric Biggers     2020-11-16 19:26:26 -0800 163) 		down_read(&key->sem);
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 164) 		if (is_master_key_secret_present(&mk->mk_secret))
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 165) 			err = fscrypt_derive_dirhash_key(ci, mk);
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 166) 		else
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 167) 			err = -ENOKEY;
4a4b8721f1a5e (Eric Biggers     2020-11-16 19:26:26 -0800 168) 		up_read(&key->sem);
aa408f835d025 (Daniel Rosenberg 2020-01-20 14:31:57 -0800 169) 		return err;
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 170) 	}
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 171) 	return 0;
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 172) }
6e1918cfb263a (Daniel Rosenberg 2020-01-20 14:31:56 -0800 173) 
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 174) /**
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 175)  * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 176)  * @dir: directory in which the symlink is being created
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 177)  * @target: plaintext symlink target
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 178)  * @len: length of @target excluding null terminator
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 179)  * @max_len: space the filesystem has available to store the symlink target
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 180)  * @disk_link: (out) the on-disk symlink target being prepared
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 181)  *
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 182)  * This function computes the size the symlink target will require on-disk,
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 183)  * stores it in @disk_link->len, and validates it against @max_len.  An
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 184)  * encrypted symlink may be longer than the original.
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 185)  *
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 186)  * Additionally, @disk_link->name is set to @target if the symlink will be
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 187)  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 188)  * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 189)  * on-disk target later.  (The reason for the two-step process is that some
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 190)  * filesystems need to know the size of the symlink target before creating the
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 191)  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 192)  *
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 193)  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 194)  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 195)  * occurred while setting up the encryption key.
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 196)  */
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 197) int fscrypt_prepare_symlink(struct inode *dir, const char *target,
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 198) 			    unsigned int len, unsigned int max_len,
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 199) 			    struct fscrypt_str *disk_link)
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 200) {
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 201) 	const union fscrypt_policy *policy;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 202) 
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 203) 	/*
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 204) 	 * To calculate the size of the encrypted symlink target we need to know
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 205) 	 * the amount of NUL padding, which is determined by the flags set in
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 206) 	 * the encryption policy which will be inherited from the directory.
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 207) 	 */
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 208) 	policy = fscrypt_policy_to_inherit(dir);
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 209) 	if (policy == NULL) {
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 210) 		/* Not encrypted */
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 211) 		disk_link->name = (unsigned char *)target;
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 212) 		disk_link->len = len + 1;
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 213) 		if (disk_link->len > max_len)
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 214) 			return -ENAMETOOLONG;
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 215) 		return 0;
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 216) 	}
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 217) 	if (IS_ERR(policy))
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 218) 		return PTR_ERR(policy);
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 219) 
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 220) 	/*
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 221) 	 * Calculate the size of the encrypted symlink and verify it won't
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 222) 	 * exceed max_len.  Note that for historical reasons, encrypted symlink
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 223) 	 * targets are prefixed with the ciphertext length, despite this
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 224) 	 * actually being redundant with i_size.  This decreases by 2 bytes the
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 225) 	 * longest symlink target we can accept.
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 226) 	 *
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 227) 	 * We could recover 1 byte by not counting a null terminator, but
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 228) 	 * counting it (even though it is meaningless for ciphertext) is simpler
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 229) 	 * for now since filesystems will assume it is there and subtract it.
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 230) 	 */
ac4acb1f4b2b6 (Eric Biggers     2020-09-16 21:11:35 -0700 231) 	if (!fscrypt_fname_encrypted_size(policy, len,
b9db0b4a68d37 (Eric Biggers     2018-01-11 23:30:08 -0500 232) 					  max_len - sizeof(struct fscrypt_symlink_data),
b9db0b4a68d37 (Eric Biggers     2018-01-11 23:30:08 -0500 233) 					  &disk_link->len))
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 234) 		return -ENAMETOOLONG;
b9db0b4a68d37 (Eric Biggers     2018-01-11 23:30:08 -0500 235) 	disk_link->len += sizeof(struct fscrypt_symlink_data);
b9db0b4a68d37 (Eric Biggers     2018-01-11 23:30:08 -0500 236) 
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 237) 	disk_link->name = NULL;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 238) 	return 0;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 239) }
31114726b6936 (Eric Biggers     2020-09-16 21:11:34 -0700 240) EXPORT_SYMBOL_GPL(fscrypt_prepare_symlink);
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 241) 
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 242) int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 243) 			      unsigned int len, struct fscrypt_str *disk_link)
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 244) {
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 245) 	int err;
0b1dfa4cc6c60 (Eric Biggers     2018-01-19 13:45:24 -0800 246) 	struct qstr iname = QSTR_INIT(target, len);
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 247) 	struct fscrypt_symlink_data *sd;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 248) 	unsigned int ciphertext_len;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 249) 
4cc1a3e7e8520 (Eric Biggers     2020-09-16 21:11:31 -0700 250) 	/*
4cc1a3e7e8520 (Eric Biggers     2020-09-16 21:11:31 -0700 251) 	 * fscrypt_prepare_new_inode() should have already set up the new
4cc1a3e7e8520 (Eric Biggers     2020-09-16 21:11:31 -0700 252) 	 * symlink inode's encryption key.  We don't wait until now to do it,
4cc1a3e7e8520 (Eric Biggers     2020-09-16 21:11:31 -0700 253) 	 * since we may be in a filesystem transaction now.
4cc1a3e7e8520 (Eric Biggers     2020-09-16 21:11:31 -0700 254) 	 */
4cc1a3e7e8520 (Eric Biggers     2020-09-16 21:11:31 -0700 255) 	if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
4cc1a3e7e8520 (Eric Biggers     2020-09-16 21:11:31 -0700 256) 		return -ENOKEY;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 257) 
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 258) 	if (disk_link->name) {
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 259) 		/* filesystem-provided buffer */
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 260) 		sd = (struct fscrypt_symlink_data *)disk_link->name;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 261) 	} else {
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 262) 		sd = kmalloc(disk_link->len, GFP_NOFS);
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 263) 		if (!sd)
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 264) 			return -ENOMEM;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 265) 	}
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 266) 	ciphertext_len = disk_link->len - sizeof(*sd);
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 267) 	sd->len = cpu_to_le16(ciphertext_len);
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 268) 
1b3b827ee5230 (Eric Biggers     2020-01-19 23:17:36 -0800 269) 	err = fscrypt_fname_encrypt(inode, &iname, sd->encrypted_path,
1b3b827ee5230 (Eric Biggers     2020-01-19 23:17:36 -0800 270) 				    ciphertext_len);
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 271) 	if (err)
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 272) 		goto err_free_sd;
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 273) 
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 274) 	/*
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 275) 	 * Null-terminating the ciphertext doesn't make sense, but we still
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 276) 	 * count the null terminator in the length, so we might as well
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 277) 	 * initialize it just in case the filesystem writes it out.
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 278) 	 */
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 279) 	sd->encrypted_path[ciphertext_len] = '\0';
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 280) 
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 281) 	/* Cache the plaintext symlink target for later use by get_link() */
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 282) 	err = -ENOMEM;
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 283) 	inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 284) 	if (!inode->i_link)
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 285) 		goto err_free_sd;
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 286) 
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 287) 	if (!disk_link->name)
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 288) 		disk_link->name = (unsigned char *)sd;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 289) 	return 0;
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 290) 
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 291) err_free_sd:
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 292) 	if (!disk_link->name)
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 293) 		kfree(sd);
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 294) 	return err;
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 295) }
76e81d6d50481 (Eric Biggers     2018-01-05 10:45:01 -0800 296) EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 297) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 298) /**
d2fe97545a1e2 (Eric Biggers     2020-05-11 12:13:56 -0700 299)  * fscrypt_get_symlink() - get the target of an encrypted symlink
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 300)  * @inode: the symlink inode
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 301)  * @caddr: the on-disk contents of the symlink
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 302)  * @max_size: size of @caddr buffer
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 303)  * @done: if successful, will be set up to free the returned target if needed
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 304)  *
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 305)  * If the symlink's encryption key is available, we decrypt its target.
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 306)  * Otherwise, we encode its target for presentation.
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 307)  *
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 308)  * This may sleep, so the filesystem must have dropped out of RCU mode already.
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 309)  *
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 310)  * Return: the presentable symlink target or an ERR_PTR()
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 311)  */
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 312) const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 313) 				unsigned int max_size,
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 314) 				struct delayed_call *done)
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 315) {
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 316) 	const struct fscrypt_symlink_data *sd;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 317) 	struct fscrypt_str cstr, pstr;
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 318) 	bool has_key;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 319) 	int err;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 320) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 321) 	/* This is for encrypted symlinks only */
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 322) 	if (WARN_ON(!IS_ENCRYPTED(inode)))
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 323) 		return ERR_PTR(-EINVAL);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 324) 
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 325) 	/* If the decrypted target is already cached, just return it. */
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 326) 	pstr.name = READ_ONCE(inode->i_link);
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 327) 	if (pstr.name)
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 328) 		return pstr.name;
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 329) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 330) 	/*
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 331) 	 * Try to set up the symlink's encryption key, but we can continue
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 332) 	 * regardless of whether the key is available or not.
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 333) 	 */
a14d0b6764917 (Eric Biggers     2020-12-02 18:20:41 -0800 334) 	err = fscrypt_get_encryption_info(inode, false);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 335) 	if (err)
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 336) 		return ERR_PTR(err);
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 337) 	has_key = fscrypt_has_encryption_key(inode);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 338) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 339) 	/*
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 340) 	 * For historical reasons, encrypted symlink targets are prefixed with
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 341) 	 * the ciphertext length, even though this is redundant with i_size.
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 342) 	 */
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 343) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 344) 	if (max_size < sizeof(*sd))
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 345) 		return ERR_PTR(-EUCLEAN);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 346) 	sd = caddr;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 347) 	cstr.name = (unsigned char *)sd->encrypted_path;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 348) 	cstr.len = le16_to_cpu(sd->len);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 349) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 350) 	if (cstr.len == 0)
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 351) 		return ERR_PTR(-EUCLEAN);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 352) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 353) 	if (cstr.len + sizeof(*sd) - 1 > max_size)
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 354) 		return ERR_PTR(-EUCLEAN);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 355) 
8b10fe6898527 (Jeff Layton      2020-08-10 10:21:39 -0400 356) 	err = fscrypt_fname_alloc_buffer(cstr.len, &pstr);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 357) 	if (err)
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 358) 		return ERR_PTR(err);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 359) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 360) 	err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 361) 	if (err)
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 362) 		goto err_kfree;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 363) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 364) 	err = -EUCLEAN;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 365) 	if (pstr.name[0] == '\0')
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 366) 		goto err_kfree;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 367) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 368) 	pstr.name[pstr.len] = '\0';
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 369) 
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 370) 	/*
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 371) 	 * Cache decrypted symlink targets in i_link for later use.  Don't cache
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 372) 	 * symlink targets encoded without the key, since those become outdated
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 373) 	 * once the key is added.  This pairs with the READ_ONCE() above and in
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 374) 	 * the VFS path lookup code.
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 375) 	 */
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 376) 	if (!has_key ||
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 377) 	    cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 378) 		set_delayed_call(done, kfree_link, pstr.name);
2c58d548f5706 (Eric Biggers     2019-04-10 13:21:15 -0700 379) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 380) 	return pstr.name;
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 381) 
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 382) err_kfree:
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 383) 	kfree(pstr.name);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 384) 	return ERR_PTR(err);
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 385) }
3b0d8837a79ba (Eric Biggers     2018-01-05 10:45:02 -0800 386) EXPORT_SYMBOL_GPL(fscrypt_get_symlink);
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 387) 
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 388) /**
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 389)  * fscrypt_symlink_getattr() - set the correct st_size for encrypted symlinks
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 390)  * @path: the path for the encrypted symlink being queried
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 391)  * @stat: the struct being filled with the symlink's attributes
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 392)  *
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 393)  * Override st_size of encrypted symlinks to be the length of the decrypted
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 394)  * symlink target (or the no-key encoded symlink target, if the key is
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 395)  * unavailable) rather than the length of the encrypted symlink target.  This is
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 396)  * necessary for st_size to match the symlink target that userspace actually
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 397)  * sees.  POSIX requires this, and some userspace programs depend on it.
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 398)  *
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 399)  * This requires reading the symlink target from disk if needed, setting up the
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 400)  * inode's encryption key if possible, and then decrypting or encoding the
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 401)  * symlink target.  This makes lstat() more heavyweight than is normally the
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 402)  * case.  However, decrypted symlink targets will be cached in ->i_link, so
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 403)  * usually the symlink won't have to be read and decrypted again later if/when
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 404)  * it is actually followed, readlink() is called, or lstat() is called again.
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 405)  *
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 406)  * Return: 0 on success, -errno on failure
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 407)  */
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 408) int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat)
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 409) {
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 410) 	struct dentry *dentry = path->dentry;
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 411) 	struct inode *inode = d_inode(dentry);
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 412) 	const char *link;
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 413) 	DEFINE_DELAYED_CALL(done);
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 414) 
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 415) 	/*
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 416) 	 * To get the symlink target that userspace will see (whether it's the
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 417) 	 * decrypted target or the no-key encoded target), we can just get it in
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 418) 	 * the same way the VFS does during path resolution and readlink().
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 419) 	 */
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 420) 	link = READ_ONCE(inode->i_link);
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 421) 	if (!link) {
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 422) 		link = inode->i_op->get_link(dentry, inode, &done);
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 423) 		if (IS_ERR(link))
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 424) 			return PTR_ERR(link);
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 425) 	}
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 426) 	stat->size = strlen(link);
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 427) 	do_delayed_call(&done);
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 428) 	return 0;
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 429) }
2bc40caa80992 (Eric Biggers     2021-07-01 23:53:46 -0700 430) EXPORT_SYMBOL_GPL(fscrypt_symlink_getattr);