Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This contains functions for filename crypto management
0004  *
0005  * Copyright (C) 2015, Google, Inc.
0006  * Copyright (C) 2015, Motorola Mobility
0007  *
0008  * Written by Uday Savagaonkar, 2014.
0009  * Modified by Jaegeuk Kim, 2015.
0010  *
0011  * This has not yet undergone a rigorous security audit.
0012  */
0013 
0014 #include <linux/namei.h>
0015 #include <linux/scatterlist.h>
0016 #include <crypto/hash.h>
0017 #include <crypto/sha2.h>
0018 #include <crypto/skcipher.h>
0019 #include "fscrypt_private.h"
0020 
0021 /*
0022  * The minimum message length (input and output length), in bytes, for all
0023  * filenames encryption modes.  Filenames shorter than this will be zero-padded
0024  * before being encrypted.
0025  */
0026 #define FSCRYPT_FNAME_MIN_MSG_LEN 16
0027 
0028 /*
0029  * struct fscrypt_nokey_name - identifier for directory entry when key is absent
0030  *
0031  * When userspace lists an encrypted directory without access to the key, the
0032  * filesystem must present a unique "no-key name" for each filename that allows
0033  * it to find the directory entry again if requested.  Naively, that would just
0034  * mean using the ciphertext filenames.  However, since the ciphertext filenames
0035  * can contain illegal characters ('\0' and '/'), they must be encoded in some
0036  * way.  We use base64url.  But that can cause names to exceed NAME_MAX (255
0037  * bytes), so we also need to use a strong hash to abbreviate long names.
0038  *
0039  * The filesystem may also need another kind of hash, the "dirhash", to quickly
0040  * find the directory entry.  Since filesystems normally compute the dirhash
0041  * over the on-disk filename (i.e. the ciphertext), it's not computable from
0042  * no-key names that abbreviate the ciphertext using the strong hash to fit in
0043  * NAME_MAX.  It's also not computable if it's a keyed hash taken over the
0044  * plaintext (but it may still be available in the on-disk directory entry);
0045  * casefolded directories use this type of dirhash.  At least in these cases,
0046  * each no-key name must include the name's dirhash too.
0047  *
0048  * To meet all these requirements, we base64url-encode the following
0049  * variable-length structure.  It contains the dirhash, or 0's if the filesystem
0050  * didn't provide one; up to 149 bytes of the ciphertext name; and for
0051  * ciphertexts longer than 149 bytes, also the SHA-256 of the remaining bytes.
0052  *
0053  * This ensures that each no-key name contains everything needed to find the
0054  * directory entry again, contains only legal characters, doesn't exceed
0055  * NAME_MAX, is unambiguous unless there's a SHA-256 collision, and that we only
0056  * take the performance hit of SHA-256 on very long filenames (which are rare).
0057  */
0058 struct fscrypt_nokey_name {
0059     u32 dirhash[2];
0060     u8 bytes[149];
0061     u8 sha256[SHA256_DIGEST_SIZE];
0062 }; /* 189 bytes => 252 bytes base64url-encoded, which is <= NAME_MAX (255) */
0063 
0064 /*
0065  * Decoded size of max-size no-key name, i.e. a name that was abbreviated using
0066  * the strong hash and thus includes the 'sha256' field.  This isn't simply
0067  * sizeof(struct fscrypt_nokey_name), as the padding at the end isn't included.
0068  */
0069 #define FSCRYPT_NOKEY_NAME_MAX  offsetofend(struct fscrypt_nokey_name, sha256)
0070 
0071 /* Encoded size of max-size no-key name */
0072 #define FSCRYPT_NOKEY_NAME_MAX_ENCODED \
0073         FSCRYPT_BASE64URL_CHARS(FSCRYPT_NOKEY_NAME_MAX)
0074 
0075 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
0076 {
0077     if (str->len == 1 && str->name[0] == '.')
0078         return true;
0079 
0080     if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
0081         return true;
0082 
0083     return false;
0084 }
0085 
0086 /**
0087  * fscrypt_fname_encrypt() - encrypt a filename
0088  * @inode: inode of the parent directory (for regular filenames)
0089  *     or of the symlink (for symlink targets). Key must already be
0090  *     set up.
0091  * @iname: the filename to encrypt
0092  * @out: (output) the encrypted filename
0093  * @olen: size of the encrypted filename.  It must be at least @iname->len.
0094  *    Any extra space is filled with NUL padding before encryption.
0095  *
0096  * Return: 0 on success, -errno on failure
0097  */
0098 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
0099               u8 *out, unsigned int olen)
0100 {
0101     struct skcipher_request *req = NULL;
0102     DECLARE_CRYPTO_WAIT(wait);
0103     const struct fscrypt_info *ci = inode->i_crypt_info;
0104     struct crypto_skcipher *tfm = ci->ci_enc_key.tfm;
0105     union fscrypt_iv iv;
0106     struct scatterlist sg;
0107     int res;
0108 
0109     /*
0110      * Copy the filename to the output buffer for encrypting in-place and
0111      * pad it with the needed number of NUL bytes.
0112      */
0113     if (WARN_ON(olen < iname->len))
0114         return -ENOBUFS;
0115     memcpy(out, iname->name, iname->len);
0116     memset(out + iname->len, 0, olen - iname->len);
0117 
0118     /* Initialize the IV */
0119     fscrypt_generate_iv(&iv, 0, ci);
0120 
0121     /* Set up the encryption request */
0122     req = skcipher_request_alloc(tfm, GFP_NOFS);
0123     if (!req)
0124         return -ENOMEM;
0125     skcipher_request_set_callback(req,
0126             CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
0127             crypto_req_done, &wait);
0128     sg_init_one(&sg, out, olen);
0129     skcipher_request_set_crypt(req, &sg, &sg, olen, &iv);
0130 
0131     /* Do the encryption */
0132     res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
0133     skcipher_request_free(req);
0134     if (res < 0) {
0135         fscrypt_err(inode, "Filename encryption failed: %d", res);
0136         return res;
0137     }
0138 
0139     return 0;
0140 }
0141 EXPORT_SYMBOL_GPL(fscrypt_fname_encrypt);
0142 
0143 /**
0144  * fname_decrypt() - decrypt a filename
0145  * @inode: inode of the parent directory (for regular filenames)
0146  *     or of the symlink (for symlink targets)
0147  * @iname: the encrypted filename to decrypt
0148  * @oname: (output) the decrypted filename.  The caller must have allocated
0149  *     enough space for this, e.g. using fscrypt_fname_alloc_buffer().
0150  *
0151  * Return: 0 on success, -errno on failure
0152  */
0153 static int fname_decrypt(const struct inode *inode,
0154              const struct fscrypt_str *iname,
0155              struct fscrypt_str *oname)
0156 {
0157     struct skcipher_request *req = NULL;
0158     DECLARE_CRYPTO_WAIT(wait);
0159     struct scatterlist src_sg, dst_sg;
0160     const struct fscrypt_info *ci = inode->i_crypt_info;
0161     struct crypto_skcipher *tfm = ci->ci_enc_key.tfm;
0162     union fscrypt_iv iv;
0163     int res;
0164 
0165     /* Allocate request */
0166     req = skcipher_request_alloc(tfm, GFP_NOFS);
0167     if (!req)
0168         return -ENOMEM;
0169     skcipher_request_set_callback(req,
0170         CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
0171         crypto_req_done, &wait);
0172 
0173     /* Initialize IV */
0174     fscrypt_generate_iv(&iv, 0, ci);
0175 
0176     /* Create decryption request */
0177     sg_init_one(&src_sg, iname->name, iname->len);
0178     sg_init_one(&dst_sg, oname->name, oname->len);
0179     skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv);
0180     res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
0181     skcipher_request_free(req);
0182     if (res < 0) {
0183         fscrypt_err(inode, "Filename decryption failed: %d", res);
0184         return res;
0185     }
0186 
0187     oname->len = strnlen(oname->name, iname->len);
0188     return 0;
0189 }
0190 
0191 static const char base64url_table[65] =
0192     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
0193 
0194 #define FSCRYPT_BASE64URL_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
0195 
0196 /**
0197  * fscrypt_base64url_encode() - base64url-encode some binary data
0198  * @src: the binary data to encode
0199  * @srclen: the length of @src in bytes
0200  * @dst: (output) the base64url-encoded string.  Not NUL-terminated.
0201  *
0202  * Encodes data using base64url encoding, i.e. the "Base 64 Encoding with URL
0203  * and Filename Safe Alphabet" specified by RFC 4648.  '='-padding isn't used,
0204  * as it's unneeded and not required by the RFC.  base64url is used instead of
0205  * base64 to avoid the '/' character, which isn't allowed in filenames.
0206  *
0207  * Return: the length of the resulting base64url-encoded string in bytes.
0208  *     This will be equal to FSCRYPT_BASE64URL_CHARS(srclen).
0209  */
0210 static int fscrypt_base64url_encode(const u8 *src, int srclen, char *dst)
0211 {
0212     u32 ac = 0;
0213     int bits = 0;
0214     int i;
0215     char *cp = dst;
0216 
0217     for (i = 0; i < srclen; i++) {
0218         ac = (ac << 8) | src[i];
0219         bits += 8;
0220         do {
0221             bits -= 6;
0222             *cp++ = base64url_table[(ac >> bits) & 0x3f];
0223         } while (bits >= 6);
0224     }
0225     if (bits)
0226         *cp++ = base64url_table[(ac << (6 - bits)) & 0x3f];
0227     return cp - dst;
0228 }
0229 
0230 /**
0231  * fscrypt_base64url_decode() - base64url-decode a string
0232  * @src: the string to decode.  Doesn't need to be NUL-terminated.
0233  * @srclen: the length of @src in bytes
0234  * @dst: (output) the decoded binary data
0235  *
0236  * Decodes a string using base64url encoding, i.e. the "Base 64 Encoding with
0237  * URL and Filename Safe Alphabet" specified by RFC 4648.  '='-padding isn't
0238  * accepted, nor are non-encoding characters such as whitespace.
0239  *
0240  * This implementation hasn't been optimized for performance.
0241  *
0242  * Return: the length of the resulting decoded binary data in bytes,
0243  *     or -1 if the string isn't a valid base64url string.
0244  */
0245 static int fscrypt_base64url_decode(const char *src, int srclen, u8 *dst)
0246 {
0247     u32 ac = 0;
0248     int bits = 0;
0249     int i;
0250     u8 *bp = dst;
0251 
0252     for (i = 0; i < srclen; i++) {
0253         const char *p = strchr(base64url_table, src[i]);
0254 
0255         if (p == NULL || src[i] == 0)
0256             return -1;
0257         ac = (ac << 6) | (p - base64url_table);
0258         bits += 6;
0259         if (bits >= 8) {
0260             bits -= 8;
0261             *bp++ = (u8)(ac >> bits);
0262         }
0263     }
0264     if (ac & ((1 << bits) - 1))
0265         return -1;
0266     return bp - dst;
0267 }
0268 
0269 bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
0270                     u32 orig_len, u32 max_len,
0271                     u32 *encrypted_len_ret)
0272 {
0273     int padding = 4 << (fscrypt_policy_flags(policy) &
0274                 FSCRYPT_POLICY_FLAGS_PAD_MASK);
0275     u32 encrypted_len;
0276 
0277     if (orig_len > max_len)
0278         return false;
0279     encrypted_len = max_t(u32, orig_len, FSCRYPT_FNAME_MIN_MSG_LEN);
0280     encrypted_len = round_up(encrypted_len, padding);
0281     *encrypted_len_ret = min(encrypted_len, max_len);
0282     return true;
0283 }
0284 
0285 /**
0286  * fscrypt_fname_encrypted_size() - calculate length of encrypted filename
0287  * @inode:      parent inode of dentry name being encrypted. Key must
0288  *          already be set up.
0289  * @orig_len:       length of the original filename
0290  * @max_len:        maximum length to return
0291  * @encrypted_len_ret:  where calculated length should be returned (on success)
0292  *
0293  * Filenames that are shorter than the maximum length may have their lengths
0294  * increased slightly by encryption, due to padding that is applied.
0295  *
0296  * Return: false if the orig_len is greater than max_len. Otherwise, true and
0297  *     fill out encrypted_len_ret with the length (up to max_len).
0298  */
0299 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
0300                   u32 max_len, u32 *encrypted_len_ret)
0301 {
0302     return __fscrypt_fname_encrypted_size(&inode->i_crypt_info->ci_policy,
0303                           orig_len, max_len,
0304                           encrypted_len_ret);
0305 }
0306 EXPORT_SYMBOL_GPL(fscrypt_fname_encrypted_size);
0307 
0308 /**
0309  * fscrypt_fname_alloc_buffer() - allocate a buffer for presented filenames
0310  * @max_encrypted_len: maximum length of encrypted filenames the buffer will be
0311  *             used to present
0312  * @crypto_str: (output) buffer to allocate
0313  *
0314  * Allocate a buffer that is large enough to hold any decrypted or encoded
0315  * filename (null-terminated), for the given maximum encrypted filename length.
0316  *
0317  * Return: 0 on success, -errno on failure
0318  */
0319 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
0320                    struct fscrypt_str *crypto_str)
0321 {
0322     u32 max_presented_len = max_t(u32, FSCRYPT_NOKEY_NAME_MAX_ENCODED,
0323                       max_encrypted_len);
0324 
0325     crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
0326     if (!crypto_str->name)
0327         return -ENOMEM;
0328     crypto_str->len = max_presented_len;
0329     return 0;
0330 }
0331 EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
0332 
0333 /**
0334  * fscrypt_fname_free_buffer() - free a buffer for presented filenames
0335  * @crypto_str: the buffer to free
0336  *
0337  * Free a buffer that was allocated by fscrypt_fname_alloc_buffer().
0338  */
0339 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
0340 {
0341     if (!crypto_str)
0342         return;
0343     kfree(crypto_str->name);
0344     crypto_str->name = NULL;
0345 }
0346 EXPORT_SYMBOL(fscrypt_fname_free_buffer);
0347 
0348 /**
0349  * fscrypt_fname_disk_to_usr() - convert an encrypted filename to
0350  *               user-presentable form
0351  * @inode: inode of the parent directory (for regular filenames)
0352  *     or of the symlink (for symlink targets)
0353  * @hash: first part of the name's dirhash, if applicable.  This only needs to
0354  *    be provided if the filename is located in an indexed directory whose
0355  *    encryption key may be unavailable.  Not needed for symlink targets.
0356  * @minor_hash: second part of the name's dirhash, if applicable
0357  * @iname: encrypted filename to convert.  May also be "." or "..", which
0358  *     aren't actually encrypted.
0359  * @oname: output buffer for the user-presentable filename.  The caller must
0360  *     have allocated enough space for this, e.g. using
0361  *     fscrypt_fname_alloc_buffer().
0362  *
0363  * If the key is available, we'll decrypt the disk name.  Otherwise, we'll
0364  * encode it for presentation in fscrypt_nokey_name format.
0365  * See struct fscrypt_nokey_name for details.
0366  *
0367  * Return: 0 on success, -errno on failure
0368  */
0369 int fscrypt_fname_disk_to_usr(const struct inode *inode,
0370                   u32 hash, u32 minor_hash,
0371                   const struct fscrypt_str *iname,
0372                   struct fscrypt_str *oname)
0373 {
0374     const struct qstr qname = FSTR_TO_QSTR(iname);
0375     struct fscrypt_nokey_name nokey_name;
0376     u32 size; /* size of the unencoded no-key name */
0377 
0378     if (fscrypt_is_dot_dotdot(&qname)) {
0379         oname->name[0] = '.';
0380         oname->name[iname->len - 1] = '.';
0381         oname->len = iname->len;
0382         return 0;
0383     }
0384 
0385     if (iname->len < FSCRYPT_FNAME_MIN_MSG_LEN)
0386         return -EUCLEAN;
0387 
0388     if (fscrypt_has_encryption_key(inode))
0389         return fname_decrypt(inode, iname, oname);
0390 
0391     /*
0392      * Sanity check that struct fscrypt_nokey_name doesn't have padding
0393      * between fields and that its encoded size never exceeds NAME_MAX.
0394      */
0395     BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, dirhash) !=
0396              offsetof(struct fscrypt_nokey_name, bytes));
0397     BUILD_BUG_ON(offsetofend(struct fscrypt_nokey_name, bytes) !=
0398              offsetof(struct fscrypt_nokey_name, sha256));
0399     BUILD_BUG_ON(FSCRYPT_NOKEY_NAME_MAX_ENCODED > NAME_MAX);
0400 
0401     nokey_name.dirhash[0] = hash;
0402     nokey_name.dirhash[1] = minor_hash;
0403 
0404     if (iname->len <= sizeof(nokey_name.bytes)) {
0405         memcpy(nokey_name.bytes, iname->name, iname->len);
0406         size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]);
0407     } else {
0408         memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes));
0409         /* Compute strong hash of remaining part of name. */
0410         sha256(&iname->name[sizeof(nokey_name.bytes)],
0411                iname->len - sizeof(nokey_name.bytes),
0412                nokey_name.sha256);
0413         size = FSCRYPT_NOKEY_NAME_MAX;
0414     }
0415     oname->len = fscrypt_base64url_encode((const u8 *)&nokey_name, size,
0416                           oname->name);
0417     return 0;
0418 }
0419 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
0420 
0421 /**
0422  * fscrypt_setup_filename() - prepare to search a possibly encrypted directory
0423  * @dir: the directory that will be searched
0424  * @iname: the user-provided filename being searched for
0425  * @lookup: 1 if we're allowed to proceed without the key because it's
0426  *  ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
0427  *  proceed without the key because we're going to create the dir_entry.
0428  * @fname: the filename information to be filled in
0429  *
0430  * Given a user-provided filename @iname, this function sets @fname->disk_name
0431  * to the name that would be stored in the on-disk directory entry, if possible.
0432  * If the directory is unencrypted this is simply @iname.  Else, if we have the
0433  * directory's encryption key, then @iname is the plaintext, so we encrypt it to
0434  * get the disk_name.
0435  *
0436  * Else, for keyless @lookup operations, @iname should be a no-key name, so we
0437  * decode it to get the struct fscrypt_nokey_name.  Non-@lookup operations will
0438  * be impossible in this case, so we fail them with ENOKEY.
0439  *
0440  * If successful, fscrypt_free_filename() must be called later to clean up.
0441  *
0442  * Return: 0 on success, -errno on failure
0443  */
0444 int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
0445                   int lookup, struct fscrypt_name *fname)
0446 {
0447     struct fscrypt_nokey_name *nokey_name;
0448     int ret;
0449 
0450     memset(fname, 0, sizeof(struct fscrypt_name));
0451     fname->usr_fname = iname;
0452 
0453     if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
0454         fname->disk_name.name = (unsigned char *)iname->name;
0455         fname->disk_name.len = iname->len;
0456         return 0;
0457     }
0458     ret = fscrypt_get_encryption_info(dir, lookup);
0459     if (ret)
0460         return ret;
0461 
0462     if (fscrypt_has_encryption_key(dir)) {
0463         if (!fscrypt_fname_encrypted_size(dir, iname->len, NAME_MAX,
0464                           &fname->crypto_buf.len))
0465             return -ENAMETOOLONG;
0466         fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
0467                          GFP_NOFS);
0468         if (!fname->crypto_buf.name)
0469             return -ENOMEM;
0470 
0471         ret = fscrypt_fname_encrypt(dir, iname, fname->crypto_buf.name,
0472                         fname->crypto_buf.len);
0473         if (ret)
0474             goto errout;
0475         fname->disk_name.name = fname->crypto_buf.name;
0476         fname->disk_name.len = fname->crypto_buf.len;
0477         return 0;
0478     }
0479     if (!lookup)
0480         return -ENOKEY;
0481     fname->is_nokey_name = true;
0482 
0483     /*
0484      * We don't have the key and we are doing a lookup; decode the
0485      * user-supplied name
0486      */
0487 
0488     if (iname->len > FSCRYPT_NOKEY_NAME_MAX_ENCODED)
0489         return -ENOENT;
0490 
0491     fname->crypto_buf.name = kmalloc(FSCRYPT_NOKEY_NAME_MAX, GFP_KERNEL);
0492     if (fname->crypto_buf.name == NULL)
0493         return -ENOMEM;
0494 
0495     ret = fscrypt_base64url_decode(iname->name, iname->len,
0496                        fname->crypto_buf.name);
0497     if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) ||
0498         (ret > offsetof(struct fscrypt_nokey_name, sha256) &&
0499          ret != FSCRYPT_NOKEY_NAME_MAX)) {
0500         ret = -ENOENT;
0501         goto errout;
0502     }
0503     fname->crypto_buf.len = ret;
0504 
0505     nokey_name = (void *)fname->crypto_buf.name;
0506     fname->hash = nokey_name->dirhash[0];
0507     fname->minor_hash = nokey_name->dirhash[1];
0508     if (ret != FSCRYPT_NOKEY_NAME_MAX) {
0509         /* The full ciphertext filename is available. */
0510         fname->disk_name.name = nokey_name->bytes;
0511         fname->disk_name.len =
0512             ret - offsetof(struct fscrypt_nokey_name, bytes);
0513     }
0514     return 0;
0515 
0516 errout:
0517     kfree(fname->crypto_buf.name);
0518     return ret;
0519 }
0520 EXPORT_SYMBOL(fscrypt_setup_filename);
0521 
0522 /**
0523  * fscrypt_match_name() - test whether the given name matches a directory entry
0524  * @fname: the name being searched for
0525  * @de_name: the name from the directory entry
0526  * @de_name_len: the length of @de_name in bytes
0527  *
0528  * Normally @fname->disk_name will be set, and in that case we simply compare
0529  * that to the name stored in the directory entry.  The only exception is that
0530  * if we don't have the key for an encrypted directory and the name we're
0531  * looking for is very long, then we won't have the full disk_name and instead
0532  * we'll need to match against a fscrypt_nokey_name that includes a strong hash.
0533  *
0534  * Return: %true if the name matches, otherwise %false.
0535  */
0536 bool fscrypt_match_name(const struct fscrypt_name *fname,
0537             const u8 *de_name, u32 de_name_len)
0538 {
0539     const struct fscrypt_nokey_name *nokey_name =
0540         (const void *)fname->crypto_buf.name;
0541     u8 digest[SHA256_DIGEST_SIZE];
0542 
0543     if (likely(fname->disk_name.name)) {
0544         if (de_name_len != fname->disk_name.len)
0545             return false;
0546         return !memcmp(de_name, fname->disk_name.name, de_name_len);
0547     }
0548     if (de_name_len <= sizeof(nokey_name->bytes))
0549         return false;
0550     if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes)))
0551         return false;
0552     sha256(&de_name[sizeof(nokey_name->bytes)],
0553            de_name_len - sizeof(nokey_name->bytes), digest);
0554     return !memcmp(digest, nokey_name->sha256, sizeof(digest));
0555 }
0556 EXPORT_SYMBOL_GPL(fscrypt_match_name);
0557 
0558 /**
0559  * fscrypt_fname_siphash() - calculate the SipHash of a filename
0560  * @dir: the parent directory
0561  * @name: the filename to calculate the SipHash of
0562  *
0563  * Given a plaintext filename @name and a directory @dir which uses SipHash as
0564  * its dirhash method and has had its fscrypt key set up, this function
0565  * calculates the SipHash of that name using the directory's secret dirhash key.
0566  *
0567  * Return: the SipHash of @name using the hash key of @dir
0568  */
0569 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name)
0570 {
0571     const struct fscrypt_info *ci = dir->i_crypt_info;
0572 
0573     WARN_ON(!ci->ci_dirhash_key_initialized);
0574 
0575     return siphash(name->name, name->len, &ci->ci_dirhash_key);
0576 }
0577 EXPORT_SYMBOL_GPL(fscrypt_fname_siphash);
0578 
0579 /*
0580  * Validate dentries in encrypted directories to make sure we aren't potentially
0581  * caching stale dentries after a key has been added.
0582  */
0583 int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
0584 {
0585     struct dentry *dir;
0586     int err;
0587     int valid;
0588 
0589     /*
0590      * Plaintext names are always valid, since fscrypt doesn't support
0591      * reverting to no-key names without evicting the directory's inode
0592      * -- which implies eviction of the dentries in the directory.
0593      */
0594     if (!(dentry->d_flags & DCACHE_NOKEY_NAME))
0595         return 1;
0596 
0597     /*
0598      * No-key name; valid if the directory's key is still unavailable.
0599      *
0600      * Although fscrypt forbids rename() on no-key names, we still must use
0601      * dget_parent() here rather than use ->d_parent directly.  That's
0602      * because a corrupted fs image may contain directory hard links, which
0603      * the VFS handles by moving the directory's dentry tree in the dcache
0604      * each time ->lookup() finds the directory and it already has a dentry
0605      * elsewhere.  Thus ->d_parent can be changing, and we must safely grab
0606      * a reference to some ->d_parent to prevent it from being freed.
0607      */
0608 
0609     if (flags & LOOKUP_RCU)
0610         return -ECHILD;
0611 
0612     dir = dget_parent(dentry);
0613     /*
0614      * Pass allow_unsupported=true, so that files with an unsupported
0615      * encryption policy can be deleted.
0616      */
0617     err = fscrypt_get_encryption_info(d_inode(dir), true);
0618     valid = !fscrypt_has_encryption_key(d_inode(dir));
0619     dput(dir);
0620 
0621     if (err < 0)
0622         return err;
0623 
0624     return valid;
0625 }
0626 EXPORT_SYMBOL_GPL(fscrypt_d_revalidate);