xref: /linux/fs/crypto/fname.c (revision d60ac92c105fd8c09224b92c3e34dd03327ba3f4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This contains functions for filename crypto management
4  *
5  * Copyright (C) 2015, Google, Inc.
6  * Copyright (C) 2015, Motorola Mobility
7  *
8  * Written by Uday Savagaonkar, 2014.
9  * Modified by Jaegeuk Kim, 2015.
10  *
11  * This has not yet undergone a rigorous security audit.
12  */
13 
14 #include <crypto/sha2.h>
15 #include <crypto/skcipher.h>
16 #include <linux/export.h>
17 #include <linux/namei.h>
18 #include <linux/scatterlist.h>
19 
20 #include "fscrypt_private.h"
21 
22 /*
23  * The minimum message length (input and output length), in bytes, for all
24  * filenames encryption modes.  Filenames shorter than this will be zero-padded
25  * before being encrypted.
26  */
27 #define FSCRYPT_FNAME_MIN_MSG_LEN 16
28 
29 /*
30  * struct fscrypt_nokey_name - identifier for directory entry when key is absent
31  *
32  * When userspace lists an encrypted directory without access to the key, the
33  * filesystem must present a unique "no-key name" for each filename that allows
34  * it to find the directory entry again if requested.  Naively, that would just
35  * mean using the ciphertext filenames.  However, since the ciphertext filenames
36  * can contain illegal characters ('\0' and '/'), they must be encoded in some
37  * way.  We use base64url.  But that can cause names to exceed NAME_MAX (255
38  * bytes), so we also need to use a strong hash to abbreviate long names.
39  *
40  * The filesystem may also need another kind of hash, the "dirhash", to quickly
41  * find the directory entry.  Since filesystems normally compute the dirhash
42  * over the on-disk filename (i.e. the ciphertext), it's not computable from
43  * no-key names that abbreviate the ciphertext using the strong hash to fit in
44  * NAME_MAX.  It's also not computable if it's a keyed hash taken over the
45  * plaintext (but it may still be available in the on-disk directory entry);
46  * casefolded directories use this type of dirhash.  At least in these cases,
47  * each no-key name must include the name's dirhash too.
48  *
49  * To meet all these requirements, we base64url-encode the following
50  * variable-length structure.  It contains the dirhash, or 0's if the filesystem
51  * didn't provide one; up to 149 bytes of the ciphertext name; and for
52  * ciphertexts longer than 149 bytes, also the SHA-256 of the remaining bytes.
53  *
54  * This ensures that each no-key name contains everything needed to find the
55  * directory entry again, contains only legal characters, doesn't exceed
56  * NAME_MAX, is unambiguous unless there's a SHA-256 collision, and that we only
57  * take the performance hit of SHA-256 on very long filenames (which are rare).
58  */
59 struct fscrypt_nokey_name {
60 	u32 dirhash[2];
61 	u8 bytes[149];
62 	u8 sha256[SHA256_DIGEST_SIZE];
63 }; /* 189 bytes => 252 bytes base64url-encoded, which is <= NAME_MAX (255) */
64 
65 /*
66  * Decoded size of max-size no-key name, i.e. a name that was abbreviated using
67  * the strong hash and thus includes the 'sha256' field.  This isn't simply
68  * sizeof(struct fscrypt_nokey_name), as the padding at the end isn't included.
69  */
70 #define FSCRYPT_NOKEY_NAME_MAX	offsetofend(struct fscrypt_nokey_name, sha256)
71 
72 /* Encoded size of max-size no-key name */
73 #define FSCRYPT_NOKEY_NAME_MAX_ENCODED \
74 		FSCRYPT_BASE64URL_CHARS(FSCRYPT_NOKEY_NAME_MAX)
75 
fscrypt_is_dot_dotdot(const struct qstr * str)76 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
77 {
78 	return is_dot_dotdot(str->name, str->len);
79 }
80 
81 /**
82  * fscrypt_fname_encrypt() - encrypt a filename
83  * @inode: inode of the parent directory (for regular filenames)
84  *	   or of the symlink (for symlink targets). Key must already be
85  *	   set up.
86  * @iname: the filename to encrypt
87  * @out: (output) the encrypted filename
88  * @olen: size of the encrypted filename.  It must be at least @iname->len.
89  *	  Any extra space is filled with NUL padding before encryption.
90  *
91  * Return: 0 on success, -errno on failure
92  */
fscrypt_fname_encrypt(const struct inode * inode,const struct qstr * iname,u8 * out,unsigned int olen)93 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
94 			  u8 *out, unsigned int olen)
95 {
96 	const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
97 	struct crypto_sync_skcipher *tfm = ci->ci_enc_key.tfm;
98 	SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
99 	union fscrypt_iv iv;
100 	struct scatterlist sg;
101 	int err;
102 
103 	/*
104 	 * Copy the filename to the output buffer for encrypting in-place and
105 	 * pad it with the needed number of NUL bytes.
106 	 */
107 	if (WARN_ON_ONCE(olen < iname->len))
108 		return -ENOBUFS;
109 	memcpy(out, iname->name, iname->len);
110 	memset(out + iname->len, 0, olen - iname->len);
111 
112 	fscrypt_generate_iv(&iv, 0, ci);
113 
114 	skcipher_request_set_callback(
115 		req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
116 		NULL, NULL);
117 	sg_init_one(&sg, out, olen);
118 	skcipher_request_set_crypt(req, &sg, &sg, olen, &iv);
119 	err = crypto_skcipher_encrypt(req);
120 	if (err)
121 		fscrypt_err(inode, "Filename encryption failed: %d", err);
122 	return err;
123 }
124 EXPORT_SYMBOL_GPL(fscrypt_fname_encrypt);
125 
126 /**
127  * fname_decrypt() - decrypt a filename
128  * @inode: inode of the parent directory (for regular filenames)
129  *	   or of the symlink (for symlink targets)
130  * @iname: the encrypted filename to decrypt
131  * @oname: (output) the decrypted filename.  The caller must have allocated
132  *	   enough space for this, e.g. using fscrypt_fname_alloc_buffer().
133  *
134  * Return: 0 on success, -errno on failure
135  */
fname_decrypt(const struct inode * inode,const struct fscrypt_str * iname,struct fscrypt_str * oname)136 static int fname_decrypt(const struct inode *inode,
137 			 const struct fscrypt_str *iname,
138 			 struct fscrypt_str *oname)
139 {
140 	const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
141 	struct crypto_sync_skcipher *tfm = ci->ci_enc_key.tfm;
142 	SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
143 	union fscrypt_iv iv;
144 	struct scatterlist src_sg, dst_sg;
145 	int err;
146 
147 	fscrypt_generate_iv(&iv, 0, ci);
148 
149 	skcipher_request_set_callback(
150 		req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
151 		NULL, NULL);
152 	sg_init_one(&src_sg, iname->name, iname->len);
153 	sg_init_one(&dst_sg, oname->name, oname->len);
154 	skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv);
155 	err = crypto_skcipher_decrypt(req);
156 	if (err) {
157 		fscrypt_err(inode, "Filename decryption failed: %d", err);
158 		return err;
159 	}
160 
161 	oname->len = strnlen(oname->name, iname->len);
162 	return 0;
163 }
164 
165 static const char base64url_table[65] =
166 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
167 
168 #define FSCRYPT_BASE64URL_CHARS(nbytes)	DIV_ROUND_UP((nbytes) * 4, 3)
169 
170 /**
171  * fscrypt_base64url_encode() - base64url-encode some binary data
172  * @src: the binary data to encode
173  * @srclen: the length of @src in bytes
174  * @dst: (output) the base64url-encoded string.  Not NUL-terminated.
175  *
176  * Encodes data using base64url encoding, i.e. the "Base 64 Encoding with URL
177  * and Filename Safe Alphabet" specified by RFC 4648.  '='-padding isn't used,
178  * as it's unneeded and not required by the RFC.  base64url is used instead of
179  * base64 to avoid the '/' character, which isn't allowed in filenames.
180  *
181  * Return: the length of the resulting base64url-encoded string in bytes.
182  *	   This will be equal to FSCRYPT_BASE64URL_CHARS(srclen).
183  */
fscrypt_base64url_encode(const u8 * src,int srclen,char * dst)184 static int fscrypt_base64url_encode(const u8 *src, int srclen, char *dst)
185 {
186 	u32 ac = 0;
187 	int bits = 0;
188 	int i;
189 	char *cp = dst;
190 
191 	for (i = 0; i < srclen; i++) {
192 		ac = (ac << 8) | src[i];
193 		bits += 8;
194 		do {
195 			bits -= 6;
196 			*cp++ = base64url_table[(ac >> bits) & 0x3f];
197 		} while (bits >= 6);
198 	}
199 	if (bits)
200 		*cp++ = base64url_table[(ac << (6 - bits)) & 0x3f];
201 	return cp - dst;
202 }
203 
204 /**
205  * fscrypt_base64url_decode() - base64url-decode a string
206  * @src: the string to decode.  Doesn't need to be NUL-terminated.
207  * @srclen: the length of @src in bytes
208  * @dst: (output) the decoded binary data
209  *
210  * Decodes a string using base64url encoding, i.e. the "Base 64 Encoding with
211  * URL and Filename Safe Alphabet" specified by RFC 4648.  '='-padding isn't
212  * accepted, nor are non-encoding characters such as whitespace.
213  *
214  * This implementation hasn't been optimized for performance.
215  *
216  * Return: the length of the resulting decoded binary data in bytes,
217  *	   or -1 if the string isn't a valid base64url string.
218  */
fscrypt_base64url_decode(const char * src,int srclen,u8 * dst)219 static int fscrypt_base64url_decode(const char *src, int srclen, u8 *dst)
220 {
221 	u32 ac = 0;
222 	int bits = 0;
223 	int i;
224 	u8 *bp = dst;
225 
226 	for (i = 0; i < srclen; i++) {
227 		const char *p = strchr(base64url_table, src[i]);
228 
229 		if (p == NULL || src[i] == 0)
230 			return -1;
231 		ac = (ac << 6) | (p - base64url_table);
232 		bits += 6;
233 		if (bits >= 8) {
234 			bits -= 8;
235 			*bp++ = (u8)(ac >> bits);
236 		}
237 	}
238 	if (ac & ((1 << bits) - 1))
239 		return -1;
240 	return bp - dst;
241 }
242 
__fscrypt_fname_encrypted_size(const union fscrypt_policy * policy,u32 orig_len,u32 max_len,u32 * encrypted_len_ret)243 bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
244 				    u32 orig_len, u32 max_len,
245 				    u32 *encrypted_len_ret)
246 {
247 	int padding = 4 << (fscrypt_policy_flags(policy) &
248 			    FSCRYPT_POLICY_FLAGS_PAD_MASK);
249 	u32 encrypted_len;
250 
251 	if (orig_len > max_len)
252 		return false;
253 	encrypted_len = max_t(u32, orig_len, FSCRYPT_FNAME_MIN_MSG_LEN);
254 	encrypted_len = round_up(encrypted_len, padding);
255 	*encrypted_len_ret = min(encrypted_len, max_len);
256 	return true;
257 }
258 
259 /**
260  * fscrypt_fname_encrypted_size() - calculate length of encrypted filename
261  * @inode:		parent inode of dentry name being encrypted. Key must
262  *			already be set up.
263  * @orig_len:		length of the original filename
264  * @max_len:		maximum length to return
265  * @encrypted_len_ret:	where calculated length should be returned (on success)
266  *
267  * Filenames that are shorter than the maximum length may have their lengths
268  * increased slightly by encryption, due to padding that is applied.
269  *
270  * Return: false if the orig_len is greater than max_len. Otherwise, true and
271  *	   fill out encrypted_len_ret with the length (up to max_len).
272  */
fscrypt_fname_encrypted_size(const struct inode * inode,u32 orig_len,u32 max_len,u32 * encrypted_len_ret)273 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
274 				  u32 max_len, u32 *encrypted_len_ret)
275 {
276 	const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
277 
278 	return __fscrypt_fname_encrypted_size(&ci->ci_policy, orig_len, max_len,
279 					      encrypted_len_ret);
280 }
281 EXPORT_SYMBOL_GPL(fscrypt_fname_encrypted_size);
282 
283 /**
284  * fscrypt_fname_alloc_buffer() - allocate a buffer for presented filenames
285  * @max_encrypted_len: maximum length of encrypted filenames the buffer will be
286  *		       used to present
287  * @crypto_str: (output) buffer to allocate
288  *
289  * Allocate a buffer that is large enough to hold any decrypted or encoded
290  * filename (null-terminated), for the given maximum encrypted filename length.
291  *
292  * Return: 0 on success, -errno on failure
293  */
fscrypt_fname_alloc_buffer(u32 max_encrypted_len,struct fscrypt_str * crypto_str)294 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
295 			       struct fscrypt_str *crypto_str)
296 {
297 	u32 max_presented_len = max_t(u32, FSCRYPT_NOKEY_NAME_MAX_ENCODED,
298 				      max_encrypted_len);
299 
300 	crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
301 	if (!crypto_str->name)
302 		return -ENOMEM;
303 	crypto_str->len = max_presented_len;
304 	return 0;
305 }
306 EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
307 
308 /**
309  * fscrypt_fname_free_buffer() - free a buffer for presented filenames
310  * @crypto_str: the buffer to free
311  *
312  * Free a buffer that was allocated by fscrypt_fname_alloc_buffer().
313  */
fscrypt_fname_free_buffer(struct fscrypt_str * crypto_str)314 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
315 {
316 	if (!crypto_str)
317 		return;
318 	kfree(crypto_str->name);
319 	crypto_str->name = NULL;
320 }
321 EXPORT_SYMBOL(fscrypt_fname_free_buffer);
322 
323 /**
324  * fscrypt_fname_disk_to_usr() - convert an encrypted filename to
325  *				 user-presentable form
326  * @inode: inode of the parent directory (for regular filenames)
327  *	   or of the symlink (for symlink targets)
328  * @hash: first part of the name's dirhash, if applicable.  This only needs to
329  *	  be provided if the filename is located in an indexed directory whose
330  *	  encryption key may be unavailable.  Not needed for symlink targets.
331  * @minor_hash: second part of the name's dirhash, if applicable
332  * @iname: encrypted filename to convert.  May also be "." or "..", which
333  *	   aren't actually encrypted.
334  * @oname: output buffer for the user-presentable filename.  The caller must
335  *	   have allocated enough space for this, e.g. using
336  *	   fscrypt_fname_alloc_buffer().
337  *
338  * If the key is available, we'll decrypt the disk name.  Otherwise, we'll
339  * encode it for presentation in fscrypt_nokey_name format.
340  * See struct fscrypt_nokey_name for details.
341  *
342  * Return: 0 on success, -errno on failure
343  */
fscrypt_fname_disk_to_usr(const struct inode * inode,u32 hash,u32 minor_hash,const struct fscrypt_str * iname,struct fscrypt_str * oname)344 int fscrypt_fname_disk_to_usr(const struct inode *inode,
345 			      u32 hash, u32 minor_hash,
346 			      const struct fscrypt_str *iname,
347 			      struct fscrypt_str *oname)
348 {
349 	const struct qstr qname = FSTR_TO_QSTR(iname);
350 	struct fscrypt_nokey_name nokey_name;
351 	u32 size; /* size of the unencoded no-key name */
352 
353 	if (fscrypt_is_dot_dotdot(&qname)) {
354 		oname->name[0] = '.';
355 		oname->name[iname->len - 1] = '.';
356 		oname->len = iname->len;
357 		return 0;
358 	}
359 
360 	if (iname->len < FSCRYPT_FNAME_MIN_MSG_LEN)
361 		return -EUCLEAN;
362 
363 	if (fscrypt_has_encryption_key(inode))
364 		return fname_decrypt(inode, iname, oname);
365 
366 	/*
367 	 * Sanity check that struct fscrypt_nokey_name doesn't have padding
368 	 * between fields and that its encoded size never exceeds NAME_MAX.
369 	 */
370 	BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, dirhash) !=
371 		     offsetof(struct fscrypt_nokey_name, bytes));
372 	BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, bytes) !=
373 		     offsetof(struct fscrypt_nokey_name, sha256));
374 	BUILD_BUG_ON(FSCRYPT_NOKEY_NAME_MAX_ENCODED > NAME_MAX);
375 
376 	nokey_name.dirhash[0] = hash;
377 	nokey_name.dirhash[1] = minor_hash;
378 
379 	if (iname->len <= sizeof(nokey_name.bytes)) {
380 		memcpy(nokey_name.bytes, iname->name, iname->len);
381 		size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]);
382 	} else {
383 		memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes));
384 		/* Compute strong hash of remaining part of name. */
385 		sha256(&iname->name[sizeof(nokey_name.bytes)],
386 		       iname->len - sizeof(nokey_name.bytes),
387 		       nokey_name.sha256);
388 		size = FSCRYPT_NOKEY_NAME_MAX;
389 	}
390 	oname->len = fscrypt_base64url_encode((const u8 *)&nokey_name, size,
391 					      oname->name);
392 	return 0;
393 }
394 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
395 
396 /**
397  * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
398  * @dir: the directory that will be searched
399  * @iname: the user-provided filename being searched for
400  * @lookup: 1 if we're allowed to proceed without the key because it's
401  *	->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
402  *	proceed without the key because we're going to create the dir_entry.
403  * @fname: the filename information to be filled in
404  *
405  * Given a user-provided filename @iname, this function sets @fname->disk_name
406  * to the name that would be stored in the on-disk directory entry, if possible.
407  * If the directory is unencrypted this is simply @iname.  Else, if we have the
408  * directory's encryption key, then @iname is the plaintext, so we encrypt it to
409  * get the disk_name.
410  *
411  * Else, for keyless @lookup operations, @iname should be a no-key name, so we
412  * decode it to get the struct fscrypt_nokey_name.  Non-@lookup operations will
413  * be impossible in this case, so we fail them with ENOKEY.
414  *
415  * If successful, fscrypt_free_filename() must be called later to clean up.
416  *
417  * Return: 0 on success, -errno on failure
418  */
fscrypt_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct fscrypt_name * fname)419 int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
420 			      int lookup, struct fscrypt_name *fname)
421 {
422 	struct fscrypt_nokey_name *nokey_name;
423 	int ret;
424 
425 	memset(fname, 0, sizeof(struct fscrypt_name));
426 	fname->usr_fname = iname;
427 
428 	if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
429 		fname->disk_name.name = (unsigned char *)iname->name;
430 		fname->disk_name.len = iname->len;
431 		return 0;
432 	}
433 	ret = fscrypt_get_encryption_info(dir, lookup);
434 	if (ret)
435 		return ret;
436 
437 	if (fscrypt_has_encryption_key(dir)) {
438 		if (!fscrypt_fname_encrypted_size(dir, iname->len, NAME_MAX,
439 						  &fname->crypto_buf.len))
440 			return -ENAMETOOLONG;
441 		fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
442 						 GFP_NOFS);
443 		if (!fname->crypto_buf.name)
444 			return -ENOMEM;
445 
446 		ret = fscrypt_fname_encrypt(dir, iname, fname->crypto_buf.name,
447 					    fname->crypto_buf.len);
448 		if (ret)
449 			goto errout;
450 		fname->disk_name.name = fname->crypto_buf.name;
451 		fname->disk_name.len = fname->crypto_buf.len;
452 		return 0;
453 	}
454 	if (!lookup)
455 		return -ENOKEY;
456 	fname->is_nokey_name = true;
457 
458 	/*
459 	 * We don't have the key and we are doing a lookup; decode the
460 	 * user-supplied name
461 	 */
462 
463 	if (iname->len > FSCRYPT_NOKEY_NAME_MAX_ENCODED)
464 		return -ENOENT;
465 
466 	fname->crypto_buf.name = kmalloc(FSCRYPT_NOKEY_NAME_MAX, GFP_KERNEL);
467 	if (fname->crypto_buf.name == NULL)
468 		return -ENOMEM;
469 
470 	ret = fscrypt_base64url_decode(iname->name, iname->len,
471 				       fname->crypto_buf.name);
472 	if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) ||
473 	    (ret > offsetof(struct fscrypt_nokey_name, sha256) &&
474 	     ret != FSCRYPT_NOKEY_NAME_MAX)) {
475 		ret = -ENOENT;
476 		goto errout;
477 	}
478 	fname->crypto_buf.len = ret;
479 
480 	nokey_name = (void *)fname->crypto_buf.name;
481 	fname->hash = nokey_name->dirhash[0];
482 	fname->minor_hash = nokey_name->dirhash[1];
483 	if (ret != FSCRYPT_NOKEY_NAME_MAX) {
484 		/* The full ciphertext filename is available. */
485 		fname->disk_name.name = nokey_name->bytes;
486 		fname->disk_name.len =
487 			ret - offsetof(struct fscrypt_nokey_name, bytes);
488 	}
489 	return 0;
490 
491 errout:
492 	kfree(fname->crypto_buf.name);
493 	return ret;
494 }
495 EXPORT_SYMBOL(fscrypt_setup_filename);
496 
497 /**
498  * fscrypt_match_name() - test whether the given name matches a directory entry
499  * @fname: the name being searched for
500  * @de_name: the name from the directory entry
501  * @de_name_len: the length of @de_name in bytes
502  *
503  * Normally @fname->disk_name will be set, and in that case we simply compare
504  * that to the name stored in the directory entry.  The only exception is that
505  * if we don't have the key for an encrypted directory and the name we're
506  * looking for is very long, then we won't have the full disk_name and instead
507  * we'll need to match against a fscrypt_nokey_name that includes a strong hash.
508  *
509  * Return: %true if the name matches, otherwise %false.
510  */
fscrypt_match_name(const struct fscrypt_name * fname,const u8 * de_name,u32 de_name_len)511 bool fscrypt_match_name(const struct fscrypt_name *fname,
512 			const u8 *de_name, u32 de_name_len)
513 {
514 	const struct fscrypt_nokey_name *nokey_name =
515 		(const void *)fname->crypto_buf.name;
516 	u8 digest[SHA256_DIGEST_SIZE];
517 
518 	if (likely(fname->disk_name.name)) {
519 		if (de_name_len != fname->disk_name.len)
520 			return false;
521 		return !memcmp(de_name, fname->disk_name.name, de_name_len);
522 	}
523 	if (de_name_len <= sizeof(nokey_name->bytes))
524 		return false;
525 	if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes)))
526 		return false;
527 	sha256(&de_name[sizeof(nokey_name->bytes)],
528 	       de_name_len - sizeof(nokey_name->bytes), digest);
529 	return !memcmp(digest, nokey_name->sha256, sizeof(digest));
530 }
531 EXPORT_SYMBOL_GPL(fscrypt_match_name);
532 
533 /**
534  * fscrypt_fname_siphash() - calculate the SipHash of a filename
535  * @dir: the parent directory
536  * @name: the filename to calculate the SipHash of
537  *
538  * Given a plaintext filename @name and a directory @dir which uses SipHash as
539  * its dirhash method and has had its fscrypt key set up, this function
540  * calculates the SipHash of that name using the directory's secret dirhash key.
541  *
542  * Return: the SipHash of @name using the hash key of @dir
543  */
fscrypt_fname_siphash(const struct inode * dir,const struct qstr * name)544 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name)
545 {
546 	const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(dir);
547 
548 	WARN_ON_ONCE(!ci->ci_dirhash_key_initialized);
549 
550 	return siphash(name->name, name->len, &ci->ci_dirhash_key);
551 }
552 EXPORT_SYMBOL_GPL(fscrypt_fname_siphash);
553 
554 /*
555  * Validate dentries in encrypted directories to make sure we aren't potentially
556  * caching stale dentries after a key has been added.
557  */
fscrypt_d_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)558 int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name,
559 			 struct dentry *dentry, unsigned int flags)
560 {
561 	int err;
562 
563 	/*
564 	 * Plaintext names are always valid, since fscrypt doesn't support
565 	 * reverting to no-key names without evicting the directory's inode
566 	 * -- which implies eviction of the dentries in the directory.
567 	 */
568 	if (!(dentry->d_flags & DCACHE_NOKEY_NAME))
569 		return 1;
570 
571 	/*
572 	 * No-key name; valid if the directory's key is still unavailable.
573 	 *
574 	 * Note in RCU mode we have to bail if we get here -
575 	 * fscrypt_get_encryption_info() may block.
576 	 */
577 
578 	if (flags & LOOKUP_RCU)
579 		return -ECHILD;
580 
581 	/*
582 	 * Pass allow_unsupported=true, so that files with an unsupported
583 	 * encryption policy can be deleted.
584 	 */
585 	err = fscrypt_get_encryption_info(dir, true);
586 	if (err < 0)
587 		return err;
588 
589 	return !fscrypt_has_encryption_key(dir);
590 }
591 EXPORT_SYMBOL_GPL(fscrypt_d_revalidate);
592