Whamcloud - gitweb
37a215308fa0649b7728650b37bc993cf66c0357
[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, xattr_for_enc(inode),
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                                            xattr_for_enc(inode),
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, xattr_for_enc(inode),
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 #ifdef HAVE_FSCRYPT_DUMMY_CONTEXT_ENABLED
157 bool ll_sb_has_test_dummy_encryption(struct super_block *sb)
158 {
159         struct ll_sb_info *sbi = s2lsi(sb)->lsi_llsbi;
160
161         return sbi ?
162                unlikely(test_bit(LL_SBI_TEST_DUMMY_ENCRYPTION, sbi->ll_flags)) :
163                false;
164 }
165
166 static bool ll_dummy_context(struct inode *inode)
167 {
168         return ll_sb_has_test_dummy_encryption(inode->i_sb);
169 }
170 #else
171 static const union llcrypt_context *
172 ll_get_dummy_context(struct super_block *sb)
173 {
174         struct lustre_sb_info *lsi = s2lsi(sb);
175
176         return lsi ? lsi->lsi_dummy_enc_ctx.ctx : NULL;
177 }
178
179 bool ll_sb_has_test_dummy_encryption(struct super_block *sb)
180 {
181         return ll_get_dummy_context(sb) != NULL;
182 }
183 #endif
184
185 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
186 {
187         return test_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
188 }
189
190 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
191 {
192         if (set) {
193                 set_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
194         } else {
195                 clear_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
196                 clear_bit(LL_SBI_TEST_DUMMY_ENCRYPTION, sbi->ll_flags);
197         }
198 }
199
200 bool ll_sbi_has_name_encrypt(struct ll_sb_info *sbi)
201 {
202         return test_bit(LL_SBI_ENCRYPT_NAME, sbi->ll_flags);
203 }
204
205 void ll_sbi_set_name_encrypt(struct ll_sb_info *sbi, bool set)
206 {
207         if (set)
208                 set_bit(LL_SBI_ENCRYPT_NAME, sbi->ll_flags);
209         else
210                 clear_bit(LL_SBI_ENCRYPT_NAME, sbi->ll_flags);
211 }
212
213 static bool ll_empty_dir(struct inode *inode)
214 {
215         /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
216          * on an empty dir.
217          */
218         /* Here we choose to return true, meaning we always call .set_context.
219          * Then we rely on server side, with mdd_fix_attr() that calls
220          * mdd_dir_is_empty() when setting encryption flag on directory.
221          */
222         return true;
223 }
224
225 /**
226  * ll_setup_filename() - overlay to llcrypt_setup_filename
227  * @dir: the directory that will be searched
228  * @iname: the user-provided filename being searched for
229  * @lookup: 1 if we're allowed to proceed without the key because it's
230  *      ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
231  *      proceed without the key because we're going to create the dir_entry.
232  * @fname: the filename information to be filled in
233  * @fid: fid retrieved from user-provided filename
234  *
235  * This overlay function is necessary to properly encode @fname after
236  * encryption, as it will be sent over the wire.
237  * This overlay function is also necessary to handle the case of operations
238  * carried out without the key. Normally llcrypt makes use of digested names in
239  * that case. Having a digested name works for local file systems that can call
240  * llcrypt_match_name(), but Lustre server side is not aware of encryption.
241  * So for keyless @lookup operations on long names, for Lustre we choose to
242  * present to users the encoded struct ll_digest_filename, instead of a digested
243  * name. FID and name hash can then easily be extracted and put into the
244  * requests sent to servers.
245  */
246 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
247                       int lookup, struct llcrypt_name *fname,
248                       struct lu_fid *fid)
249 {
250         int digested = 0;
251         struct qstr dname;
252         int rc;
253
254         if (fid && IS_ENCRYPTED(dir) && llcrypt_policy_has_filename_enc(dir) &&
255             !llcrypt_has_encryption_key(dir)) {
256                 struct lustre_sb_info *lsi = s2lsi(dir->i_sb);
257
258                 if ((!(lsi->lsi_flags & LSI_FILENAME_ENC_B64_OLD_CLI) &&
259                      iname->name[0] == LLCRYPT_DIGESTED_CHAR) ||
260                     ((lsi->lsi_flags & LSI_FILENAME_ENC_B64_OLD_CLI) &&
261                      iname->name[0] == LLCRYPT_DIGESTED_CHAR_OLD))
262                         digested = 1;
263         }
264
265         dname.name = iname->name + digested;
266         dname.len = iname->len - digested;
267
268         if (fid) {
269                 fid->f_seq = 0;
270                 fid->f_oid = 0;
271                 fid->f_ver = 0;
272         }
273         if (unlikely(filename_is_volatile(iname->name,
274                                           iname->len, NULL))) {
275                 /* keep volatile name as-is, matters for server side */
276                 memset(fname, 0, sizeof(struct llcrypt_name));
277                 fname->disk_name.name = (unsigned char *)iname->name;
278                 fname->disk_name.len = iname->len;
279                 rc = 0;
280         } else {
281                 rc = llcrypt_setup_filename(dir, &dname, lookup, fname);
282         }
283         if (rc == -ENOENT && lookup) {
284                 if (((is_root_inode(dir) &&
285                      iname->len == strlen(dot_fscrypt_name) &&
286                      strncmp(iname->name, dot_fscrypt_name, iname->len) == 0) ||
287                      (!llcrypt_has_encryption_key(dir) &&
288                       unlikely(filename_is_volatile(iname->name,
289                                                     iname->len, NULL))))) {
290                         /* In case of subdir mount of an encrypted directory,
291                          * we allow lookup of /.fscrypt directory.
292                          */
293                         /* For purpose of migration or mirroring without enc key
294                          * we allow lookup of volatile file without enc context.
295                          */
296                         memset(fname, 0, sizeof(struct llcrypt_name));
297                         fname->disk_name.name = (unsigned char *)iname->name;
298                         fname->disk_name.len = iname->len;
299                         rc = 0;
300                 } else if (!llcrypt_has_encryption_key(dir)) {
301                         rc = -ENOKEY;
302                 }
303         }
304         if (rc)
305                 return rc;
306
307         if (digested) {
308                 /* Without the key, for long names user should have struct
309                  * ll_digest_filename representation of the dentry instead of
310                  * the name. So make sure it is valid, return fid and put
311                  * excerpt of cipher text name in disk_name.
312                  */
313                 struct ll_digest_filename *digest;
314
315                 if (fname->crypto_buf.len < sizeof(struct ll_digest_filename)) {
316                         rc = -EINVAL;
317                         goto out_free;
318                 }
319                 digest = (struct ll_digest_filename *)fname->disk_name.name;
320                 *fid = digest->ldf_fid;
321                 if (!fid_is_sane(fid)) {
322                         rc = -EINVAL;
323                         goto out_free;
324                 }
325                 fname->disk_name.name = digest->ldf_excerpt;
326                 fname->disk_name.len = sizeof(digest->ldf_excerpt);
327         }
328         if (IS_ENCRYPTED(dir) &&
329             !name_is_dot_or_dotdot(fname->disk_name.name,
330                                    fname->disk_name.len)) {
331                 int presented_len = critical_chars(fname->disk_name.name,
332                                                    fname->disk_name.len);
333                 char *buf;
334
335                 buf = kmalloc(presented_len + 1, GFP_NOFS);
336                 if (!buf) {
337                         rc = -ENOMEM;
338                         goto out_free;
339                 }
340
341                 if (presented_len == fname->disk_name.len)
342                         memcpy(buf, fname->disk_name.name, presented_len);
343                 else
344                         critical_encode(fname->disk_name.name,
345                                         fname->disk_name.len, buf);
346                 buf[presented_len] = '\0';
347                 kfree(fname->crypto_buf.name);
348                 fname->crypto_buf.name = buf;
349                 fname->crypto_buf.len = presented_len;
350                 fname->disk_name.name = fname->crypto_buf.name;
351                 fname->disk_name.len = fname->crypto_buf.len;
352         }
353
354         return rc;
355
356 out_free:
357         llcrypt_free_filename(fname);
358         return rc;
359 }
360
361 /**
362  * ll_fname_disk_to_usr() - overlay to llcrypt_fname_disk_to_usr
363  * @inode: the inode to convert name
364  * @hash: major hash for inode
365  * @minor_hash: minor hash for inode
366  * @iname: the user-provided filename needing conversion
367  * @oname: the filename information to be filled in
368  * @fid: the user-provided fid for filename
369  *
370  * The caller must have allocated sufficient memory for the @oname string.
371  *
372  * This overlay function is necessary to properly decode @iname before
373  * decryption, as it comes from the wire.
374  * This overlay function is also necessary to handle the case of operations
375  * carried out without the key. Normally llcrypt makes use of digested names in
376  * that case. Having a digested name works for local file systems that can call
377  * llcrypt_match_name(), but Lustre server side is not aware of encryption.
378  * So for keyless @lookup operations on long names, for Lustre we choose to
379  * present to users the encoded struct ll_digest_filename, instead of a digested
380  * name. FID and name hash can then easily be extracted and put into the
381  * requests sent to servers.
382  */
383 int ll_fname_disk_to_usr(struct inode *inode,
384                          u32 hash, u32 minor_hash,
385                          struct llcrypt_str *iname, struct llcrypt_str *oname,
386                          struct lu_fid *fid)
387 {
388         struct llcrypt_str lltr = LLTR_INIT(iname->name, iname->len);
389         struct ll_digest_filename digest;
390         int digested = 0;
391         char *buf = NULL;
392         int rc;
393
394         if (IS_ENCRYPTED(inode)) {
395                 if (!name_is_dot_or_dotdot(lltr.name, lltr.len) &&
396                     strnchr(lltr.name, lltr.len, '=')) {
397                         /* Only proceed to critical decode if
398                          * iname contains espace char '='.
399                          */
400                         int len = lltr.len;
401
402                         buf = kmalloc(len, GFP_NOFS);
403                         if (!buf)
404                                 return -ENOMEM;
405
406                         len = critical_decode(lltr.name, len, buf);
407                         lltr.name = buf;
408                         lltr.len = len;
409                 }
410                 if (lltr.len > LL_CRYPTO_BLOCK_SIZE * 2 &&
411                     !llcrypt_has_encryption_key(inode) &&
412                     llcrypt_policy_has_filename_enc(inode)) {
413                         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
414
415                         digested = 1;
416                         /* Without the key for long names, set the dentry name
417                          * to the representing struct ll_digest_filename. It
418                          * will be encoded by llcrypt for display, and will
419                          * enable further lookup requests.
420                          */
421                         if (!fid)
422                                 return -EINVAL;
423                         digest.ldf_fid = *fid;
424                         memcpy(digest.ldf_excerpt,
425                                LLCRYPT_EXTRACT_DIGEST(lltr.name, lltr.len),
426                                sizeof(digest.ldf_excerpt));
427
428                         lltr.name = (char *)&digest;
429                         lltr.len = sizeof(digest);
430
431                         if (!(lsi->lsi_flags & LSI_FILENAME_ENC_B64_OLD_CLI))
432                                 oname->name[0] = LLCRYPT_DIGESTED_CHAR;
433                         else
434                                 oname->name[0] = LLCRYPT_DIGESTED_CHAR_OLD;
435                         oname->name = oname->name + 1;
436                         oname->len--;
437                 }
438         }
439
440         rc = llcrypt_fname_disk_to_usr(inode, hash, minor_hash, &lltr, oname);
441
442         kfree(buf);
443         oname->name = oname->name - digested;
444         oname->len = oname->len + digested;
445
446         return rc;
447 }
448
449 /* Copied from llcrypt_d_revalidate, as it is not exported */
450 /*
451  * Validate dentries in encrypted directories to make sure we aren't potentially
452  * caching stale dentries after a key has been added.
453  */
454 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
455 {
456         struct dentry *dir;
457         int err;
458         int valid;
459
460         /*
461          * Plaintext names are always valid, since llcrypt doesn't support
462          * reverting to ciphertext names without evicting the directory's inode
463          * -- which implies eviction of the dentries in the directory.
464          */
465         if (!llcrypt_is_nokey_name(dentry))
466                 return 1;
467
468         /*
469          * Ciphertext name; valid if the directory's key is still unavailable.
470          *
471          * Although llcrypt forbids rename() on ciphertext names, we still must
472          * use dget_parent() here rather than use ->d_parent directly.  That's
473          * because a corrupted fs image may contain directory hard links, which
474          * the VFS handles by moving the directory's dentry tree in the dcache
475          * each time ->lookup() finds the directory and it already has a dentry
476          * elsewhere.  Thus ->d_parent can be changing, and we must safely grab
477          * a reference to some ->d_parent to prevent it from being freed.
478          */
479
480         if (flags & LOOKUP_RCU)
481                 return -ECHILD;
482
483         dir = dget_parent(dentry);
484         err = llcrypt_get_encryption_info(d_inode(dir));
485         valid = !llcrypt_has_encryption_key(d_inode(dir));
486         dput(dir);
487
488         if (err < 0)
489                 return err;
490
491         return valid;
492 }
493
494 const struct llcrypt_operations lustre_cryptops = {
495         .key_prefix             = "lustre:",
496         .get_context            = ll_get_context,
497         .set_context            = ll_set_context,
498 #ifdef HAVE_FSCRYPT_DUMMY_CONTEXT_ENABLED
499         .dummy_context          = ll_dummy_context,
500 #else
501         .get_dummy_context      = ll_get_dummy_context,
502 #endif
503         .empty_dir              = ll_empty_dir,
504         .max_namelen            = NAME_MAX,
505 };
506 #else /* !HAVE_LUSTRE_CRYPTO */
507 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
508                     bool preload)
509 {
510         return 0;
511 }
512
513 int ll_file_open_encrypt(struct inode *inode, struct file *filp)
514 {
515         return llcrypt_file_open(inode, filp);
516 }
517
518 void llcrypt_free_ctx(void *encctx, __u32 size)
519 {
520 }
521
522 bool ll_sb_has_test_dummy_encryption(struct super_block *sb)
523 {
524         return false;
525 }
526
527 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
528 {
529         return false;
530 }
531
532 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
533 {
534 }
535
536 bool ll_sbi_has_name_encrypt(struct ll_sb_info *sbi)
537 {
538         return false;
539 }
540
541 void ll_sbi_set_name_encrypt(struct ll_sb_info *sbi, bool set)
542 {
543 }
544
545 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
546                       int lookup, struct llcrypt_name *fname,
547                       struct lu_fid *fid)
548 {
549         if (fid) {
550                 fid->f_seq = 0;
551                 fid->f_oid = 0;
552                 fid->f_ver = 0;
553         }
554
555         return llcrypt_setup_filename(dir, iname, lookup, fname);
556 }
557
558 int ll_fname_disk_to_usr(struct inode *inode,
559                          u32 hash, u32 minor_hash,
560                          struct llcrypt_str *iname, struct llcrypt_str *oname,
561                          struct lu_fid *fid)
562 {
563         return llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname);
564 }
565
566 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
567 {
568         return 1;
569 }
570 #endif
571