Skip to contentSkip to conversation

net/ipv4/nexthop: check the return value of nexthop_find_by_id()

PATCH In review Li Zhong 3 participants 4 messages Sep 17, 2022 – Sep 18, 2022

Subsystems (3)
lore
Share & tools
lore

Li Zhong

author
#0 lore raw

PATCH 1/1 net/ipv4/nexthop: check the return value of nexthop_find_by_id()

Check the return value of nexthop_find_by_id(), which could be NULL on when not found. So we check to avoid null pointer dereference.

---
 net/ipv4/nexthop.c | 4 ++++
 1 file changed, 4 insertions(+)
net/ipv4/nexthop.c +4 -0
index 853a75a8fbaf..9f91bb78eed5 100644
@@ -2445,6 +2445,10 @@ static struct nexthop *nexthop_create_group(struct net *net,
 		struct nh_info *nhi;
 
 		nhe = nexthop_find_by_id(net, entry[i].id);
+		if (!nhe) {
+			err = -EINVAL;
+			goto out_no_nh;
+		}
 		if (!nexthop_get(nhe)) {
 			err = -ENOENT;
 			goto out_no_nh;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 13 lines · Li Zhong

Check the return value of nexthop_find_by_id(), which could be NULL on when not found. So we check to avoid null pointer dereference.

---
 net/ipv4/nexthop.c | 4 ++++
 1 file changed, 4 insertions(+)
net/ipv4/nexthop.c +0 -0
index 853a75a8fbaf..9f91bb78eed5 100644

net/ipv4/nexthop.c static struct nexthop *nexthop_create_group(struct net *net, · line 2454

Quoted 10 lines
net/ipv4/nexthop.c +4 -0
@@ -2445,6 +2445,10 @@ static struct nexthop *nexthop_create_group(struct net *net,
 		struct nh_info *nhi;
 
 		nhe = nexthop_find_by_id(net, entry[i].id);
+		if (!nhe) {
+			err = -EINVAL;
+			goto out_no_nh;
+		}
 		if (!nexthop_get(nhe)) {
 			err = -ENOENT;
 			goto out_no_nh;

These are validated in nh_check_attr_group() and should exist at this point. Since remove_nexthop() should run under rtnl I don't see a way for a nexthop to disappear after nh_check_attr_group() and before nexthop_create_group().

Did you notice a problem or have a stack dump?

maintainer
#2 lore raw
Quoted 16 lines · Nikolay Aleksandrov
Quoted 15 lines · Li Zhong

Check the return value of nexthop_find_by_id(), which could be NULL on when not found. So we check to avoid null pointer dereference.

---
 net/ipv4/nexthop.c | 4 ++++
 1 file changed, 4 insertions(+)
net/ipv4/nexthop.c +0 -0
index 853a75a8fbaf..9f91bb78eed5 100644
@@ -2445,6 +2445,10 @@ static struct nexthop *nexthop_create_group(struct net *net,
 		struct nh_info *nhi;
 
Quoted 13 lines

net/ipv4/nexthop.c static struct nexthop *nexthop_create_group(struct net *net, · line 2454

Quoted 8 lines
net/ipv4/nexthop.c +4 -0
 		nhe = nexthop_find_by_id(net, entry[i].id);
+		if (!nhe) {
+			err = -EINVAL;
+			goto out_no_nh;
+		}
 		if (!nexthop_get(nhe)) {
 			err = -ENOENT;
 			goto out_no_nh;

These are validated in nh_check_attr_group() and should exist at this point. Since remove_nexthop() should run under rtnl I don't see a way for a nexthop to disappear after nh_check_attr_group() and before nexthop_create_group().

exactly. That lookup can't fail because the ids have been validated and all of this is under rtnl preventing nexthop removes.

author
#3 lore raw
Quoted 32 lines · David Ahern
Quoted 16 lines · Nikolay Aleksandrov
Quoted 15 lines · Li Zhong

Check the return value of nexthop_find_by_id(), which could be NULL on when not found. So we check to avoid null pointer dereference.

---
 net/ipv4/nexthop.c | 4 ++++
 1 file changed, 4 insertions(+)
net/ipv4/nexthop.c +0 -0
index 853a75a8fbaf..9f91bb78eed5 100644
@@ -2445,6 +2445,10 @@ static struct nexthop *nexthop_create_group(struct net *net,
             struct nh_info *nhi;
 
Quoted 13 lines

net/ipv4/nexthop.c static struct nexthop *nexthop_create_group(struct net *net, · line 2454

Quoted 8 lines
net/ipv4/nexthop.c +4 -0
             nhe = nexthop_find_by_id(net, entry[i].id);
+            if (!nhe) {
+                    err = -EINVAL;
+                    goto out_no_nh;
+            }
             if (!nexthop_get(nhe)) {
                     err = -ENOENT;
                     goto out_no_nh;

These are validated in nh_check_attr_group() and should exist at this point. Since remove_nexthop() should run under rtnl I don't see a way for a nexthop to disappear after nh_check_attr_group() and before nexthop_create_group().

Quoted 3 lines

exactly. That lookup can't fail because the ids have been validated and all of this is under rtnl preventing nexthop removes.

Thanks for all your replies. That makes sense to me.