Skip to content

Commit 5dae460

Browse files
committed
fscrypt: v2 encryption policy support
Add a new fscrypt policy version, "v2". It has the following changes from the original policy version, which we call "v1" (*): - Master keys (the user-provided encryption keys) are only ever used as input to HKDF-SHA512. This is more flexible and less error-prone, and it avoids the quirks and limitations of the AES-128-ECB based KDF. Three classes of cryptographically isolated subkeys are defined: - Per-file keys, like used in v1 policies except for the new KDF. - Per-mode keys. These implement the semantics of the DIRECT_KEY flag, which for v1 policies made the master key be used directly. These are also planned to be used for inline encryption when support for it is added. - Key identifiers (see below). - Each master key is identified by a 16-byte master_key_identifier, which is derived from the key itself using HKDF-SHA512. This prevents users from associating the wrong key with an encrypted file or directory. This was easily possible with v1 policies, which identified the key by an arbitrary 8-byte master_key_descriptor. - The key must be provided in the filesystem-level keyring, not in a process-subscribed keyring. The following UAPI additions are made: - The existing ioctl FS_IOC_SET_ENCRYPTION_POLICY can now be passed a fscrypt_policy_v2 to set a v2 encryption policy. It's disambiguated from fscrypt_policy/fscrypt_policy_v1 by the version code prefix. - A new ioctl FS_IOC_GET_ENCRYPTION_POLICY_EX is added. It allows getting the v1 or v2 encryption policy of an encrypted file or directory. The existing FS_IOC_GET_ENCRYPTION_POLICY ioctl could not be used because it did not have a way for userspace to indicate which policy structure is expected. The new ioctl includes a size field, so it is extensible to future fscrypt policy versions. - The ioctls FS_IOC_ADD_ENCRYPTION_KEY, FS_IOC_REMOVE_ENCRYPTION_KEY, and FS_IOC_GET_ENCRYPTION_KEY_STATUS now support managing keys for v2 encryption policies. Such keys are kept logically separate from keys for v1 encryption policies, and are identified by 'identifier' rather than by 'descriptor'. The 'identifier' need not be provided when adding a key, since the kernel will calculate it anyway. This patch temporarily keeps adding/removing v2 policy keys behind the same permission check done for adding/removing v1 policy keys: capable(CAP_SYS_ADMIN). However, the next patch will carefully take advantage of the cryptographically secure master_key_identifier to allow non-root users to add/remove v2 policy keys, thus providing a full replacement for v1 policies. (*) Actually, in the API fscrypt_policy::version is 0 while on-disk fscrypt_context::format is 1. But I believe it makes the most sense to advance both to '2' to have them be in sync, and to consider the numbering to start at 1 except for the API quirk. Reviewed-by: Paul Crowley <paulcrowley@google.com> Reviewed-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent c1144c9 commit 5dae460

9 files changed

Lines changed: 741 additions & 194 deletions

File tree

‎fs/crypto/crypto.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
141141
memset(iv, 0, ci->ci_mode->ivsize);
142142
iv->lblk_num = cpu_to_le64(lblk_num);
143143

144-
if (ci->ci_flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
144+
if (fscrypt_is_direct_key_policy(&ci->ci_policy))
145145
memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE);
146146

147147
if (ci->ci_essiv_tfm != NULL)

‎fs/crypto/fname.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ static int base64_decode(const char *src, int len, u8 *dst)
181181
bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
182182
u32 max_len, u32 *encrypted_len_ret)
183183
{
184-
int padding = 4 << (inode->i_crypt_info->ci_flags &
184+
const struct fscrypt_info *ci = inode->i_crypt_info;
185+
int padding = 4 << (fscrypt_policy_flags(&ci->ci_policy) &
185186
FSCRYPT_POLICY_FLAGS_PAD_MASK);
186187
u32 encrypted_len;
187188

‎fs/crypto/fscrypt_private.h‎

Lines changed: 162 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,127 @@
2020

2121
#define FSCRYPT_MIN_KEY_SIZE 16
2222

23-
/**
24-
* Encryption context for inode
25-
*
26-
* Protector format:
27-
* 1 byte: Protector format (1 = this version)
28-
* 1 byte: File contents encryption mode
29-
* 1 byte: File names encryption mode
30-
* 1 byte: Flags
31-
* 8 bytes: Master Key descriptor
32-
* 16 bytes: Encryption Key derivation nonce
33-
*/
34-
struct fscrypt_context {
35-
u8 format;
23+
#define FSCRYPT_CONTEXT_V1 1
24+
#define FSCRYPT_CONTEXT_V2 2
25+
26+
struct fscrypt_context_v1 {
27+
u8 version; /* FSCRYPT_CONTEXT_V1 */
3628
u8 contents_encryption_mode;
3729
u8 filenames_encryption_mode;
3830
u8 flags;
3931
u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
4032
u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
41-
} __packed;
33+
};
4234

43-
#define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
35+
struct fscrypt_context_v2 {
36+
u8 version; /* FSCRYPT_CONTEXT_V2 */
37+
u8 contents_encryption_mode;
38+
u8 filenames_encryption_mode;
39+
u8 flags;
40+
u8 __reserved[4];
41+
u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
42+
u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
43+
};
44+
45+
/**
46+
* fscrypt_context - the encryption context of an inode
47+
*
48+
* This is the on-disk equivalent of an fscrypt_policy, stored alongside each
49+
* encrypted file usually in a hidden extended attribute. It contains the
50+
* fields from the fscrypt_policy, in order to identify the encryption algorithm
51+
* and key with which the file is encrypted. It also contains a nonce that was
52+
* randomly generated by fscrypt itself; this is used as KDF input or as a tweak
53+
* to cause different files to be encrypted differently.
54+
*/
55+
union fscrypt_context {
56+
u8 version;
57+
struct fscrypt_context_v1 v1;
58+
struct fscrypt_context_v2 v2;
59+
};
60+
61+
/*
62+
* Return the size expected for the given fscrypt_context based on its version
63+
* number, or 0 if the context version is unrecognized.
64+
*/
65+
static inline int fscrypt_context_size(const union fscrypt_context *ctx)
66+
{
67+
switch (ctx->version) {
68+
case FSCRYPT_CONTEXT_V1:
69+
BUILD_BUG_ON(sizeof(ctx->v1) != 28);
70+
return sizeof(ctx->v1);
71+
case FSCRYPT_CONTEXT_V2:
72+
BUILD_BUG_ON(sizeof(ctx->v2) != 40);
73+
return sizeof(ctx->v2);
74+
}
75+
return 0;
76+
}
77+
78+
#undef fscrypt_policy
79+
union fscrypt_policy {
80+
u8 version;
81+
struct fscrypt_policy_v1 v1;
82+
struct fscrypt_policy_v2 v2;
83+
};
84+
85+
/*
86+
* Return the size expected for the given fscrypt_policy based on its version
87+
* number, or 0 if the policy version is unrecognized.
88+
*/
89+
static inline int fscrypt_policy_size(const union fscrypt_policy *policy)
90+
{
91+
switch (policy->version) {
92+
case FSCRYPT_POLICY_V1:
93+
return sizeof(policy->v1);
94+
case FSCRYPT_POLICY_V2:
95+
return sizeof(policy->v2);
96+
}
97+
return 0;
98+
}
99+
100+
/* Return the contents encryption mode of a valid encryption policy */
101+
static inline u8
102+
fscrypt_policy_contents_mode(const union fscrypt_policy *policy)
103+
{
104+
switch (policy->version) {
105+
case FSCRYPT_POLICY_V1:
106+
return policy->v1.contents_encryption_mode;
107+
case FSCRYPT_POLICY_V2:
108+
return policy->v2.contents_encryption_mode;
109+
}
110+
BUG();
111+
}
112+
113+
/* Return the filenames encryption mode of a valid encryption policy */
114+
static inline u8
115+
fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)
116+
{
117+
switch (policy->version) {
118+
case FSCRYPT_POLICY_V1:
119+
return policy->v1.filenames_encryption_mode;
120+
case FSCRYPT_POLICY_V2:
121+
return policy->v2.filenames_encryption_mode;
122+
}
123+
BUG();
124+
}
125+
126+
/* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */
127+
static inline u8
128+
fscrypt_policy_flags(const union fscrypt_policy *policy)
129+
{
130+
switch (policy->version) {
131+
case FSCRYPT_POLICY_V1:
132+
return policy->v1.flags;
133+
case FSCRYPT_POLICY_V2:
134+
return policy->v2.flags;
135+
}
136+
BUG();
137+
}
138+
139+
static inline bool
140+
fscrypt_is_direct_key_policy(const union fscrypt_policy *policy)
141+
{
142+
return fscrypt_policy_flags(policy) & FSCRYPT_POLICY_FLAG_DIRECT_KEY;
143+
}
44144

45145
/**
46146
* For encrypted symlinks, the ciphertext length is stored at the beginning
@@ -70,8 +170,8 @@ struct fscrypt_info {
70170
struct crypto_cipher *ci_essiv_tfm;
71171

72172
/*
73-
* Encryption mode used for this inode. It corresponds to either
74-
* ci_data_mode or ci_filename_mode, depending on the inode type.
173+
* Encryption mode used for this inode. It corresponds to either the
174+
* contents or filenames encryption mode, depending on the inode type.
75175
*/
76176
struct fscrypt_mode *ci_mode;
77177

@@ -97,11 +197,10 @@ struct fscrypt_info {
97197
*/
98198
struct fscrypt_direct_key *ci_direct_key;
99199

100-
/* fields from the fscrypt_context */
101-
u8 ci_data_mode;
102-
u8 ci_filename_mode;
103-
u8 ci_flags;
104-
u8 ci_master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
200+
/* The encryption policy used by this inode */
201+
union fscrypt_policy ci_policy;
202+
203+
/* This inode's nonce, copied from the fscrypt_context */
105204
u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
106205
};
107206

@@ -181,6 +280,17 @@ struct fscrypt_hkdf {
181280
extern int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
182281
unsigned int master_key_size);
183282

283+
/*
284+
* The list of contexts in which fscrypt uses HKDF. These values are used as
285+
* the first byte of the HKDF application-specific info string to guarantee that
286+
* info strings are never repeated between contexts. This ensures that all HKDF
287+
* outputs are unique and cryptographically isolated, i.e. knowledge of one
288+
* output doesn't reveal another.
289+
*/
290+
#define HKDF_CONTEXT_KEY_IDENTIFIER 1
291+
#define HKDF_CONTEXT_PER_FILE_KEY 2
292+
#define HKDF_CONTEXT_PER_MODE_KEY 3
293+
184294
extern int fscrypt_hkdf_expand(struct fscrypt_hkdf *hkdf, u8 context,
185295
const u8 *info, unsigned int infolen,
186296
u8 *okm, unsigned int okmlen);
@@ -194,10 +304,16 @@ extern void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
194304
*/
195305
struct fscrypt_master_key_secret {
196306

197-
/* Size of the raw key in bytes */
307+
/*
308+
* For v2 policy keys: HKDF context keyed by this master key.
309+
* For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
310+
*/
311+
struct fscrypt_hkdf hkdf;
312+
313+
/* Size of the raw key in bytes. Set even if ->raw isn't set. */
198314
u32 size;
199315

200-
/* The raw key */
316+
/* For v1 policy keys: the raw key. Wiped for v2 policy keys. */
201317
u8 raw[FSCRYPT_MAX_KEY_SIZE];
202318

203319
} __randomize_layout;
@@ -223,7 +339,12 @@ struct fscrypt_master_key {
223339
*/
224340
struct fscrypt_master_key_secret mk_secret;
225341

226-
/* Arbitrary key descriptor which was assigned by userspace */
342+
/*
343+
* For v1 policy keys: an arbitrary key descriptor which was assigned by
344+
* userspace (->descriptor).
345+
*
346+
* For v2 policy keys: a cryptographic hash of this key (->identifier).
347+
*/
227348
struct fscrypt_key_specifier mk_spec;
228349

229350
/*
@@ -242,6 +363,9 @@ struct fscrypt_master_key {
242363
struct list_head mk_decrypted_inodes;
243364
spinlock_t mk_decrypted_inodes_lock;
244365

366+
/* Per-mode tfms for DIRECT_KEY policies, allocated on-demand */
367+
struct crypto_skcipher *mk_mode_keys[__FSCRYPT_MODE_MAX + 1];
368+
245369
} __randomize_layout;
246370

247371
static inline bool
@@ -263,6 +387,8 @@ static inline const char *master_key_spec_type(
263387
switch (spec->type) {
264388
case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
265389
return "descriptor";
390+
case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
391+
return "identifier";
266392
}
267393
return "[unknown]";
268394
}
@@ -272,6 +398,8 @@ static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
272398
switch (spec->type) {
273399
case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
274400
return FSCRYPT_KEY_DESCRIPTOR_SIZE;
401+
case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
402+
return FSCRYPT_KEY_IDENTIFIER_SIZE;
275403
}
276404
return 0;
277405
}
@@ -315,5 +443,14 @@ extern int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
315443

316444
extern int fscrypt_setup_v1_file_key_via_subscribed_keyrings(
317445
struct fscrypt_info *ci);
446+
/* policy.c */
447+
448+
extern bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
449+
const union fscrypt_policy *policy2);
450+
extern bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
451+
const struct inode *inode);
452+
extern int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
453+
const union fscrypt_context *ctx_u,
454+
int ctx_size);
318455

319456
#endif /* _FSCRYPT_PRIVATE_H */

‎fs/crypto/keyring.c‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
* information about these ioctls.
1818
*/
1919

20+
#include <crypto/skcipher.h>
2021
#include <linux/key-type.h>
2122
#include <linux/seq_file.h>
2223

2324
#include "fscrypt_private.h"
2425

2526
static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
2627
{
28+
fscrypt_destroy_hkdf(&secret->hkdf);
2729
memzero_explicit(secret, sizeof(*secret));
2830
}
2931

@@ -36,7 +38,13 @@ static void move_master_key_secret(struct fscrypt_master_key_secret *dst,
3638

3739
static void free_master_key(struct fscrypt_master_key *mk)
3840
{
41+
size_t i;
42+
3943
wipe_master_key_secret(&mk->mk_secret);
44+
45+
for (i = 0; i < ARRAY_SIZE(mk->mk_mode_keys); i++)
46+
crypto_free_skcipher(mk->mk_mode_keys[i]);
47+
4048
kzfree(mk);
4149
}
4250

@@ -109,7 +117,7 @@ static struct key *search_fscrypt_keyring(struct key *keyring,
109117
#define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \
110118
(CONST_STRLEN("fscrypt-") + FIELD_SIZEOF(struct super_block, s_id))
111119

112-
#define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_DESCRIPTOR_SIZE + 1)
120+
#define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1)
113121

114122
static void format_fs_keyring_description(
115123
char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE],
@@ -314,6 +322,31 @@ int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
314322
if (!capable(CAP_SYS_ADMIN))
315323
goto out_wipe_secret;
316324

325+
if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
326+
err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size);
327+
if (err)
328+
goto out_wipe_secret;
329+
330+
/*
331+
* Now that the HKDF context is initialized, the raw key is no
332+
* longer needed.
333+
*/
334+
memzero_explicit(secret.raw, secret.size);
335+
336+
/* Calculate the key identifier and return it to userspace. */
337+
err = fscrypt_hkdf_expand(&secret.hkdf,
338+
HKDF_CONTEXT_KEY_IDENTIFIER,
339+
NULL, 0, arg.key_spec.u.identifier,
340+
FSCRYPT_KEY_IDENTIFIER_SIZE);
341+
if (err)
342+
goto out_wipe_secret;
343+
err = -EFAULT;
344+
if (copy_to_user(uarg->key_spec.u.identifier,
345+
arg.key_spec.u.identifier,
346+
FSCRYPT_KEY_IDENTIFIER_SIZE))
347+
goto out_wipe_secret;
348+
}
349+
317350
err = add_master_key(sb, &secret, &arg.key_spec);
318351
out_wipe_secret:
319352
wipe_master_key_secret(&secret);

0 commit comments

Comments
 (0)