Skip to contentSkip to conversation

AX.25: Fix out-of-bounds read in ax25_connect()

PATCH In review Peilin Ye 4 participants 10 messages Jul 22, 2020 – Jul 23, 2020

Subsystems (2)
lore
Share & tools
lore

Peilin Ye

author
#0 lore raw

PATCH 1/1 AX.25: Fix out-of-bounds read in ax25_connect()

Checks on addr_len and fsa->fsa_ax25.sax25_ndigis are insufficient. ax25_connect() can go out of bounds when fsa->fsa_ax25.sax25_ndigis equals to 7 or 8. Fix it.

This issue has been reported as a KMSAN uninit-value bug, because in such a case, ax25_connect() reaches into the uninitialized portion of the struct sockaddr_storage statically allocated in __sys_connect().

It is safe to remove fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS because addr_len is guaranteed to be less than or equal to sizeof(struct full_sockaddr_ax25).

---
 net/ax25/af_ax25.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ax25/af_ax25.c +3 -1
index fd91cd34f25e..ef5bf116157a 100644
@@ -1187,7 +1187,9 @@ static int __must_check ax25_connect(struct socket *sock,
 	if (addr_len > sizeof(struct sockaddr_ax25) &&
 	    fsa->fsa_ax25.sax25_ndigis != 0) {
 		/* Valid number of digipeaters ? */
-		if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS) {
+		if (fsa->fsa_ax25.sax25_ndigis < 1 ||
+		    addr_len < sizeof(struct sockaddr_ax25) +
+		    sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis) {
 			err = -EINVAL;
 			goto out_release;
 		}
Signature · 1 line
2.25.1
maintainer
#1 lore raw

From: Peilin Ye <[email protected]>
Date: Wed, 22 Jul 2020 11:19:01 -0400

Quoted 8 lines

Checks on addr_len and fsa->fsa_ax25.sax25_ndigis are insufficient. ax25_connect() can go out of bounds when fsa->fsa_ax25.sax25_ndigis equals to 7 or 8. Fix it.

This issue has been reported as a KMSAN uninit-value bug, because in such a case, ax25_connect() reaches into the uninitialized portion of the struct sockaddr_storage statically allocated in __sys_connect().

Quoted 7 lines

It is safe to remove fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS because addr_len is guaranteed to be less than or equal to sizeof(struct full_sockaddr_ax25).

Applied and queued up for -stable, thanks.

#2 lore raw

PATCH 1/1 AX.25: Prevent integer overflows in connect and sendmsg

We recently added some bounds checking in ax25_connect() and ax25_sendmsg() and we so we removed the AX25_MAX_DIGIS checks because they were no longer required.

Unfortunately, I believe they are required to prevent integer overflows so I have added them back.

Fixes 8885bb0621f0 ("AX.25: Prevent out-of-bounds read in ax25_sendmsg()")
Fixes 2f2a7ffad5c6 ("AX.25: Fix out-of-bounds read in ax25_connect()")

---
From code review. Not tested. It should be harmless though.

 net/ax25/af_ax25.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/ax25/af_ax25.c +4 -1
index 0862fe49d434..dec3f35467c9 100644
@@ -1188,6 +1188,7 @@ static int __must_check ax25_connect(struct socket *sock,
 	    fsa->fsa_ax25.sax25_ndigis != 0) {
 		/* Valid number of digipeaters ? */
 		if (fsa->fsa_ax25.sax25_ndigis < 1 ||
+		    fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS ||
 		    addr_len < sizeof(struct sockaddr_ax25) +
 		    sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis) {
 			err = -EINVAL;
@@ -1509,7 +1510,9 @@ static int ax25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 			struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)usax;
 
 			/* Valid number of digipeaters ? */
-			if (usax->sax25_ndigis < 1 || addr_len < sizeof(struct sockaddr_ax25) +
+			if (usax->sax25_ndigis < 1 ||
+			    usax->sax25_ndigis > AX25_MAX_DIGIS ||
+			    addr_len < sizeof(struct sockaddr_ax25) +
 			    sizeof(ax25_address) * usax->sax25_ndigis) {
 				err = -EINVAL;
 				goto out;
Signature · 1 line
2.27.0
maintainer Applied
#3 lore raw

Re: [PATCH net] AX.25: Prevent integer overflows in connect and sendmsg

From: Dan Carpenter <[email protected]>
Date: Thu, 23 Jul 2020 17:49:57 +0300

Quoted 10 lines

We recently added some bounds checking in ax25_connect() and ax25_sendmsg() and we so we removed the AX25_MAX_DIGIS checks because they were no longer required.

Unfortunately, I believe they are required to prevent integer overflows so I have added them back.

Fixes 8885bb0621f0 ("AX.25: Prevent out-of-bounds read in ax25_sendmsg()")
Fixes 2f2a7ffad5c6 ("AX.25: Fix out-of-bounds read in ax25_connect()")

Applied, thanks Dan.

author
#4 lore raw
In reply to David Miller
Quoted 15 lines · David Miller

From: Peilin Ye <[email protected]>
Date: Wed, 22 Jul 2020 11:19:01 -0400

Quoted 12 lines

Checks on addr_len and fsa->fsa_ax25.sax25_ndigis are insufficient. ax25_connect() can go out of bounds when fsa->fsa_ax25.sax25_ndigis equals to 7 or 8. Fix it.

This issue has been reported as a KMSAN uninit-value bug, because in such a case, ax25_connect() reaches into the uninitialized portion of the struct sockaddr_storage statically allocated in __sys_connect().

It is safe to remove fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS because addr_len is guaranteed to be less than or equal to sizeof(struct full_sockaddr_ax25).

Quoted 5 lines
Quoted 3 lines

Applied and queued up for -stable, thanks.

Thank you for reviewing my patch!

Peilin Ye

#5 lore raw
In reply to Peilin Ye
Quoted 24 lines · Peilin Ye

Checks on addr_len and fsa->fsa_ax25.sax25_ndigis are insufficient. ax25_connect() can go out of bounds when fsa->fsa_ax25.sax25_ndigis equals to 7 or 8. Fix it.

This issue has been reported as a KMSAN uninit-value bug, because in such a case, ax25_connect() reaches into the uninitialized portion of the struct sockaddr_storage statically allocated in __sys_connect().

It is safe to remove fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS because addr_len is guaranteed to be less than or equal to sizeof(struct full_sockaddr_ax25).

---
 net/ax25/af_ax25.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ax25/af_ax25.c +0 -0
index fd91cd34f25e..ef5bf116157a 100644

net/ax25/af_ax25.c static int __must_check ax25_connect(struct socket *sock, · line 1192

Quoted 7 lines
net/ax25/af_ax25.c +3 -1
@@ -1187,7 +1187,9 @@ static int __must_check ax25_connect(struct socket *sock,
 	if (addr_len > sizeof(struct sockaddr_ax25) &&
 	    fsa->fsa_ax25.sax25_ndigis != 0) {
 		/* Valid number of digipeaters ? */
-		if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS) {
+		if (fsa->fsa_ax25.sax25_ndigis < 1 ||
+		    addr_len < sizeof(struct sockaddr_ax25) +
+		    sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis) {

The "sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis" can have an integer overflow so you still need the "fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS" check.

regards,
dan carpenter

author
#6 lore raw
Quoted 20 lines · Dan Carpenter
Quoted 19 lines · Peilin Ye

Checks on addr_len and fsa->fsa_ax25.sax25_ndigis are insufficient. ax25_connect() can go out of bounds when fsa->fsa_ax25.sax25_ndigis equals to 7 or 8. Fix it.

This issue has been reported as a KMSAN uninit-value bug, because in such a case, ax25_connect() reaches into the uninitialized portion of the struct sockaddr_storage statically allocated in __sys_connect().

It is safe to remove fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS because addr_len is guaranteed to be less than or equal to sizeof(struct full_sockaddr_ax25).

---
 net/ax25/af_ax25.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 16 lines

net/ax25/af_ax25.c static int __must_check ax25_connect(struct socket *sock, · line 1192

Quoted 12 lines
net/ax25/af_ax25.c +3 -1
index fd91cd34f25e..ef5bf116157a 100644
@@ -1187,7 +1187,9 @@ static int __must_check ax25_connect(struct socket *sock,
 	if (addr_len > sizeof(struct sockaddr_ax25) &&
 	    fsa->fsa_ax25.sax25_ndigis != 0) {
 		/* Valid number of digipeaters ? */
-		if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS) {
+		if (fsa->fsa_ax25.sax25_ndigis < 1 ||
+		    addr_len < sizeof(struct sockaddr_ax25) +
+		    sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis) {

The "sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis" can have an integer overflow so you still need the "fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS" check.

Thank you for fixing this up! I did some math but I didn't think of that. Will be more careful when removing things.

Peilin Ye

#7 lore raw
Quoted 38 lines · Peilin Ye
Quoted 20 lines · Dan Carpenter
Quoted 19 lines · Peilin Ye

Checks on addr_len and fsa->fsa_ax25.sax25_ndigis are insufficient. ax25_connect() can go out of bounds when fsa->fsa_ax25.sax25_ndigis equals to 7 or 8. Fix it.

This issue has been reported as a KMSAN uninit-value bug, because in such a case, ax25_connect() reaches into the uninitialized portion of the struct sockaddr_storage statically allocated in __sys_connect().

It is safe to remove fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS because addr_len is guaranteed to be less than or equal to sizeof(struct full_sockaddr_ax25).

---
 net/ax25/af_ax25.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 16 lines

net/ax25/af_ax25.c static int __must_check ax25_connect(struct socket *sock, · line 1192

Quoted 12 lines
net/ax25/af_ax25.c +3 -1
index fd91cd34f25e..ef5bf116157a 100644
@@ -1187,7 +1187,9 @@ static int __must_check ax25_connect(struct socket *sock,
 	if (addr_len > sizeof(struct sockaddr_ax25) &&
 	    fsa->fsa_ax25.sax25_ndigis != 0) {
 		/* Valid number of digipeaters ? */
-		if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS) {
+		if (fsa->fsa_ax25.sax25_ndigis < 1 ||
+		    addr_len < sizeof(struct sockaddr_ax25) +
+		    sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis) {

The "sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis" can have an integer overflow so you still need the "fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS" check.

Quoted 2 lines

Thank you for fixing this up! I did some math but I didn't think of that. Will be more careful when removing things.

No problem. You had the right approach to look for ways to clean things up.

Your patches make me happy because you're trying to fix important bugs.

regards,
dan carpenter

author
#8 lore raw
Quoted 39 lines · Dan Carpenter
Quoted 38 lines · Peilin Ye
Quoted 36 lines · Dan Carpenter
Quoted 24 lines · Peilin Ye

Checks on addr_len and fsa->fsa_ax25.sax25_ndigis are insufficient. ax25_connect() can go out of bounds when fsa->fsa_ax25.sax25_ndigis equals to 7 or 8. Fix it.

This issue has been reported as a KMSAN uninit-value bug, because in such a case, ax25_connect() reaches into the uninitialized portion of the struct sockaddr_storage statically allocated in __sys_connect().

It is safe to remove fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS because addr_len is guaranteed to be less than or equal to sizeof(struct full_sockaddr_ax25).

---
 net/ax25/af_ax25.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ax25/af_ax25.c +0 -0
index fd91cd34f25e..ef5bf116157a 100644

net/ax25/af_ax25.c static int __must_check ax25_connect(struct socket *sock, · line 1192

Quoted 7 lines
net/ax25/af_ax25.c +3 -1
@@ -1187,7 +1187,9 @@ static int __must_check ax25_connect(struct socket *sock,
 	if (addr_len > sizeof(struct sockaddr_ax25) &&
 	    fsa->fsa_ax25.sax25_ndigis != 0) {
 		/* Valid number of digipeaters ? */
-		if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS) {
+		if (fsa->fsa_ax25.sax25_ndigis < 1 ||
+		    addr_len < sizeof(struct sockaddr_ax25) +
+		    sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis) {

The "sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis" can have an integer overflow so you still need the "fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS" check.

Quoted 7 lines
Quoted 2 lines

Thank you for fixing this up! I did some math but I didn't think of that. Will be more careful when removing things.

No problem. You had the right approach to look for ways to clean things up.

Your patches make me happy because you're trying to fix important bugs.

It is very encouraging to hear that! I will try to do what I can do.

Thank you,
Peilin Ye

#9 lore raw
In reply to Dan Carpenter
Quoted 39 lines · Dan Carpenter
Quoted 36 lines · Peilin Ye
Quoted 20 lines · Dan Carpenter
Quoted 19 lines · Peilin Ye

Checks on addr_len and fsa->fsa_ax25.sax25_ndigis are insufficient. ax25_connect() can go out of bounds when fsa->fsa_ax25.sax25_ndigis equals to 7 or 8. Fix it.

This issue has been reported as a KMSAN uninit-value bug, because in such a case, ax25_connect() reaches into the uninitialized portion of the struct sockaddr_storage statically allocated in __sys_connect().

It is safe to remove fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS because addr_len is guaranteed to be less than or equal to sizeof(struct full_sockaddr_ax25).

---
  net/ax25/af_ax25.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

net/ax25/af_ax25.c static int __must_check ax25_connect(struct socket *sock, · line 1192

Quoted 12 lines
net/ax25/af_ax25.c +3 -1
index fd91cd34f25e..ef5bf116157a 100644
@@ -1187,7 +1187,9 @@ static int __must_check ax25_connect(struct socket *sock,
  	if (addr_len > sizeof(struct sockaddr_ax25) &&
  	    fsa->fsa_ax25.sax25_ndigis != 0) {
  		/* Valid number of digipeaters ? */
-		if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS) {
+		if (fsa->fsa_ax25.sax25_ndigis < 1 ||
+		    addr_len < sizeof(struct sockaddr_ax25) +
+		    sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis) {

The "sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis" can have an integer overflow so you still need the "fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS" check.

Quoted 2 lines

Thank you for fixing this up! I did some math but I didn't think of that. Will be more careful when removing things.

Quoted 7 lines

No problem. You had the right approach to look for ways to clean things up.

Your patches make me happy because you're trying to fix important bugs.

regards,
dan carpenter

As a long-term user (25 years) of kernel ax25 I appreciate any and all efforts to improve the code (which I mostly don't understand), and I applaud those individuals rising to the task.

Thanks guys (and gals).

Ray vk2tv