Skip to contentSkip to conversation

staging: rtl8723bs: Check for NULL header value

PATCH Superseded Kees Cook 3 participants 6 messages Jan 13, 2022 – Jan 15, 2022 Staging

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 staging: rtl8723bs: Check for NULL header value

When building with -Warray-bounds, the following warning is emitted:

In file included from ./include/linux/string.h:253,

                 from ./arch/x86/include/asm/page_32.h:22,
                 from ./arch/x86/include/asm/page.h:14,
                 from ./arch/x86/include/asm/thread_info.h:12,
                 from ./include/linux/thread_info.h:60,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:78,
                 from ./include/linux/rcupdate.h:27,
                 from ./include/linux/rculist.h:11,
                 from ./include/linux/sched/signal.h:5,
                 from ./drivers/staging/rtl8723bs/include/drv_types.h:17,
                 from drivers/staging/rtl8723bs/core/rtw_recv.c:7:
In function 'memcpy',
    inlined from 'wlanhdr_to_ethhdr' at drivers/staging/rtl8723bs/core/rtw_recv.c:1554:2:
./include/linux/fortify-string.h:41:33: warning: '__builtin_memcpy' offset [0, 5] is out of the bounds [0, 0] [-Warray-bounds]
   41 | #define __underlying_memcpy     __builtin_memcpy
      |                                 ^

This is because the compiler sees it is possible for "ptr" to be a NULL value, and concludes that it has zero size and attempts to copy to it would overflow. Instead, detect the NULL return and error out early.

Cc Larry Finger <[email protected]>
Cc Phillip Potter <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Michael Straube <[email protected]>
Cc Fabio Aiuto <[email protected]>
---
 drivers/staging/rtl8723bs/core/rtw_recv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/staging/rtl8723bs/core/rtw_recv.c +3 -0
index 41bfca549c64..61135c49322b 100644
@@ -1513,6 +1513,9 @@ static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe)
 	u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
 
+	if (!ptr)
+		return _FAIL;
+
 	if (pattrib->encrypt)
 		recvframe_pull_tail(precvframe, pattrib->icv_len);
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 20 lines · Kees Cook

When building with -Warray-bounds, the following warning is emitted:

In file included from ./include/linux/string.h:253,

                 from ./arch/x86/include/asm/page_32.h:22,
                 from ./arch/x86/include/asm/page.h:14,
                 from ./arch/x86/include/asm/thread_info.h:12,
                 from ./include/linux/thread_info.h:60,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:78,
                 from ./include/linux/rcupdate.h:27,
                 from ./include/linux/rculist.h:11,
                 from ./include/linux/sched/signal.h:5,
                 from ./drivers/staging/rtl8723bs/include/drv_types.h:17,
                 from drivers/staging/rtl8723bs/core/rtw_recv.c:7:
In function 'memcpy',
    inlined from 'wlanhdr_to_ethhdr' at drivers/staging/rtl8723bs/core/rtw_recv.c:1554:2:
./include/linux/fortify-string.h:41:33: warning: '__builtin_memcpy' offset [0, 5] is out of the bounds [0, 0] [-Warray-bounds]
   41 | #define __underlying_memcpy     __builtin_memcpy
      |                                 ^
Quoted 2 lines

This is because the compiler sees it is possible for "ptr" to be a NULL value,

It's not really possible though. I bet it's just saying that because of the NULL check in get_recvframe_data(). Meanwhile we already have dereferenced "precvframe" so GCC should know that the NULL check can never be true.

Quoted 19 lines

and concludes that it has zero size and attempts to copy to it would overflow. Instead, detect the NULL return and error out early.

Cc Larry Finger <[email protected]>
Cc Phillip Potter <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Michael Straube <[email protected]>
Cc Fabio Aiuto <[email protected]>
---
 drivers/staging/rtl8723bs/core/rtw_recv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/staging/rtl8723bs/core/rtw_recv.c +0 -0
index 41bfca549c64..61135c49322b 100644

drivers/staging/rtl8723bs/core/rtw_recv.c static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe) · line 1518

Quoted 6 lines
drivers/staging/rtl8723bs/core/rtw_recv.c +3 -0
@@ -1513,6 +1513,9 @@ static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe)
 	u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
 
+	if (!ptr)
+		return _FAIL;
+

Can we put a comment to say /* Silence GCC false positive with -Warray-bounds */

We should have a standard comment so that we can grep for this ten years from now an remove any outdated work arounds.

regards,
dan carpenter

#2 lore raw
In reply to Kees Cook
Quoted 40 lines · Kees Cook

When building with -Warray-bounds, the following warning is emitted:

In file included from ./include/linux/string.h:253,

                 from ./arch/x86/include/asm/page_32.h:22,
                 from ./arch/x86/include/asm/page.h:14,
                 from ./arch/x86/include/asm/thread_info.h:12,
                 from ./include/linux/thread_info.h:60,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:78,
                 from ./include/linux/rcupdate.h:27,
                 from ./include/linux/rculist.h:11,
                 from ./include/linux/sched/signal.h:5,
                 from ./drivers/staging/rtl8723bs/include/drv_types.h:17,
                 from drivers/staging/rtl8723bs/core/rtw_recv.c:7:
In function 'memcpy',
    inlined from 'wlanhdr_to_ethhdr' at drivers/staging/rtl8723bs/core/rtw_recv.c:1554:2:
./include/linux/fortify-string.h:41:33: warning: '__builtin_memcpy' offset [0, 5] is out of the bounds [0, 0] [-Warray-bounds]
   41 | #define __underlying_memcpy     __builtin_memcpy
      |                                 ^

This is because the compiler sees it is possible for "ptr" to be a NULL value, and concludes that it has zero size and attempts to copy to it would overflow. Instead, detect the NULL return and error out early.

Cc Larry Finger <[email protected]>
Cc Phillip Potter <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Michael Straube <[email protected]>
Cc Fabio Aiuto <[email protected]>
---
 drivers/staging/rtl8723bs/core/rtw_recv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/staging/rtl8723bs/core/rtw_recv.c +0 -0
index 41bfca549c64..61135c49322b 100644

drivers/staging/rtl8723bs/core/rtw_recv.c static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe) · line 1517

Quoted 5 lines
drivers/staging/rtl8723bs/core/rtw_recv.c +2 -0
@@ -1513,6 +1513,9 @@ static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe)
 	u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
 
+	if (!ptr)
+		return _FAIL;

This will never happen, so let's not paper over compiler issues with stuff like this please.

As the call to get_recvframe_data() is only done in one place in this driver (in all drivers that look like this as well), it can just be replaced with the real code instead of the nonsensical test for NULL and then the compiler should be happy.

I'll gladly take that fix instead of this one, as that would be the correct solution here.

thanks,

greg k-h

author
#3 lore raw
Quoted 50 lines · Greg Kroah-Hartman
Quoted 40 lines · Kees Cook

When building with -Warray-bounds, the following warning is emitted:

In file included from ./include/linux/string.h:253,

                 from ./arch/x86/include/asm/page_32.h:22,
                 from ./arch/x86/include/asm/page.h:14,
                 from ./arch/x86/include/asm/thread_info.h:12,
                 from ./include/linux/thread_info.h:60,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:78,
                 from ./include/linux/rcupdate.h:27,
                 from ./include/linux/rculist.h:11,
                 from ./include/linux/sched/signal.h:5,
                 from ./drivers/staging/rtl8723bs/include/drv_types.h:17,
                 from drivers/staging/rtl8723bs/core/rtw_recv.c:7:
In function 'memcpy',
    inlined from 'wlanhdr_to_ethhdr' at drivers/staging/rtl8723bs/core/rtw_recv.c:1554:2:
./include/linux/fortify-string.h:41:33: warning: '__builtin_memcpy' offset [0, 5] is out of the bounds [0, 0] [-Warray-bounds]
   41 | #define __underlying_memcpy     __builtin_memcpy
      |                                 ^

This is because the compiler sees it is possible for "ptr" to be a NULL value, and concludes that it has zero size and attempts to copy to it would overflow. Instead, detect the NULL return and error out early.

Cc Larry Finger <[email protected]>
Cc Phillip Potter <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Michael Straube <[email protected]>
Cc Fabio Aiuto <[email protected]>
---
 drivers/staging/rtl8723bs/core/rtw_recv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/staging/rtl8723bs/core/rtw_recv.c +0 -0
index 41bfca549c64..61135c49322b 100644

drivers/staging/rtl8723bs/core/rtw_recv.c static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe) · line 1517

Quoted 5 lines
drivers/staging/rtl8723bs/core/rtw_recv.c +2 -0
@@ -1513,6 +1513,9 @@ static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe)
 	u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
 
+	if (!ptr)
+		return _FAIL;

This will never happen, so let's not paper over compiler issues with stuff like this please.

Quoted 7 lines

As the call to get_recvframe_data() is only done in one place in this driver (in all drivers that look like this as well), it can just be replaced with the real code instead of the nonsensical test for NULL and then the compiler should be happy.

I'll gladly take that fix instead of this one, as that would be the correct solution here.

I changed it around, but it doesn't help. I assume this is because we build with -fno-delete-null-pointer-checks, so the compiler continues to assume it's possible for the incoming argument to be NULL.

Should I rearrange this to do a NULL check for precvframe before all the assignments in addition to removing get_recvframe_data()?

-Kees

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 56 lines · Kees Cook
Quoted 55 lines · Greg Kroah-Hartman
Quoted 40 lines · Kees Cook

When building with -Warray-bounds, the following warning is emitted:

In file included from ./include/linux/string.h:253,

                 from ./arch/x86/include/asm/page_32.h:22,
                 from ./arch/x86/include/asm/page.h:14,
                 from ./arch/x86/include/asm/thread_info.h:12,
                 from ./include/linux/thread_info.h:60,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:78,
                 from ./include/linux/rcupdate.h:27,
                 from ./include/linux/rculist.h:11,
                 from ./include/linux/sched/signal.h:5,
                 from ./drivers/staging/rtl8723bs/include/drv_types.h:17,
                 from drivers/staging/rtl8723bs/core/rtw_recv.c:7:
In function 'memcpy',
    inlined from 'wlanhdr_to_ethhdr' at drivers/staging/rtl8723bs/core/rtw_recv.c:1554:2:
./include/linux/fortify-string.h:41:33: warning: '__builtin_memcpy' offset [0, 5] is out of the bounds [0, 0] [-Warray-bounds]
   41 | #define __underlying_memcpy     __builtin_memcpy
      |                                 ^

This is because the compiler sees it is possible for "ptr" to be a NULL value, and concludes that it has zero size and attempts to copy to it would overflow. Instead, detect the NULL return and error out early.

Cc Larry Finger <[email protected]>
Cc Phillip Potter <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Michael Straube <[email protected]>
Cc Fabio Aiuto <[email protected]>
---
 drivers/staging/rtl8723bs/core/rtw_recv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/staging/rtl8723bs/core/rtw_recv.c +0 -0
index 41bfca549c64..61135c49322b 100644

drivers/staging/rtl8723bs/core/rtw_recv.c static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe) · line 1517

Quoted 5 lines
drivers/staging/rtl8723bs/core/rtw_recv.c +2 -0
@@ -1513,6 +1513,9 @@ static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe)
 	u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
 
+	if (!ptr)
+		return _FAIL;

This will never happen, so let's not paper over compiler issues with stuff like this please.

As the call to get_recvframe_data() is only done in one place in this driver (in all drivers that look like this as well), it can just be replaced with the real code instead of the nonsensical test for NULL and then the compiler should be happy.

Quoted 6 lines
Quoted 2 lines

I'll gladly take that fix instead of this one, as that would be the correct solution here.

I changed it around, but it doesn't help. I assume this is because we build with -fno-delete-null-pointer-checks, so the compiler continues to assume it's possible for the incoming argument to be NULL.

That's a broken compiler then.

Quoted 2 lines

Should I rearrange this to do a NULL check for precvframe before all the assignments in addition to removing get_recvframe_data()?

If you walk the call-chain back, you will see that precvframe can't ever be NULL here (which is why this code doesn't crash), so don't check for something that is impossible to ever hit please just because the compiler is broken.

thanks,

greg k-h

author
#5 lore raw
Quoted 66 lines · Greg Kroah-Hartman
Quoted 56 lines · Kees Cook
Quoted 55 lines · Greg Kroah-Hartman
Quoted 40 lines · Kees Cook

When building with -Warray-bounds, the following warning is emitted:

In file included from ./include/linux/string.h:253,

                 from ./arch/x86/include/asm/page_32.h:22,
                 from ./arch/x86/include/asm/page.h:14,
                 from ./arch/x86/include/asm/thread_info.h:12,
                 from ./include/linux/thread_info.h:60,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:78,
                 from ./include/linux/rcupdate.h:27,
                 from ./include/linux/rculist.h:11,
                 from ./include/linux/sched/signal.h:5,
                 from ./drivers/staging/rtl8723bs/include/drv_types.h:17,
                 from drivers/staging/rtl8723bs/core/rtw_recv.c:7:
In function 'memcpy',
    inlined from 'wlanhdr_to_ethhdr' at drivers/staging/rtl8723bs/core/rtw_recv.c:1554:2:
./include/linux/fortify-string.h:41:33: warning: '__builtin_memcpy' offset [0, 5] is out of the bounds [0, 0] [-Warray-bounds]
   41 | #define __underlying_memcpy     __builtin_memcpy
      |                                 ^

This is because the compiler sees it is possible for "ptr" to be a NULL value, and concludes that it has zero size and attempts to copy to it would overflow. Instead, detect the NULL return and error out early.

Cc Larry Finger <[email protected]>
Cc Phillip Potter <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Michael Straube <[email protected]>
Cc Fabio Aiuto <[email protected]>
---
 drivers/staging/rtl8723bs/core/rtw_recv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/staging/rtl8723bs/core/rtw_recv.c +0 -0
index 41bfca549c64..61135c49322b 100644

drivers/staging/rtl8723bs/core/rtw_recv.c static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe) · line 1517

Quoted 5 lines
drivers/staging/rtl8723bs/core/rtw_recv.c +2 -0
@@ -1513,6 +1513,9 @@ static signed int wlanhdr_to_ethhdr(union recv_frame *precvframe)
 	u8 *ptr = get_recvframe_data(precvframe) ; /*  point to frame_ctrl field */
 	struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
 
+	if (!ptr)
+		return _FAIL;

This will never happen, so let's not paper over compiler issues with stuff like this please.

As the call to get_recvframe_data() is only done in one place in this driver (in all drivers that look like this as well), it can just be replaced with the real code instead of the nonsensical test for NULL and then the compiler should be happy.

Quoted 6 lines
Quoted 2 lines

I'll gladly take that fix instead of this one, as that would be the correct solution here.

I changed it around, but it doesn't help. I assume this is because we build with -fno-delete-null-pointer-checks, so the compiler continues to assume it's possible for the incoming argument to be NULL.

That's a broken compiler then.

Quoted 7 lines
Quoted 2 lines

Should I rearrange this to do a NULL check for precvframe before all the assignments in addition to removing get_recvframe_data()?

If you walk the call-chain back, you will see that precvframe can't ever be NULL here (which is why this code doesn't crash), so don't check for something that is impossible to ever hit please just because the compiler is broken.

I agree that the _original_ precvframe is always non-NULL, but the pointer landing at memcpy() gets updated along the way. It seems the new problem is that recvframe_pull() may return NULL and nothing is checking for that. Adding this silences the warning:

drivers/staging/r8188eu/core/rtw_recv.c +4 -0
index 49a02d6239d6..946e659ae97a 100644
@@ -1223,10 +1223,14 @@ static int wlanhdr_to_ethhdr(struct recv_frame *precvframe)
 		eth_type = 0x8712;
 		/*  append rx status for mp test packets */
 		ptr = recvframe_pull(precvframe, (rmv_len - sizeof(struct ethhdr) + 2) - 24);
+		if (!ptr)
+			return _FAIL;
 		memcpy(ptr, get_rxmem(precvframe), 24);
 		ptr += 24;
 	} else {
 		ptr = recvframe_pull(precvframe, (rmv_len - sizeof(struct ethhdr) + (bsnaphdr ? 2 : 0)));
+		if (!ptr)
+			return _FAIL;
 	}
 
 	memcpy(ptr, pattrib->dst, ETH_ALEN);
Quoted 4 lines

thanks,

greg k-h

Signature · 1 line
Kees Cook