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