diff options
author | Justin Husted <sigstop@gmail.com> | 2019-10-12 03:20:30 +0300 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2023-10-23 00:08:30 +0300 |
commit | e3728b50034504e5e64604d72896973374cb1fa5 (patch) | |
tree | 59b558932576462c23bc9fac0c87d5d769bf33d8 /fs | |
parent | e219965586b0e18a12e12fdf37f26eb74bb17bcd (diff) | |
download | linux-e3728b50034504e5e64604d72896973374cb1fa5.tar.xz |
bcachefs: Initialize padding space after alloc bkey
Packed bkeys are padded up to 64 bit alignment, but the alloc bkey type
was not clearing the pad bytes after the last data byte. This left the
key possibly containing some random garbage at the end.
This problem was found using valgrind.
This patch also changes a path with the inode bkey to clear in the same
way.
Signed-off-by: Justin Husted <sigstop@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/bcachefs/alloc_background.c | 5 | ||||
-rw-r--r-- | fs/bcachefs/inode.c | 8 | ||||
-rw-r--r-- | fs/bcachefs/util.h | 8 |
3 files changed, 16 insertions, 5 deletions
diff --git a/fs/bcachefs/alloc_background.c b/fs/bcachefs/alloc_background.c index 7d5d94dbc64f..7b9079a740ef 100644 --- a/fs/bcachefs/alloc_background.c +++ b/fs/bcachefs/alloc_background.c @@ -152,6 +152,7 @@ void bch2_alloc_pack(struct bkey_i_alloc *dst, { unsigned idx = 0; void *d = dst->v.data; + unsigned bytes; dst->v.fields = 0; dst->v.gen = src.gen; @@ -160,7 +161,9 @@ void bch2_alloc_pack(struct bkey_i_alloc *dst, BCH_ALLOC_FIELDS() #undef x - set_bkey_val_bytes(&dst->k, (void *) d - (void *) &dst->v); + bytes = (void *) d - (void *) &dst->v; + set_bkey_val_bytes(&dst->k, bytes); + memset_u64s_tail(&dst->v, 0, bytes); } static unsigned bch_alloc_val_u64s(const struct bch_alloc *a) diff --git a/fs/bcachefs/inode.c b/fs/bcachefs/inode.c index b0e670cc9d0f..67555db01dc4 100644 --- a/fs/bcachefs/inode.c +++ b/fs/bcachefs/inode.c @@ -95,6 +95,7 @@ void bch2_inode_pack(struct bkey_inode_buf *packed, u8 *end = (void *) &packed[1]; u8 *last_nonzero_field = out; unsigned nr_fields = 0, last_nonzero_fieldnr = 0; + unsigned bytes; bkey_inode_init(&packed->inode.k_i); packed->inode.k.p.inode = inode->bi_inum; @@ -117,10 +118,9 @@ void bch2_inode_pack(struct bkey_inode_buf *packed, out = last_nonzero_field; nr_fields = last_nonzero_fieldnr; - set_bkey_val_bytes(&packed->inode.k, out - (u8 *) &packed->inode.v); - memset(out, 0, - (u8 *) &packed->inode.v + - bkey_val_bytes(&packed->inode.k) - out); + bytes = out - (u8 *) &packed->inode.v; + set_bkey_val_bytes(&packed->inode.k, bytes); + memset_u64s_tail(&packed->inode.v, 0, bytes); SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields); diff --git a/fs/bcachefs/util.h b/fs/bcachefs/util.h index 965b6dd73bfa..33589362f5df 100644 --- a/fs/bcachefs/util.h +++ b/fs/bcachefs/util.h @@ -648,6 +648,14 @@ static inline void memmove_u64s(void *dst, const void *src, __memmove_u64s_up(dst, src, u64s); } +/* Set the last few bytes up to a u64 boundary given an offset into a buffer. */ +static inline void memset_u64s_tail(void *s, int c, unsigned bytes) +{ + unsigned rem = round_up(bytes, sizeof(u64)) - bytes; + + memset(s + bytes, c, rem); +} + void sort_cmp_size(void *base, size_t num, size_t size, int (*cmp_func)(const void *, const void *, size_t), void (*swap_func)(void *, void *, size_t)); |