Skip to content

Commit 6b12e0a

Browse files
keesPaolo Abeni
authored andcommitted
rtnetlink: do_setlink: Use struct sockaddr_storage
Instead of a heap allocating a variably sized struct sockaddr and lying about the type in the call to netif_set_mac_address(), use a stack allocated struct sockaddr_storage. This lets us drop the cast and avoid the allocation. Putting "ss" on the stack means it will get a reused stack slot since it is the same size (128B) as other existing single-scope stack variables, like the vfinfo array (128B), so no additional stack space is used by this function. Acked-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Kees Cook <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent 9ca6804 commit 6b12e0a

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

net/core/rtnetlink.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,28 +3080,17 @@ static int do_setlink(const struct sk_buff *skb, struct net_device *dev,
30803080
}
30813081

30823082
if (tb[IFLA_ADDRESS]) {
3083-
struct sockaddr *sa;
3084-
int len;
3085-
3086-
len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
3087-
sizeof(*sa));
3088-
sa = kmalloc(len, GFP_KERNEL);
3089-
if (!sa) {
3090-
err = -ENOMEM;
3091-
goto errout;
3092-
}
3093-
sa->sa_family = dev->type;
3083+
struct sockaddr_storage ss = { };
30943084

30953085
netdev_unlock_ops(dev);
30963086

30973087
/* dev_addr_sem is an outer lock, enforce proper ordering */
30983088
down_write(&dev_addr_sem);
30993089
netdev_lock_ops(dev);
31003090

3101-
memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
3102-
dev->addr_len);
3103-
err = netif_set_mac_address(dev, (struct sockaddr_storage *)sa, extack);
3104-
kfree(sa);
3091+
ss.ss_family = dev->type;
3092+
memcpy(ss.__data, nla_data(tb[IFLA_ADDRESS]), dev->addr_len);
3093+
err = netif_set_mac_address(dev, &ss, extack);
31053094
if (err) {
31063095
up_write(&dev_addr_sem);
31073096
goto errout;

0 commit comments

Comments
 (0)