🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Stephen Hemminger Mon, 30 Nov 2026 23:14
[PATCH iproute2] ip xfrm: fix buffer overrun in deleteall
#24833
Author: Stephen Hemminger
Date: Mon, 30 Nov 2026 23:14
117 lines
14966 bytes
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
Re: [PATCH iproute2] ip xfrm: fix buffer overrun in deleteall
#24855
Author: dave seddon
Date: Mon, 30 Nov 2026 23:51
146 lines
17204 bytes
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
Re: [PATCH iproute2] ip xfrm: fix buffer overrun in deleteall
#25453
Author: patchwork-bot+netdevbpf
Date: Tue, 01 Dec 2026 19:50
26 lines
8678 bytes
Hello:

This patch was applied to iproute2/iproute2.git (main)
by Stephen Hemminger <stephen@networkplumber.org>:

On Mon, 30 Nov 2026 15:14:12 -0800 you 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.
> 
> [...]

Here is the summary with links:
  - [iproute2] ip xfrm: fix buffer overrun in deleteall
    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=21de35a159cd

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads