xref: /openbmc/linux/fs/crypto/hooks.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2efcc7ae2SEric Biggers /*
3efcc7ae2SEric Biggers  * fs/crypto/hooks.c
4efcc7ae2SEric Biggers  *
5efcc7ae2SEric Biggers  * Encryption hooks for higher-level filesystem operations.
6efcc7ae2SEric Biggers  */
7efcc7ae2SEric Biggers 
8efcc7ae2SEric Biggers #include "fscrypt_private.h"
9efcc7ae2SEric Biggers 
10efcc7ae2SEric Biggers /**
11d2fe9754SEric Biggers  * fscrypt_file_open() - prepare to open a possibly-encrypted regular file
12efcc7ae2SEric Biggers  * @inode: the inode being opened
13efcc7ae2SEric Biggers  * @filp: the struct file being set up
14efcc7ae2SEric Biggers  *
15efcc7ae2SEric Biggers  * Currently, an encrypted regular file can only be opened if its encryption key
16efcc7ae2SEric Biggers  * is available; access to the raw encrypted contents is not supported.
17efcc7ae2SEric Biggers  * Therefore, we first set up the inode's encryption key (if not already done)
18efcc7ae2SEric Biggers  * and return an error if it's unavailable.
19efcc7ae2SEric Biggers  *
20efcc7ae2SEric Biggers  * We also verify that if the parent directory (from the path via which the file
21efcc7ae2SEric Biggers  * is being opened) is encrypted, then the inode being opened uses the same
22efcc7ae2SEric Biggers  * encryption policy.  This is needed as part of the enforcement that all files
23efcc7ae2SEric Biggers  * in an encrypted directory tree use the same encryption policy, as a
24efcc7ae2SEric Biggers  * protection against certain types of offline attacks.  Note that this check is
25efcc7ae2SEric Biggers  * needed even when opening an *unencrypted* file, since it's forbidden to have
26efcc7ae2SEric Biggers  * an unencrypted file in an encrypted directory.
27efcc7ae2SEric Biggers  *
28efcc7ae2SEric Biggers  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
29efcc7ae2SEric Biggers  */
fscrypt_file_open(struct inode * inode,struct file * filp)30efcc7ae2SEric Biggers int fscrypt_file_open(struct inode *inode, struct file *filp)
31efcc7ae2SEric Biggers {
32efcc7ae2SEric Biggers 	int err;
33efcc7ae2SEric Biggers 	struct dentry *dir;
34efcc7ae2SEric Biggers 
35efcc7ae2SEric Biggers 	err = fscrypt_require_key(inode);
36efcc7ae2SEric Biggers 	if (err)
37efcc7ae2SEric Biggers 		return err;
38efcc7ae2SEric Biggers 
39efcc7ae2SEric Biggers 	dir = dget_parent(file_dentry(filp));
40efcc7ae2SEric Biggers 	if (IS_ENCRYPTED(d_inode(dir)) &&
41efcc7ae2SEric Biggers 	    !fscrypt_has_permitted_context(d_inode(dir), inode)) {
42886da8b3SEric Biggers 		fscrypt_warn(inode,
43886da8b3SEric Biggers 			     "Inconsistent encryption context (parent directory: %lu)",
44886da8b3SEric Biggers 			     d_inode(dir)->i_ino);
45efcc7ae2SEric Biggers 		err = -EPERM;
46efcc7ae2SEric Biggers 	}
47efcc7ae2SEric Biggers 	dput(dir);
48efcc7ae2SEric Biggers 	return err;
49efcc7ae2SEric Biggers }
50efcc7ae2SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_file_open);
510ea87a96SEric Biggers 
__fscrypt_prepare_link(struct inode * inode,struct inode * dir,struct dentry * dentry)52968dd6d0SEric Biggers int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
53968dd6d0SEric Biggers 			   struct dentry *dentry)
540ea87a96SEric Biggers {
55159e1de2SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
56968dd6d0SEric Biggers 		return -ENOKEY;
57234f1b7fSEric Biggers 	/*
58234f1b7fSEric Biggers 	 * We don't need to separately check that the directory inode's key is
59234f1b7fSEric Biggers 	 * available, as it's implied by the dentry not being a no-key name.
60234f1b7fSEric Biggers 	 */
61968dd6d0SEric Biggers 
620ea87a96SEric Biggers 	if (!fscrypt_has_permitted_context(dir, inode))
63f5e55e77SEric Biggers 		return -EXDEV;
640ea87a96SEric Biggers 
650ea87a96SEric Biggers 	return 0;
660ea87a96SEric Biggers }
670ea87a96SEric Biggers EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
6894b26f36SEric Biggers 
__fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)6994b26f36SEric Biggers int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
7094b26f36SEric Biggers 			     struct inode *new_dir, struct dentry *new_dentry,
7194b26f36SEric Biggers 			     unsigned int flags)
7294b26f36SEric Biggers {
73159e1de2SEric Biggers 	if (fscrypt_is_nokey_name(old_dentry) ||
74159e1de2SEric Biggers 	    fscrypt_is_nokey_name(new_dentry))
75968dd6d0SEric Biggers 		return -ENOKEY;
76234f1b7fSEric Biggers 	/*
77234f1b7fSEric Biggers 	 * We don't need to separately check that the directory inodes' keys are
78234f1b7fSEric Biggers 	 * available, as it's implied by the dentries not being no-key names.
79234f1b7fSEric Biggers 	 */
80968dd6d0SEric Biggers 
8194b26f36SEric Biggers 	if (old_dir != new_dir) {
8294b26f36SEric Biggers 		if (IS_ENCRYPTED(new_dir) &&
8394b26f36SEric Biggers 		    !fscrypt_has_permitted_context(new_dir,
8494b26f36SEric Biggers 						   d_inode(old_dentry)))
85f5e55e77SEric Biggers 			return -EXDEV;
8694b26f36SEric Biggers 
8794b26f36SEric Biggers 		if ((flags & RENAME_EXCHANGE) &&
8894b26f36SEric Biggers 		    IS_ENCRYPTED(old_dir) &&
8994b26f36SEric Biggers 		    !fscrypt_has_permitted_context(old_dir,
9094b26f36SEric Biggers 						   d_inode(new_dentry)))
91f5e55e77SEric Biggers 			return -EXDEV;
9294b26f36SEric Biggers 	}
9394b26f36SEric Biggers 	return 0;
9494b26f36SEric Biggers }
9594b26f36SEric Biggers EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
9632c3cf02SEric Biggers 
__fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)97b01531dbSEric Biggers int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
98b01531dbSEric Biggers 			     struct fscrypt_name *fname)
9932c3cf02SEric Biggers {
100b01531dbSEric Biggers 	int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
10132c3cf02SEric Biggers 
102b01531dbSEric Biggers 	if (err && err != -ENOENT)
10332c3cf02SEric Biggers 		return err;
10432c3cf02SEric Biggers 
10570fb2612SEric Biggers 	if (fname->is_nokey_name) {
10632c3cf02SEric Biggers 		spin_lock(&dentry->d_lock);
107501e43fbSEric Biggers 		dentry->d_flags |= DCACHE_NOKEY_NAME;
10832c3cf02SEric Biggers 		spin_unlock(&dentry->d_lock);
109d456a33fSEric Biggers 	}
110b01531dbSEric Biggers 	return err;
11132c3cf02SEric Biggers }
11232c3cf02SEric Biggers EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
11376e81d6dSEric Biggers 
1146f2656eaSLuís Henriques /**
1156f2656eaSLuís Henriques  * fscrypt_prepare_lookup_partial() - prepare lookup without filename setup
1166f2656eaSLuís Henriques  * @dir: the encrypted directory being searched
1176f2656eaSLuís Henriques  * @dentry: the dentry being looked up in @dir
1186f2656eaSLuís Henriques  *
1196f2656eaSLuís Henriques  * This function should be used by the ->lookup and ->atomic_open methods of
1206f2656eaSLuís Henriques  * filesystems that handle filename encryption and no-key name encoding
1216f2656eaSLuís Henriques  * themselves and thus can't use fscrypt_prepare_lookup().  Like
1226f2656eaSLuís Henriques  * fscrypt_prepare_lookup(), this will try to set up the directory's encryption
1236f2656eaSLuís Henriques  * key and will set DCACHE_NOKEY_NAME on the dentry if the key is unavailable.
1246f2656eaSLuís Henriques  * However, this function doesn't set up a struct fscrypt_name for the filename.
1256f2656eaSLuís Henriques  *
1266f2656eaSLuís Henriques  * Return: 0 on success; -errno on error.  Note that the encryption key being
1276f2656eaSLuís Henriques  *	   unavailable is not considered an error.  It is also not an error if
1286f2656eaSLuís Henriques  *	   the encryption policy is unsupported by this kernel; that is treated
1296f2656eaSLuís Henriques  *	   like the key being unavailable, so that files can still be deleted.
1306f2656eaSLuís Henriques  */
fscrypt_prepare_lookup_partial(struct inode * dir,struct dentry * dentry)1316f2656eaSLuís Henriques int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry)
1326f2656eaSLuís Henriques {
1336f2656eaSLuís Henriques 	int err = fscrypt_get_encryption_info(dir, true);
1346f2656eaSLuís Henriques 
1356f2656eaSLuís Henriques 	if (!err && !fscrypt_has_encryption_key(dir)) {
1366f2656eaSLuís Henriques 		spin_lock(&dentry->d_lock);
1376f2656eaSLuís Henriques 		dentry->d_flags |= DCACHE_NOKEY_NAME;
1386f2656eaSLuís Henriques 		spin_unlock(&dentry->d_lock);
1396f2656eaSLuís Henriques 	}
1406f2656eaSLuís Henriques 	return err;
1416f2656eaSLuís Henriques }
1426f2656eaSLuís Henriques EXPORT_SYMBOL_GPL(fscrypt_prepare_lookup_partial);
1436f2656eaSLuís Henriques 
__fscrypt_prepare_readdir(struct inode * dir)144ec0caa97SEric Biggers int __fscrypt_prepare_readdir(struct inode *dir)
145ec0caa97SEric Biggers {
146a14d0b67SEric Biggers 	return fscrypt_get_encryption_info(dir, true);
147ec0caa97SEric Biggers }
148ec0caa97SEric Biggers EXPORT_SYMBOL_GPL(__fscrypt_prepare_readdir);
149ec0caa97SEric Biggers 
__fscrypt_prepare_setattr(struct dentry * dentry,struct iattr * attr)1507622350eSEric Biggers int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr)
1517622350eSEric Biggers {
1527622350eSEric Biggers 	if (attr->ia_valid & ATTR_SIZE)
1537622350eSEric Biggers 		return fscrypt_require_key(d_inode(dentry));
1547622350eSEric Biggers 	return 0;
1557622350eSEric Biggers }
1567622350eSEric Biggers EXPORT_SYMBOL_GPL(__fscrypt_prepare_setattr);
1577622350eSEric Biggers 
1586e1918cfSDaniel Rosenberg /**
1596e1918cfSDaniel Rosenberg  * fscrypt_prepare_setflags() - prepare to change flags with FS_IOC_SETFLAGS
1606e1918cfSDaniel Rosenberg  * @inode: the inode on which flags are being changed
1616e1918cfSDaniel Rosenberg  * @oldflags: the old flags
1626e1918cfSDaniel Rosenberg  * @flags: the new flags
1636e1918cfSDaniel Rosenberg  *
1646e1918cfSDaniel Rosenberg  * The caller should be holding i_rwsem for write.
1656e1918cfSDaniel Rosenberg  *
1666e1918cfSDaniel Rosenberg  * Return: 0 on success; -errno if the flags change isn't allowed or if
1676e1918cfSDaniel Rosenberg  *	   another error occurs.
1686e1918cfSDaniel Rosenberg  */
fscrypt_prepare_setflags(struct inode * inode,unsigned int oldflags,unsigned int flags)1696e1918cfSDaniel Rosenberg int fscrypt_prepare_setflags(struct inode *inode,
1706e1918cfSDaniel Rosenberg 			     unsigned int oldflags, unsigned int flags)
1716e1918cfSDaniel Rosenberg {
1726e1918cfSDaniel Rosenberg 	struct fscrypt_info *ci;
173aa408f83SDaniel Rosenberg 	struct fscrypt_master_key *mk;
1746e1918cfSDaniel Rosenberg 	int err;
1756e1918cfSDaniel Rosenberg 
176aa408f83SDaniel Rosenberg 	/*
177aa408f83SDaniel Rosenberg 	 * When the CASEFOLD flag is set on an encrypted directory, we must
178aa408f83SDaniel Rosenberg 	 * derive the secret key needed for the dirhash.  This is only possible
179aa408f83SDaniel Rosenberg 	 * if the directory uses a v2 encryption policy.
180aa408f83SDaniel Rosenberg 	 */
1816e1918cfSDaniel Rosenberg 	if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
1826e1918cfSDaniel Rosenberg 		err = fscrypt_require_key(inode);
1836e1918cfSDaniel Rosenberg 		if (err)
1846e1918cfSDaniel Rosenberg 			return err;
1856e1918cfSDaniel Rosenberg 		ci = inode->i_crypt_info;
1866e1918cfSDaniel Rosenberg 		if (ci->ci_policy.version != FSCRYPT_POLICY_V2)
1876e1918cfSDaniel Rosenberg 			return -EINVAL;
188d7e7b9afSEric Biggers 		mk = ci->ci_master_key;
189d7e7b9afSEric Biggers 		down_read(&mk->mk_sem);
190aa408f83SDaniel Rosenberg 		if (is_master_key_secret_present(&mk->mk_secret))
191aa408f83SDaniel Rosenberg 			err = fscrypt_derive_dirhash_key(ci, mk);
192aa408f83SDaniel Rosenberg 		else
193aa408f83SDaniel Rosenberg 			err = -ENOKEY;
194d7e7b9afSEric Biggers 		up_read(&mk->mk_sem);
195aa408f83SDaniel Rosenberg 		return err;
1966e1918cfSDaniel Rosenberg 	}
1976e1918cfSDaniel Rosenberg 	return 0;
1986e1918cfSDaniel Rosenberg }
1996e1918cfSDaniel Rosenberg 
20031114726SEric Biggers /**
20131114726SEric Biggers  * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink
20231114726SEric Biggers  * @dir: directory in which the symlink is being created
20331114726SEric Biggers  * @target: plaintext symlink target
20431114726SEric Biggers  * @len: length of @target excluding null terminator
20531114726SEric Biggers  * @max_len: space the filesystem has available to store the symlink target
20631114726SEric Biggers  * @disk_link: (out) the on-disk symlink target being prepared
20731114726SEric Biggers  *
20831114726SEric Biggers  * This function computes the size the symlink target will require on-disk,
20931114726SEric Biggers  * stores it in @disk_link->len, and validates it against @max_len.  An
21031114726SEric Biggers  * encrypted symlink may be longer than the original.
21131114726SEric Biggers  *
21231114726SEric Biggers  * Additionally, @disk_link->name is set to @target if the symlink will be
21331114726SEric Biggers  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
21431114726SEric Biggers  * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
21531114726SEric Biggers  * on-disk target later.  (The reason for the two-step process is that some
21631114726SEric Biggers  * filesystems need to know the size of the symlink target before creating the
21731114726SEric Biggers  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
21831114726SEric Biggers  *
21931114726SEric Biggers  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
22031114726SEric Biggers  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
22131114726SEric Biggers  * occurred while setting up the encryption key.
22231114726SEric Biggers  */
fscrypt_prepare_symlink(struct inode * dir,const char * target,unsigned int len,unsigned int max_len,struct fscrypt_str * disk_link)22331114726SEric Biggers int fscrypt_prepare_symlink(struct inode *dir, const char *target,
22431114726SEric Biggers 			    unsigned int len, unsigned int max_len,
22576e81d6dSEric Biggers 			    struct fscrypt_str *disk_link)
22676e81d6dSEric Biggers {
227ac4acb1fSEric Biggers 	const union fscrypt_policy *policy;
22876e81d6dSEric Biggers 
229ac4acb1fSEric Biggers 	/*
230ac4acb1fSEric Biggers 	 * To calculate the size of the encrypted symlink target we need to know
231ac4acb1fSEric Biggers 	 * the amount of NUL padding, which is determined by the flags set in
232ac4acb1fSEric Biggers 	 * the encryption policy which will be inherited from the directory.
233ac4acb1fSEric Biggers 	 */
234ac4acb1fSEric Biggers 	policy = fscrypt_policy_to_inherit(dir);
235ac4acb1fSEric Biggers 	if (policy == NULL) {
236ac4acb1fSEric Biggers 		/* Not encrypted */
23731114726SEric Biggers 		disk_link->name = (unsigned char *)target;
23831114726SEric Biggers 		disk_link->len = len + 1;
23931114726SEric Biggers 		if (disk_link->len > max_len)
24031114726SEric Biggers 			return -ENAMETOOLONG;
24131114726SEric Biggers 		return 0;
24231114726SEric Biggers 	}
243ac4acb1fSEric Biggers 	if (IS_ERR(policy))
244ac4acb1fSEric Biggers 		return PTR_ERR(policy);
24576e81d6dSEric Biggers 
24676e81d6dSEric Biggers 	/*
24776e81d6dSEric Biggers 	 * Calculate the size of the encrypted symlink and verify it won't
24876e81d6dSEric Biggers 	 * exceed max_len.  Note that for historical reasons, encrypted symlink
24976e81d6dSEric Biggers 	 * targets are prefixed with the ciphertext length, despite this
25076e81d6dSEric Biggers 	 * actually being redundant with i_size.  This decreases by 2 bytes the
25176e81d6dSEric Biggers 	 * longest symlink target we can accept.
25276e81d6dSEric Biggers 	 *
25376e81d6dSEric Biggers 	 * We could recover 1 byte by not counting a null terminator, but
25476e81d6dSEric Biggers 	 * counting it (even though it is meaningless for ciphertext) is simpler
25576e81d6dSEric Biggers 	 * for now since filesystems will assume it is there and subtract it.
25676e81d6dSEric Biggers 	 */
257d3e94fdcSJeff Layton 	if (!__fscrypt_fname_encrypted_size(policy, len,
258b9db0b4aSEric Biggers 					    max_len - sizeof(struct fscrypt_symlink_data) - 1,
259b9db0b4aSEric Biggers 					    &disk_link->len))
26076e81d6dSEric Biggers 		return -ENAMETOOLONG;
261b9db0b4aSEric Biggers 	disk_link->len += sizeof(struct fscrypt_symlink_data) + 1;
262b9db0b4aSEric Biggers 
26376e81d6dSEric Biggers 	disk_link->name = NULL;
26476e81d6dSEric Biggers 	return 0;
26576e81d6dSEric Biggers }
26631114726SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_prepare_symlink);
26776e81d6dSEric Biggers 
__fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)26876e81d6dSEric Biggers int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
26976e81d6dSEric Biggers 			      unsigned int len, struct fscrypt_str *disk_link)
27076e81d6dSEric Biggers {
27176e81d6dSEric Biggers 	int err;
2720b1dfa4cSEric Biggers 	struct qstr iname = QSTR_INIT(target, len);
27376e81d6dSEric Biggers 	struct fscrypt_symlink_data *sd;
27476e81d6dSEric Biggers 	unsigned int ciphertext_len;
27576e81d6dSEric Biggers 
2764cc1a3e7SEric Biggers 	/*
2774cc1a3e7SEric Biggers 	 * fscrypt_prepare_new_inode() should have already set up the new
2784cc1a3e7SEric Biggers 	 * symlink inode's encryption key.  We don't wait until now to do it,
2794cc1a3e7SEric Biggers 	 * since we may be in a filesystem transaction now.
2804cc1a3e7SEric Biggers 	 */
2814cc1a3e7SEric Biggers 	if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
2824cc1a3e7SEric Biggers 		return -ENOKEY;
28376e81d6dSEric Biggers 
28476e81d6dSEric Biggers 	if (disk_link->name) {
28576e81d6dSEric Biggers 		/* filesystem-provided buffer */
28676e81d6dSEric Biggers 		sd = (struct fscrypt_symlink_data *)disk_link->name;
28776e81d6dSEric Biggers 	} else {
28876e81d6dSEric Biggers 		sd = kmalloc(disk_link->len, GFP_NOFS);
28976e81d6dSEric Biggers 		if (!sd)
29076e81d6dSEric Biggers 			return -ENOMEM;
29176e81d6dSEric Biggers 	}
29276e81d6dSEric Biggers 	ciphertext_len = disk_link->len - sizeof(*sd) - 1;
29376e81d6dSEric Biggers 	sd->len = cpu_to_le16(ciphertext_len);
29476e81d6dSEric Biggers 
2951b3b827eSEric Biggers 	err = fscrypt_fname_encrypt(inode, &iname, sd->encrypted_path,
2961b3b827eSEric Biggers 				    ciphertext_len);
2972c58d548SEric Biggers 	if (err)
2982c58d548SEric Biggers 		goto err_free_sd;
2992c58d548SEric Biggers 
30076e81d6dSEric Biggers 	/*
30176e81d6dSEric Biggers 	 * Null-terminating the ciphertext doesn't make sense, but we still
30276e81d6dSEric Biggers 	 * count the null terminator in the length, so we might as well
30376e81d6dSEric Biggers 	 * initialize it just in case the filesystem writes it out.
30476e81d6dSEric Biggers 	 */
30576e81d6dSEric Biggers 	sd->encrypted_path[ciphertext_len] = '\0';
30676e81d6dSEric Biggers 
3072c58d548SEric Biggers 	/* Cache the plaintext symlink target for later use by get_link() */
3082c58d548SEric Biggers 	err = -ENOMEM;
3092c58d548SEric Biggers 	inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
3102c58d548SEric Biggers 	if (!inode->i_link)
3112c58d548SEric Biggers 		goto err_free_sd;
3122c58d548SEric Biggers 
31376e81d6dSEric Biggers 	if (!disk_link->name)
31476e81d6dSEric Biggers 		disk_link->name = (unsigned char *)sd;
31576e81d6dSEric Biggers 	return 0;
3162c58d548SEric Biggers 
3172c58d548SEric Biggers err_free_sd:
3182c58d548SEric Biggers 	if (!disk_link->name)
3192c58d548SEric Biggers 		kfree(sd);
3202c58d548SEric Biggers 	return err;
32176e81d6dSEric Biggers }
32276e81d6dSEric Biggers EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
3233b0d8837SEric Biggers 
3243b0d8837SEric Biggers /**
325d2fe9754SEric Biggers  * fscrypt_get_symlink() - get the target of an encrypted symlink
3263b0d8837SEric Biggers  * @inode: the symlink inode
3273b0d8837SEric Biggers  * @caddr: the on-disk contents of the symlink
3283b0d8837SEric Biggers  * @max_size: size of @caddr buffer
3292c58d548SEric Biggers  * @done: if successful, will be set up to free the returned target if needed
3303b0d8837SEric Biggers  *
3313b0d8837SEric Biggers  * If the symlink's encryption key is available, we decrypt its target.
3323b0d8837SEric Biggers  * Otherwise, we encode its target for presentation.
3333b0d8837SEric Biggers  *
3343b0d8837SEric Biggers  * This may sleep, so the filesystem must have dropped out of RCU mode already.
3353b0d8837SEric Biggers  *
3363b0d8837SEric Biggers  * Return: the presentable symlink target or an ERR_PTR()
3373b0d8837SEric Biggers  */
fscrypt_get_symlink(struct inode * inode,const void * caddr,unsigned int max_size,struct delayed_call * done)3383b0d8837SEric Biggers const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
3393b0d8837SEric Biggers 				unsigned int max_size,
3403b0d8837SEric Biggers 				struct delayed_call *done)
3413b0d8837SEric Biggers {
3423b0d8837SEric Biggers 	const struct fscrypt_symlink_data *sd;
3433b0d8837SEric Biggers 	struct fscrypt_str cstr, pstr;
3442c58d548SEric Biggers 	bool has_key;
3453b0d8837SEric Biggers 	int err;
3463b0d8837SEric Biggers 
3473b0d8837SEric Biggers 	/* This is for encrypted symlinks only */
348*41b2ad80SEric Biggers 	if (WARN_ON_ONCE(!IS_ENCRYPTED(inode)))
3493b0d8837SEric Biggers 		return ERR_PTR(-EINVAL);
3503b0d8837SEric Biggers 
3512c58d548SEric Biggers 	/* If the decrypted target is already cached, just return it. */
3522c58d548SEric Biggers 	pstr.name = READ_ONCE(inode->i_link);
3532c58d548SEric Biggers 	if (pstr.name)
3542c58d548SEric Biggers 		return pstr.name;
3552c58d548SEric Biggers 
3563b0d8837SEric Biggers 	/*
3573b0d8837SEric Biggers 	 * Try to set up the symlink's encryption key, but we can continue
3583b0d8837SEric Biggers 	 * regardless of whether the key is available or not.
3593b0d8837SEric Biggers 	 */
360a14d0b67SEric Biggers 	err = fscrypt_get_encryption_info(inode, false);
3613b0d8837SEric Biggers 	if (err)
3623b0d8837SEric Biggers 		return ERR_PTR(err);
3632c58d548SEric Biggers 	has_key = fscrypt_has_encryption_key(inode);
3643b0d8837SEric Biggers 
3653b0d8837SEric Biggers 	/*
3663b0d8837SEric Biggers 	 * For historical reasons, encrypted symlink targets are prefixed with
3673b0d8837SEric Biggers 	 * the ciphertext length, even though this is redundant with i_size.
3683b0d8837SEric Biggers 	 */
3693b0d8837SEric Biggers 
3703b0d8837SEric Biggers 	if (max_size < sizeof(*sd) + 1)
3713b0d8837SEric Biggers 		return ERR_PTR(-EUCLEAN);
3723b0d8837SEric Biggers 	sd = caddr;
3733b0d8837SEric Biggers 	cstr.name = (unsigned char *)sd->encrypted_path;
3743b0d8837SEric Biggers 	cstr.len = le16_to_cpu(sd->len);
3753b0d8837SEric Biggers 
3763b0d8837SEric Biggers 	if (cstr.len == 0)
3773b0d8837SEric Biggers 		return ERR_PTR(-EUCLEAN);
3783b0d8837SEric Biggers 
3793b0d8837SEric Biggers 	if (cstr.len + sizeof(*sd) > max_size)
3803b0d8837SEric Biggers 		return ERR_PTR(-EUCLEAN);
3813b0d8837SEric Biggers 
3828b10fe68SJeff Layton 	err = fscrypt_fname_alloc_buffer(cstr.len, &pstr);
3833b0d8837SEric Biggers 	if (err)
3843b0d8837SEric Biggers 		return ERR_PTR(err);
3853b0d8837SEric Biggers 
3863b0d8837SEric Biggers 	err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
3873b0d8837SEric Biggers 	if (err)
3883b0d8837SEric Biggers 		goto err_kfree;
3893b0d8837SEric Biggers 
3903b0d8837SEric Biggers 	err = -EUCLEAN;
3913b0d8837SEric Biggers 	if (pstr.name[0] == '\0')
3923b0d8837SEric Biggers 		goto err_kfree;
3933b0d8837SEric Biggers 
3943b0d8837SEric Biggers 	pstr.name[pstr.len] = '\0';
3952c58d548SEric Biggers 
3962c58d548SEric Biggers 	/*
3972c58d548SEric Biggers 	 * Cache decrypted symlink targets in i_link for later use.  Don't cache
3982c58d548SEric Biggers 	 * symlink targets encoded without the key, since those become outdated
3992c58d548SEric Biggers 	 * once the key is added.  This pairs with the READ_ONCE() above and in
4002c58d548SEric Biggers 	 * the VFS path lookup code.
4012c58d548SEric Biggers 	 */
4022c58d548SEric Biggers 	if (!has_key ||
4032c58d548SEric Biggers 	    cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
4043b0d8837SEric Biggers 		set_delayed_call(done, kfree_link, pstr.name);
4052c58d548SEric Biggers 
4063b0d8837SEric Biggers 	return pstr.name;
4073b0d8837SEric Biggers 
4083b0d8837SEric Biggers err_kfree:
4093b0d8837SEric Biggers 	kfree(pstr.name);
4103b0d8837SEric Biggers 	return ERR_PTR(err);
4113b0d8837SEric Biggers }
4123b0d8837SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_get_symlink);
413d1876056SEric Biggers 
414d1876056SEric Biggers /**
415d1876056SEric Biggers  * fscrypt_symlink_getattr() - set the correct st_size for encrypted symlinks
416d1876056SEric Biggers  * @path: the path for the encrypted symlink being queried
417d1876056SEric Biggers  * @stat: the struct being filled with the symlink's attributes
418d1876056SEric Biggers  *
419d1876056SEric Biggers  * Override st_size of encrypted symlinks to be the length of the decrypted
420d1876056SEric Biggers  * symlink target (or the no-key encoded symlink target, if the key is
421d1876056SEric Biggers  * unavailable) rather than the length of the encrypted symlink target.  This is
422d1876056SEric Biggers  * necessary for st_size to match the symlink target that userspace actually
423d1876056SEric Biggers  * sees.  POSIX requires this, and some userspace programs depend on it.
424d1876056SEric Biggers  *
425d1876056SEric Biggers  * This requires reading the symlink target from disk if needed, setting up the
426d1876056SEric Biggers  * inode's encryption key if possible, and then decrypting or encoding the
427d1876056SEric Biggers  * symlink target.  This makes lstat() more heavyweight than is normally the
428d1876056SEric Biggers  * case.  However, decrypted symlink targets will be cached in ->i_link, so
429d1876056SEric Biggers  * usually the symlink won't have to be read and decrypted again later if/when
430d1876056SEric Biggers  * it is actually followed, readlink() is called, or lstat() is called again.
431d1876056SEric Biggers  *
432d1876056SEric Biggers  * Return: 0 on success, -errno on failure
433d1876056SEric Biggers  */
fscrypt_symlink_getattr(const struct path * path,struct kstat * stat)434d1876056SEric Biggers int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat)
435d1876056SEric Biggers {
436d1876056SEric Biggers 	struct dentry *dentry = path->dentry;
437d1876056SEric Biggers 	struct inode *inode = d_inode(dentry);
438d1876056SEric Biggers 	const char *link;
439d1876056SEric Biggers 	DEFINE_DELAYED_CALL(done);
440d1876056SEric Biggers 
441d1876056SEric Biggers 	/*
442d1876056SEric Biggers 	 * To get the symlink target that userspace will see (whether it's the
443d1876056SEric Biggers 	 * decrypted target or the no-key encoded target), we can just get it in
444d1876056SEric Biggers 	 * the same way the VFS does during path resolution and readlink().
445d1876056SEric Biggers 	 */
446d1876056SEric Biggers 	link = READ_ONCE(inode->i_link);
447d1876056SEric Biggers 	if (!link) {
448d1876056SEric Biggers 		link = inode->i_op->get_link(dentry, inode, &done);
449d1876056SEric Biggers 		if (IS_ERR(link))
450d1876056SEric Biggers 			return PTR_ERR(link);
451d1876056SEric Biggers 	}
452d1876056SEric Biggers 	stat->size = strlen(link);
453d1876056SEric Biggers 	do_delayed_call(&done);
454d1876056SEric Biggers 	return 0;
455d1876056SEric Biggers }
456d1876056SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_symlink_getattr);
457