Whamcloud - gitweb
LU-15428 contrib: add branch_comm
[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         int rc;
37
38         /* Get enc context xattr directly instead of going through the VFS,
39          * as there is no xattr handler for "encryption.".
40          */
41         rc = ll_xattr_list(inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
42                            XATTR_ENCRYPTION_T, ctx, len, OBD_MD_FLXATTR);
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, letting proceed with setxattr operation.
80  *   This use case should only be used when explicitly setting a new encryption
81  *   policy on an existing, empty directory.
82  */
83 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
84                           void *fs_data)
85 {
86         struct ptlrpc_request *req = NULL;
87         struct ll_sb_info *sbi;
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         sbi = ll_i2sbi(inode);
109         /* Send setxattr request to lower layers directly instead of going
110          * through the VFS, as there is no xattr handler for "encryption.".
111          */
112         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
113                          OBD_MD_FLXATTR, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
114                          ctx, len, XATTR_CREATE, ll_i2suppgid(inode), &req);
115         if (rc)
116                 return rc;
117         ptlrpc_req_finished(req);
118
119         return ll_set_encflags(inode, (void *)ctx, len, false);
120 }
121
122 /**
123  * ll_file_open_encrypt() - overlay to llcrypt_file_open
124  * @inode: the inode being opened
125  * @filp: the struct file being set up
126  *
127  * This overlay function is necessary to handle encrypted file open without
128  * the key. We allow this access pattern to applications that know what they
129  * are doing, by using the specific flag O_FILE_ENC.
130  * This flag is only compatible with O_DIRECT IOs, to make sure ciphertext
131  * data is wiped from page cache once IOs are finished.
132  */
133 int ll_file_open_encrypt(struct inode *inode, struct file *filp)
134 {
135         int rc;
136
137         rc = llcrypt_file_open(inode, filp);
138         if (likely(rc != -ENOKEY))
139                 return rc;
140
141         if (rc == -ENOKEY &&
142             (filp->f_flags & O_FILE_ENC) == O_FILE_ENC &&
143             filp->f_flags & O_DIRECT)
144                 /* allow file open with O_FILE_ENC flag when we have O_DIRECT */
145                 rc = 0;
146
147         return rc;
148 }
149
150 void llcrypt_free_ctx(void *encctx, __u32 size)
151 {
152         if (encctx)
153                 OBD_FREE(encctx, size);
154 }
155
156 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
157 {
158         return unlikely(test_bit(LL_SBI_TEST_DUMMY_ENCRYPTION, sbi->ll_flags));
159 }
160
161 static bool ll_dummy_context(struct inode *inode)
162 {
163         struct ll_sb_info *sbi = ll_i2sbi(inode);
164
165         return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
166 }
167
168 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
169 {
170         return test_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
171 }
172
173 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
174 {
175         if (set) {
176                 set_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
177         } else {
178                 clear_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
179                 clear_bit(LL_SBI_TEST_DUMMY_ENCRYPTION, sbi->ll_flags);
180         }
181 }
182
183 static bool ll_empty_dir(struct inode *inode)
184 {
185         /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
186          * on an empty dir.
187          */
188         /* Here we choose to return true, meaning we always call .set_context.
189          * Then we rely on server side, with mdd_fix_attr() that calls
190          * mdd_dir_is_empty() when setting encryption flag on directory.
191          */
192         return true;
193 }
194
195 /**
196  * ll_setup_filename() - overlay to llcrypt_setup_filename
197  * @dir: the directory that will be searched
198  * @iname: the user-provided filename being searched for
199  * @lookup: 1 if we're allowed to proceed without the key because it's
200  *      ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
201  *      proceed without the key because we're going to create the dir_entry.
202  * @fname: the filename information to be filled in
203  * @fid: fid retrieved from user-provided filename
204  *
205  * This overlay function is necessary to properly encode @fname after
206  * encryption, as it will be sent over the wire.
207  * This overlay function is also necessary to handle the case of operations
208  * carried out without the key. Normally llcrypt makes use of digested names in
209  * that case. Having a digested name works for local file systems that can call
210  * llcrypt_match_name(), but Lustre server side is not aware of encryption.
211  * So for keyless @lookup operations on long names, for Lustre we choose to
212  * present to users the encoded struct ll_digest_filename, instead of a digested
213  * name. FID and name hash can then easily be extracted and put into the
214  * requests sent to servers.
215  */
216 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
217                       int lookup, struct llcrypt_name *fname,
218                       struct lu_fid *fid)
219 {
220         int digested = 0;
221         struct qstr dname;
222         int rc;
223
224         if (fid && IS_ENCRYPTED(dir) && !llcrypt_has_encryption_key(dir) &&
225             iname->name[0] == '_')
226                 digested = 1;
227
228         dname.name = iname->name + digested;
229         dname.len = iname->len - digested;
230
231         if (fid) {
232                 fid->f_seq = 0;
233                 fid->f_oid = 0;
234                 fid->f_ver = 0;
235         }
236         rc = llcrypt_setup_filename(dir, &dname, lookup, fname);
237         if (rc == -ENOENT && lookup &&
238             ((is_root_inode(dir) && iname->len == strlen(dot_fscrypt_name) &&
239               strncmp(iname->name, dot_fscrypt_name, iname->len) == 0) ||
240              (!llcrypt_has_encryption_key(dir) &&
241               unlikely(filename_is_volatile(iname->name, iname->len, NULL))))) {
242                 /* In case of subdir mount of an encrypted directory, we allow
243                  * lookup of /.fscrypt directory.
244                  */
245                 /* For purpose of migration or mirroring without enc key, we
246                  * allow lookup of volatile file without enc context.
247                  */
248                 memset(fname, 0, sizeof(struct llcrypt_name));
249                 fname->disk_name.name = (unsigned char *)iname->name;
250                 fname->disk_name.len = iname->len;
251                 rc = 0;
252         }
253         if (rc)
254                 return rc;
255
256         if (digested) {
257                 /* Without the key, for long names user should have struct
258                  * ll_digest_filename representation of the dentry instead of
259                  * the name. So make sure it is valid, return fid and put
260                  * excerpt of cipher text name in disk_name.
261                  */
262                 struct ll_digest_filename *digest;
263
264                 if (fname->crypto_buf.len < sizeof(struct ll_digest_filename)) {
265                         rc = -EINVAL;
266                         goto out_free;
267                 }
268                 digest = (struct ll_digest_filename *)fname->crypto_buf.name;
269                 *fid = digest->ldf_fid;
270                 if (!fid_is_sane(fid)) {
271                         rc = -EINVAL;
272                         goto out_free;
273                 }
274                 fname->disk_name.name = digest->ldf_excerpt;
275                 fname->disk_name.len = LLCRYPT_FNAME_DIGEST_SIZE;
276         }
277         if (IS_ENCRYPTED(dir) &&
278             !name_is_dot_or_dotdot(fname->disk_name.name,
279                                    fname->disk_name.len)) {
280                 int presented_len = critical_chars(fname->disk_name.name,
281                                                    fname->disk_name.len);
282                 char *buf;
283
284                 buf = kmalloc(presented_len + 1, GFP_NOFS);
285                 if (!buf) {
286                         rc = -ENOMEM;
287                         goto out_free;
288                 }
289
290                 if (presented_len == fname->disk_name.len)
291                         memcpy(buf, fname->disk_name.name, presented_len);
292                 else
293                         critical_encode(fname->disk_name.name,
294                                         fname->disk_name.len, buf);
295                 buf[presented_len] = '\0';
296                 kfree(fname->crypto_buf.name);
297                 fname->crypto_buf.name = buf;
298                 fname->crypto_buf.len = presented_len;
299                 fname->disk_name.name = fname->crypto_buf.name;
300                 fname->disk_name.len = fname->crypto_buf.len;
301         }
302
303         return rc;
304
305 out_free:
306         llcrypt_free_filename(fname);
307         return rc;
308 }
309
310 /**
311  * ll_fname_disk_to_usr() - overlay to llcrypt_fname_disk_to_usr
312  * @inode: the inode to convert name
313  * @hash: major hash for inode
314  * @minor_hash: minor hash for inode
315  * @iname: the user-provided filename needing conversion
316  * @oname: the filename information to be filled in
317  * @fid: the user-provided fid for filename
318  *
319  * The caller must have allocated sufficient memory for the @oname string.
320  *
321  * This overlay function is necessary to properly decode @iname before
322  * decryption, as it comes from the wire.
323  * This overlay function is also necessary to handle the case of operations
324  * carried out without the key. Normally llcrypt makes use of digested names in
325  * that case. Having a digested name works for local file systems that can call
326  * llcrypt_match_name(), but Lustre server side is not aware of encryption.
327  * So for keyless @lookup operations on long names, for Lustre we choose to
328  * present to users the encoded struct ll_digest_filename, instead of a digested
329  * name. FID and name hash can then easily be extracted and put into the
330  * requests sent to servers.
331  */
332 int ll_fname_disk_to_usr(struct inode *inode,
333                          u32 hash, u32 minor_hash,
334                          struct llcrypt_str *iname, struct llcrypt_str *oname,
335                          struct lu_fid *fid)
336 {
337         struct llcrypt_str lltr = LLTR_INIT(iname->name, iname->len);
338         struct ll_digest_filename digest;
339         int digested = 0;
340         char *buf = NULL;
341         int rc;
342
343         if (IS_ENCRYPTED(inode)) {
344                 if (!name_is_dot_or_dotdot(lltr.name, lltr.len) &&
345                     strnchr(lltr.name, lltr.len, '=')) {
346                         /* Only proceed to critical decode if
347                          * iname contains espace char '='.
348                          */
349                         int len = lltr.len;
350
351                         buf = kmalloc(len, GFP_NOFS);
352                         if (!buf)
353                                 return -ENOMEM;
354
355                         len = critical_decode(lltr.name, len, buf);
356                         lltr.name = buf;
357                         lltr.len = len;
358                 }
359                 if (lltr.len > LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE &&
360                     !llcrypt_has_encryption_key(inode) &&
361                     likely(llcrypt_policy_has_filename_enc(inode))) {
362                         digested = 1;
363                         /* Without the key for long names, set the dentry name
364                          * to the representing struct ll_digest_filename. It
365                          * will be encoded by llcrypt for display, and will
366                          * enable further lookup requests.
367                          */
368                         if (!fid)
369                                 return -EINVAL;
370                         digest.ldf_fid = *fid;
371                         memcpy(digest.ldf_excerpt,
372                                LLCRYPT_FNAME_DIGEST(lltr.name, lltr.len),
373                                LLCRYPT_FNAME_DIGEST_SIZE);
374
375                         lltr.name = (char *)&digest;
376                         lltr.len = sizeof(digest);
377
378                         oname->name[0] = '_';
379                         oname->name = oname->name + 1;
380                         oname->len--;
381                 }
382         }
383
384         rc = llcrypt_fname_disk_to_usr(inode, hash, minor_hash, &lltr, oname);
385
386         kfree(buf);
387         oname->name = oname->name - digested;
388         oname->len = oname->len + digested;
389
390         return rc;
391 }
392
393 /* Copied from llcrypt_d_revalidate, as it is not exported */
394 /*
395  * Validate dentries in encrypted directories to make sure we aren't potentially
396  * caching stale dentries after a key has been added.
397  */
398 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
399 {
400         struct dentry *dir;
401         int err;
402         int valid;
403
404         /*
405          * Plaintext names are always valid, since llcrypt doesn't support
406          * reverting to ciphertext names without evicting the directory's inode
407          * -- which implies eviction of the dentries in the directory.
408          */
409         if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
410                 return 1;
411
412         /*
413          * Ciphertext name; valid if the directory's key is still unavailable.
414          *
415          * Although llcrypt forbids rename() on ciphertext names, we still must
416          * use dget_parent() here rather than use ->d_parent directly.  That's
417          * because a corrupted fs image may contain directory hard links, which
418          * the VFS handles by moving the directory's dentry tree in the dcache
419          * each time ->lookup() finds the directory and it already has a dentry
420          * elsewhere.  Thus ->d_parent can be changing, and we must safely grab
421          * a reference to some ->d_parent to prevent it from being freed.
422          */
423
424         if (flags & LOOKUP_RCU)
425                 return -ECHILD;
426
427         dir = dget_parent(dentry);
428         err = llcrypt_get_encryption_info(d_inode(dir));
429         valid = !llcrypt_has_encryption_key(d_inode(dir));
430         dput(dir);
431
432         if (err < 0)
433                 return err;
434
435         return valid;
436 }
437
438 const struct llcrypt_operations lustre_cryptops = {
439         .key_prefix             = "lustre:",
440         .get_context            = ll_get_context,
441         .set_context            = ll_set_context,
442         .dummy_context          = ll_dummy_context,
443         .empty_dir              = ll_empty_dir,
444         .max_namelen            = NAME_MAX,
445 };
446 #else /* !HAVE_LUSTRE_CRYPTO */
447 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
448                     bool preload)
449 {
450         return 0;
451 }
452
453 int ll_file_open_encrypt(struct inode *inode, struct file *filp)
454 {
455         return llcrypt_file_open(inode, filp);
456 }
457
458 void llcrypt_free_ctx(void *encctx, __u32 size)
459 {
460 }
461
462 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
463 {
464         return false;
465 }
466
467 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
468 {
469         return false;
470 }
471
472 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
473 {
474 }
475
476 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
477                       int lookup, struct llcrypt_name *fname,
478                       struct lu_fid *fid)
479 {
480         if (fid) {
481                 fid->f_seq = 0;
482                 fid->f_oid = 0;
483                 fid->f_ver = 0;
484         }
485
486         return llcrypt_setup_filename(dir, iname, lookup, fname);
487 }
488
489 int ll_fname_disk_to_usr(struct inode *inode,
490                          u32 hash, u32 minor_hash,
491                          struct llcrypt_str *iname, struct llcrypt_str *oname,
492                          struct lu_fid *fid)
493 {
494         return llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname);
495 }
496
497 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
498 {
499         return 1;
500 }
501 #endif
502