162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * fs/crypto/hooks.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Encryption hooks for higher-level filesystem operations.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "fscrypt_private.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/**
1162306a36Sopenharmony_ci * fscrypt_file_open() - prepare to open a possibly-encrypted regular file
1262306a36Sopenharmony_ci * @inode: the inode being opened
1362306a36Sopenharmony_ci * @filp: the struct file being set up
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * Currently, an encrypted regular file can only be opened if its encryption key
1662306a36Sopenharmony_ci * is available; access to the raw encrypted contents is not supported.
1762306a36Sopenharmony_ci * Therefore, we first set up the inode's encryption key (if not already done)
1862306a36Sopenharmony_ci * and return an error if it's unavailable.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * We also verify that if the parent directory (from the path via which the file
2162306a36Sopenharmony_ci * is being opened) is encrypted, then the inode being opened uses the same
2262306a36Sopenharmony_ci * encryption policy.  This is needed as part of the enforcement that all files
2362306a36Sopenharmony_ci * in an encrypted directory tree use the same encryption policy, as a
2462306a36Sopenharmony_ci * protection against certain types of offline attacks.  Note that this check is
2562306a36Sopenharmony_ci * needed even when opening an *unencrypted* file, since it's forbidden to have
2662306a36Sopenharmony_ci * an unencrypted file in an encrypted directory.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ciint fscrypt_file_open(struct inode *inode, struct file *filp)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	int err;
3362306a36Sopenharmony_ci	struct dentry *dir;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	err = fscrypt_require_key(inode);
3662306a36Sopenharmony_ci	if (err)
3762306a36Sopenharmony_ci		return err;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	dir = dget_parent(file_dentry(filp));
4062306a36Sopenharmony_ci	if (IS_ENCRYPTED(d_inode(dir)) &&
4162306a36Sopenharmony_ci	    !fscrypt_has_permitted_context(d_inode(dir), inode)) {
4262306a36Sopenharmony_ci		fscrypt_warn(inode,
4362306a36Sopenharmony_ci			     "Inconsistent encryption context (parent directory: %lu)",
4462306a36Sopenharmony_ci			     d_inode(dir)->i_ino);
4562306a36Sopenharmony_ci		err = -EPERM;
4662306a36Sopenharmony_ci	}
4762306a36Sopenharmony_ci	dput(dir);
4862306a36Sopenharmony_ci	return err;
4962306a36Sopenharmony_ci}
5062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(fscrypt_file_open);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciint __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
5362306a36Sopenharmony_ci			   struct dentry *dentry)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	if (fscrypt_is_nokey_name(dentry))
5662306a36Sopenharmony_ci		return -ENOKEY;
5762306a36Sopenharmony_ci	/*
5862306a36Sopenharmony_ci	 * We don't need to separately check that the directory inode's key is
5962306a36Sopenharmony_ci	 * available, as it's implied by the dentry not being a no-key name.
6062306a36Sopenharmony_ci	 */
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	if (!fscrypt_has_permitted_context(dir, inode))
6362306a36Sopenharmony_ci		return -EXDEV;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	return 0;
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciint __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
7062306a36Sopenharmony_ci			     struct inode *new_dir, struct dentry *new_dentry,
7162306a36Sopenharmony_ci			     unsigned int flags)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	if (fscrypt_is_nokey_name(old_dentry) ||
7462306a36Sopenharmony_ci	    fscrypt_is_nokey_name(new_dentry))
7562306a36Sopenharmony_ci		return -ENOKEY;
7662306a36Sopenharmony_ci	/*
7762306a36Sopenharmony_ci	 * We don't need to separately check that the directory inodes' keys are
7862306a36Sopenharmony_ci	 * available, as it's implied by the dentries not being no-key names.
7962306a36Sopenharmony_ci	 */
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	if (old_dir != new_dir) {
8262306a36Sopenharmony_ci		if (IS_ENCRYPTED(new_dir) &&
8362306a36Sopenharmony_ci		    !fscrypt_has_permitted_context(new_dir,
8462306a36Sopenharmony_ci						   d_inode(old_dentry)))
8562306a36Sopenharmony_ci			return -EXDEV;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci		if ((flags & RENAME_EXCHANGE) &&
8862306a36Sopenharmony_ci		    IS_ENCRYPTED(old_dir) &&
8962306a36Sopenharmony_ci		    !fscrypt_has_permitted_context(old_dir,
9062306a36Sopenharmony_ci						   d_inode(new_dentry)))
9162306a36Sopenharmony_ci			return -EXDEV;
9262306a36Sopenharmony_ci	}
9362306a36Sopenharmony_ci	return 0;
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciint __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
9862306a36Sopenharmony_ci			     struct fscrypt_name *fname)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	if (err && err != -ENOENT)
10362306a36Sopenharmony_ci		return err;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	if (fname->is_nokey_name) {
10662306a36Sopenharmony_ci		spin_lock(&dentry->d_lock);
10762306a36Sopenharmony_ci		dentry->d_flags |= DCACHE_NOKEY_NAME;
10862306a36Sopenharmony_ci		spin_unlock(&dentry->d_lock);
10962306a36Sopenharmony_ci	}
11062306a36Sopenharmony_ci	return err;
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci/**
11562306a36Sopenharmony_ci * fscrypt_prepare_lookup_partial() - prepare lookup without filename setup
11662306a36Sopenharmony_ci * @dir: the encrypted directory being searched
11762306a36Sopenharmony_ci * @dentry: the dentry being looked up in @dir
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci * This function should be used by the ->lookup and ->atomic_open methods of
12062306a36Sopenharmony_ci * filesystems that handle filename encryption and no-key name encoding
12162306a36Sopenharmony_ci * themselves and thus can't use fscrypt_prepare_lookup().  Like
12262306a36Sopenharmony_ci * fscrypt_prepare_lookup(), this will try to set up the directory's encryption
12362306a36Sopenharmony_ci * key and will set DCACHE_NOKEY_NAME on the dentry if the key is unavailable.
12462306a36Sopenharmony_ci * However, this function doesn't set up a struct fscrypt_name for the filename.
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci * Return: 0 on success; -errno on error.  Note that the encryption key being
12762306a36Sopenharmony_ci *	   unavailable is not considered an error.  It is also not an error if
12862306a36Sopenharmony_ci *	   the encryption policy is unsupported by this kernel; that is treated
12962306a36Sopenharmony_ci *	   like the key being unavailable, so that files can still be deleted.
13062306a36Sopenharmony_ci */
13162306a36Sopenharmony_ciint fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	int err = fscrypt_get_encryption_info(dir, true);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	if (!err && !fscrypt_has_encryption_key(dir)) {
13662306a36Sopenharmony_ci		spin_lock(&dentry->d_lock);
13762306a36Sopenharmony_ci		dentry->d_flags |= DCACHE_NOKEY_NAME;
13862306a36Sopenharmony_ci		spin_unlock(&dentry->d_lock);
13962306a36Sopenharmony_ci	}
14062306a36Sopenharmony_ci	return err;
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(fscrypt_prepare_lookup_partial);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ciint __fscrypt_prepare_readdir(struct inode *dir)
14562306a36Sopenharmony_ci{
14662306a36Sopenharmony_ci	return fscrypt_get_encryption_info(dir, true);
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__fscrypt_prepare_readdir);
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciint __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr)
15162306a36Sopenharmony_ci{
15262306a36Sopenharmony_ci	if (attr->ia_valid & ATTR_SIZE)
15362306a36Sopenharmony_ci		return fscrypt_require_key(d_inode(dentry));
15462306a36Sopenharmony_ci	return 0;
15562306a36Sopenharmony_ci}
15662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__fscrypt_prepare_setattr);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci/**
15962306a36Sopenharmony_ci * fscrypt_prepare_setflags() - prepare to change flags with FS_IOC_SETFLAGS
16062306a36Sopenharmony_ci * @inode: the inode on which flags are being changed
16162306a36Sopenharmony_ci * @oldflags: the old flags
16262306a36Sopenharmony_ci * @flags: the new flags
16362306a36Sopenharmony_ci *
16462306a36Sopenharmony_ci * The caller should be holding i_rwsem for write.
16562306a36Sopenharmony_ci *
16662306a36Sopenharmony_ci * Return: 0 on success; -errno if the flags change isn't allowed or if
16762306a36Sopenharmony_ci *	   another error occurs.
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_ciint fscrypt_prepare_setflags(struct inode *inode,
17062306a36Sopenharmony_ci			     unsigned int oldflags, unsigned int flags)
17162306a36Sopenharmony_ci{
17262306a36Sopenharmony_ci	struct fscrypt_info *ci;
17362306a36Sopenharmony_ci	struct fscrypt_master_key *mk;
17462306a36Sopenharmony_ci	int err;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	/*
17762306a36Sopenharmony_ci	 * When the CASEFOLD flag is set on an encrypted directory, we must
17862306a36Sopenharmony_ci	 * derive the secret key needed for the dirhash.  This is only possible
17962306a36Sopenharmony_ci	 * if the directory uses a v2 encryption policy.
18062306a36Sopenharmony_ci	 */
18162306a36Sopenharmony_ci	if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
18262306a36Sopenharmony_ci		err = fscrypt_require_key(inode);
18362306a36Sopenharmony_ci		if (err)
18462306a36Sopenharmony_ci			return err;
18562306a36Sopenharmony_ci		ci = inode->i_crypt_info;
18662306a36Sopenharmony_ci		if (ci->ci_policy.version != FSCRYPT_POLICY_V2)
18762306a36Sopenharmony_ci			return -EINVAL;
18862306a36Sopenharmony_ci		mk = ci->ci_master_key;
18962306a36Sopenharmony_ci		down_read(&mk->mk_sem);
19062306a36Sopenharmony_ci		if (is_master_key_secret_present(&mk->mk_secret))
19162306a36Sopenharmony_ci			err = fscrypt_derive_dirhash_key(ci, mk);
19262306a36Sopenharmony_ci		else
19362306a36Sopenharmony_ci			err = -ENOKEY;
19462306a36Sopenharmony_ci		up_read(&mk->mk_sem);
19562306a36Sopenharmony_ci		return err;
19662306a36Sopenharmony_ci	}
19762306a36Sopenharmony_ci	return 0;
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci/**
20162306a36Sopenharmony_ci * fscrypt_prepare_symlink() - prepare to create a possibly-encrypted symlink
20262306a36Sopenharmony_ci * @dir: directory in which the symlink is being created
20362306a36Sopenharmony_ci * @target: plaintext symlink target
20462306a36Sopenharmony_ci * @len: length of @target excluding null terminator
20562306a36Sopenharmony_ci * @max_len: space the filesystem has available to store the symlink target
20662306a36Sopenharmony_ci * @disk_link: (out) the on-disk symlink target being prepared
20762306a36Sopenharmony_ci *
20862306a36Sopenharmony_ci * This function computes the size the symlink target will require on-disk,
20962306a36Sopenharmony_ci * stores it in @disk_link->len, and validates it against @max_len.  An
21062306a36Sopenharmony_ci * encrypted symlink may be longer than the original.
21162306a36Sopenharmony_ci *
21262306a36Sopenharmony_ci * Additionally, @disk_link->name is set to @target if the symlink will be
21362306a36Sopenharmony_ci * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
21462306a36Sopenharmony_ci * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
21562306a36Sopenharmony_ci * on-disk target later.  (The reason for the two-step process is that some
21662306a36Sopenharmony_ci * filesystems need to know the size of the symlink target before creating the
21762306a36Sopenharmony_ci * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
21862306a36Sopenharmony_ci *
21962306a36Sopenharmony_ci * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
22062306a36Sopenharmony_ci * -ENOKEY if the encryption key is missing, or another -errno code if a problem
22162306a36Sopenharmony_ci * occurred while setting up the encryption key.
22262306a36Sopenharmony_ci */
22362306a36Sopenharmony_ciint fscrypt_prepare_symlink(struct inode *dir, const char *target,
22462306a36Sopenharmony_ci			    unsigned int len, unsigned int max_len,
22562306a36Sopenharmony_ci			    struct fscrypt_str *disk_link)
22662306a36Sopenharmony_ci{
22762306a36Sopenharmony_ci	const union fscrypt_policy *policy;
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	/*
23062306a36Sopenharmony_ci	 * To calculate the size of the encrypted symlink target we need to know
23162306a36Sopenharmony_ci	 * the amount of NUL padding, which is determined by the flags set in
23262306a36Sopenharmony_ci	 * the encryption policy which will be inherited from the directory.
23362306a36Sopenharmony_ci	 */
23462306a36Sopenharmony_ci	policy = fscrypt_policy_to_inherit(dir);
23562306a36Sopenharmony_ci	if (policy == NULL) {
23662306a36Sopenharmony_ci		/* Not encrypted */
23762306a36Sopenharmony_ci		disk_link->name = (unsigned char *)target;
23862306a36Sopenharmony_ci		disk_link->len = len + 1;
23962306a36Sopenharmony_ci		if (disk_link->len > max_len)
24062306a36Sopenharmony_ci			return -ENAMETOOLONG;
24162306a36Sopenharmony_ci		return 0;
24262306a36Sopenharmony_ci	}
24362306a36Sopenharmony_ci	if (IS_ERR(policy))
24462306a36Sopenharmony_ci		return PTR_ERR(policy);
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	/*
24762306a36Sopenharmony_ci	 * Calculate the size of the encrypted symlink and verify it won't
24862306a36Sopenharmony_ci	 * exceed max_len.  Note that for historical reasons, encrypted symlink
24962306a36Sopenharmony_ci	 * targets are prefixed with the ciphertext length, despite this
25062306a36Sopenharmony_ci	 * actually being redundant with i_size.  This decreases by 2 bytes the
25162306a36Sopenharmony_ci	 * longest symlink target we can accept.
25262306a36Sopenharmony_ci	 *
25362306a36Sopenharmony_ci	 * We could recover 1 byte by not counting a null terminator, but
25462306a36Sopenharmony_ci	 * counting it (even though it is meaningless for ciphertext) is simpler
25562306a36Sopenharmony_ci	 * for now since filesystems will assume it is there and subtract it.
25662306a36Sopenharmony_ci	 */
25762306a36Sopenharmony_ci	if (!__fscrypt_fname_encrypted_size(policy, len,
25862306a36Sopenharmony_ci					    max_len - sizeof(struct fscrypt_symlink_data) - 1,
25962306a36Sopenharmony_ci					    &disk_link->len))
26062306a36Sopenharmony_ci		return -ENAMETOOLONG;
26162306a36Sopenharmony_ci	disk_link->len += sizeof(struct fscrypt_symlink_data) + 1;
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	disk_link->name = NULL;
26462306a36Sopenharmony_ci	return 0;
26562306a36Sopenharmony_ci}
26662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(fscrypt_prepare_symlink);
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ciint __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
26962306a36Sopenharmony_ci			      unsigned int len, struct fscrypt_str *disk_link)
27062306a36Sopenharmony_ci{
27162306a36Sopenharmony_ci	int err;
27262306a36Sopenharmony_ci	struct qstr iname = QSTR_INIT(target, len);
27362306a36Sopenharmony_ci	struct fscrypt_symlink_data *sd;
27462306a36Sopenharmony_ci	unsigned int ciphertext_len;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	/*
27762306a36Sopenharmony_ci	 * fscrypt_prepare_new_inode() should have already set up the new
27862306a36Sopenharmony_ci	 * symlink inode's encryption key.  We don't wait until now to do it,
27962306a36Sopenharmony_ci	 * since we may be in a filesystem transaction now.
28062306a36Sopenharmony_ci	 */
28162306a36Sopenharmony_ci	if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
28262306a36Sopenharmony_ci		return -ENOKEY;
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	if (disk_link->name) {
28562306a36Sopenharmony_ci		/* filesystem-provided buffer */
28662306a36Sopenharmony_ci		sd = (struct fscrypt_symlink_data *)disk_link->name;
28762306a36Sopenharmony_ci	} else {
28862306a36Sopenharmony_ci		sd = kmalloc(disk_link->len, GFP_NOFS);
28962306a36Sopenharmony_ci		if (!sd)
29062306a36Sopenharmony_ci			return -ENOMEM;
29162306a36Sopenharmony_ci	}
29262306a36Sopenharmony_ci	ciphertext_len = disk_link->len - sizeof(*sd) - 1;
29362306a36Sopenharmony_ci	sd->len = cpu_to_le16(ciphertext_len);
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	err = fscrypt_fname_encrypt(inode, &iname, sd->encrypted_path,
29662306a36Sopenharmony_ci				    ciphertext_len);
29762306a36Sopenharmony_ci	if (err)
29862306a36Sopenharmony_ci		goto err_free_sd;
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci	/*
30162306a36Sopenharmony_ci	 * Null-terminating the ciphertext doesn't make sense, but we still
30262306a36Sopenharmony_ci	 * count the null terminator in the length, so we might as well
30362306a36Sopenharmony_ci	 * initialize it just in case the filesystem writes it out.
30462306a36Sopenharmony_ci	 */
30562306a36Sopenharmony_ci	sd->encrypted_path[ciphertext_len] = '\0';
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	/* Cache the plaintext symlink target for later use by get_link() */
30862306a36Sopenharmony_ci	err = -ENOMEM;
30962306a36Sopenharmony_ci	inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
31062306a36Sopenharmony_ci	if (!inode->i_link)
31162306a36Sopenharmony_ci		goto err_free_sd;
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	if (!disk_link->name)
31462306a36Sopenharmony_ci		disk_link->name = (unsigned char *)sd;
31562306a36Sopenharmony_ci	return 0;
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_cierr_free_sd:
31862306a36Sopenharmony_ci	if (!disk_link->name)
31962306a36Sopenharmony_ci		kfree(sd);
32062306a36Sopenharmony_ci	return err;
32162306a36Sopenharmony_ci}
32262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci/**
32562306a36Sopenharmony_ci * fscrypt_get_symlink() - get the target of an encrypted symlink
32662306a36Sopenharmony_ci * @inode: the symlink inode
32762306a36Sopenharmony_ci * @caddr: the on-disk contents of the symlink
32862306a36Sopenharmony_ci * @max_size: size of @caddr buffer
32962306a36Sopenharmony_ci * @done: if successful, will be set up to free the returned target if needed
33062306a36Sopenharmony_ci *
33162306a36Sopenharmony_ci * If the symlink's encryption key is available, we decrypt its target.
33262306a36Sopenharmony_ci * Otherwise, we encode its target for presentation.
33362306a36Sopenharmony_ci *
33462306a36Sopenharmony_ci * This may sleep, so the filesystem must have dropped out of RCU mode already.
33562306a36Sopenharmony_ci *
33662306a36Sopenharmony_ci * Return: the presentable symlink target or an ERR_PTR()
33762306a36Sopenharmony_ci */
33862306a36Sopenharmony_ciconst char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
33962306a36Sopenharmony_ci				unsigned int max_size,
34062306a36Sopenharmony_ci				struct delayed_call *done)
34162306a36Sopenharmony_ci{
34262306a36Sopenharmony_ci	const struct fscrypt_symlink_data *sd;
34362306a36Sopenharmony_ci	struct fscrypt_str cstr, pstr;
34462306a36Sopenharmony_ci	bool has_key;
34562306a36Sopenharmony_ci	int err;
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci	/* This is for encrypted symlinks only */
34862306a36Sopenharmony_ci	if (WARN_ON_ONCE(!IS_ENCRYPTED(inode)))
34962306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	/* If the decrypted target is already cached, just return it. */
35262306a36Sopenharmony_ci	pstr.name = READ_ONCE(inode->i_link);
35362306a36Sopenharmony_ci	if (pstr.name)
35462306a36Sopenharmony_ci		return pstr.name;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	/*
35762306a36Sopenharmony_ci	 * Try to set up the symlink's encryption key, but we can continue
35862306a36Sopenharmony_ci	 * regardless of whether the key is available or not.
35962306a36Sopenharmony_ci	 */
36062306a36Sopenharmony_ci	err = fscrypt_get_encryption_info(inode, false);
36162306a36Sopenharmony_ci	if (err)
36262306a36Sopenharmony_ci		return ERR_PTR(err);
36362306a36Sopenharmony_ci	has_key = fscrypt_has_encryption_key(inode);
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	/*
36662306a36Sopenharmony_ci	 * For historical reasons, encrypted symlink targets are prefixed with
36762306a36Sopenharmony_ci	 * the ciphertext length, even though this is redundant with i_size.
36862306a36Sopenharmony_ci	 */
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	if (max_size < sizeof(*sd) + 1)
37162306a36Sopenharmony_ci		return ERR_PTR(-EUCLEAN);
37262306a36Sopenharmony_ci	sd = caddr;
37362306a36Sopenharmony_ci	cstr.name = (unsigned char *)sd->encrypted_path;
37462306a36Sopenharmony_ci	cstr.len = le16_to_cpu(sd->len);
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	if (cstr.len == 0)
37762306a36Sopenharmony_ci		return ERR_PTR(-EUCLEAN);
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	if (cstr.len + sizeof(*sd) > max_size)
38062306a36Sopenharmony_ci		return ERR_PTR(-EUCLEAN);
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	err = fscrypt_fname_alloc_buffer(cstr.len, &pstr);
38362306a36Sopenharmony_ci	if (err)
38462306a36Sopenharmony_ci		return ERR_PTR(err);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
38762306a36Sopenharmony_ci	if (err)
38862306a36Sopenharmony_ci		goto err_kfree;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	err = -EUCLEAN;
39162306a36Sopenharmony_ci	if (pstr.name[0] == '\0')
39262306a36Sopenharmony_ci		goto err_kfree;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	pstr.name[pstr.len] = '\0';
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	/*
39762306a36Sopenharmony_ci	 * Cache decrypted symlink targets in i_link for later use.  Don't cache
39862306a36Sopenharmony_ci	 * symlink targets encoded without the key, since those become outdated
39962306a36Sopenharmony_ci	 * once the key is added.  This pairs with the READ_ONCE() above and in
40062306a36Sopenharmony_ci	 * the VFS path lookup code.
40162306a36Sopenharmony_ci	 */
40262306a36Sopenharmony_ci	if (!has_key ||
40362306a36Sopenharmony_ci	    cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
40462306a36Sopenharmony_ci		set_delayed_call(done, kfree_link, pstr.name);
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	return pstr.name;
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_cierr_kfree:
40962306a36Sopenharmony_ci	kfree(pstr.name);
41062306a36Sopenharmony_ci	return ERR_PTR(err);
41162306a36Sopenharmony_ci}
41262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(fscrypt_get_symlink);
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci/**
41562306a36Sopenharmony_ci * fscrypt_symlink_getattr() - set the correct st_size for encrypted symlinks
41662306a36Sopenharmony_ci * @path: the path for the encrypted symlink being queried
41762306a36Sopenharmony_ci * @stat: the struct being filled with the symlink's attributes
41862306a36Sopenharmony_ci *
41962306a36Sopenharmony_ci * Override st_size of encrypted symlinks to be the length of the decrypted
42062306a36Sopenharmony_ci * symlink target (or the no-key encoded symlink target, if the key is
42162306a36Sopenharmony_ci * unavailable) rather than the length of the encrypted symlink target.  This is
42262306a36Sopenharmony_ci * necessary for st_size to match the symlink target that userspace actually
42362306a36Sopenharmony_ci * sees.  POSIX requires this, and some userspace programs depend on it.
42462306a36Sopenharmony_ci *
42562306a36Sopenharmony_ci * This requires reading the symlink target from disk if needed, setting up the
42662306a36Sopenharmony_ci * inode's encryption key if possible, and then decrypting or encoding the
42762306a36Sopenharmony_ci * symlink target.  This makes lstat() more heavyweight than is normally the
42862306a36Sopenharmony_ci * case.  However, decrypted symlink targets will be cached in ->i_link, so
42962306a36Sopenharmony_ci * usually the symlink won't have to be read and decrypted again later if/when
43062306a36Sopenharmony_ci * it is actually followed, readlink() is called, or lstat() is called again.
43162306a36Sopenharmony_ci *
43262306a36Sopenharmony_ci * Return: 0 on success, -errno on failure
43362306a36Sopenharmony_ci */
43462306a36Sopenharmony_ciint fscrypt_symlink_getattr(const struct path *path, struct kstat *stat)
43562306a36Sopenharmony_ci{
43662306a36Sopenharmony_ci	struct dentry *dentry = path->dentry;
43762306a36Sopenharmony_ci	struct inode *inode = d_inode(dentry);
43862306a36Sopenharmony_ci	const char *link;
43962306a36Sopenharmony_ci	DEFINE_DELAYED_CALL(done);
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	/*
44262306a36Sopenharmony_ci	 * To get the symlink target that userspace will see (whether it's the
44362306a36Sopenharmony_ci	 * decrypted target or the no-key encoded target), we can just get it in
44462306a36Sopenharmony_ci	 * the same way the VFS does during path resolution and readlink().
44562306a36Sopenharmony_ci	 */
44662306a36Sopenharmony_ci	link = READ_ONCE(inode->i_link);
44762306a36Sopenharmony_ci	if (!link) {
44862306a36Sopenharmony_ci		link = inode->i_op->get_link(dentry, inode, &done);
44962306a36Sopenharmony_ci		if (IS_ERR(link))
45062306a36Sopenharmony_ci			return PTR_ERR(link);
45162306a36Sopenharmony_ci	}
45262306a36Sopenharmony_ci	stat->size = strlen(link);
45362306a36Sopenharmony_ci	do_delayed_call(&done);
45462306a36Sopenharmony_ci	return 0;
45562306a36Sopenharmony_ci}
45662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(fscrypt_symlink_getattr);
457