Skip to contentSkip to conversation

seg6local: fix crash when next-csid ends the command

PATCH In review Dave Seddon 2 participants 2 messages Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 seg6local: fix crash when next-csid ends the command

ip crashes when "flavors next-csid" ends the command, with or without "lblen N":

  ip -6 route add 2001:db8:2::/64 dev lo encap seg6local action End \
    flavors next-csid

seg6local_parse_flavors() steps past the last argument and passes the NULL terminator to strcmp(). Check argc first.

Fixes 04a6b456bf74 ("seg6: add support for flavors in SRv6 End* behaviors")
Assisted-by Claude:claude-opus-5-5
---
 ip/iproute_lwtunnel.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ip/iproute_lwtunnel.c +2 -2
index 9a1e747c..53004b5f 100644
@@ -1434,7 +1434,7 @@ static int seg6local_parse_flavors(struct rtattr *rta, size_t len,
 
 	if (flavors & (1 << SEG6_LOCAL_FLV_OP_NEXT_CSID)) {
 		NEXT_ARG_FWD();
-		if (strcmp(*argv, "lblen") == 0){
+		if (argc > 0 && strcmp(*argv, "lblen") == 0) {
 			NEXT_ARG();
 			if (lbl_ok++)
 				duparg2("lblen", *argv);
@@ -1444,7 +1444,7 @@ static int seg6local_parse_flavors(struct rtattr *rta, size_t len,
 			NEXT_ARG_FWD();
 		}
 
-		if (strcmp(*argv, "nflen") == 0){
+		if (argc > 0 && strcmp(*argv, "nflen") == 0) {
 			NEXT_ARG();
 			if (nfl_ok++)
 				duparg2("nflen", *argv);
Signature · 1 line
2.56.0
#1 lore raw
Quoted 30 lines

ip crashes when "flavors next-csid" ends the command, with or without "lblen N":

   ip -6 route add 2001:db8:2::/64 dev lo encap seg6local action End \
     flavors next-csid

seg6local_parse_flavors() steps past the last argument and passes the NULL terminator to strcmp(). Check argc first.

Fixes 04a6b456bf74 ("seg6: add support for flavors in SRv6 End* behaviors")
Assisted-by Claude:claude-opus-5-5
---
  ip/iproute_lwtunnel.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
ip/iproute_lwtunnel.c +1 -1
index 9a1e747c..53004b5f 100644
@@ -1434,7 +1434,7 @@ static int seg6local_parse_flavors(struct rtattr *rta, size_t len,
 
  	if (flavors & (1 << SEG6_LOCAL_FLV_OP_NEXT_CSID)) {
  		NEXT_ARG_FWD();
-		if (strcmp(*argv, "lblen") == 0){
+		if (argc > 0 && strcmp(*argv, "lblen") == 0) {
  			NEXT_ARG();
  			if (lbl_ok++)
  				duparg2("lblen", *argv);

ip/iproute_lwtunnel.c static int seg6local_parse_flavors(struct rtattr *rta, size_t len, · line 1450

Quoted 8 lines
ip/iproute_lwtunnel.c +1 -1
@@ -1444,7 +1444,7 @@ static int seg6local_parse_flavors(struct rtattr *rta, size_t len,
  			NEXT_ARG_FWD();
  		}
 
-		if (strcmp(*argv, "nflen") == 0){
+		if (argc > 0 && strcmp(*argv, "nflen") == 0) {
  			NEXT_ARG();
  			if (nfl_ok++)
  				duparg2("nflen", *argv);

Good find, and your change fixes the segfault.

But I think the way you fix it is a bit uncommon in the code base that seems to instead prefer a pattern like

  while (NEXT_ARG_OK()) {
    NEXT_ARG_FWD();
    if (strcmp(...) == 0) {
      ...
    } else if (strcmp(...) == 0) {
      ...
    } else {
      PREV_ARG();
      break;
    }
  }

for parsing a list of optional arguments in any order.

With that style, you would also fix another bug which is that it currently expects lblen and nflen in that specific order which contradicts the man page that allows them in any order.

The presence of the if (lbl_ok++) and if (nfl_ok++) also seem to indicate that the code was originally intended to run in a loop.