Re: [PATCH] drivers/staging/bcm: Integer overflow

From: Dan Carpenter
Date: Fri Dec 20 2013 - 04:13:20 EST


On Fri, Dec 20, 2013 at 04:51:45PM +0800, Wenliang Fan wrote:
> Thanks for your advice.
> But the variable 'psFlash2xReadWrite->offset' in '
> *drivers/staging/bcm/nvm.c*:validateFlash2xReadWrite()' is also comes from
> user space, which would cause an integer overflow in the following line:
>
> if ((uiSectStartOffset + psFlash2xReadWrite->offset + uiNumOfBytes) <=
> uiSectEndOffset)
>
> in '*drivers/staging/bcm/**nvm.c*: validateFlash2xReadWrite()',
> and another integer overflow in the following line:
>
>
> ReadOffset = ReadOffset + ReadBytes; (or WriteOffset = WriteOffset +
> WriteBytes;)
>
> in '*drivers/staging/bcm/**Bcmchar.c*: bcm_char_ioctl()'.
>

Alright, fine. But the new check is messy. Do it like this:

/* these are user controlled and can lead to integer overflows */
if (psFlash2xReadWrite->offset > uiSectEndOffset)
return false;
if (uiNumOfBytes > uiSectEndOffset)
return false;
if (uiSectStartOffset + psFlash2xReadWrite->offset + uiNumOfBytes > uiSectEndOffset)
return false;

return true;

That way each step is simpler to understand. People are too fond of
compound conditions... *grumble*.

regards,
dan carpenter

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/