Skip to content

Commit 8415ce0

Browse files
ebiggerstytso
authored andcommitted
ext4: fix unaligned memory access in ext4_fc_reserve_space()
As is done elsewhere in the file, build the struct ext4_fc_tl on the stack and memcpy() it into the buffer, rather than directly writing it to a potentially-unaligned location in the buffer. Fixes: aa75f4d ("ext4: main fast-commit commit path") Cc: <[email protected]> # v5.10+ Signed-off-by: Eric Biggers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 64b4a25 commit 8415ce0

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

fs/ext4/fast_commit.c

+21-18
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,15 @@ static void ext4_fc_submit_bh(struct super_block *sb, bool is_tail)
675675

676676
/* Ext4 commit path routines */
677677

678+
/* memcpy to fc reserved space and update CRC */
679+
static void *ext4_fc_memcpy(struct super_block *sb, void *dst, const void *src,
680+
int len, u32 *crc)
681+
{
682+
if (crc)
683+
*crc = ext4_chksum(EXT4_SB(sb), *crc, src, len);
684+
return memcpy(dst, src, len);
685+
}
686+
678687
/* memzero and update CRC */
679688
static void *ext4_fc_memzero(struct super_block *sb, void *dst, int len,
680689
u32 *crc)
@@ -700,12 +709,13 @@ static void *ext4_fc_memzero(struct super_block *sb, void *dst, int len,
700709
*/
701710
static u8 *ext4_fc_reserve_space(struct super_block *sb, int len, u32 *crc)
702711
{
703-
struct ext4_fc_tl *tl;
712+
struct ext4_fc_tl tl;
704713
struct ext4_sb_info *sbi = EXT4_SB(sb);
705714
struct buffer_head *bh;
706715
int bsize = sbi->s_journal->j_blocksize;
707716
int ret, off = sbi->s_fc_bytes % bsize;
708717
int pad_len;
718+
u8 *dst;
709719

710720
/*
711721
* After allocating len, we should have space at least for a 0 byte
@@ -729,16 +739,18 @@ static u8 *ext4_fc_reserve_space(struct super_block *sb, int len, u32 *crc)
729739
return sbi->s_fc_bh->b_data + off;
730740
}
731741
/* Need to add PAD tag */
732-
tl = (struct ext4_fc_tl *)(sbi->s_fc_bh->b_data + off);
733-
tl->fc_tag = cpu_to_le16(EXT4_FC_TAG_PAD);
742+
dst = sbi->s_fc_bh->b_data + off;
743+
tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_PAD);
734744
pad_len = bsize - off - 1 - EXT4_FC_TAG_BASE_LEN;
735-
tl->fc_len = cpu_to_le16(pad_len);
736-
if (crc)
737-
*crc = ext4_chksum(sbi, *crc, tl, EXT4_FC_TAG_BASE_LEN);
738-
if (pad_len > 0)
739-
ext4_fc_memzero(sb, tl + 1, pad_len, crc);
745+
tl.fc_len = cpu_to_le16(pad_len);
746+
ext4_fc_memcpy(sb, dst, &tl, EXT4_FC_TAG_BASE_LEN, crc);
747+
dst += EXT4_FC_TAG_BASE_LEN;
748+
if (pad_len > 0) {
749+
ext4_fc_memzero(sb, dst, pad_len, crc);
750+
dst += pad_len;
751+
}
740752
/* Don't leak uninitialized memory in the unused last byte. */
741-
*((u8 *)(tl + 1) + pad_len) = 0;
753+
*dst = 0;
742754

743755
ext4_fc_submit_bh(sb, false);
744756

@@ -750,15 +762,6 @@ static u8 *ext4_fc_reserve_space(struct super_block *sb, int len, u32 *crc)
750762
return sbi->s_fc_bh->b_data;
751763
}
752764

753-
/* memcpy to fc reserved space and update CRC */
754-
static void *ext4_fc_memcpy(struct super_block *sb, void *dst, const void *src,
755-
int len, u32 *crc)
756-
{
757-
if (crc)
758-
*crc = ext4_chksum(EXT4_SB(sb), *crc, src, len);
759-
return memcpy(dst, src, len);
760-
}
761-
762765
/*
763766
* Complete a fast commit by writing tail tag.
764767
*

0 commit comments

Comments
 (0)