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);
39 rc = ll_vfs_getxattr(dentry, inode, LL_XATTR_NAME_ENCRYPTION_CONTEXT,
44 /* used as encryption unit size */
45 if (S_ISREG(inode->i_mode))
46 inode->i_blkbits = LUSTRE_ENCRYPTION_BLOCKBITS;
50 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
53 unsigned int ext_flags;
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);
62 if (encctx && encctxlen)
63 rc = ll_xattr_cache_insert(inode,
64 LL_XATTR_NAME_ENCRYPTION_CONTEXT,
69 return preload ? llcrypt_get_encryption_info(inode) : 0;
72 /* ll_set_context has 2 distinct behaviors, depending on the value of inode
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.
84 static int ll_set_context(struct inode *inode, const void *ctx, size_t len,
87 struct dentry *dentry;
91 struct md_op_data *op_data = (struct md_op_data *)fs_data;
96 OBD_ALLOC(op_data->op_file_encctx, len);
97 if (op_data->op_file_encctx == NULL)
99 op_data->op_file_encctx_size = len;
100 memcpy(op_data->op_file_encctx, ctx, len);
104 /* Encrypting the root directory is not allowed */
105 if (is_root_inode(inode))
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);
115 return ll_set_encflags(inode, (void *)ctx, len, false);
118 void llcrypt_free_ctx(void *encctx, __u32 size)
121 OBD_FREE(encctx, size);
124 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
126 return unlikely(sbi->ll_flags & LL_SBI_TEST_DUMMY_ENCRYPTION);
129 static bool ll_dummy_context(struct inode *inode)
131 struct ll_sb_info *sbi = ll_i2sbi(inode);
133 return sbi ? ll_sbi_has_test_dummy_encryption(sbi) : false;
136 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
138 return sbi->ll_flags & LL_SBI_ENCRYPT;
141 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
144 sbi->ll_flags |= LL_SBI_ENCRYPT;
147 ~(LL_SBI_ENCRYPT | LL_SBI_TEST_DUMMY_ENCRYPTION);
150 static bool ll_empty_dir(struct inode *inode)
152 /* used by llcrypt_ioctl_set_policy(), because a policy can only be set
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.
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
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.
183 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
184 int lookup, struct llcrypt_name *fname,
191 if (fid && IS_ENCRYPTED(dir) && !llcrypt_has_encryption_key(dir) &&
192 iname->name[0] == '_')
195 dname.name = iname->name + digested;
196 dname.len = iname->len - digested;
203 rc = llcrypt_setup_filename(dir, &dname, lookup, fname);
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.
213 struct ll_digest_filename *digest;
215 if (fname->crypto_buf.len < sizeof(struct ll_digest_filename)) {
219 digest = (struct ll_digest_filename *)fname->crypto_buf.name;
220 *fid = digest->ldf_fid;
221 if (!fid_is_sane(fid)) {
225 fname->disk_name.name = digest->ldf_excerpt;
226 fname->disk_name.len = LLCRYPT_FNAME_DIGEST_SIZE;
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);
235 buf = kmalloc(presented_len + 1, GFP_NOFS);
241 if (presented_len == fname->disk_name.len)
242 memcpy(buf, fname->disk_name.name, presented_len);
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;
257 llcrypt_free_filename(fname);
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
270 * The caller must have allocated sufficient memory for the @oname string.
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.
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,
288 struct llcrypt_str lltr = LLTR_INIT(iname->name, iname->len);
289 struct ll_digest_filename digest;
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 '='.
302 buf = kmalloc(len, GFP_NOFS);
306 len = critical_decode(lltr.name, len, buf);
310 if (lltr.len > LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE &&
311 !llcrypt_has_encryption_key(inode) &&
312 likely(llcrypt_policy_has_filename_enc(inode))) {
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.
321 digest.ldf_fid = *fid;
322 memcpy(digest.ldf_excerpt,
323 LLCRYPT_FNAME_DIGEST(lltr.name, lltr.len),
324 LLCRYPT_FNAME_DIGEST_SIZE);
326 lltr.name = (char *)&digest;
327 lltr.len = sizeof(digest);
329 oname->name[0] = '_';
330 oname->name = oname->name + 1;
335 rc = llcrypt_fname_disk_to_usr(inode, hash, minor_hash, &lltr, oname);
338 oname->name = oname->name - digested;
339 oname->len = oname->len + digested;
344 /* Copied from llcrypt_d_revalidate, as it is not exported */
346 * Validate dentries in encrypted directories to make sure we aren't potentially
347 * caching stale dentries after a key has been added.
349 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)
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.
360 if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
364 * Ciphertext name; valid if the directory's key is still unavailable.
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.
375 if (flags & LOOKUP_RCU)
378 dir = dget_parent(dentry);
379 err = llcrypt_get_encryption_info(d_inode(dir));
380 valid = !llcrypt_has_encryption_key(d_inode(dir));
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,
397 #else /* !HAVE_LUSTRE_CRYPTO */
398 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
404 void llcrypt_free_ctx(void *encctx, __u32 size)
408 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi)
413 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi)
418 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set)
422 int ll_setup_filename(struct inode *dir, const struct qstr *iname,
423 int lookup, struct llcrypt_name *fname,
432 return llcrypt_setup_filename(dir, iname, lookup, fname);
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,
440 return llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname);
443 int ll_revalidate_d_crypto(struct dentry *dentry, unsigned int flags)