From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, dave.seddon.ca@gmail.com,
Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2] ip xfrm: fix buffer overrun in deleteall
Date: Mon, 30 Nov 2026 15:14:12 -0800 [thread overview]
Message-ID: <20261130231412.1104721-1-stephen@networkplumber.org> (raw)
Deleteall packs delete requests into a fixed size stack buffer. The
state side only checks (xb->offset > xb->size), which ignores the
message about to be written, so a full buffer is overrun by the next
delete request.
The addattr_l() calls cannot catch this either. They passed xb->size
as maxlen, but the bound is on nlmsg_len, which is relative to new_n
at xb->buf + xb->offset, so the check passes however full the buffer.
Pass the space remaining as maxlen and reserve room for the attributes
up front, so a message that does not fit waits for the next round.
Fixes: 0c7d651b38d6 ("xfrm_{state, policy}: Allow to deleteall polices/states with marks")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
ip/xfrm_policy.c | 15 +++++++++++----
ip/xfrm_state.c | 19 ++++++++++++-------
2 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/ip/xfrm_policy.c b/ip/xfrm_policy.c
index ba6f9718..8c25cb98 100644
--- a/ip/xfrm_policy.c
+++ b/ip/xfrm_policy.c
@@ -734,6 +734,7 @@ static int xfrm_policy_keep(struct nlmsghdr *n, void *arg)
__u8 ptype = XFRM_POLICY_TYPE_MAIN;
struct nlmsghdr *new_n;
struct xfrm_userpolicy_id *xpid;
+ int avail;
if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
fprintf(stderr, "Not a policy: %08x %08x %08x\n",
@@ -767,7 +768,10 @@ static int xfrm_policy_keep(struct nlmsghdr *n, void *arg)
if (xpinfo->dir >= XFRM_POLICY_MAX)
return 0;
- if (xb->offset + NLMSG_LENGTH(sizeof(*xpid)) > xb->size)
+ /* make sure there is space left in the buffer for whole message */
+ avail = xb->size - xb->offset;
+ if (NLMSG_LENGTH(sizeof(*xpid)) + RTA_SPACE(sizeof(struct xfrm_mark)) +
+ RTA_SPACE(sizeof(__u32)) > avail)
return 0;
new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
@@ -782,7 +786,7 @@ static int xfrm_policy_keep(struct nlmsghdr *n, void *arg)
xpid->index = xpinfo->index;
if (tb[XFRMA_MARK]) {
- int r = addattr_l(new_n, xb->size, XFRMA_MARK,
+ int r = addattr_l(new_n, avail, XFRMA_MARK,
RTA_DATA(tb[XFRMA_MARK]), RTA_PAYLOAD(tb[XFRMA_MARK]));
if (r < 0) {
fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
@@ -791,8 +795,11 @@ static int xfrm_policy_keep(struct nlmsghdr *n, void *arg)
}
if (tb[XFRMA_IF_ID]) {
- addattr32(new_n, xb->size, XFRMA_IF_ID,
- rta_getattr_u32(tb[XFRMA_IF_ID]));
+ if (addattr32(new_n, avail, XFRMA_IF_ID,
+ rta_getattr_u32(tb[XFRMA_IF_ID]))) {
+ fprintf(stderr, "%s: XFRMA_IF_ID failed\n", __func__);
+ exit(1);
+ }
}
xb->offset += new_n->nlmsg_len;
diff --git a/ip/xfrm_state.c b/ip/xfrm_state.c
index cbbdae99..e61d479e 100644
--- a/ip/xfrm_state.c
+++ b/ip/xfrm_state.c
@@ -1159,6 +1159,7 @@ static int xfrm_state_keep(struct nlmsghdr *n, void *arg)
struct nlmsghdr *new_n;
struct xfrm_usersa_id *xsid;
struct rtattr *tb[XFRMA_MAX+1];
+ int avail;
if (n->nlmsg_type != XFRM_MSG_NEWSA) {
fprintf(stderr, "Not a state: %08x %08x %08x\n",
@@ -1179,10 +1180,11 @@ static int xfrm_state_keep(struct nlmsghdr *n, void *arg)
xsinfo->id.proto == IPPROTO_IPV6)
return 0;
- if (xb->offset > xb->size) {
- fprintf(stderr, "State buffer overflow\n");
- return -1;
- }
+ /* make sure there is space left in the buffer for whole message */
+ avail = xb->size - xb->offset;
+ if (NLMSG_LENGTH(sizeof(*xsid)) + RTA_SPACE(sizeof(xsid->daddr)) +
+ RTA_SPACE(sizeof(struct xfrm_mark)) > avail)
+ return 0;
new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
@@ -1196,13 +1198,16 @@ static int xfrm_state_keep(struct nlmsghdr *n, void *arg)
xsid->spi = xsinfo->id.spi;
xsid->proto = xsinfo->id.proto;
- addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
- sizeof(xsid->daddr));
+ if (addattr_l(new_n, avail, XFRMA_SRCADDR, &xsinfo->saddr,
+ sizeof(xsid->daddr))) {
+ fprintf(stderr, "%s: XFRMA_SRCADDR failed\n", __func__);
+ exit(1);
+ }
parse_rtattr(tb, XFRMA_MAX, XFRMS_RTA(xsinfo), len);
if (tb[XFRMA_MARK]) {
- int r = addattr_l(new_n, xb->size, XFRMA_MARK,
+ int r = addattr_l(new_n, avail, XFRMA_MARK,
RTA_DATA(tb[XFRMA_MARK]), RTA_PAYLOAD(tb[XFRMA_MARK]));
if (r < 0) {
fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
--
2.53.0
next reply other threads:[~2026-11-30 23:14 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 23:14 Stephen Hemminger [this message]
2026-11-30 23:51 ` [PATCH iproute2] ip xfrm: fix buffer overrun in deleteall dave seddon
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=20261130231412.1104721-1-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=dave.seddon.ca@gmail.com \
--cc=dsahern@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