4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2019, 2020, Whamcloud.
26 * This file is part of Lustre, http://www.lustre.org/
29 #include "llite_internal.h"
31 #ifdef HAVE_LUSTRE_CRYPTO
32 #include <libcfs/libcfs_crypto.h>
34 static int ll_get_context(struct inode *inode, void *ctx, size_t len)
36 struct dentry *dentry = d_find_any_alias(inode);
41 env = cl_env_get(&refcheck);
45 /* Set lcc_getencctx=1 to allow this thread to read
46 * LL_XATTR_NAME_ENCRYPTION_CONTEXT xattr, as requested by llcrypt.
48 ll_cl_add(inode, env, NULL, LCC_RW);
49 ll_env_info(env)->lti_io_ctx.lcc_getencctx = 1;
51 rc = ll_vfs_getxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
54 ll_cl_remove(inode, env);
55 cl_env_put(env, &refcheck);
60 /* used as encryption unit size */
61 if (S_ISREG(inode->i_mode))
62 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
66 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
69 unsigned int ext_flags;
72 /* used as encryption unit size */
73 if (S_ISREG(inode->i_mode))
74 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
75 ext_flags = ll_inode_to_ext_flags(inode->i_flags) | LUSTRE_ENCRYPT_FL;
76 ll_update_inode_flags(inode, ext_flags);
78 if (encctx && encctxlen)
79 rc = ll_xattr_cache_insert(inode,
80 LL_XATTR_NAME_ENCRYPTION_CONTEXT,
85 return preload ? llcrypt_get_encryption_info(inode) : 0;
88 /* ll_set_context has 2 distinct behaviors, depending on the value of inode
91 * passed fs_data is a struct md_op_data *. We need to store enc ctx in
92 * op_data, so that it will be sent along to the server with the request that
93 * the caller is preparing, thus saving a setxattr request.
94 * - inode is not NULL:
95 * normal case in which passed fs_data is a struct dentry *, letting proceed
96 * with setxattr operation.
97 * This use case should only be used when explicitly setting a new encryption
98 * policy on an existing, empty directory.
100 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
103 struct dentry *dentry;
107 struct md_op_data *op_data = (struct md_op_data *)fs_data;
112 OBD_ALLOC(op_data->op_file_encctx, len);
113 if (op_data->op_file_encctx == NULL)
115 op_data->op_file_encctx_size = len;
116 memcpy(op_data->op_file_encctx, ctx, len);
120 /* Encrypting the root directory is not allowed */
121 if (is_root_inode(inode))
124 dentry = (struct dentry *)fs_data;
125 set_bit(LLIF_SET_ENC_CTX, &ll_i2info(inode)->lli_flags);
126 rc = ll_vfs_setxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
127 ctx, len, XATTR_CREATE);
131 return ll_set_encflags(inode, (void *)ctx, len, false);
134 void llcrypt_free_ctx(void *encctx, __u32 size)
137 OBD_FREE(encctx, size);
140 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
142 return unlikely(test_bit(LL_SBI_TEST_DUMMY_ENCRYPTION, sbi->ll_flags));
145 static bool ll_dummy_context(struct inode *inode)
147 struct ll_sb_info *sbi = ll_i2sbi(inode);
149 return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
152 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
154 return test_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
157 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
160 set_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
162 clear_bit(LL_SBI_ENCRYPT, sbi->ll_flags);
163 clear_bit(LL_SBI_TEST_DUMMY_ENCRYPTION, sbi->ll_flags);
167 static bool ll_empty_dir(struct inode *inode)
169 /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
172 /* Here we choose to return true, meaning we always call .set_context.
173 * Then we rely on server side, with mdd_fix_attr() that calls
174 * mdd_dir_is_empty() when setting encryption flag on directory.
180 * ll_setup_filename() - overlay to llcrypt_setup_filename
181 * @dir: the directory that will be searched
182 * @iname: the user-provided filename being searched for
183 * @lookup: 1 if we're allowed to proceed without the key because it's
184 * ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
185 * proceed without the key because we're going to create the dir_entry.
186 * @fname: the filename information to be filled in
187 * @fid: fid retrieved from user-provided filename
189 * This overlay function is necessary to properly encode @fname after
190 * encryption, as it will be sent over the wire.
191 * This overlay function is also necessary to handle the case of operations
192 * carried out without the key. Normally llcrypt makes use of digested names in
193 * that case. Having a digested name works for local file systems that can call
194 * llcrypt_match_name(), but Lustre server side is not aware of encryption.
195 * So for keyless @lookup operations on long names, for Lustre we choose to
196 * present to users the encoded struct ll_digest_filename, instead of a digested
197 * name. FID and name hash can then easily be extracted and put into the
198 * requests sent to servers.
200 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
201 int lookup, struct llcrypt_name *fname,
208 if (fid && IS_ENCRYPTED(dir) && !llcrypt_has_encryption_key(dir) &&
209 iname->name[0] == '_')
212 dname.name = iname->name + digested;
213 dname.len = iname->len - digested;
220 rc = llcrypt_setup_filename(dir, &dname, lookup, fname);
225 /* Without the key, for long names user should have struct
226 * ll_digest_filename representation of the dentry instead of
227 * the name. So make sure it is valid, return fid and put
228 * excerpt of cipher text name in disk_name.
230 struct ll_digest_filename *digest;
232 if (fname->crypto_buf.len < sizeof(struct ll_digest_filename)) {
236 digest = (struct ll_digest_filename *)fname->crypto_buf.name;
237 *fid = digest->ldf_fid;
238 if (!fid_is_sane(fid)) {
242 fname->disk_name.name = digest->ldf_excerpt;
243 fname->disk_name.len = LLCRYPT_FNAME_DIGEST_SIZE;
245 if (IS_ENCRYPTED(dir) &&
246 !name_is_dot_or_dotdot(fname->disk_name.name,
247 fname->disk_name.len)) {
248 int presented_len = critical_chars(fname->disk_name.name,
249 fname->disk_name.len);
252 buf = kmalloc(presented_len + 1, GFP_NOFS);
258 if (presented_len == fname->disk_name.len)
259 memcpy(buf, fname->disk_name.name, presented_len);
261 critical_encode(fname->disk_name.name,
262 fname->disk_name.len, buf);
263 buf[presented_len] = '\0';
264 kfree(fname->crypto_buf.name);
265 fname->crypto_buf.name = buf;
266 fname->crypto_buf.len = presented_len;
267 fname->disk_name.name = fname->crypto_buf.name;
268 fname->disk_name.len = fname->crypto_buf.len;
274 llcrypt_free_filename(fname);
279 * ll_fname_disk_to_usr() - overlay to llcrypt_fname_disk_to_usr
280 * @inode: the inode to convert name
281 * @hash: major hash for inode
282 * @minor_hash: minor hash for inode
283 * @iname: the user-provided filename needing conversion
284 * @oname: the filename information to be filled in
285 * @fid: the user-provided fid for filename
287 * The caller must have allocated sufficient memory for the @oname string.
289 * This overlay function is necessary to properly decode @iname before
290 * decryption, as it comes from the wire.
291 * This overlay function is also necessary to handle the case of operations
292 * carried out without the key. Normally llcrypt makes use of digested names in
293 * that case. Having a digested name works for local file systems that can call
294 * llcrypt_match_name(), but Lustre server side is not aware of encryption.
295 * So for keyless @lookup operations on long names, for Lustre we choose to
296 * present to users the encoded struct ll_digest_filename, instead of a digested
297 * name. FID and name hash can then easily be extracted and put into the
298 * requests sent to servers.
300 int ll_fname_disk_to_usr(struct inode *inode,
301 u32 hash, u32 minor_hash,
302 struct llcrypt_str *iname, struct llcrypt_str *oname,
305 struct llcrypt_str lltr = LLTR_INIT(iname->name, iname->len);
306 struct ll_digest_filename digest;
311 if (IS_ENCRYPTED(inode)) {
312 if (!name_is_dot_or_dotdot(lltr.name, lltr.len) &&
313 strnchr(lltr.name, lltr.len, '=')) {
314 /* Only proceed to critical decode if
315 * iname contains espace char '='.
319 buf = kmalloc(len, GFP_NOFS);
323 len = critical_decode(lltr.name, len, buf);
327 if (lltr.len > LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE &&
328 !llcrypt_has_encryption_key(inode) &&
329 likely(llcrypt_policy_has_filename_enc(inode))) {
331 /* Without the key for long names, set the dentry name
332 * to the representing struct ll_digest_filename. It
333 * will be encoded by llcrypt for display, and will
334 * enable further lookup requests.
338 digest.ldf_fid = *fid;
339 memcpy(digest.ldf_excerpt,
340 LLCRYPT_FNAME_DIGEST(lltr.name, lltr.len),
341 LLCRYPT_FNAME_DIGEST_SIZE);
343 lltr.name = (char *)&digest;
344 lltr.len = sizeof(digest);
346 oname->name[0] = '_';
347 oname->name = oname->name + 1;
352 rc = llcrypt_fname_disk_to_usr(inode, hash, minor_hash, &lltr, oname);
355 oname->name = oname->name - digested;
356 oname->len = oname->len + digested;
361 /* Copied from llcrypt_d_revalidate, as it is not exported */
363 * Validate dentries in encrypted directories to make sure we aren't potentially
364 * caching stale dentries after a key has been added.
366 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
373 * Plaintext names are always valid, since llcrypt doesn't support
374 * reverting to ciphertext names without evicting the directory's inode
375 * -- which implies eviction of the dentries in the directory.
377 if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
381 * Ciphertext name; valid if the directory's key is still unavailable.
383 * Although llcrypt forbids rename() on ciphertext names, we still must
384 * use dget_parent() here rather than use ->d_parent directly. That's
385 * because a corrupted fs image may contain directory hard links, which
386 * the VFS handles by moving the directory's dentry tree in the dcache
387 * each time ->lookup() finds the directory and it already has a dentry
388 * elsewhere. Thus ->d_parent can be changing, and we must safely grab
389 * a reference to some ->d_parent to prevent it from being freed.
392 if (flags & LOOKUP_RCU)
395 dir = dget_parent(dentry);
396 err = llcrypt_get_encryption_info(d_inode(dir));
397 valid = !llcrypt_has_encryption_key(d_inode(dir));
406 const struct llcrypt_operations lustre_cryptops = {
407 .key_prefix = "lustre:",
408 .get_context = ll_get_context,
409 .set_context = ll_set_context,
410 .dummy_context = ll_dummy_context,
411 .empty_dir = ll_empty_dir,
412 .max_namelen = NAME_MAX,
414 #else /* !HAVE_LUSTRE_CRYPTO */
415 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
421 void llcrypt_free_ctx(void *encctx, __u32 size)
425 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
430 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
435 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
439 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
440 int lookup, struct llcrypt_name *fname,
449 return llcrypt_setup_filename(dir, iname, lookup, fname);
452 int ll_fname_disk_to_usr(struct inode *inode,
453 u32 hash, u32 minor_hash,
454 struct llcrypt_str *iname, struct llcrypt_str *oname,
457 return llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname);
460 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)