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