Whamcloud - gitweb
17c42d0bd48030179a695e18029e5eaefc7d474d
[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  *
171  * This overlay function is necessary to properly encode @fname after
172  * encryption, as it will be sent over the wire.
173  */
174 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
175                       int lookup, struct llcrypt_name *fname)
176 {
177         int rc;
178
179         rc = llcrypt_setup_filename(dir, iname, lookup, fname);
180         if (rc)
181                 return rc;
182
183         if (IS_ENCRYPTED(dir) &&
184             !name_is_dot_or_dotdot(fname->disk_name.name,
185                                    fname->disk_name.len)) {
186                 int presented_len = critical_chars(fname->disk_name.name,
187                                                    fname->disk_name.len);
188                 char *buf;
189
190                 buf = kmalloc(presented_len + 1, GFP_NOFS);
191                 if (!buf) {
192                         rc = -ENOMEM;
193                         goto out_free;
194                 }
195
196                 if (presented_len == fname->disk_name.len)
197                         memcpy(buf, fname->disk_name.name, presented_len);
198                 else
199                         critical_encode(fname->disk_name.name,
200                                         fname->disk_name.len, buf);
201                 buf[presented_len] = '\0';
202                 kfree(fname->crypto_buf.name);
203                 fname->crypto_buf.name = buf;
204                 fname->crypto_buf.len = presented_len;
205                 fname->disk_name.name = fname->crypto_buf.name;
206                 fname->disk_name.len = fname->crypto_buf.len;
207         }
208
209         return rc;
210
211 out_free:
212         llcrypt_free_filename(fname);
213         return rc;
214 }
215
216 /**
217  * ll_fname_disk_to_usr() - overlay to llcrypt_fname_disk_to_usr
218  * @inode: the inode to convert name
219  * @hash: major hash for inode
220  * @minor_hash: minor hash for inode
221  * @iname: the user-provided filename needing conversion
222  * @oname: the filename information to be filled in
223  *
224  * The caller must have allocated sufficient memory for the @oname string.
225  *
226  * This overlay function is necessary to properly decode @iname before
227  * decryption, as it comes from the wire.
228  */
229 int ll_fname_disk_to_usr(struct inode *inode,
230                          u32 hash, u32 minor_hash,
231                          struct llcrypt_str *iname, struct llcrypt_str *oname)
232 {
233         struct llcrypt_str lltr = LLTR_INIT(iname->name, iname->len);
234         char *buf = NULL;
235         int rc;
236
237         if (IS_ENCRYPTED(inode) &&
238             !name_is_dot_or_dotdot(lltr.name, lltr.len) &&
239             strnchr(lltr.name, lltr.len, '=')) {
240                 /* Only proceed to critical decode if
241                  * iname contains espace char '='.
242                  */
243                 int len = lltr.len;
244
245                 buf = kmalloc(len, GFP_NOFS);
246                 if (!buf)
247                         return -ENOMEM;
248
249                 len = critical_decode(lltr.name, len, buf);
250                 lltr.name = buf;
251                 lltr.len = len;
252         }
253
254         rc = llcrypt_fname_disk_to_usr(inode, hash, minor_hash, &lltr, oname);
255
256         kfree(buf);
257
258         return rc;
259 }
260
261 /* Copied from llcrypt_d_revalidate, as it is not exported */
262 /*
263  * Validate dentries in encrypted directories to make sure we aren't potentially
264  * caching stale dentries after a key has been added.
265  */
266 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
267 {
268         struct dentry *dir;
269         int err;
270         int valid;
271
272         /*
273          * Plaintext names are always valid, since llcrypt doesn't support
274          * reverting to ciphertext names without evicting the directory's inode
275          * -- which implies eviction of the dentries in the directory.
276          */
277         if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
278                 return 1;
279
280         /*
281          * Ciphertext name; valid if the directory's key is still unavailable.
282          *
283          * Although llcrypt forbids rename() on ciphertext names, we still must
284          * use dget_parent() here rather than use ->d_parent directly.  That's
285          * because a corrupted fs image may contain directory hard links, which
286          * the VFS handles by moving the directory's dentry tree in the dcache
287          * each time ->lookup() finds the directory and it already has a dentry
288          * elsewhere.  Thus ->d_parent can be changing, and we must safely grab
289          * a reference to some ->d_parent to prevent it from being freed.
290          */
291
292         if (flags & LOOKUP_RCU)
293                 return -ECHILD;
294
295         dir = dget_parent(dentry);
296         err = llcrypt_get_encryption_info(d_inode(dir));
297         valid = !llcrypt_has_encryption_key(d_inode(dir));
298         dput(dir);
299
300         if (err < 0)
301                 return err;
302
303         return valid;
304 }
305
306 const struct llcrypt_operations lustre_cryptops = {
307         .key_prefix             = "lustre:",
308         .get_context            = ll_get_context,
309         .set_context            = ll_set_context,
310         .dummy_context          = ll_dummy_context,
311         .empty_dir              = ll_empty_dir,
312         .max_namelen            = NAME_MAX,
313 };
314 #else /* !HAVE_LUSTRE_CRYPTO */
315 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
316                     bool preload)
317 {
318         return 0;
319 }
320
321 void llcrypt_free_ctx(void *encctx, __u32 size)
322 {
323 }
324
325 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
326 {
327         return false;
328 }
329
330 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
331 {
332         return false;
333 }
334
335 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
336 {
337 }
338
339 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
340                       int lookup, struct llcrypt_name *fname)
341 {
342         return llcrypt_setup_filename(dir, iname, lookup, fname);
343 }
344
345 int ll_fname_disk_to_usr(struct inode *inode,
346                          u32 hash, u32 minor_hash,
347                          struct llcrypt_str *iname, struct llcrypt_str *oname)
348 {
349         return llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname);
350 }
351
352 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
353 {
354         return 1;
355 }
356 #endif
357