xref: /openbmc/linux/fs/crypto/hooks.c (revision 96de2506)
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 {
54 	int err;
55 
56 	err = fscrypt_require_key(dir);
57 	if (err)
58 		return err;
59 
60 	if (!fscrypt_has_permitted_context(dir, inode))
61 		return -EPERM;
62 
63 	return 0;
64 }
65 EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
66 
67 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
68 			     struct inode *new_dir, struct dentry *new_dentry,
69 			     unsigned int flags)
70 {
71 	int err;
72 
73 	err = fscrypt_require_key(old_dir);
74 	if (err)
75 		return err;
76 
77 	err = fscrypt_require_key(new_dir);
78 	if (err)
79 		return err;
80 
81 	if (old_dir != new_dir) {
82 		if (IS_ENCRYPTED(new_dir) &&
83 		    !fscrypt_has_permitted_context(new_dir,
84 						   d_inode(old_dentry)))
85 			return -EPERM;
86 
87 		if ((flags & RENAME_EXCHANGE) &&
88 		    IS_ENCRYPTED(old_dir) &&
89 		    !fscrypt_has_permitted_context(old_dir,
90 						   d_inode(new_dentry)))
91 			return -EPERM;
92 	}
93 	return 0;
94 }
95 EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
96 
97 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry)
98 {
99 	int err = fscrypt_get_encryption_info(dir);
100 
101 	if (err)
102 		return err;
103 
104 	if (fscrypt_has_encryption_key(dir)) {
105 		spin_lock(&dentry->d_lock);
106 		dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
107 		spin_unlock(&dentry->d_lock);
108 	}
109 
110 	d_set_d_op(dentry, &fscrypt_d_ops);
111 	return 0;
112 }
113 EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
114 
115 int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
116 			      unsigned int max_len,
117 			      struct fscrypt_str *disk_link)
118 {
119 	int err;
120 
121 	/*
122 	 * To calculate the size of the encrypted symlink target we need to know
123 	 * the amount of NUL padding, which is determined by the flags set in
124 	 * the encryption policy which will be inherited from the directory.
125 	 * The easiest way to get access to this is to just load the directory's
126 	 * fscrypt_info, since we'll need it to create the dir_entry anyway.
127 	 *
128 	 * Note: in test_dummy_encryption mode, @dir may be unencrypted.
129 	 */
130 	err = fscrypt_get_encryption_info(dir);
131 	if (err)
132 		return err;
133 	if (!fscrypt_has_encryption_key(dir))
134 		return -ENOKEY;
135 
136 	/*
137 	 * Calculate the size of the encrypted symlink and verify it won't
138 	 * exceed max_len.  Note that for historical reasons, encrypted symlink
139 	 * targets are prefixed with the ciphertext length, despite this
140 	 * actually being redundant with i_size.  This decreases by 2 bytes the
141 	 * longest symlink target we can accept.
142 	 *
143 	 * We could recover 1 byte by not counting a null terminator, but
144 	 * counting it (even though it is meaningless for ciphertext) is simpler
145 	 * for now since filesystems will assume it is there and subtract it.
146 	 */
147 	if (!fscrypt_fname_encrypted_size(dir, len,
148 					  max_len - sizeof(struct fscrypt_symlink_data),
149 					  &disk_link->len))
150 		return -ENAMETOOLONG;
151 	disk_link->len += sizeof(struct fscrypt_symlink_data);
152 
153 	disk_link->name = NULL;
154 	return 0;
155 }
156 EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
157 
158 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
159 			      unsigned int len, struct fscrypt_str *disk_link)
160 {
161 	int err;
162 	struct qstr iname = QSTR_INIT(target, len);
163 	struct fscrypt_symlink_data *sd;
164 	unsigned int ciphertext_len;
165 
166 	err = fscrypt_require_key(inode);
167 	if (err)
168 		return err;
169 
170 	if (disk_link->name) {
171 		/* filesystem-provided buffer */
172 		sd = (struct fscrypt_symlink_data *)disk_link->name;
173 	} else {
174 		sd = kmalloc(disk_link->len, GFP_NOFS);
175 		if (!sd)
176 			return -ENOMEM;
177 	}
178 	ciphertext_len = disk_link->len - sizeof(*sd);
179 	sd->len = cpu_to_le16(ciphertext_len);
180 
181 	err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
182 	if (err) {
183 		if (!disk_link->name)
184 			kfree(sd);
185 		return err;
186 	}
187 	/*
188 	 * Null-terminating the ciphertext doesn't make sense, but we still
189 	 * count the null terminator in the length, so we might as well
190 	 * initialize it just in case the filesystem writes it out.
191 	 */
192 	sd->encrypted_path[ciphertext_len] = '\0';
193 
194 	if (!disk_link->name)
195 		disk_link->name = (unsigned char *)sd;
196 	return 0;
197 }
198 EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
199 
200 /**
201  * fscrypt_get_symlink - get the target of an encrypted symlink
202  * @inode: the symlink inode
203  * @caddr: the on-disk contents of the symlink
204  * @max_size: size of @caddr buffer
205  * @done: if successful, will be set up to free the returned target
206  *
207  * If the symlink's encryption key is available, we decrypt its target.
208  * Otherwise, we encode its target for presentation.
209  *
210  * This may sleep, so the filesystem must have dropped out of RCU mode already.
211  *
212  * Return: the presentable symlink target or an ERR_PTR()
213  */
214 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
215 				unsigned int max_size,
216 				struct delayed_call *done)
217 {
218 	const struct fscrypt_symlink_data *sd;
219 	struct fscrypt_str cstr, pstr;
220 	int err;
221 
222 	/* This is for encrypted symlinks only */
223 	if (WARN_ON(!IS_ENCRYPTED(inode)))
224 		return ERR_PTR(-EINVAL);
225 
226 	/*
227 	 * Try to set up the symlink's encryption key, but we can continue
228 	 * regardless of whether the key is available or not.
229 	 */
230 	err = fscrypt_get_encryption_info(inode);
231 	if (err)
232 		return ERR_PTR(err);
233 
234 	/*
235 	 * For historical reasons, encrypted symlink targets are prefixed with
236 	 * the ciphertext length, even though this is redundant with i_size.
237 	 */
238 
239 	if (max_size < sizeof(*sd))
240 		return ERR_PTR(-EUCLEAN);
241 	sd = caddr;
242 	cstr.name = (unsigned char *)sd->encrypted_path;
243 	cstr.len = le16_to_cpu(sd->len);
244 
245 	if (cstr.len == 0)
246 		return ERR_PTR(-EUCLEAN);
247 
248 	if (cstr.len + sizeof(*sd) - 1 > max_size)
249 		return ERR_PTR(-EUCLEAN);
250 
251 	err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
252 	if (err)
253 		return ERR_PTR(err);
254 
255 	err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
256 	if (err)
257 		goto err_kfree;
258 
259 	err = -EUCLEAN;
260 	if (pstr.name[0] == '\0')
261 		goto err_kfree;
262 
263 	pstr.name[pstr.len] = '\0';
264 	set_delayed_call(done, kfree_link, pstr.name);
265 	return pstr.name;
266 
267 err_kfree:
268 	kfree(pstr.name);
269 	return ERR_PTR(err);
270 }
271 EXPORT_SYMBOL_GPL(fscrypt_get_symlink);
272