Skip to content

Commit 7a52021

Browse files
ebiggersgregkh
authored andcommitted
fscrypt: remove broken support for detecting keyring key revocation
commit 1b53cf9815bb4744958d41f3795d5d5a1d365e2d upstream. Filesystem encryption ostensibly supported revoking a keyring key that had been used to "unlock" encrypted files, causing those files to become "locked" again. This was, however, buggy for several reasons, the most severe of which was that when key revocation happened to be detected for an inode, its fscrypt_info was immediately freed, even while other threads could be using it for encryption or decryption concurrently. This could be exploited to crash the kernel or worse. This patch fixes the use-after-free by removing the code which detects the keyring key having been revoked, invalidated, or expired. Instead, an encrypted inode that is "unlocked" now simply remains unlocked until it is evicted from memory. Note that this is no worse than the case for block device-level encryption, e.g. dm-crypt, and it still remains possible for a privileged user to evict unused pages, inodes, and dentries by running 'sync; echo 3 > /proc/sys/vm/drop_caches', or by simply unmounting the filesystem. In fact, one of those actions was already needed anyway for key revocation to work even somewhat sanely. This change is not expected to break any applications. In the future I'd like to implement a real API for fscrypt key revocation that interacts sanely with ongoing filesystem operations --- waiting for existing operations to complete and blocking new operations, and invalidating and sanitizing key material and plaintext from the VFS caches. But this is a hard problem, and for now this bug must be fixed. This bug affected almost all versions of ext4, f2fs, and ubifs encryption, and it was potentially reachable in any kernel configured with encryption support (CONFIG_EXT4_ENCRYPTION=y, CONFIG_EXT4_FS_ENCRYPTION=y, CONFIG_F2FS_FS_ENCRYPTION=y, or CONFIG_UBIFS_FS_ENCRYPTION=y). Note that older kernels did not use the shared fs/crypto/ code, but due to the potential security implications of this bug, it may still be worthwhile to backport this fix to them. Fixes: b7236e2 ("ext4 crypto: reorganize how we store keys in the inode") Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Acked-by: Michael Halcrow <mhalcrow@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 573341e commit 7a52021

6 files changed

Lines changed: 16 additions & 70 deletions

File tree

fs/ext4/crypto_key.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ void ext4_free_crypt_info(struct ext4_crypt_info *ci)
8888
if (!ci)
8989
return;
9090

91-
if (ci->ci_keyring_key)
92-
key_put(ci->ci_keyring_key);
9391
crypto_free_ablkcipher(ci->ci_ctfm);
9492
kmem_cache_free(ext4_crypt_info_cachep, ci);
9593
}
@@ -111,7 +109,7 @@ void ext4_free_encryption_info(struct inode *inode,
111109
ext4_free_crypt_info(ci);
112110
}
113111

114-
int _ext4_get_encryption_info(struct inode *inode)
112+
int ext4_get_encryption_info(struct inode *inode)
115113
{
116114
struct ext4_inode_info *ei = EXT4_I(inode);
117115
struct ext4_crypt_info *crypt_info;
@@ -128,22 +126,15 @@ int _ext4_get_encryption_info(struct inode *inode)
128126
char mode;
129127
int res;
130128

129+
if (ei->i_crypt_info)
130+
return 0;
131+
131132
if (!ext4_read_workqueue) {
132133
res = ext4_init_crypto();
133134
if (res)
134135
return res;
135136
}
136137

137-
retry:
138-
crypt_info = ACCESS_ONCE(ei->i_crypt_info);
139-
if (crypt_info) {
140-
if (!crypt_info->ci_keyring_key ||
141-
key_validate(crypt_info->ci_keyring_key) == 0)
142-
return 0;
143-
ext4_free_encryption_info(inode, crypt_info);
144-
goto retry;
145-
}
146-
147138
res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
148139
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
149140
&ctx, sizeof(ctx));
@@ -166,7 +157,6 @@ int _ext4_get_encryption_info(struct inode *inode)
166157
crypt_info->ci_data_mode = ctx.contents_encryption_mode;
167158
crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
168159
crypt_info->ci_ctfm = NULL;
169-
crypt_info->ci_keyring_key = NULL;
170160
memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
171161
sizeof(crypt_info->ci_master_key));
172162
if (S_ISREG(inode->i_mode))
@@ -206,7 +196,6 @@ int _ext4_get_encryption_info(struct inode *inode)
206196
keyring_key = NULL;
207197
goto out;
208198
}
209-
crypt_info->ci_keyring_key = keyring_key;
210199
if (keyring_key->type != &key_type_logon) {
211200
printk_once(KERN_WARNING
212201
"ext4: key type must be logon\n");
@@ -253,16 +242,13 @@ int _ext4_get_encryption_info(struct inode *inode)
253242
ext4_encryption_key_size(mode));
254243
if (res)
255244
goto out;
256-
memzero_explicit(raw_key, sizeof(raw_key));
257-
if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) != NULL) {
258-
ext4_free_crypt_info(crypt_info);
259-
goto retry;
260-
}
261-
return 0;
262245

246+
if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) == NULL)
247+
crypt_info = NULL;
263248
out:
264249
if (res == -ENOKEY)
265250
res = 0;
251+
key_put(keyring_key);
266252
ext4_free_crypt_info(crypt_info);
267253
memzero_explicit(raw_key, sizeof(raw_key));
268254
return res;

fs/ext4/ext4.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,23 +2330,11 @@ static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
23302330
/* crypto_key.c */
23312331
void ext4_free_crypt_info(struct ext4_crypt_info *ci);
23322332
void ext4_free_encryption_info(struct inode *inode, struct ext4_crypt_info *ci);
2333-
int _ext4_get_encryption_info(struct inode *inode);
23342333

23352334
#ifdef CONFIG_EXT4_FS_ENCRYPTION
23362335
int ext4_has_encryption_key(struct inode *inode);
23372336

2338-
static inline int ext4_get_encryption_info(struct inode *inode)
2339-
{
2340-
struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
2341-
2342-
if (!ci ||
2343-
(ci->ci_keyring_key &&
2344-
(ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
2345-
(1 << KEY_FLAG_REVOKED) |
2346-
(1 << KEY_FLAG_DEAD)))))
2347-
return _ext4_get_encryption_info(inode);
2348-
return 0;
2349-
}
2337+
int ext4_get_encryption_info(struct inode *inode);
23502338

23512339
static inline struct ext4_crypt_info *ext4_encryption_info(struct inode *inode)
23522340
{

fs/ext4/ext4_crypto.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ struct ext4_crypt_info {
7878
char ci_filename_mode;
7979
char ci_flags;
8080
struct crypto_ablkcipher *ci_ctfm;
81-
struct key *ci_keyring_key;
8281
char ci_master_key[EXT4_KEY_DESCRIPTOR_SIZE];
8382
};
8483

fs/f2fs/crypto_key.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ static void f2fs_free_crypt_info(struct f2fs_crypt_info *ci)
9292
if (!ci)
9393
return;
9494

95-
key_put(ci->ci_keyring_key);
9695
crypto_free_ablkcipher(ci->ci_ctfm);
9796
kmem_cache_free(f2fs_crypt_info_cachep, ci);
9897
}
@@ -113,7 +112,7 @@ void f2fs_free_encryption_info(struct inode *inode, struct f2fs_crypt_info *ci)
113112
f2fs_free_crypt_info(ci);
114113
}
115114

116-
int _f2fs_get_encryption_info(struct inode *inode)
115+
int f2fs_get_encryption_info(struct inode *inode)
117116
{
118117
struct f2fs_inode_info *fi = F2FS_I(inode);
119118
struct f2fs_crypt_info *crypt_info;
@@ -129,18 +128,12 @@ int _f2fs_get_encryption_info(struct inode *inode)
129128
char mode;
130129
int res;
131130

131+
if (fi->i_crypt_info)
132+
return 0;
133+
132134
res = f2fs_crypto_initialize();
133135
if (res)
134136
return res;
135-
retry:
136-
crypt_info = ACCESS_ONCE(fi->i_crypt_info);
137-
if (crypt_info) {
138-
if (!crypt_info->ci_keyring_key ||
139-
key_validate(crypt_info->ci_keyring_key) == 0)
140-
return 0;
141-
f2fs_free_encryption_info(inode, crypt_info);
142-
goto retry;
143-
}
144137

145138
res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
146139
F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
@@ -159,7 +152,6 @@ int _f2fs_get_encryption_info(struct inode *inode)
159152
crypt_info->ci_data_mode = ctx.contents_encryption_mode;
160153
crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
161154
crypt_info->ci_ctfm = NULL;
162-
crypt_info->ci_keyring_key = NULL;
163155
memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
164156
sizeof(crypt_info->ci_master_key));
165157
if (S_ISREG(inode->i_mode))
@@ -197,7 +189,6 @@ int _f2fs_get_encryption_info(struct inode *inode)
197189
keyring_key = NULL;
198190
goto out;
199191
}
200-
crypt_info->ci_keyring_key = keyring_key;
201192
BUG_ON(keyring_key->type != &key_type_logon);
202193
ukp = user_key_payload(keyring_key);
203194
if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
@@ -230,17 +221,12 @@ int _f2fs_get_encryption_info(struct inode *inode)
230221
if (res)
231222
goto out;
232223

233-
memzero_explicit(raw_key, sizeof(raw_key));
234-
if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) != NULL) {
235-
f2fs_free_crypt_info(crypt_info);
236-
goto retry;
237-
}
238-
return 0;
239-
224+
if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) == NULL)
225+
crypt_info = NULL;
240226
out:
241227
if (res == -ENOKEY && !S_ISREG(inode->i_mode))
242228
res = 0;
243-
229+
key_put(keyring_key);
244230
f2fs_free_crypt_info(crypt_info);
245231
memzero_explicit(raw_key, sizeof(raw_key));
246232
return res;

fs/f2fs/f2fs.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,7 +2149,6 @@ void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *, struct bio *);
21492149

21502150
/* crypto_key.c */
21512151
void f2fs_free_encryption_info(struct inode *, struct f2fs_crypt_info *);
2152-
int _f2fs_get_encryption_info(struct inode *inode);
21532152

21542153
/* crypto_fname.c */
21552154
bool f2fs_valid_filenames_enc_mode(uint32_t);
@@ -2170,18 +2169,7 @@ void f2fs_exit_crypto(void);
21702169

21712170
int f2fs_has_encryption_key(struct inode *);
21722171

2173-
static inline int f2fs_get_encryption_info(struct inode *inode)
2174-
{
2175-
struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
2176-
2177-
if (!ci ||
2178-
(ci->ci_keyring_key &&
2179-
(ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
2180-
(1 << KEY_FLAG_REVOKED) |
2181-
(1 << KEY_FLAG_DEAD)))))
2182-
return _f2fs_get_encryption_info(inode);
2183-
return 0;
2184-
}
2172+
int f2fs_get_encryption_info(struct inode *inode);
21852173

21862174
void f2fs_fname_crypto_free_buffer(struct f2fs_str *);
21872175
int f2fs_fname_setup_filename(struct inode *, const struct qstr *,

fs/f2fs/f2fs_crypto.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ struct f2fs_crypt_info {
7979
char ci_filename_mode;
8080
char ci_flags;
8181
struct crypto_ablkcipher *ci_ctfm;
82-
struct key *ci_keyring_key;
8382
char ci_master_key[F2FS_KEY_DESCRIPTOR_SIZE];
8483
};
8584

0 commit comments

Comments
 (0)