linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bcachefs: Prefer struct_size over open coded arithmetic
@ 2024-02-24 15:16 Erick Archer
  2024-02-25  0:52 ` Kent Overstreet
  0 siblings, 1 reply; 3+ messages in thread
From: Erick Archer @ 2024-02-24 15:16 UTC (permalink / raw)
  To: Kent Overstreet, Brian Foster, Gustavo A. R. Silva, Kees Cook
  Cc: Erick Archer, linux-bcachefs, linux-kernel, linux-hardening

This is an effort to get rid of all multiplications from allocation
functions in order to prevent integer overflows [1][2].

As the "t" variable is a pointer to "struct journal_seq_blacklist_table"
and this structure ends in a flexible array:

struct journal_seq_blacklist_table {
	[...]
	struct journal_seq_blacklist_table_entry {
		u64		start;
		u64		end;
		bool		dirty;
	}			entries[];
};

the preferred way in the kernel is to use the struct_size() helper to
do the arithmetic instead of the argument "size + size * count" in the
kzalloc() function.

This way, the code is more readable and safer.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
Link: https://github.com/KSPP/linux/issues/160 [2]
Signed-off-by: Erick Archer <erick.archer@gmx.com>
---
 fs/bcachefs/journal_seq_blacklist.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/bcachefs/journal_seq_blacklist.c b/fs/bcachefs/journal_seq_blacklist.c
index 024c9b1b323f..2c2490aa15fe 100644
--- a/fs/bcachefs/journal_seq_blacklist.c
+++ b/fs/bcachefs/journal_seq_blacklist.c
@@ -165,8 +165,7 @@ int bch2_blacklist_table_initialize(struct bch_fs *c)
 	if (!bl)
 		return 0;

-	t = kzalloc(sizeof(*t) + sizeof(t->entries[0]) * nr,
-		    GFP_KERNEL);
+	t = kzalloc(struct_size(t, entries, nr), GFP_KERNEL);
 	if (!t)
 		return -BCH_ERR_ENOMEM_blacklist_table_init;

--
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] bcachefs: Prefer struct_size over open coded arithmetic
  2024-02-24 15:16 [PATCH] bcachefs: Prefer struct_size over open coded arithmetic Erick Archer
@ 2024-02-25  0:52 ` Kent Overstreet
  0 siblings, 0 replies; 3+ messages in thread
From: Kent Overstreet @ 2024-02-25  0:52 UTC (permalink / raw)
  To: Erick Archer
  Cc: Brian Foster, Gustavo A. R. Silva, Kees Cook, linux-bcachefs,
	linux-kernel, linux-hardening

On Sat, Feb 24, 2024 at 04:16:58PM +0100, Erick Archer wrote:
> This is an effort to get rid of all multiplications from allocation
> functions in order to prevent integer overflows [1][2].
> 
> As the "t" variable is a pointer to "struct journal_seq_blacklist_table"
> and this structure ends in a flexible array:

there's no reason to break these out into multiple patches, just send me
a single patch when you're done - thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] bcachefs: Prefer struct_size over open coded arithmetic
@ 2024-02-24 14:59 Erick Archer
  0 siblings, 0 replies; 3+ messages in thread
From: Erick Archer @ 2024-02-24 14:59 UTC (permalink / raw)
  To: Kent Overstreet, Brian Foster, Gustavo A. R. Silva, Kees Cook
  Cc: Erick Archer, linux-bcachefs, linux-kernel, linux-hardening

This is an effort to get rid of all multiplications from allocation
functions in order to prevent integer overflows [1][2].

As the "op" variable is a pointer to "struct promote_op" and this
structure ends in a flexible array:

struct promote_op {
	[...]
	struct bio_vec bi_inline_vecs[];
};

the preferred way in the kernel is to use the struct_size() helper to
do the arithmetic instead of the argument "size + size * count" in the
kzalloc() function.

This way, the code is more readable and safer.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
Link: https://github.com/KSPP/linux/issues/160 [2]
Signed-off-by: Erick Archer <erick.archer@gmx.com>
---
 fs/bcachefs/io_read.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/bcachefs/io_read.c b/fs/bcachefs/io_read.c
index dce136cd2271..01beab55c6b3 100644
--- a/fs/bcachefs/io_read.c
+++ b/fs/bcachefs/io_read.c
@@ -174,7 +174,7 @@ static struct promote_op *__promote_alloc(struct btree_trans *trans,
 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_promote))
 		return ERR_PTR(-BCH_ERR_nopromote_no_writes);

-	op = kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_KERNEL);
+	op = kzalloc(struct_size(op, bi_inline_vecs, pages), GFP_KERNEL);
 	if (!op) {
 		ret = -BCH_ERR_nopromote_enomem;
 		goto err;
--
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-25  0:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-24 15:16 [PATCH] bcachefs: Prefer struct_size over open coded arithmetic Erick Archer
2024-02-25  0:52 ` Kent Overstreet
  -- strict thread matches above, loose matches on Subject: below --
2024-02-24 14:59 Erick Archer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).