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.brandeb...@intel.com>
Cc: Tony Nguyen <anthony.l.ngu...@intel.com>
Cc: "David S. Miller" <da...@davemloft.net>
Cc: Eric Dumazet <eduma...@google.com>
Cc: Jakub Kicinski <k...@kernel.org>
Cc: Paolo Abeni <pab...@redhat.com>
Cc: intel-wired-...@lists.osuosl.org
Cc: net...@vger.kernel.org
Signed-off-by: Kees Cook <keesc...@chromium.org>
---
 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));
 
        /* 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);
        } else {
                memset(q_vector, 0, size);
        }
-- 
2.34.1

Reply via email to