Skip to content

Commit ed1cd83

Browse files
Eric Biggersgregkh
authored andcommitted
ext4: don't enable DAX on new encrypted files
commit da32af4 upstream. Currently, when a new encrypted regular file is created, the call to ext4_set_inode_flags(inode, init=true) in __ext4_new_inode() is made before EXT4_INODE_ENCRYPT is set. As a result, it can set S_DAX if the filesystem is mounted with "-o dax=always". EXT4_INODE_ENCRYPT then actually gets set a bit later in __ext4_new_inode(), when it calls fscrypt_set_context() which calls ext4_set_context(). ext4_set_context() sets EXT4_INODE_ENCRYPT and calls ext4_set_inode_flags(inode, init=false) to set S_ENCRYPTED too. This was intended to clear S_DAX as well. However, this was broken by commit 043546e ("fs/ext4: Only change S_DAX on inode load"). This causes data written to the file to bypass encryption, also causing xfstests failures such as generic/548 (when "-o dax=always" is used). Fix this by simplifying the flow by making __ext4_new_inode() set EXT4_INODE_ENCRYPT earlier. This makes it take effect in ext4_set_inode_flags(inode, init=true), making S_DAX never be set. Similarly, make EXT4_STATE_MAY_INLINE_DATA never be set in the first place on new encrypted inodes. Then it doesn't need to be cleared. As a result of these simplifications, ext4_set_context() no longer needs to change inode flags or state when 'handle != NULL'. Remove that too. Reported-by: Disha Goel <disgoel@linux.ibm.com> Reported-by: Ojaswin Mujoo <ojaswin@linux.ibm.com> Closes: https://lore.kernel.org/r/20260723085648.1500357-1-ojaswin@linux.ibm.com Fixes: 043546e ("fs/ext4: Only change S_DAX on inode load") Cc: stable@vger.kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org> Tested-by: Disha Goel <disgoel@linux.ibm.com> Reviewed-by: Ojaswin Mujoo <ojaswin@linux.ibm.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://patch.msgid.link/20260730175212.36923-1-ebiggers@kernel.org Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent d8636c8 commit ed1cd83

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

fs/ext4/crypto.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
144144
if (inode->i_ino == EXT4_ROOT_INO)
145145
return -EPERM;
146146

147-
if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
147+
/*
148+
* For new encrypted inodes, S_DAX is never set in the first place.
149+
*
150+
* For existing inodes, this is called only on empty directories. ext4
151+
* never sets S_DAX on directories.
152+
*/
153+
if (WARN_ON_ONCE(IS_DAX(inode)))
148154
return -EINVAL;
149155

150156
if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
@@ -163,28 +169,25 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
163169
*/
164170

165171
if (handle) {
172+
/*
173+
* __ext4_new_inode() should have already set the encrypt flag
174+
* on the inode and avoided enabling inline data.
175+
*/
176+
if (WARN_ON_ONCE(!IS_ENCRYPTED(inode)))
177+
return -EINVAL;
178+
if (WARN_ON_ONCE(ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)))
179+
return -EINVAL;
166180
/*
167181
* Since the inode is new it is ok to pass the
168182
* XATTR_CREATE flag. This is necessary to match the
169183
* remaining journal credits check in the set_handle
170184
* function with the credits allocated for the new
171185
* inode.
172186
*/
173-
res = ext4_xattr_set_handle(handle, inode,
174-
EXT4_XATTR_INDEX_ENCRYPTION,
175-
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
176-
ctx, len, XATTR_CREATE);
177-
if (!res) {
178-
ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
179-
ext4_clear_inode_state(inode,
180-
EXT4_STATE_MAY_INLINE_DATA);
181-
/*
182-
* Update inode->i_flags - S_ENCRYPTED will be enabled,
183-
* S_DAX may be disabled
184-
*/
185-
ext4_set_inode_flags(inode, false);
186-
}
187-
return res;
187+
return ext4_xattr_set_handle(handle, inode,
188+
EXT4_XATTR_INDEX_ENCRYPTION,
189+
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
190+
ctx, len, XATTR_CREATE);
188191
}
189192

190193
res = dquot_initialize(inode);
@@ -205,10 +208,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
205208
ctx, len, 0);
206209
if (!res) {
207210
ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
208-
/*
209-
* Update inode->i_flags - S_ENCRYPTED will be enabled,
210-
* S_DAX may be disabled
211-
*/
211+
/* Update inode->i_flags to set S_ENCRYPTED. */
212212
ext4_set_inode_flags(inode, false);
213213
res = ext4_mark_inode_dirty(handle, inode);
214214
if (res)

fs/ext4/ialloc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,8 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
993993
err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
994994
if (err)
995995
goto out;
996+
if (encrypt)
997+
i_flags |= EXT4_ENCRYPT_FL;
996998
}
997999

9981000
err = dquot_initialize(inode);
@@ -1303,6 +1305,8 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
13031305
ei->i_extra_isize = sbi->s_want_extra_isize;
13041306
ei->i_inline_off = 0;
13051307
if (ext4_has_feature_inline_data(sb) &&
1308+
/* Encrypted inodes cannot have inline data */
1309+
!(ei->i_flags & EXT4_ENCRYPT_FL) &&
13061310
(!(ei->i_flags & EXT4_DAX_FL) || S_ISDIR(mode)))
13071311
ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
13081312
ret = inode;

0 commit comments

Comments
 (0)