Skip to content

Commit fb44543

Browse files
ebiggerstytso
authored andcommitted
fscrypto: make XTS tweak initialization endian-independent
The XTS tweak (or IV) was initialized differently on little endian and big endian systems. Because the ciphertext depends on the XTS tweak, it was not possible to use an encrypted filesystem created by a little endian system on a big endian system and vice versa, even if they shared the same PAGE_SIZE. Fix this by always using little endian. This will break hypothetical big endian users of ext4 or f2fs encryption. However, all users we are aware of are little endian, and it's believed that "real" big endian users are unlikely to exist yet. So this might as well be fixed now before it's too late. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@vger.kernel.org
1 parent c4704a4 commit fb44543

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

fs/crypto/crypto.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ static int do_page_crypto(struct inode *inode,
151151
struct page *src_page, struct page *dest_page,
152152
gfp_t gfp_flags)
153153
{
154-
u8 xts_tweak[FS_XTS_TWEAK_SIZE];
154+
struct {
155+
__le64 index;
156+
u8 padding[FS_XTS_TWEAK_SIZE - sizeof(__le64)];
157+
} xts_tweak;
155158
struct skcipher_request *req = NULL;
156159
DECLARE_FS_COMPLETION_RESULT(ecr);
157160
struct scatterlist dst, src;
@@ -171,17 +174,15 @@ static int do_page_crypto(struct inode *inode,
171174
req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
172175
page_crypt_complete, &ecr);
173176

174-
BUILD_BUG_ON(FS_XTS_TWEAK_SIZE < sizeof(index));
175-
memcpy(xts_tweak, &index, sizeof(index));
176-
memset(&xts_tweak[sizeof(index)], 0,
177-
FS_XTS_TWEAK_SIZE - sizeof(index));
177+
BUILD_BUG_ON(sizeof(xts_tweak) != FS_XTS_TWEAK_SIZE);
178+
xts_tweak.index = cpu_to_le64(index);
179+
memset(xts_tweak.padding, 0, sizeof(xts_tweak.padding));
178180

179181
sg_init_table(&dst, 1);
180182
sg_set_page(&dst, dest_page, PAGE_SIZE, 0);
181183
sg_init_table(&src, 1);
182184
sg_set_page(&src, src_page, PAGE_SIZE, 0);
183-
skcipher_request_set_crypt(req, &src, &dst, PAGE_SIZE,
184-
xts_tweak);
185+
skcipher_request_set_crypt(req, &src, &dst, PAGE_SIZE, &xts_tweak);
185186
if (rw == FS_DECRYPT)
186187
res = crypto_skcipher_decrypt(req);
187188
else

0 commit comments

Comments
 (0)