Whamcloud - gitweb
89917c71a553d31a713918b707dff77dcaf56e95
[fs/lustre-release.git] / lustre / llite / crypto.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2019, 2020, Whamcloud.
24  */
25 /*
26  * This file is part of Lustre, http://www.lustre.org/
27  */
28
29 #include "llite_internal.h"
30
31 #ifdef HAVE_LUSTRE_CRYPTO
32 #include <libcfs/libcfs_crypto.h>
33
34 static int ll_get_context(struct inode *inode, void *ctx, size_t len)
35 {
36         struct dentry *dentry = d_find_any_alias(inode);
37         int rc;
38
39         rc = ll_vfs_getxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
40                              ctx, len);
41         if (dentry)
42                 dput(dentry);
43
44         /* used as encryption unit size */
45         if (S_ISREG(inode->i_mode))
46                 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
47         return rc;
48 }
49
50 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
51                     bool preload)
52 {
53         unsigned int ext_flags;
54         int rc = 0;
55
56         /* used as encryption unit size */
57         if (S_ISREG(inode->i_mode))
58                 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
59         ext_flags = ll_inode_to_ext_flags(inode->i_flags) | LUSTRE_ENCRYPT_FL;
60         ll_update_inode_flags(inode, ext_flags);
61
62         if (encctx && encctxlen)
63                 rc = ll_xattr_cache_insert(inode,
64                                            LL_XATTR_NAME_ENCRYPTION_CONTEXT,
65                                            encctx, encctxlen);
66         if (rc)
67                 return rc;
68
69         return preload ? llcrypt_get_encryption_info(inode) : 0;
70 }
71
72 /* ll_set_context has 2 distinct behaviors, depending on the value of inode
73  * parameter:
74  * - inode is NULL:
75  *   passed fs_data is a struct md_op_data *. We need to store enc ctx in
76  *   op_data, so that it will be sent along to the server with the request that
77  *   the caller is preparing, thus saving a setxattr request.
78  * - inode is not NULL:
79  *   normal case in which passed fs_data is a struct dentry *, letting proceed
80  *   with setxattr operation.
81  *   This use case should only be used when explicitly setting a new encryption
82  *   policy on an existing, empty directory.
83  */
84 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
85                           void *fs_data)
86 {
87         struct dentry *dentry;
88         int rc;
89
90         if (inode == NULL) {
91                 struct md_op_data *op_data = (struct md_op_data *)fs_data;
92
93                 if (!op_data)
94                         return -EINVAL;
95
96                 OBD_ALLOC(op_data->op_file_encctx, len);
97                 if (op_data->op_file_encctx == NULL)
98                         return -ENOMEM;
99                 op_data->op_file_encctx_size = len;
100                 memcpy(op_data->op_file_encctx, ctx, len);
101                 return 0;
102         }
103
104         /* Encrypting the root directory is not allowed */
105         if (is_root_inode(inode))
106                 return -EPERM;
107
108         dentry = (struct dentry *)fs_data;
109         set_bit(LLIF_SET_ENC_CTX, &ll_i2info(inode)->lli_flags);
110         rc = ll_vfs_setxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
111                              ctx, len, XATTR_CREATE);
112         if (rc)
113                 return rc;
114
115         return ll_set_encflags(inode, (void *)ctx, len, false);
116 }
117
118 void llcrypt_free_ctx(void *encctx, __u32 size)
119 {
120         if (encctx)
121                 OBD_FREE(encctx, size);
122 }
123
124 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
125 {
126         return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
127 }
128
129 static bool ll_dummy_context(struct inode *inode)
130 {
131         struct ll_sb_info *sbi = ll_i2sbi(inode);
132
133         return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
134 }
135
136 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
137 {
138         return sbi->ll_flags & LL_SBI_ENCRYPT;
139 }
140
141 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
142 {
143         if (set)
144                 sbi->ll_flags |= LL_SBI_ENCRYPT;
145         else
146                 sbi->ll_flags &=
147                         ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
148 }
149
150 static bool ll_empty_dir(struct inode *inode)
151 {
152         /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
153          * on an empty dir.
154          */
155         /* Here we choose to return true, meaning we always call .set_context.
156          * Then we rely on server side, with mdd_fix_attr() that calls
157          * mdd_dir_is_empty() when setting encryption flag on directory.
158          */
159         return true;
160 }
161
162 /**
163  * ll_setup_filename() - overlay to llcrypt_setup_filename
164  * @dir: the directory that will be searched
165  * @iname: the user-provided filename being searched for
166  * @lookup: 1 if we're allowed to proceed without the key because it's
167  *      ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
168  *      proceed without the key because we're going to create the dir_entry.
169  * @fname: the filename information to be filled in
170  * @fid: fid retrieved from user-provided filename
171  *
172  * This overlay function is necessary to properly encode @fname after
173  * encryption, as it will be sent over the wire.
174  * This overlay function is also necessary to handle the case of operations
175  * carried out without the key. Normally llcrypt makes use of digested names in
176  * that case. Having a digested name works for local file systems that can call
177  * llcrypt_match_name(), but Lustre server side is not aware of encryption.
178  * So for keyless @lookup operations on long names, for Lustre we choose to
179  * present to users the encoded struct ll_digest_filename, instead of a digested
180  * name. FID and name hash can then easily be extracted and put into the
181  * requests sent to servers.
182  */
183 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
184                       int lookup, struct llcrypt_name *fname,
185                       struct lu_fid *fid)
186 {
187         int digested = 0;
188         struct qstr dname;
189         int rc;
190
191         if (fid && IS_ENCRYPTED(dir) && !llcrypt_has_encryption_key(dir) &&
192             iname->name[0] == '_')
193                 digested = 1;
194
195         dname.name = iname->name + digested;
196         dname.len = iname->len - digested;
197
198         if (fid) {
199                 fid->f_seq = 0;
200                 fid->f_oid = 0;
201                 fid->f_ver = 0;
202         }
203         rc = llcrypt_setup_filename(dir, &dname, lookup, fname);
204         if (rc)
205                 return rc;
206
207         if (digested) {
208                 /* Without the key, for long names user should have struct
209                  * ll_digest_filename representation of the dentry instead of
210                  * the name. So make sure it is valid, return fid and put
211                  * excerpt of cipher text name in disk_name.
212                  */
213                 struct ll_digest_filename *digest;
214
215                 if (fname->crypto_buf.len < sizeof(struct ll_digest_filename)) {
216                         rc = -EINVAL;
217                         goto out_free;
218                 }
219                 digest = (struct ll_digest_filename *)fname->crypto_buf.name;
220                 *fid = digest->ldf_fid;
221                 if (!fid_is_sane(fid)) {
222                         rc = -EINVAL;
223                         goto out_free;
224                 }
225                 fname->disk_name.name = digest->ldf_excerpt;
226                 fname->disk_name.len = LLCRYPT_FNAME_DIGEST_SIZE;
227         }
228         if (IS_ENCRYPTED(dir) &&
229             !name_is_dot_or_dotdot(fname->disk_name.name,
230                                    fname->disk_name.len)) {
231                 int presented_len = critical_chars(fname->disk_name.name,
232                                                    fname->disk_name.len);
233                 char *buf;
234
235                 buf = kmalloc(presented_len + 1, GFP_NOFS);
236                 if (!buf) {
237                         rc = -ENOMEM;
238                         goto out_free;
239                 }
240
241                 if (presented_len == fname->disk_name.len)
242                         memcpy(buf, fname->disk_name.name, presented_len);
243                 else
244                         critical_encode(fname->disk_name.name,
245                                         fname->disk_name.len, buf);
246                 buf[presented_len] = '\0';
247                 kfree(fname->crypto_buf.name);
248                 fname->crypto_buf.name = buf;
249                 fname->crypto_buf.len = presented_len;
250                 fname->disk_name.name = fname->crypto_buf.name;
251                 fname->disk_name.len = fname->crypto_buf.len;
252         }
253
254         return rc;
255
256 out_free:
257         llcrypt_free_filename(fname);
258         return rc;
259 }
260
261 /**
262  * ll_fname_disk_to_usr() - overlay to llcrypt_fname_disk_to_usr
263  * @inode: the inode to convert name
264  * @hash: major hash for inode
265  * @minor_hash: minor hash for inode
266  * @iname: the user-provided filename needing conversion
267  * @oname: the filename information to be filled in
268  * @fid: the user-provided fid for filename
269  *
270  * The caller must have allocated sufficient memory for the @oname string.
271  *
272  * This overlay function is necessary to properly decode @iname before
273  * decryption, as it comes from the wire.
274  * This overlay function is also necessary to handle the case of operations
275  * carried out without the key. Normally llcrypt makes use of digested names in
276  * that case. Having a digested name works for local file systems that can call
277  * llcrypt_match_name(), but Lustre server side is not aware of encryption.
278  * So for keyless @lookup operations on long names, for Lustre we choose to
279  * present to users the encoded struct ll_digest_filename, instead of a digested
280  * name. FID and name hash can then easily be extracted and put into the
281  * requests sent to servers.
282  */
283 int ll_fname_disk_to_usr(struct inode *inode,
284                          u32 hash, u32 minor_hash,
285                          struct llcrypt_str *iname, struct llcrypt_str *oname,
286                          struct lu_fid *fid)
287 {
288         struct llcrypt_str lltr = LLTR_INIT(iname->name, iname->len);
289         struct ll_digest_filename digest;
290         int digested = 0;
291         char *buf = NULL;
292         int rc;
293
294         if (IS_ENCRYPTED(inode)) {
295                 if (!name_is_dot_or_dotdot(lltr.name, lltr.len) &&
296                     strnchr(lltr.name, lltr.len, '=')) {
297                         /* Only proceed to critical decode if
298                          * iname contains espace char '='.
299                          */
300                         int len = lltr.len;
301
302                         buf = kmalloc(len, GFP_NOFS);
303                         if (!buf)
304                                 return -ENOMEM;
305
306                         len = critical_decode(lltr.name, len, buf);
307                         lltr.name = buf;
308                         lltr.len = len;
309                 }
310                 if (lltr.len > LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE &&
311                     !llcrypt_has_encryption_key(inode) &&
312                     likely(llcrypt_policy_has_filename_enc(inode))) {
313                         digested = 1;
314                         /* Without the key for long names, set the dentry name
315                          * to the representing struct ll_digest_filename. It
316                          * will be encoded by llcrypt for display, and will
317                          * enable further lookup requests.
318                          */
319                         if (!fid)
320                                 return -EINVAL;
321                         digest.ldf_fid = *fid;
322                         memcpy(digest.ldf_excerpt,
323                                LLCRYPT_FNAME_DIGEST(lltr.name, lltr.len),
324                                LLCRYPT_FNAME_DIGEST_SIZE);
325
326                         lltr.name = (char *)&digest;
327                         lltr.len = sizeof(digest);
328
329                         oname->name[0] = '_';
330                         oname->name = oname->name + 1;
331                         oname->len--;
332                 }
333         }
334
335         rc = llcrypt_fname_disk_to_usr(inode, hash, minor_hash, &lltr, oname);
336
337         kfree(buf);
338         oname->name = oname->name - digested;
339         oname->len = oname->len + digested;
340
341         return rc;
342 }
343
344 /* Copied from llcrypt_d_revalidate, as it is not exported */
345 /*
346  * Validate dentries in encrypted directories to make sure we aren't potentially
347  * caching stale dentries after a key has been added.
348  */
349 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
350 {
351         struct dentry *dir;
352         int err;
353         int valid;
354
355         /*
356          * Plaintext names are always valid, since llcrypt doesn't support
357          * reverting to ciphertext names without evicting the directory's inode
358          * -- which implies eviction of the dentries in the directory.
359          */
360         if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
361                 return 1;
362
363         /*
364          * Ciphertext name; valid if the directory's key is still unavailable.
365          *
366          * Although llcrypt forbids rename() on ciphertext names, we still must
367          * use dget_parent() here rather than use ->d_parent directly.  That's
368          * because a corrupted fs image may contain directory hard links, which
369          * the VFS handles by moving the directory's dentry tree in the dcache
370          * each time ->lookup() finds the directory and it already has a dentry
371          * elsewhere.  Thus ->d_parent can be changing, and we must safely grab
372          * a reference to some ->d_parent to prevent it from being freed.
373          */
374
375         if (flags & LOOKUP_RCU)
376                 return -ECHILD;
377
378         dir = dget_parent(dentry);
379         err = llcrypt_get_encryption_info(d_inode(dir));
380         valid = !llcrypt_has_encryption_key(d_inode(dir));
381         dput(dir);
382
383         if (err < 0)
384                 return err;
385
386         return valid;
387 }
388
389 const struct llcrypt_operations lustre_cryptops = {
390         .key_prefix             = "lustre:",
391         .get_context            = ll_get_context,
392         .set_context            = ll_set_context,
393         .dummy_context          = ll_dummy_context,
394         .empty_dir              = ll_empty_dir,
395         .max_namelen            = NAME_MAX,
396 };
397 #else /* !HAVE_LUSTRE_CRYPTO */
398 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
399                     bool preload)
400 {
401         return 0;
402 }
403
404 void llcrypt_free_ctx(void *encctx, __u32 size)
405 {
406 }
407
408 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
409 {
410         return false;
411 }
412
413 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
414 {
415         return false;
416 }
417
418 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
419 {
420 }
421
422 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
423                       int lookup, struct llcrypt_name *fname,
424                       struct lu_fid *fid)
425 {
426         if (fid) {
427                 fid->f_seq = 0;
428                 fid->f_oid = 0;
429                 fid->f_ver = 0;
430         }
431
432         return llcrypt_setup_filename(dir, iname, lookup, fname);
433 }
434
435 int ll_fname_disk_to_usr(struct inode *inode,
436                          u32 hash, u32 minor_hash,
437                          struct llcrypt_str *iname, struct llcrypt_str *oname,
438                          struct lu_fid *fid)
439 {
440         return llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname);
441 }
442
443 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
444 {
445         return 1;
446 }
447 #endif
448