RE: [PATCH v2 06/16] igb: Proactively round up to kmalloc bucket size

From: Ruhl, Michael J
Date: Mon Sep 26 2022 - 12:55:40 EST


>-----Original Message-----
>From: Kees Cook <keescook@xxxxxxxxxxxx>
>Sent: Friday, September 23, 2022 4:28 PM
>To: Vlastimil Babka <vbabka@xxxxxxx>
>Cc: Kees Cook <keescook@xxxxxxxxxxxx>; Brandeburg, Jesse
><jesse.brandeburg@xxxxxxxxx>; Nguyen, Anthony L
><anthony.l.nguyen@xxxxxxxxx>; David S. Miller <davem@xxxxxxxxxxxxx>;
>Eric Dumazet <edumazet@xxxxxxxxxx>; Jakub Kicinski <kuba@xxxxxxxxxx>;
>Paolo Abeni <pabeni@xxxxxxxxxx>; intel-wired-lan@xxxxxxxxxxxxxxxx;
>netdev@xxxxxxxxxxxxxxx; Ruhl, Michael J <michael.j.ruhl@xxxxxxxxx>;
>Hyeonggon Yoo <42.hyeyoo@xxxxxxxxx>; Christoph Lameter
><cl@xxxxxxxxx>; Pekka Enberg <penberg@xxxxxxxxxx>; David Rientjes
><rientjes@xxxxxxxxxx>; Joonsoo Kim <iamjoonsoo.kim@xxxxxxx>; Andrew
>Morton <akpm@xxxxxxxxxxxxxxxxxxxx>; Greg Kroah-Hartman
><gregkh@xxxxxxxxxxxxxxxxxxx>; Nick Desaulniers
><ndesaulniers@xxxxxxxxxx>; Alex Elder <elder@xxxxxxxxxx>; Josef Bacik
><josef@xxxxxxxxxxxxxx>; David Sterba <dsterba@xxxxxxxx>; Sumit Semwal
><sumit.semwal@xxxxxxxxxx>; Christian König <christian.koenig@xxxxxxx>;
>Daniel Micay <danielmicay@xxxxxxxxx>; Yonghong Song <yhs@xxxxxx>;
>Marco Elver <elver@xxxxxxxxxx>; Miguel Ojeda <ojeda@xxxxxxxxxx>; linux-
>kernel@xxxxxxxxxxxxxxx; linux-mm@xxxxxxxxx; linux-btrfs@xxxxxxxxxxxxxxx;
>linux-media@xxxxxxxxxxxxxxx; dri-devel@xxxxxxxxxxxxxxxxxxxxx; linaro-mm-
>sig@xxxxxxxxxxxxxxxx; linux-fsdevel@xxxxxxxxxxxxxxx; dev@xxxxxxxxxxxxxxx;
>x86@xxxxxxxxxx; llvm@xxxxxxxxxxxxxxx; linux-hardening@xxxxxxxxxxxxxxx
>Subject: [PATCH v2 06/16] igb: Proactively round up to kmalloc bucket size
>
>In preparation for removing the "silently change allocation size"
>users of ksize(), explicitly round up all q_vector allocations so that
>allocations can be correctly compared to ksize().
>
>Additionally fix potential use-after-free in the case of new allocation
>failure: only free memory if the replacement allocation succeeds.
>
>Cc: Jesse Brandeburg <jesse.brandeburg@xxxxxxxxx>
>Cc: Tony Nguyen <anthony.l.nguyen@xxxxxxxxx>
>Cc: "David S. Miller" <davem@xxxxxxxxxxxxx>
>Cc: Eric Dumazet <edumazet@xxxxxxxxxx>
>Cc: Jakub Kicinski <kuba@xxxxxxxxxx>
>Cc: Paolo Abeni <pabeni@xxxxxxxxxx>
>Cc: intel-wired-lan@xxxxxxxxxxxxxxxx
>Cc: netdev@xxxxxxxxxxxxxxx
>Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>
>---
> drivers/net/ethernet/intel/igb/igb_main.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
>diff --git a/drivers/net/ethernet/intel/igb/igb_main.c
>b/drivers/net/ethernet/intel/igb/igb_main.c
>index 2796e81d2726..eb51e531c096 100644
>--- a/drivers/net/ethernet/intel/igb/igb_main.c
>+++ b/drivers/net/ethernet/intel/igb/igb_main.c
>@@ -1195,15 +1195,16 @@ static int igb_alloc_q_vector(struct igb_adapter
>*adapter,
> return -ENOMEM;
>
> ring_count = txr_count + rxr_count;
>- size = struct_size(q_vector, ring, ring_count);
>+ size = kmalloc_size_roundup(struct_size(q_vector, ring, ring_count));

This looks good to me...

> /* allocate q_vector and rings */
> q_vector = adapter->q_vector[v_idx];
> if (!q_vector) {
> q_vector = kzalloc(size, GFP_KERNEL);
> } else if (size > ksize(q_vector)) {
>- kfree_rcu(q_vector, rcu);
> q_vector = kzalloc(size, GFP_KERNEL);
>+ if (q_vector)
>+ kfree_rcu(q_vector, rcu);

Even though this is in the ksize part, this seems like an unrelated change?
Should this be in a different patch?

Also, the kfree_rcu will free q_vector after the RCU grace period?

Is that what you want to do?

How does rcu distinguish between the original q_vector, and the newly kzalloced one?

Thanks,

Mike



> } else {
> memset(q_vector, 0, size);
> }
>--
>2.34.1