xref: /openbmc/linux/fs/crypto/hooks.c (revision d6fc9fcb)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * fs/crypto/hooks.c
4  *
5  * Encryption hooks for higher-level filesystem operations.
6  */
7 
8 #include <linux/ratelimit.h>
9 #include "fscrypt_private.h"
10 
11 /**
12  * fscrypt_file_open - prepare to open a possibly-encrypted regular file
13  * @inode: the inode being opened
14  * @filp: the struct file being set up
15  *
16  * Currently, an encrypted regular file can only be opened if its encryption key
17  * is available; access to the raw encrypted contents is not supported.
18  * Therefore, we first set up the inode's encryption key (if not already done)
19  * and return an error if it's unavailable.
20  *
21  * We also verify that if the parent directory (from the path via which the file
22  * is being opened) is encrypted, then the inode being opened uses the same
23  * encryption policy.  This is needed as part of the enforcement that all files
24  * in an encrypted directory tree use the same encryption policy, as a
25  * protection against certain types of offline attacks.  Note that this check is
26  * needed even when opening an *unencrypted* file, since it's forbidden to have
27  * an unencrypted file in an encrypted directory.
28  *
29  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
30  */
31 int fscrypt_file_open(struct inode *inode, struct file *filp)
32 {
33 	int err;
34 	struct dentry *dir;
35 
36 	err = fscrypt_require_key(inode);
37 	if (err)
38 		return err;
39 
40 	dir = dget_parent(file_dentry(filp));
41 	if (IS_ENCRYPTED(d_inode(dir)) &&
42 	    !fscrypt_has_permitted_context(d_inode(dir), inode)) {
43 		fscrypt_warn(inode->i_sb,
44 			     "inconsistent encryption contexts: %lu/%lu",
45 			     d_inode(dir)->i_ino, inode->i_ino);
46 		err = -EPERM;
47 	}
48 	dput(dir);
49 	return err;
50 }
51 EXPORT_SYMBOL_GPL(fscrypt_file_open);
52 
53 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
54 			   struct dentry *dentry)
55 {
56 	int err;
57 
58 	err = fscrypt_require_key(dir);
59 	if (err)
60 		return err;
61 
62 	/* ... in case we looked up ciphertext name before key was added */
63 	if (dentry->d_flags & DCACHE_ENCRYPTED_NAME)
64 		return -ENOKEY;
65 
66 	if (!fscrypt_has_permitted_context(dir, inode))
67 		return -EXDEV;
68 
69 	return 0;
70 }
71 EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
72 
73 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
74 			     struct inode *new_dir, struct dentry *new_dentry,
75 			     unsigned int flags)
76 {
77 	int err;
78 
79 	err = fscrypt_require_key(old_dir);
80 	if (err)
81 		return err;
82 
83 	err = fscrypt_require_key(new_dir);
84 	if (err)
85 		return err;
86 
87 	/* ... in case we looked up ciphertext name(s) before key was added */
88 	if ((old_dentry->d_flags | new_dentry->d_flags) &
89 	    DCACHE_ENCRYPTED_NAME)
90 		return -ENOKEY;
91 
92 	if (old_dir != new_dir) {
93 		if (IS_ENCRYPTED(new_dir) &&
94 		    !fscrypt_has_permitted_context(new_dir,
95 						   d_inode(old_dentry)))
96 			return -EXDEV;
97 
98 		if ((flags & RENAME_EXCHANGE) &&
99 		    IS_ENCRYPTED(old_dir) &&
100 		    !fscrypt_has_permitted_context(old_dir,
101 						   d_inode(new_dentry)))
102 			return -EXDEV;
103 	}
104 	return 0;
105 }
106 EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
107 
108 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
109 			     struct fscrypt_name *fname)
110 {
111 	int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
112 
113 	if (err && err != -ENOENT)
114 		return err;
115 
116 	if (fname->is_ciphertext_name) {
117 		spin_lock(&dentry->d_lock);
118 		dentry->d_flags |= DCACHE_ENCRYPTED_NAME;
119 		spin_unlock(&dentry->d_lock);
120 		d_set_d_op(dentry, &fscrypt_d_ops);
121 	}
122 	return err;
123 }
124 EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
125 
126 int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
127 			      unsigned int max_len,
128 			      struct fscrypt_str *disk_link)
129 {
130 	int err;
131 
132 	/*
133 	 * To calculate the size of the encrypted symlink target we need to know
134 	 * the amount of NUL padding, which is determined by the flags set in
135 	 * the encryption policy which will be inherited from the directory.
136 	 * The easiest way to get access to this is to just load the directory's
137 	 * fscrypt_info, since we'll need it to create the dir_entry anyway.
138 	 *
139 	 * Note: in test_dummy_encryption mode, @dir may be unencrypted.
140 	 */
141 	err = fscrypt_get_encryption_info(dir);
142 	if (err)
143 		return err;
144 	if (!fscrypt_has_encryption_key(dir))
145 		return -ENOKEY;
146 
147 	/*
148 	 * Calculate the size of the encrypted symlink and verify it won't
149 	 * exceed max_len.  Note that for historical reasons, encrypted symlink
150 	 * targets are prefixed with the ciphertext length, despite this
151 	 * actually being redundant with i_size.  This decreases by 2 bytes the
152 	 * longest symlink target we can accept.
153 	 *
154 	 * We could recover 1 byte by not counting a null terminator, but
155 	 * counting it (even though it is meaningless for ciphertext) is simpler
156 	 * for now since filesystems will assume it is there and subtract it.
157 	 */
158 	if (!fscrypt_fname_encrypted_size(dir, len,
159 					  max_len - sizeof(struct fscrypt_symlink_data),
160 					  &disk_link->len))
161 		return -ENAMETOOLONG;
162 	disk_link->len += sizeof(struct fscrypt_symlink_data);
163 
164 	disk_link->name = NULL;
165 	return 0;
166 }
167 EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
168 
169 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
170 			      unsigned int len, struct fscrypt_str *disk_link)
171 {
172 	int err;
173 	struct qstr iname = QSTR_INIT(target, len);
174 	struct fscrypt_symlink_data *sd;
175 	unsigned int ciphertext_len;
176 
177 	err = fscrypt_require_key(inode);
178 	if (err)
179 		return err;
180 
181 	if (disk_link->name) {
182 		/* filesystem-provided buffer */
183 		sd = (struct fscrypt_symlink_data *)disk_link->name;
184 	} else {
185 		sd = kmalloc(disk_link->len, GFP_NOFS);
186 		if (!sd)
187 			return -ENOMEM;
188 	}
189 	ciphertext_len = disk_link->len - sizeof(*sd);
190 	sd->len = cpu_to_le16(ciphertext_len);
191 
192 	err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
193 	if (err)
194 		goto err_free_sd;
195 
196 	/*
197 	 * Null-terminating the ciphertext doesn't make sense, but we still
198 	 * count the null terminator in the length, so we might as well
199 	 * initialize it just in case the filesystem writes it out.
200 	 */
201 	sd->encrypted_path[ciphertext_len] = '\0';
202 
203 	/* Cache the plaintext symlink target for later use by get_link() */
204 	err = -ENOMEM;
205 	inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
206 	if (!inode->i_link)
207 		goto err_free_sd;
208 
209 	if (!disk_link->name)
210 		disk_link->name = (unsigned char *)sd;
211 	return 0;
212 
213 err_free_sd:
214 	if (!disk_link->name)
215 		kfree(sd);
216 	return err;
217 }
218 EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
219 
220 /**
221  * fscrypt_get_symlink - get the target of an encrypted symlink
222  * @inode: the symlink inode
223  * @caddr: the on-disk contents of the symlink
224  * @max_size: size of @caddr buffer
225  * @done: if successful, will be set up to free the returned target if needed
226  *
227  * If the symlink's encryption key is available, we decrypt its target.
228  * Otherwise, we encode its target for presentation.
229  *
230  * This may sleep, so the filesystem must have dropped out of RCU mode already.
231  *
232  * Return: the presentable symlink target or an ERR_PTR()
233  */
234 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
235 				unsigned int max_size,
236 				struct delayed_call *done)
237 {
238 	const struct fscrypt_symlink_data *sd;
239 	struct fscrypt_str cstr, pstr;
240 	bool has_key;
241 	int err;
242 
243 	/* This is for encrypted symlinks only */
244 	if (WARN_ON(!IS_ENCRYPTED(inode)))
245 		return ERR_PTR(-EINVAL);
246 
247 	/* If the decrypted target is already cached, just return it. */
248 	pstr.name = READ_ONCE(inode->i_link);
249 	if (pstr.name)
250 		return pstr.name;
251 
252 	/*
253 	 * Try to set up the symlink's encryption key, but we can continue
254 	 * regardless of whether the key is available or not.
255 	 */
256 	err = fscrypt_get_encryption_info(inode);
257 	if (err)
258 		return ERR_PTR(err);
259 	has_key = fscrypt_has_encryption_key(inode);
260 
261 	/*
262 	 * For historical reasons, encrypted symlink targets are prefixed with
263 	 * the ciphertext length, even though this is redundant with i_size.
264 	 */
265 
266 	if (max_size < sizeof(*sd))
267 		return ERR_PTR(-EUCLEAN);
268 	sd = caddr;
269 	cstr.name = (unsigned char *)sd->encrypted_path;
270 	cstr.len = le16_to_cpu(sd->len);
271 
272 	if (cstr.len == 0)
273 		return ERR_PTR(-EUCLEAN);
274 
275 	if (cstr.len + sizeof(*sd) - 1 > max_size)
276 		return ERR_PTR(-EUCLEAN);
277 
278 	err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
279 	if (err)
280 		return ERR_PTR(err);
281 
282 	err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
283 	if (err)
284 		goto err_kfree;
285 
286 	err = -EUCLEAN;
287 	if (pstr.name[0] == '\0')
288 		goto err_kfree;
289 
290 	pstr.name[pstr.len] = '\0';
291 
292 	/*
293 	 * Cache decrypted symlink targets in i_link for later use.  Don't cache
294 	 * symlink targets encoded without the key, since those become outdated
295 	 * once the key is added.  This pairs with the READ_ONCE() above and in
296 	 * the VFS path lookup code.
297 	 */
298 	if (!has_key ||
299 	    cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
300 		set_delayed_call(done, kfree_link, pstr.name);
301 
302 	return pstr.name;
303 
304 err_kfree:
305 	kfree(pstr.name);
306 	return ERR_PTR(err);
307 }
308 EXPORT_SYMBOL_GPL(fscrypt_get_symlink);
309