[PATCH 08/12] openvswitch: Proactively round up to kmalloc bucket size

From: Kees Cook
Date: Wed Sep 21 2022 - 23:13:43 EST


Instead of having a mismatch between the requested allocation size and
the actual kmalloc bucket size, which is examined later via ksize(),
round up proactively so the allocation is explicitly made for the full
size, allowing the compiler to correctly reason about the resulting size
of the buffer through the existing __alloc_size() hint.

Cc: Pravin B Shelar <pshelar@xxxxxxx>
Cc: "David S. Miller" <davem@xxxxxxxxxxxxx>
Cc: Eric Dumazet <edumazet@xxxxxxxxxx>
Cc: Jakub Kicinski <kuba@xxxxxxxxxx>
Cc: Paolo Abeni <pabeni@xxxxxxxxxx>
Cc: netdev@xxxxxxxxxxxxxxx
Cc: dev@xxxxxxxxxxxxxxx
Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>
---
net/openvswitch/flow_netlink.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/openvswitch/flow_netlink.c b/net/openvswitch/flow_netlink.c
index 4c09cf8a0ab2..11b2e2c94c7e 100644
--- a/net/openvswitch/flow_netlink.c
+++ b/net/openvswitch/flow_netlink.c
@@ -2306,10 +2306,12 @@ int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
static struct sw_flow_actions *nla_alloc_flow_actions(int size)
{
struct sw_flow_actions *sfa;
+ int alloc_size;

WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);

- sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
+ alloc_size = kmalloc_size_roundup(sizeof(*sfa) + size);
+ sfa = kmalloc(alloc_size, GFP_KERNEL);
if (!sfa)
return ERR_PTR(-ENOMEM);

--
2.34.1