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

Kees Cook
2022-09-22T03:10:29Z

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@ovn.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org
Cc: dev@openvswitch.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
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