linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()
@ 2018-04-19 12:29 Alexander Popov
  2018-04-19 13:49 ` Uwe Kleine-König
  2018-04-27 12:06 ` Wolfram Sang
  0 siblings, 2 replies; 7+ messages in thread
From: Alexander Popov @ 2018-04-19 12:29 UTC (permalink / raw)
  To: Wolfram Sang, Uwe Kleine-König, linux-i2c, linux-kernel,
	sil2review, Dmitry Vyukov, syzkaller, Alexander Popov

i2cdev_ioctl_rdwr() allocates i2c_msg.buf using memdup_user(), which
returns ZERO_SIZE_PTR if i2c_msg.len is zero.

Currently i2cdev_ioctl_rdwr() always dereferences the buf pointer in case
of I2C_M_RD | I2C_M_RECV_LEN transfer. That causes a kernel oops in
case of zero len.

Let's check the len against zero before dereferencing buf pointer.

This issue was triggered by syzkaller.

Signed-off-by: Alexander Popov <alex.popov@linux.com>
---
 drivers/i2c/i2c-dev.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index 036a03f..5790bc8 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -280,6 +280,7 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
 		 */
 		if (msgs[i].flags & I2C_M_RECV_LEN) {
 			if (!(msgs[i].flags & I2C_M_RD) ||
+			    !msgs[i].len ||
 			    msgs[i].buf[0] < 1 ||
 			    msgs[i].len < msgs[i].buf[0] +
 					     I2C_SMBUS_BLOCK_MAX) {
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()
  2018-04-19 12:29 [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr() Alexander Popov
@ 2018-04-19 13:49 ` Uwe Kleine-König
  2018-04-19 17:01   ` Alexander Popov
  2018-04-23 20:18   ` Alexander Popov
  2018-04-27 12:06 ` Wolfram Sang
  1 sibling, 2 replies; 7+ messages in thread
From: Uwe Kleine-König @ 2018-04-19 13:49 UTC (permalink / raw)
  To: Alexander Popov
  Cc: Wolfram Sang, linux-i2c, linux-kernel, sil2review, Dmitry Vyukov,
	syzkaller

On Thu, Apr 19, 2018 at 03:29:22PM +0300, Alexander Popov wrote:
> i2cdev_ioctl_rdwr() allocates i2c_msg.buf using memdup_user(), which
> returns ZERO_SIZE_PTR if i2c_msg.len is zero.
> 
> Currently i2cdev_ioctl_rdwr() always dereferences the buf pointer in case
> of I2C_M_RD | I2C_M_RECV_LEN transfer. That causes a kernel oops in
> case of zero len.
> 
> Let's check the len against zero before dereferencing buf pointer.
> 
> This issue was triggered by syzkaller.
> 
> Signed-off-by: Alexander Popov <alex.popov@linux.com>
> ---
>  drivers/i2c/i2c-dev.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
> index 036a03f..5790bc8 100644
> --- a/drivers/i2c/i2c-dev.c
> +++ b/drivers/i2c/i2c-dev.c
> @@ -280,6 +280,7 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
>  		 */
>  		if (msgs[i].flags & I2C_M_RECV_LEN) {
>  			if (!(msgs[i].flags & I2C_M_RD) ||
> +			    !msgs[i].len ||

I'd prefer

			msgs[i].len > 0

here instead of

			!msgs[i].len

because I can parse that more easily. Semantically the patch is fine and
can have my

Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

>  			    msgs[i].buf[0] < 1 ||
>  			    msgs[i].len < msgs[i].buf[0] +
>  					     I2C_SMBUS_BLOCK_MAX) {

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()
  2018-04-19 13:49 ` Uwe Kleine-König
@ 2018-04-19 17:01   ` Alexander Popov
  2018-04-20  5:58     ` Uwe Kleine-König
  2018-04-23 20:18   ` Alexander Popov
  1 sibling, 1 reply; 7+ messages in thread
From: Alexander Popov @ 2018-04-19 17:01 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Wolfram Sang, linux-i2c, linux-kernel, sil2review, Dmitry Vyukov,
	syzkaller

On 19.04.2018 16:49, Uwe Kleine-König wrote:
> On Thu, Apr 19, 2018 at 03:29:22PM +0300, Alexander Popov wrote:
>> i2cdev_ioctl_rdwr() allocates i2c_msg.buf using memdup_user(), which
>> returns ZERO_SIZE_PTR if i2c_msg.len is zero.
>>
>> Currently i2cdev_ioctl_rdwr() always dereferences the buf pointer in case
>> of I2C_M_RD | I2C_M_RECV_LEN transfer. That causes a kernel oops in
>> case of zero len.
>>
>> Let's check the len against zero before dereferencing buf pointer.
>>
>> This issue was triggered by syzkaller.
>>
>> Signed-off-by: Alexander Popov <alex.popov@linux.com>
>> ---
>>  drivers/i2c/i2c-dev.c | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
>> index 036a03f..5790bc8 100644
>> --- a/drivers/i2c/i2c-dev.c
>> +++ b/drivers/i2c/i2c-dev.c
>> @@ -280,6 +280,7 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
>>  		 */
>>  		if (msgs[i].flags & I2C_M_RECV_LEN) {
>>  			if (!(msgs[i].flags & I2C_M_RD) ||
>> +			    !msgs[i].len ||
> 
> I'd prefer
> 
> 			msgs[i].len > 0

Excuse me, it will be wrong. We stop if len is 0 to avoid the following
ZERO_SIZE_PTR dereference.

> here instead of
> 
> 			!msgs[i].len

I can change it to "msgs[i].len == 0". But is it really important?

I've carefully tested the current version with the original repro. It works correct.

> because I can parse that more easily. Semantically the patch is fine and
> can have my
> 
> Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> 
>>  			    msgs[i].buf[0] < 1 ||
>>  			    msgs[i].len < msgs[i].buf[0] +
>>  					     I2C_SMBUS_BLOCK_MAX) {

Best regards,
Alexander

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()
  2018-04-19 17:01   ` Alexander Popov
@ 2018-04-20  5:58     ` Uwe Kleine-König
  0 siblings, 0 replies; 7+ messages in thread
From: Uwe Kleine-König @ 2018-04-20  5:58 UTC (permalink / raw)
  To: Alexander Popov
  Cc: Wolfram Sang, linux-i2c, linux-kernel, sil2review, Dmitry Vyukov,
	syzkaller

Hello,

On Thu, Apr 19, 2018 at 08:01:46PM +0300, Alexander Popov wrote:
> On 19.04.2018 16:49, Uwe Kleine-König wrote:
> >> @@ -280,6 +280,7 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
> >>  		 */
> >>  		if (msgs[i].flags & I2C_M_RECV_LEN) {
> >>  			if (!(msgs[i].flags & I2C_M_RD) ||
> >> +			    !msgs[i].len ||
> > 
> > I'd prefer
> > 
> > 			msgs[i].len > 0
> 
> Excuse me, it will be wrong. We stop if len is 0 to avoid the following
> ZERO_SIZE_PTR dereference.

right you are. I missed the negation.
 
> > here instead of
> > 
> > 			!msgs[i].len
> 
> I can change it to "msgs[i].len == 0". But is it really important?
> 
> I've carefully tested the current version with the original repro. It works correct.

I don't doubt it, and the code generated is maybe even the same. The
point I wanted to make is that

	!len

is harder to read for a human than

	len < 1

(or another suitable arithmetic expression). But feel free to disagree
and keep the code as is.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()
  2018-04-19 13:49 ` Uwe Kleine-König
  2018-04-19 17:01   ` Alexander Popov
@ 2018-04-23 20:18   ` Alexander Popov
  1 sibling, 0 replies; 7+ messages in thread
From: Alexander Popov @ 2018-04-23 20:18 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Wolfram Sang, linux-i2c, linux-kernel, sil2review, Dmitry Vyukov,
	syzkaller

On 19.04.2018 16:49, Uwe Kleine-König wrote:
> On Thu, Apr 19, 2018 at 03:29:22PM +0300, Alexander Popov wrote:
>> i2cdev_ioctl_rdwr() allocates i2c_msg.buf using memdup_user(), which
>> returns ZERO_SIZE_PTR if i2c_msg.len is zero.
>>
>> Currently i2cdev_ioctl_rdwr() always dereferences the buf pointer in case
>> of I2C_M_RD | I2C_M_RECV_LEN transfer. That causes a kernel oops in
>> case of zero len.
>>
>> Let's check the len against zero before dereferencing buf pointer.
>>
>> This issue was triggered by syzkaller.
>>
>> Signed-off-by: Alexander Popov <alex.popov@linux.com>
>> ---
>>  drivers/i2c/i2c-dev.c | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
>> index 036a03f..5790bc8 100644
>> --- a/drivers/i2c/i2c-dev.c
>> +++ b/drivers/i2c/i2c-dev.c
>> @@ -280,6 +280,7 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
>>  		 */
>>  		if (msgs[i].flags & I2C_M_RECV_LEN) {
>>  			if (!(msgs[i].flags & I2C_M_RD) ||
>> +			    !msgs[i].len ||
> 
> I'd prefer
> 
> 			msgs[i].len > 0
> 
> here instead of
> 
> 			!msgs[i].len
> 
> because I can parse that more easily. Semantically the patch is fine and
> can have my
> 
> Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Friendly ping!

Wolfram, would you like to take this patch?

Best regards,
Alexander

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()
  2018-04-19 12:29 [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr() Alexander Popov
  2018-04-19 13:49 ` Uwe Kleine-König
@ 2018-04-27 12:06 ` Wolfram Sang
  2018-04-30  7:13   ` Uwe Kleine-König
  1 sibling, 1 reply; 7+ messages in thread
From: Wolfram Sang @ 2018-04-27 12:06 UTC (permalink / raw)
  To: Alexander Popov
  Cc: Uwe Kleine-König, linux-i2c, linux-kernel, sil2review,
	Dmitry Vyukov, syzkaller

[-- Attachment #1: Type: text/plain, Size: 765 bytes --]

On Thu, Apr 19, 2018 at 03:29:22PM +0300, Alexander Popov wrote:
> i2cdev_ioctl_rdwr() allocates i2c_msg.buf using memdup_user(), which
> returns ZERO_SIZE_PTR if i2c_msg.len is zero.
> 
> Currently i2cdev_ioctl_rdwr() always dereferences the buf pointer in case
> of I2C_M_RD | I2C_M_RECV_LEN transfer. That causes a kernel oops in
> case of zero len.
> 
> Let's check the len against zero before dereferencing buf pointer.
> 
> This issue was triggered by syzkaller.
> 
> Signed-off-by: Alexander Popov <alex.popov@linux.com>

Applied to for-current with the arithmetic expression changed to '< 1'
to keep in sync with the previous one. Will push out soon, so you can
double check if you are interested.

Thanks for the debugging, Alexander!


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()
  2018-04-27 12:06 ` Wolfram Sang
@ 2018-04-30  7:13   ` Uwe Kleine-König
  0 siblings, 0 replies; 7+ messages in thread
From: Uwe Kleine-König @ 2018-04-30  7:13 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Alexander Popov, linux-i2c, linux-kernel, sil2review,
	Dmitry Vyukov, syzkaller

On Fri, Apr 27, 2018 at 02:06:58PM +0200, Wolfram Sang wrote:
> On Thu, Apr 19, 2018 at 03:29:22PM +0300, Alexander Popov wrote:
> > i2cdev_ioctl_rdwr() allocates i2c_msg.buf using memdup_user(), which
> > returns ZERO_SIZE_PTR if i2c_msg.len is zero.
> > 
> > Currently i2cdev_ioctl_rdwr() always dereferences the buf pointer in case
> > of I2C_M_RD | I2C_M_RECV_LEN transfer. That causes a kernel oops in
> > case of zero len.
> > 
> > Let's check the len against zero before dereferencing buf pointer.
> > 
> > This issue was triggered by syzkaller.
> > 
> > Signed-off-by: Alexander Popov <alex.popov@linux.com>
> 
> Applied to for-current with the arithmetic expression changed to '< 1'
> to keep in sync with the previous one. Will push out soon, so you can
> double check if you are interested.

Thanks, I like it. An added bonus is also that you don't need to think
about the type of msgs[i].len and what happens if it is negative.

Best regards
Uwe


-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-04-30  7:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-19 12:29 [v2 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr() Alexander Popov
2018-04-19 13:49 ` Uwe Kleine-König
2018-04-19 17:01   ` Alexander Popov
2018-04-20  5:58     ` Uwe Kleine-König
2018-04-23 20:18   ` Alexander Popov
2018-04-27 12:06 ` Wolfram Sang
2018-04-30  7:13   ` Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).