Whamcloud - gitweb
LU-12275 sec: add llcrypt as file encryption library
[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         err = llcrypt_require_key(inode);
181         if (err)
182                 return err;
183
184         if (disk_link->name) {
185                 /* filesystem-provided buffer */
186                 sd = (struct llcrypt_symlink_data *)disk_link->name;
187         } else {
188                 sd = kmalloc(disk_link->len, GFP_NOFS);
189                 if (!sd)
190                         return -ENOMEM;
191         }
192         ciphertext_len = disk_link->len - sizeof(*sd);
193         sd->len = cpu_to_le16(ciphertext_len);
194
195         err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
196         if (err)
197                 goto err_free_sd;
198
199         /*
200          * Null-terminating the ciphertext doesn't make sense, but we still
201          * count the null terminator in the length, so we might as well
202          * initialize it just in case the filesystem writes it out.
203          */
204         sd->encrypted_path[ciphertext_len] = '\0';
205
206         /* Cache the plaintext symlink target for later use by get_link() */
207         err = -ENOMEM;
208         inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
209         if (!inode->i_link)
210                 goto err_free_sd;
211
212         if (!disk_link->name)
213                 disk_link->name = (unsigned char *)sd;
214         return 0;
215
216 err_free_sd:
217         if (!disk_link->name)
218                 kfree(sd);
219         return err;
220 }
221 EXPORT_SYMBOL_GPL(__llcrypt_encrypt_symlink);
222
223 /**
224  * llcrypt_get_symlink - get the target of an encrypted symlink
225  * @inode: the symlink inode
226  * @caddr: the on-disk contents of the symlink
227  * @max_size: size of @caddr buffer
228  * @done: if successful, will be set up to free the returned target if needed
229  *
230  * If the symlink's encryption key is available, we decrypt its target.
231  * Otherwise, we encode its target for presentation.
232  *
233  * This may sleep, so the filesystem must have dropped out of RCU mode already.
234  *
235  * Return: the presentable symlink target or an ERR_PTR()
236  */
237 const char *llcrypt_get_symlink(struct inode *inode, const void *caddr,
238                                 unsigned int max_size,
239                                 struct delayed_call *done)
240 {
241         const struct llcrypt_symlink_data *sd;
242         struct llcrypt_str cstr, pstr;
243         bool has_key;
244         int err;
245
246         /* This is for encrypted symlinks only */
247         if (WARN_ON(!IS_ENCRYPTED(inode)))
248                 return ERR_PTR(-EINVAL);
249
250         /* If the decrypted target is already cached, just return it. */
251         pstr.name = READ_ONCE(inode->i_link);
252         if (pstr.name)
253                 return pstr.name;
254
255         /*
256          * Try to set up the symlink's encryption key, but we can continue
257          * regardless of whether the key is available or not.
258          */
259         err = llcrypt_get_encryption_info(inode);
260         if (err)
261                 return ERR_PTR(err);
262         has_key = llcrypt_has_encryption_key(inode);
263
264         /*
265          * For historical reasons, encrypted symlink targets are prefixed with
266          * the ciphertext length, even though this is redundant with i_size.
267          */
268
269         if (max_size < sizeof(*sd))
270                 return ERR_PTR(-EUCLEAN);
271         sd = caddr;
272         cstr.name = (unsigned char *)sd->encrypted_path;
273         cstr.len = le16_to_cpu(sd->len);
274
275         if (cstr.len == 0)
276                 return ERR_PTR(-EUCLEAN);
277
278         if (cstr.len + sizeof(*sd) - 1 > max_size)
279                 return ERR_PTR(-EUCLEAN);
280
281         err = llcrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
282         if (err)
283                 return ERR_PTR(err);
284
285         err = llcrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
286         if (err)
287                 goto err_kfree;
288
289         err = -EUCLEAN;
290         if (pstr.name[0] == '\0')
291                 goto err_kfree;
292
293         pstr.name[pstr.len] = '\0';
294
295         /*
296          * Cache decrypted symlink targets in i_link for later use.  Don't cache
297          * symlink targets encoded without the key, since those become outdated
298          * once the key is added.  This pairs with the READ_ONCE() above and in
299          * the VFS path lookup code.
300          */
301         if (!has_key ||
302             cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
303                 set_delayed_call(done, kfree_link, pstr.name);
304
305         return pstr.name;
306
307 err_kfree:
308         kfree(pstr.name);
309         return ERR_PTR(err);
310 }
311 EXPORT_SYMBOL_GPL(llcrypt_get_symlink);