Netdev List
 help / color / mirror / Atom feed
* [PATCH iproute2] ip xfrm: fix buffer overrun in deleteall
@ 2026-11-30 23:14 Stephen Hemminger
  2026-11-30 23:51 ` dave seddon
  0 siblings, 1 reply; 2+ messages in thread
From: Stephen Hemminger @ 2026-11-30 23:14 UTC (permalink / raw)
  To: netdev; +Cc: dsahern, dave.seddon.ca, Stephen Hemminger

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH iproute2] ip xfrm: fix buffer overrun in deleteall
  2026-11-30 23:14 [PATCH iproute2] ip xfrm: fix buffer overrun in deleteall Stephen Hemminger
@ 2026-11-30 23:51 ` dave seddon
  0 siblings, 0 replies; 2+ messages in thread
From: dave seddon @ 2026-11-30 23:51 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev, dsahern

Thanks.  Looks good to me.

On Mon, Nov 30, 2026 at 3:14 PM Stephen Hemminger
<stephen@networkplumber.org> wrote:
>
> 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
>


-- 
Regards,
Dave Seddon
+1 415 857 5102

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 23:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 23:14 [PATCH iproute2] ip xfrm: fix buffer overrun in deleteall Stephen Hemminger
2026-11-30 23:51 ` dave seddon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox