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)  * Encryption policy functions for per-file encryption support.
^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)  * Originally written by Michael Halcrow, 2015.
^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)  * Modified by Eric Biggers, 2019 for v2 policy support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "fscrypt_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * fscrypt_policies_equal() - check whether two encryption policies are the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * @policy1: the first policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * @policy2: the second policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * Return: %true if equal, else %false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 			    const union fscrypt_policy *policy2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	if (policy1->version != policy2->version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static const union fscrypt_policy *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) fscrypt_get_dummy_policy(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	if (!sb->s_cop->get_dummy_policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return sb->s_cop->get_dummy_policy(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	    filenames_mode == FSCRYPT_MODE_AES_256_CTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	    filenames_mode == FSCRYPT_MODE_AES_128_CTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	    filenames_mode == FSCRYPT_MODE_ADIANTUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static bool supported_direct_key_modes(const struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 				       u32 contents_mode, u32 filenames_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	const struct fscrypt_mode *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (contents_mode != filenames_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		fscrypt_warn(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			     "Direct key flag not allowed with different contents and filenames modes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	mode = &fscrypt_modes[contents_mode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		fscrypt_warn(inode, "Direct key flag not allowed with %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			     mode->friendly_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 					 const struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 					 const char *type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 					 int max_ino_bits, int max_lblk_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int ino_bits = 64, lblk_bits = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	 * IV_INO_LBLK_* exist only because of hardware limitations, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 * currently the only known use case for them involves AES-256-XTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 * That's also all we test currently.  For these reasons, for now only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 * allow AES-256-XTS here.  This can be relaxed later if a use case for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * IV_INO_LBLK_* with other encryption modes arises.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		fscrypt_warn(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			     "Can't use %s policy with contents mode other than AES-256-XTS",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			     type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	 * It's unsafe to include inode numbers in the IVs if the filesystem can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 * potentially renumber inodes, e.g. via filesystem shrinking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (!sb->s_cop->has_stable_inodes ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	    !sb->s_cop->has_stable_inodes(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		fscrypt_warn(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			     "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			     type, sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (sb->s_cop->get_ino_and_lblk_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (ino_bits > max_ino_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		fscrypt_warn(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			     "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			     type, sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (lblk_bits > max_lblk_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		fscrypt_warn(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			     "Can't use %s policy on filesystem '%s' because its block numbers are too long",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			     type, sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return true;
^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) static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 					const struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				     policy->filenames_encryption_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		fscrypt_warn(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			     "Unsupported encryption modes (contents %d, filenames %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			     policy->contents_encryption_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			     policy->filenames_encryption_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			      FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			     policy->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	    !supported_direct_key_modes(inode, policy->contents_encryption_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 					policy->filenames_encryption_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (IS_CASEFOLDED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		/* With v1, there's no way to derive dirhash keys. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		fscrypt_warn(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			     "v1 policies can't be used on casefolded directories");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 					const struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				     policy->filenames_encryption_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		fscrypt_warn(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			     "Unsupported encryption modes (contents %d, filenames %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			     policy->contents_encryption_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			     policy->filenames_encryption_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return false;
^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) 	if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			      FSCRYPT_POLICY_FLAG_DIRECT_KEY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			      FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			      FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			     policy->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (count > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			     policy->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	    !supported_direct_key_modes(inode, policy->contents_encryption_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 					policy->filenames_encryption_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	    !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 					  32, 32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 * IV_INO_LBLK_32 hashes the inode number, so in principle it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * support any ino_bits.  However, currently the inode number is gotten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * from inode::i_ino which is 'unsigned long'.  So for now the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * implementation limit is 32 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	    !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 					  32, 32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		fscrypt_warn(inode, "Reserved bits set in encryption policy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * fscrypt_supported_policy() - check whether an encryption policy is supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * @policy_u: the encryption policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * @inode: the inode on which the policy will be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * Given an encryption policy, check whether all its encryption modes and other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * settings are supported by this kernel on the given inode.  (But we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  * currently don't check for crypto API support here, so attempting to use an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * algorithm not configured into the crypto API will still fail later.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * Return: %true if supported, else %false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			      const struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	switch (policy_u->version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	case FSCRYPT_POLICY_V1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return fscrypt_supported_v1_policy(&policy_u->v1, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	case FSCRYPT_POLICY_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		return fscrypt_supported_v2_policy(&policy_u->v2, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  * fscrypt_new_context() - create a new fscrypt_context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * @ctx_u: output context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * @policy_u: input policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * @nonce: nonce to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * Create an fscrypt_context for an inode that is being assigned the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * encryption policy.  @nonce must be a new random nonce.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * Return: the size of the new context in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int fscrypt_new_context(union fscrypt_context *ctx_u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			       const union fscrypt_policy *policy_u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			       const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	memset(ctx_u, 0, sizeof(*ctx_u));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	switch (policy_u->version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	case FSCRYPT_POLICY_V1: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		const struct fscrypt_policy_v1 *policy = &policy_u->v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		struct fscrypt_context_v1 *ctx = &ctx_u->v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		ctx->version = FSCRYPT_CONTEXT_V1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		ctx->contents_encryption_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			policy->contents_encryption_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		ctx->filenames_encryption_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			policy->filenames_encryption_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		ctx->flags = policy->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		memcpy(ctx->master_key_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		       policy->master_key_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		       sizeof(ctx->master_key_descriptor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		return sizeof(*ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	case FSCRYPT_POLICY_V2: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		const struct fscrypt_policy_v2 *policy = &policy_u->v2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		struct fscrypt_context_v2 *ctx = &ctx_u->v2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		ctx->version = FSCRYPT_CONTEXT_V2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		ctx->contents_encryption_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			policy->contents_encryption_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		ctx->filenames_encryption_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			policy->filenames_encryption_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		ctx->flags = policy->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		memcpy(ctx->master_key_identifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		       policy->master_key_identifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		       sizeof(ctx->master_key_identifier));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		return sizeof(*ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  * fscrypt_policy_from_context() - convert an fscrypt_context to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  *				   an fscrypt_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * @policy_u: output policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * @ctx_u: input context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * @ctx_size: size of input context in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  * Given an fscrypt_context, build the corresponding fscrypt_policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  * version number or size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * This does *not* validate the settings within the policy itself, e.g. the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * modes, flags, and reserved bits.  Use fscrypt_supported_policy() for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				const union fscrypt_context *ctx_u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				int ctx_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	memset(policy_u, 0, sizeof(*policy_u));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (!fscrypt_context_is_valid(ctx_u, ctx_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	switch (ctx_u->version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	case FSCRYPT_CONTEXT_V1: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		struct fscrypt_policy_v1 *policy = &policy_u->v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		policy->version = FSCRYPT_POLICY_V1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		policy->contents_encryption_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			ctx->contents_encryption_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		policy->filenames_encryption_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			ctx->filenames_encryption_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		policy->flags = ctx->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		memcpy(policy->master_key_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		       ctx->master_key_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		       sizeof(policy->master_key_descriptor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	case FSCRYPT_CONTEXT_V2: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		struct fscrypt_policy_v2 *policy = &policy_u->v2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		policy->version = FSCRYPT_POLICY_V2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		policy->contents_encryption_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			ctx->contents_encryption_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		policy->filenames_encryption_mode =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			ctx->filenames_encryption_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		policy->flags = ctx->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		memcpy(policy->__reserved, ctx->__reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		       sizeof(policy->__reserved));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		memcpy(policy->master_key_identifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		       ctx->master_key_identifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		       sizeof(policy->master_key_identifier));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	/* unreachable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* Retrieve an inode's encryption policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	const struct fscrypt_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	union fscrypt_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	ci = fscrypt_get_info(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (ci) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		/* key available, use the cached policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		*policy = ci->ci_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (!IS_ENCRYPTED(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		return (ret == -ERANGE) ? -EINVAL : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	return fscrypt_policy_from_context(policy, &ctx, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int set_encryption_policy(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 				 const union fscrypt_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	union fscrypt_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	int ctxsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (!fscrypt_supported_policy(policy, inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	switch (policy->version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	case FSCRYPT_POLICY_V1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		 * The original encryption policy version provided no way of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		 * verifying that the correct master key was supplied, which was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		 * insecure in scenarios where multiple users have access to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		 * same encrypted files (even just read-only access).  The new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		 * encryption policy version fixes this and also implies use of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		 * an improved key derivation function and allows non-root users
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		 * to securely remove keys.  So as long as compatibility with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		 * old kernels isn't required, it is recommended to use the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		 * policy version for all new encrypted directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			     current->comm, current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	case FSCRYPT_POLICY_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		err = fscrypt_verify_key_added(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 					       policy->v2.master_key_identifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			pr_warn_once("%s (pid %d) is setting an IV_INO_LBLK_32 encryption policy.  This should only be used if there are certain hardware limitations.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 				     current->comm, current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	ctxsize = fscrypt_new_context(&ctx, policy, nonce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	union fscrypt_policy policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	union fscrypt_policy existing_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	u8 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (get_user(policy.version, (const u8 __user *)arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	size = fscrypt_policy_size(&policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (size <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	 * We should just copy the remaining 'size - 1' bytes here, but a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	 * think that size can be 0 here (despite the check above!) *and* that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	 * it's a compile-time constant.  Thus it would think copy_from_user()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	 * is passed compile-time constant ULONG_MAX, causing the compile-time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	 * buffer overflow check to fail, breaking the build. This only occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	 * when building an i386 kernel with -Os and branch profiling enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	 * Work around it by just copying the first byte again...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	version = policy.version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (copy_from_user(&policy, arg, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	policy.version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	ret = mnt_want_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	ret = fscrypt_get_policy(inode, &existing_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (ret == -ENODATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		if (!S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			ret = -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		else if (IS_DEADDIR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		else if (!inode->i_sb->s_cop->empty_dir(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			ret = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			ret = set_encryption_policy(inode, &policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	} else if (ret == -EINVAL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		   (ret == 0 && !fscrypt_policies_equal(&policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 							&existing_policy))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		/* The file already uses a different encryption policy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	mnt_drop_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) /* Original ioctl version; can only get the original policy version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	union fscrypt_policy policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	err = fscrypt_get_policy(file_inode(filp), &policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (policy.version != FSCRYPT_POLICY_V1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (copy_to_user(arg, &policy, sizeof(policy.v1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /* Extended ioctl version; can get policies of any version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	struct fscrypt_get_policy_ex_arg arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	size_t policy_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	/* arg is policy_size, then policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		     offsetof(typeof(arg), policy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	err = fscrypt_get_policy(file_inode(filp), policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	policy_size = fscrypt_policy_size(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	if (policy_size > arg.policy_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	arg.policy_size = policy_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	union fscrypt_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	if (!fscrypt_context_is_valid(&ctx, ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			 FSCRYPT_FILE_NONCE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)  * fscrypt_has_permitted_context() - is a file's encryption policy permitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)  *				     within its directory?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)  * @parent: inode for parent directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)  * @child: inode for file being looked up, opened, or linked into @parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)  * Filesystems must call this before permitting access to an inode in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)  * situation where the parent directory is encrypted (either before allowing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)  * ->lookup() to succeed, or for a regular file before allowing it to be opened)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)  * and before any operation that involves linking an inode into an encrypted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)  * directory, including link, rename, and cross rename.  It enforces the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)  * constraint that within a given encrypted directory tree, all files use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)  * same encryption policy.  The pre-access check is needed to detect potentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)  * malicious offline violations of this constraint, while the link and rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  * checks are needed to prevent online violations of this constraint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)  * Return: 1 if permitted, 0 if forbidden.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	union fscrypt_policy parent_policy, child_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	int err, err1, err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	/* No restrictions on file types which are never encrypted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	    !S_ISLNK(child->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	/* No restrictions if the parent directory is unencrypted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	if (!IS_ENCRYPTED(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	/* Encrypted directories must not contain unencrypted files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	if (!IS_ENCRYPTED(child))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	 * Both parent and child are encrypted, so verify they use the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	 * encryption policy.  Compare the fscrypt_info structs if the keys are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	 * available, otherwise retrieve and compare the fscrypt_contexts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	 * Note that the fscrypt_context retrieval will be required frequently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	 * when accessing an encrypted directory tree without the key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	 * Performance-wise this is not a big deal because we already don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	 * really optimize for file access without the key (to the extent that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	 * such access is even possible), given that any attempted access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	 * already causes a fscrypt_context retrieval and keyring search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	 * In any case, if an unexpected error occurs, fall back to "forbidden".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	err = fscrypt_get_encryption_info(parent, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	err = fscrypt_get_encryption_info(child, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	err1 = fscrypt_get_policy(parent, &parent_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	err2 = fscrypt_get_policy(child, &child_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	 * Allow the case where the parent and child both have an unrecognized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	 * encryption policy, so that files with an unrecognized encryption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	 * policy can be deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	if (err1 == -EINVAL && err2 == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (err1 || err2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	return fscrypt_policies_equal(&parent_policy, &child_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) EXPORT_SYMBOL(fscrypt_has_permitted_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)  * Return the encryption policy that new files in the directory will inherit, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)  * NULL if none, or an ERR_PTR() on error.  If the directory is encrypted, also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)  * ensure that its key is set up, so that the new filename can be encrypted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	if (IS_ENCRYPTED(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		err = fscrypt_require_key(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		return &dir->i_crypt_info->ci_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	return fscrypt_get_dummy_policy(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)  * fscrypt_set_context() - Set the fscrypt context of a new inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)  * @inode: a new inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)  * @fs_data: private data given by FS and passed to ->set_context()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)  * This should be called after fscrypt_prepare_new_inode(), generally during a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)  * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)  * Return: 0 on success, -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) int fscrypt_set_context(struct inode *inode, void *fs_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	struct fscrypt_info *ci = inode->i_crypt_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	union fscrypt_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	int ctxsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	/* fscrypt_prepare_new_inode() should have set up the key already. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	if (WARN_ON_ONCE(!ci))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	 * This may be the first time the inode number is available, so do any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	 * delayed key setup that requires the inode number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		const struct fscrypt_master_key *mk =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 			ci->ci_master_key->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		fscrypt_hash_inode_number(ci, mk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) EXPORT_SYMBOL_GPL(fscrypt_set_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)  * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)  * @sb: the filesystem on which test_dummy_encryption is being specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)  * @arg: the argument to the test_dummy_encryption option.  May be NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)  * @dummy_policy: the filesystem's current dummy policy (input/output, see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)  *		  below)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)  * Handle the test_dummy_encryption mount option by creating a dummy encryption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)  * policy, saving it in @dummy_policy, and adding the corresponding dummy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)  * encryption key to the filesystem.  If the @dummy_policy is already set, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)  * instead validate that it matches @arg.  Don't support changing it via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)  * remount, as that is difficult to do safely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)  * Return: 0 on success (dummy policy set, or the same policy is already set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)  *         -EEXIST if a different dummy policy is already set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)  *         or another -errno value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 				      struct fscrypt_dummy_policy *dummy_policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	struct fscrypt_key_specifier key_spec = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	union fscrypt_policy *policy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	if (!arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		arg = "v2";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	if (!strcmp(arg, "v1")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		version = FSCRYPT_POLICY_V1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		memset(key_spec.u.descriptor, 0x42,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	} else if (!strcmp(arg, "v2")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		version = FSCRYPT_POLICY_V2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		/* key_spec.u.identifier gets filled in when adding the key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	if (!policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	err = fscrypt_add_test_dummy_key(sb, &key_spec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	policy->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	switch (policy->version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	case FSCRYPT_POLICY_V1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	case FSCRYPT_POLICY_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 		policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		memcpy(policy->v2.master_key_identifier, key_spec.u.identifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 		       FSCRYPT_KEY_IDENTIFIER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	if (dummy_policy->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 		if (fscrypt_policies_equal(policy, dummy_policy->policy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 			err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	dummy_policy->policy = policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	policy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	kfree(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)  * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)  * @seq: the seq_file to print the option to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)  * @sep: the separator character to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)  * @sb: the filesystem whose options are being shown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)  * Show the test_dummy_encryption mount option, if it was specified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)  * This is mainly used for /proc/mounts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 					struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	int vers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	vers = policy->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 		vers = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);