From: Kees Cook <kees@kernel.org>
To: Jakub Kicinski <kuba@kernel.org>
Cc: Kees Cook <kees@kernel.org>,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: [PATCH RFC 3/5] rtnetlink: do_setlink: Use sockaddr_storage
Date: Mon, 4 Nov 2024 14:25:05 -0800 [thread overview]
Message-ID: <20241104222513.3469025-3-kees@kernel.org> (raw)
In-Reply-To: <20241104221450.work.053-kees@kernel.org>
Instead of a heap allocation use a stack allocated sockaddr_storage to
support arbitrary length addr_len value (but bounds check it against the
maximum address length).
Signed-off-by: Kees Cook <kees@kernel.org>
---
net/core/rtnetlink.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index f0a520987085..eddd10b74f06 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -2839,21 +2839,17 @@ static int do_setlink(const struct sk_buff *skb,
}
if (tb[IFLA_ADDRESS]) {
- struct sockaddr *sa;
- int len;
+ struct sockaddr_storage addr;
+ struct sockaddr *sa = (struct sockaddr *)&addr;
- len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
- sizeof(*sa));
- sa = kmalloc(len, GFP_KERNEL);
- if (!sa) {
+ if (dev->addr_len > sizeof(addr.__data)) {
err = -ENOMEM;
goto errout;
}
sa->sa_family = dev->type;
- memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
+ memcpy(addr.__data, nla_data(tb[IFLA_ADDRESS]),
dev->addr_len);
err = dev_set_mac_address_user(dev, sa, extack);
- kfree(sa);
if (err)
goto errout;
status |= DO_SETLINK_MODIFIED;
--
2.34.1
next prev parent reply other threads:[~2024-11-04 22:25 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-04 22:25 [PATCH RFC 0/5] sockaddr usage removal Kees Cook
2024-11-04 22:25 ` [PATCH RFC 1/5] Revert "net: dev: Convert sa_data to flexible array in struct sockaddr" Kees Cook
2024-11-04 22:25 ` [PATCH RFC 2/5] net: core: dev.c confirmed to use classic sockaddr Kees Cook
2024-11-04 22:25 ` Kees Cook [this message]
2024-11-05 10:59 ` [PATCH RFC 3/5] rtnetlink: do_setlink: Use sockaddr_storage Eric Dumazet
2024-12-17 1:52 ` Kees Cook
2024-11-04 22:25 ` [PATCH RFC 4/5] net: core: Convert inet_addr_is_any() to sockaddr_storage Kees Cook
2024-11-04 22:25 ` [PATCH RFC 5/5] net: Convert proto_ops::getname " Kees Cook
2024-11-06 1:16 ` [PATCH RFC 0/5] sockaddr usage removal Jakub Kicinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241104222513.3469025-3-kees@kernel.org \
--to=kees@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).