diff mbox series

CDC-NCM: avoid overflow in sanity checking

Message ID 20220215103547.29599-1-oneukum@suse.com (mailing list archive)
State Accepted
Commit 8d2b1a1ec9f559d30b724877da4ce592edc41fdc
Delegated to: Netdev Maintainers
Headers show
Series CDC-NCM: avoid overflow in sanity checking | expand

Checks

Context Check Description
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 1 maintainers not CCed: oliver@neukum.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: Alignment should match open parenthesis CHECK: Unnecessary parentheses around 'len < ETH_HLEN' CHECK: Unnecessary parentheses around 'len > ctx->rx_max' CHECK: Unnecessary parentheses around 'offset > skb_in->len'
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/tree_selection success Guessing tree name failed - patch did not apply

Commit Message

Oliver Neukum Feb. 15, 2022, 10:35 a.m. UTC
A broken device may give an extreme offset like 0xFFF0
and a reasonable length for a fragment. In the sanity
check as formulated now, this will create an integer
overflow, defeating the sanity check. Both offset
and offset + len need to be checked in such a manner
that no overflow can occur.
And those quantities should be unsigned.

Signed-off-by: Oliver Neukum <oneukum@suse.com>
---
 drivers/net/usb/cdc_ncm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Greg Kroah-Hartman Feb. 15, 2022, 1:30 p.m. UTC | #1
On Tue, Feb 15, 2022 at 11:35:47AM +0100, Oliver Neukum wrote:
> A broken device may give an extreme offset like 0xFFF0
> and a reasonable length for a fragment. In the sanity
> check as formulated now, this will create an integer
> overflow, defeating the sanity check. Both offset
> and offset + len need to be checked in such a manner
> that no overflow can occur.
> And those quantities should be unsigned.
> 
> Signed-off-by: Oliver Neukum <oneukum@suse.com>
> ---
>  drivers/net/usb/cdc_ncm.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
> index e303b522efb5..15f91d691bba 100644
> --- a/drivers/net/usb/cdc_ncm.c
> +++ b/drivers/net/usb/cdc_ncm.c
> @@ -1715,10 +1715,10 @@ int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
>  {
>  	struct sk_buff *skb;
>  	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
> -	int len;
> +	unsigned int len;
>  	int nframes;
>  	int x;
> -	int offset;
> +	unsigned int offset;
>  	union {
>  		struct usb_cdc_ncm_ndp16 *ndp16;
>  		struct usb_cdc_ncm_ndp32 *ndp32;
> @@ -1790,8 +1790,8 @@ int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
>  			break;
>  		}
>  
> -		/* sanity checking */
> -		if (((offset + len) > skb_in->len) ||
> +		/* sanity checking - watch out for integer wrap*/
> +		if ((offset > skb_in->len) || (len > skb_in->len - offset) ||
>  				(len > ctx->rx_max) || (len < ETH_HLEN)) {
>  			netif_dbg(dev, rx_err, dev->net,
>  				  "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
> -- 
> 2.34.1
> 

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: stable <stable@vger.kernel.org>
patchwork-bot+netdevbpf@kernel.org Feb. 15, 2022, 3 p.m. UTC | #2
Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Tue, 15 Feb 2022 11:35:47 +0100 you wrote:
> A broken device may give an extreme offset like 0xFFF0
> and a reasonable length for a fragment. In the sanity
> check as formulated now, this will create an integer
> overflow, defeating the sanity check. Both offset
> and offset + len need to be checked in such a manner
> that no overflow can occur.
> And those quantities should be unsigned.
> 
> [...]

Here is the summary with links:
  - CDC-NCM: avoid overflow in sanity checking
    https://git.kernel.org/netdev/net/c/8d2b1a1ec9f5

You are awesome, thank you!
diff mbox series

Patch

diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
index e303b522efb5..15f91d691bba 100644
--- a/drivers/net/usb/cdc_ncm.c
+++ b/drivers/net/usb/cdc_ncm.c
@@ -1715,10 +1715,10 @@  int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
 {
 	struct sk_buff *skb;
 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
-	int len;
+	unsigned int len;
 	int nframes;
 	int x;
-	int offset;
+	unsigned int offset;
 	union {
 		struct usb_cdc_ncm_ndp16 *ndp16;
 		struct usb_cdc_ncm_ndp32 *ndp32;
@@ -1790,8 +1790,8 @@  int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
 			break;
 		}
 
-		/* sanity checking */
-		if (((offset + len) > skb_in->len) ||
+		/* sanity checking - watch out for integer wrap*/
+		if ((offset > skb_in->len) || (len > skb_in->len - offset) ||
 				(len > ctx->rx_max) || (len < ETH_HLEN)) {
 			netif_dbg(dev, rx_err, dev->net,
 				  "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",