Skip to contentSkip to conversation

rose: Fix Null pointer dereference in rose_send_frame()

PATCH Superseded Anmol Karn 3 participants 13 messages Nov 5, 2020 – Nov 20, 2020

Subsystems (2)
lore
Share & tools
lore

Anmol Karn

author
#0 lore raw

PATCH v2 1/1 rose: Fix Null pointer dereference in rose_send_frame()

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurance of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

---
 net/rose/rose_loopback.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rose/rose_loopback.c +1 -1
index 7b094275ea8b..cd7774cb1d07 100644
@@ -96,7 +96,7 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
+			if (rose_loopback_neigh->dev && (dev = rose_dev_get(dest)) != NULL) {
 				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
 					kfree_skb(skb);
 			} else {
Signature · 1 line
2.29.2
#1 lore raw
Quoted 18 lines · Anmol Karn

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurance of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized
in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in
rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer()
was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

Quoted 6 lines

Reported-and-tested-by:
[email protected] Link:
https://syzkaller.appspot.com/bug?id=9d2a7ca8c7f2e4b682c97578dfa3f236258300b3

missing proper fixes tag.

Quoted 18 lines
---
 net/rose/rose_loopback.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rose/rose_loopback.c +1 -1
index 7b094275ea8b..cd7774cb1d07 100644
@@ -96,7 +96,7 @@ static void rose_loopback_timer(struct timer_list
 *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
+			if (rose_loopback_neigh->dev && (dev =
 rose_dev_get(dest)) != NULL) {
 				if (rose_rx_call_request(skb, dev,

rose_loopback_neigh, lci_o) == 0)

net/rose/rose_loopback.c static void rose_loopback_timer(struct timer_list *unused) · line 102

Quoted 2 lines
net/rose/rose_loopback.c +0 -0
 					kfree_skb(skb);
 			} else {

check patch is not happy:

WARNING:TYPO_SPELLING: 'occurance' may be misspelled - perhaps 'occurrence'?
#7:
rose_transmit_clear_request(), and the first occurance of the neigh

ERROR:ASSIGN_IN_IF: do not use assignment in if condition #36: FILE: net/rose/rose_loopback.c:99:
+ if (rose_loopback_neigh->dev && (dev =
rose_dev_get(dest)) != NULL) {

total: 1 errors, 1 warnings, 0 checks, 8 lines checked

author
#2 lore raw

Hello Sir,

Quoted 56 lines · Saeed Mahameed
Quoted 18 lines · Anmol Karn

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurance of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized
in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in
rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer()
was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

Quoted 6 lines

Reported-and-tested-by:
[email protected] Link:
https://syzkaller.appspot.com/bug?id=9d2a7ca8c7f2e4b682c97578dfa3f236258300b3

missing proper fixes tag.

Quoted 18 lines
---
 net/rose/rose_loopback.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rose/rose_loopback.c +1 -1
index 7b094275ea8b..cd7774cb1d07 100644
@@ -96,7 +96,7 @@ static void rose_loopback_timer(struct timer_list
 *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
+			if (rose_loopback_neigh->dev && (dev =
 rose_dev_get(dest)) != NULL) {
 				if (rose_rx_call_request(skb, dev,

rose_loopback_neigh, lci_o) == 0)

net/rose/rose_loopback.c static void rose_loopback_timer(struct timer_list *unused) · line 102

Quoted 2 lines
net/rose/rose_loopback.c +0 -0
 					kfree_skb(skb);
 			} else {

check patch is not happy:

WARNING:TYPO_SPELLING: 'occurance' may be misspelled - perhaps 'occurrence'?
#7:
rose_transmit_clear_request(), and the first occurance of the neigh

Quoted 8 lines

ERROR:ASSIGN_IN_IF: do not use assignment in if condition #36: FILE: net/rose/rose_loopback.c:99:
+ if (rose_loopback_neigh->dev && (dev =
rose_dev_get(dest)) != NULL) {

total: 1 errors, 1 warnings, 0 checks, 8 lines checked

Thank you for your review will rectify these and send another version.

Thanks,
Anmol

author
#3 lore raw

PATCH v3 1/1 rose: Fix Null pointer dereference in rose_send_frame()

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurrence of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

---
Changes in v3:
- Corrected checkpatch warnings and errors (Suggested-by: Saeed Mahameed <[email protected]>)
- Added "Fixes:" tag (Suggested-by: Saeed Mahameed <[email protected]>)
Changes in v2:
- Added NULL check in rose_loopback_timer() (Suggested-by: Greg KH <[email protected]>)

 net/rose/rose_loopback.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rose/rose_loopback.c +2 -1
index 7b094275ea8b..2c51756ed7bf 100644
@@ -96,7 +96,8 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
+			dev = rose_dev_get(dest);
+			if (rose_loopback_neigh->dev && dev) {
 				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
 					kfree_skb(skb);
 			} else {

-
2.29.2

maintainer
#4 lore raw

net/rose/rose_loopback.c static void rose_loopback_timer(struct timer_list *unused) · line 99

Quoted 1 line · On Sun, 8 Nov 2020 00:48:35 +0530 Anmol Karn wrote:
net/rose/rose_loopback.c +1 -0
+			dev = rose_dev_get(dest);

this calls dev_hold internally, you never release that reference in case ..neigh->dev is NULL

Quoted 1 line
net/rose/rose_loopback.c +1 -0
+			if (rose_loopback_neigh->dev && dev) {
author
#5 lore raw

Hello Sir,

Quoted 7 lines · Jakub Kicinski

net/rose/rose_loopback.c static void rose_loopback_timer(struct timer_list *unused) · line 99

Quoted 1 line · On Sun, 8 Nov 2020 00:48:35 +0530 Anmol Karn wrote:
net/rose/rose_loopback.c +1 -0
+			dev = rose_dev_get(dest);

this calls dev_hold internally, you never release that reference in case ..neigh->dev is NULL

net/rose/rose_loopback.c static void rose_loopback_timer(struct timer_list *unused) · line 100

Quoted 1 line
net/rose/rose_loopback.c +1 -0
+			if (rose_loopback_neigh->dev && dev) {

Ah, I missed to dev_put() the dev after checking for, if neigh->dev is NULL, I will fix it soon and send another version.

Thank you for review.

Anmol

author
#6 lore raw

PATCH v4 1/1 rose: Fix Null pointer dereference in rose_send_frame()

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurrence of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

---
Changes in v4:
- Free dev(on dev_hold()), when neigh->dev is NULL. (Suggested-by: Jakub Kicinski <[email protected]>)
Changes in v3:
- Corrected checkpatch warnings and errors (Suggested-by: Saeed Mahameed <[email protected]>)
- Added "Fixes:" tag (Suggested-by: Saeed Mahameed <[email protected]>)
Changes in v2:
- Added NULL check in rose_loopback_timer() (Suggested-by: Greg KH <[email protected]>)

 net/rose/rose_loopback.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/rose/rose_loopback.c +3 -1
index 7b094275ea8b..6a71b6947d92 100644
@@ -96,10 +96,12 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
+			dev = rose_dev_get(dest);
+			if (rose_loopback_neigh->dev && dev) {
 				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
 					kfree_skb(skb);
 			} else {
+				dev_put(dev);
 				kfree_skb(skb);
 			}
 		} else {

--
2.29.2

maintainer
#7 lore raw
Quoted 30 lines · On Wed, 11 Nov 2020 22:29:54 +0530 Anmol Karn wrote:

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurrence of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

net/rose/rose_loopback.c +0 -0
index 7b094275ea8b..6a71b6947d92 100644
@@ -96,10 +96,12 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {

net/rose/rose_loopback.c static void rose_loopback_timer(struct timer_list *unused) · line 107

Quoted 10 lines
net/rose/rose_loopback.c +3 -1
-			if ((dev = rose_dev_get(dest)) != NULL) {
+			dev = rose_dev_get(dest);
+			if (rose_loopback_neigh->dev && dev) {
 				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
 					kfree_skb(skb);
 			} else {
+				dev_put(dev);
 				kfree_skb(skb);
 			}
 		} else {

This is still not correct. With this code dev_put() could be called with NULL, which would cause a crash.

There is also a dev_put() missing if rose_rx_call_request() returns 0.

I think that this is the correct code:

net/rose/rose_loopback.c +14 -4
index 7b094275ea8b..ff252ef73592 100644
@@ -96,11 +96,22 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
-				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
-					kfree_skb(skb);
-			} else {
+			if (!rose_loopback_neigh->dev) {
 				kfree_skb(skb);
+				continue;
+			}
+
+			dev = rose_dev_get(dest);
+			if (!dev) {
+				kfree_skb(skb);
+				continue;
+			}
+
+			if (rose_rx_call_request(skb, dev, rose_loopback_neigh,
+						 lci_o) == 0) {
+				dev_put(dev);
+				kfree_skb(skb);
 			}
 		} else {
 			kfree_skb(skb);

Please test this and resubmit it if it works.

author
#8 lore raw
Quoted 80 lines · Jakub Kicinski
Quoted 30 lines · On Wed, 11 Nov 2020 22:29:54 +0530 Anmol Karn wrote:

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurrence of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

net/rose/rose_loopback.c +0 -0
index 7b094275ea8b..6a71b6947d92 100644
@@ -96,10 +96,12 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {

net/rose/rose_loopback.c static void rose_loopback_timer(struct timer_list *unused) · line 107

Quoted 10 lines
net/rose/rose_loopback.c +3 -1
-			if ((dev = rose_dev_get(dest)) != NULL) {
+			dev = rose_dev_get(dest);
+			if (rose_loopback_neigh->dev && dev) {
 				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
 					kfree_skb(skb);
 			} else {
+				dev_put(dev);
 				kfree_skb(skb);
 			}
 		} else {

This is still not correct. With this code dev_put() could be called with NULL, which would cause a crash.

There is also a dev_put() missing if rose_rx_call_request() returns 0.

I think that this is the correct code:

net/rose/rose_loopback.c +14 -4
index 7b094275ea8b..ff252ef73592 100644
@@ -96,11 +96,22 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
-				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
-					kfree_skb(skb);
-			} else {
+			if (!rose_loopback_neigh->dev) {
 				kfree_skb(skb);
+				continue;
+			}
+
+			dev = rose_dev_get(dest);
+			if (!dev) {
+				kfree_skb(skb);
+				continue;
+			}
+
+			if (rose_rx_call_request(skb, dev, rose_loopback_neigh,
+						 lci_o) == 0) {
+				dev_put(dev);
+				kfree_skb(skb);
 			}
 		} else {
 			kfree_skb(skb);
Quoted 1 line

Please test this and resubmit it if it works.

Sure sir, I will test it and resend, if it works.

Thanks,
Anmol

author
#9 lore raw

PATCH v5 1/1 rose: Fix Null pointer dereference in rose_send_frame()

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurrence of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

---
Changes in v5:
- Free dev in rose_rx_call_request() and add NULL check for dev before freeing it. (Suggested-by: Jakub Kicinski <[email protected]>)
Changes in v4:
- Free dev(on dev_hold()), when neigh->dev is NULL. (Suggested-by: Jakub Kicinski <[email protected]>)
Changes in v3:
- Corrected checkpatch warnings and errors (Suggested-by: Saeed Mahameed <[email protected]>)
- Added "Fixes:" tag (Suggested-by: Saeed Mahameed <[email protected]>)
Changes in v2:
- Added NULL check in rose_loopback_timer() (Suggested-by: Greg KH <[email protected]>)

 net/rose/rose_loopback.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
net/rose/rose_loopback.c +13 -4
index 7b094275ea8b..11c45c8c6c16 100644
@@ -96,10 +96,19 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
-				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
-					kfree_skb(skb);
-			} else {
+			if (!rose_loopback_neigh->dev) {
+				kfree_skb(skb);
+				continue;
+			}
+
+			dev = rose_dev_get(dest);
+			if (!dev) {
+				kfree_skb(skb);
+				continue;
+			}
+
+			if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0) {
+				dev_put(dev);
 				kfree_skb(skb);
 			}
 		} else {

--
2.29.2

maintainer Applied
#10 lore raw
Quoted 16 lines · On Fri, 20 Nov 2020 00:40:43 +0530 Anmol Karn wrote:

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurrence of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

Quoted 6 lines

Applied to net, thanks!

author
#11 lore raw

PATCH v2 1/1 net: rose: Fix Null pointer dereference in rose_send_frame()

In reply to Anmol Karn

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurrence of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

---
Changes in v3:
- Corrected checkpatch warnings and errors (Suggested-by: Saeed Mahameed <[email protected]>)
- Added "Fixes:" tag (Suggested-by: Saeed Mahameed <[email protected]>)
Changes in v2:
- Added NULL check in rose_loopback_timer() (Suggested-by: Greg KH <[email protected]>)

 net/rose/rose_loopback.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rose/rose_loopback.c +2 -1
index 7b094275ea8b..2c51756ed7bf 100644
@@ -96,7 +96,8 @@ static void rose_loopback_timer(struct timer_list *unused)
 		}
 
 		if (frametype == ROSE_CALL_REQUEST) {
-			if ((dev = rose_dev_get(dest)) != NULL) {
+			dev = rose_dev_get(dest);
+			if (rose_loopback_neigh->dev && dev) {
 				if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
 					kfree_skb(skb);
 			} else {

-
2.29.2

author
#12 lore raw

Re: [Linux-kernel-mentees] [PATCH v2] net: rose: Fix Null pointer dereference in rose_send_frame()

Hello,

Sorry for this wrong subject(It should be v3 instead of v2), please ignore this patch.

I will send a new one with the corrected subject.

Thanks,
Anmol

Quoted 49 lines · Anmol Karn

rose_send_frame() dereferences neigh->dev when called from rose_transmit_clear_request(), and the first occurrence of the neigh is in rose_loopback_timer() as rose_loopback_neigh, and it is initialized in rose_add_loopback_neigh() as NULL. i.e when rose_loopback_neigh used in rose_loopback_timer() its ->dev was still NULL and rose_loopback_timer() was calling rose_rx_call_request() without checking for NULL.

- net/rose/rose_link.c
This bug seems to get triggered in this line:

rose_call = (ax25_address *)neigh->dev->dev_addr;

Fix it by adding NULL checking for rose_loopback_neigh->dev in rose_loopback_timer().

---
Changes in v3:
- Corrected checkpatch warnings and errors (Suggested-by: Saeed Mahameed <[email protected]>)
- Added "Fixes:" tag (Suggested-by: Saeed Mahameed <[email protected]>)
Changes in v2:
- Added NULL check in rose_loopback_timer() (Suggested-by: Greg KH <[email protected]>)

 net/rose/rose_loopback.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/rose/rose_loopback.c +2 -2
index 7b094275ea8b..2c51756ed7bf 100644
@@ -96,7 +96,8 @@ static void rose_loopback_timer(struct timer_list *unused)
                }
 
                if (frametype == ROSE_CALL_REQUEST) {
-                       if ((dev = rose_dev_get(dest)) != NULL) {
+                       dev = rose_dev_get(dest);
+                       if (rose_loopback_neigh->dev && dev) {
                                if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
                                        kfree_skb(skb);
                        } else {
-

2.29.2