Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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