stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 4.14 0/1] Fix NULL dereference in i2cdev_ioctl_rdwr()
@ 2022-10-03  8:43 Harshit Mogalapalli
  2022-10-03  8:43 ` [PATCH 4.14 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref " Harshit Mogalapalli
  0 siblings, 1 reply; 2+ messages in thread
From: Harshit Mogalapalli @ 2022-10-03  8:43 UTC (permalink / raw)
  Cc: harshit.m.mogalapalli, george.kennedy, darren.kenny,
	vegard.nossum, stable, Wolfram Sang, linux-i2c, linux-kernel

This backport patch addresses a NULL pointer dereference bug in 4.14.y

BUG: unable to handle kernel NULL pointer dereference at 0000000000000010
IP: i2cdev_ioctl_rdwr.isra.2+0xe4/0x360
PGD 13af50067 P4D 13af50067 PUD 13504c067 PMD 0
Oops: 0000 [#1] PREEMPT SMP
Dumping ftrace buffer:
  (ftrace buffer empty)
Modules linked in:
CPU: 1 PID: 17421 Comm: rep Not tainted 4.14.295 #7
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-2.el7 04/01/2014
task: ffff88807c43a080 task.stack: ffffc90000d0c000
RIP: 0010:i2cdev_ioctl_rdwr.isra.2+0xe4/0x360
RSP: 0018:ffffc90000d0fdf0 EFLAGS: 00010297
RAX: ffff88807c43a080 RBX: 0000000000000000 RCX: 0000000000000000
....
Call Trace:
  i2cdev_ioctl+0x1a5/0x2a0
  ? i2cdev_ioctl_rdwr.isra.2+0x360/0x360
  do_vfs_ioctl+0xac/0x840
  ? syscall_trace_enter+0x159/0x4a0
  SyS_ioctl+0x7e/0xb0
  do_syscall_64+0x8d/0x220
....
 RIP: i2cdev_ioctl_rdwr.isra.2+0xe4/0x360 RSP: ffffc90000d0fdf0
....
 Kernel panic - not syncing: Fatal exception
 Rebooting in 86400 seconds..

rdwr_pa[i].buf[0] is a NULL dereference when len=0, so to avoid
dereferencing zero-length buffer we add a check on len before
dereferencing.

I have tested only with the reproducer and the bug doesnot occur
after this patch.

This patch is only made for 4.14.y as other higher LTS branches
(>=4.19.y) already have the fix.

Thanks,
Harshit

Alexander Popov (1):
  i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()

 drivers/i2c/i2c-dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

-- 
2.37.1


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

* [PATCH 4.14 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr()
  2022-10-03  8:43 [PATCH 4.14 0/1] Fix NULL dereference in i2cdev_ioctl_rdwr() Harshit Mogalapalli
@ 2022-10-03  8:43 ` Harshit Mogalapalli
  0 siblings, 0 replies; 2+ messages in thread
From: Harshit Mogalapalli @ 2022-10-03  8:43 UTC (permalink / raw)
  Cc: harshit.m.mogalapalli, george.kennedy, darren.kenny,
	vegard.nossum, stable, Alexander Popov, Uwe Kleine-König,
	Wolfram Sang, linux-i2c, linux-kernel

From: Alexander Popov <alex.popov@linux.com>

commit 23a27722b5292ef0b27403c87a109feea8296a5c upstream.

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>
Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
[wsa: use '< 1' instead of '!' for easier readability]
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
[Harshit: backport to 4.14.y, use rdwr_pa[i].len instead of msgs[i].len
as the 4.14.y  code uses rdwr_pa.]
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
 Conflicts:
	drivers/i2c/i2c-dev.c - use rdwr_pa[i].len instead of
	msgs[i].len
Since a NULL pointer dereference happens on 4.14.y this backport patch
will fix the issue.
---
 drivers/i2c/i2c-dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index b7f9fb00f695..08aff5ebc99a 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -297,7 +297,7 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
 		 */
 		if (rdwr_pa[i].flags & I2C_M_RECV_LEN) {
 			if (!(rdwr_pa[i].flags & I2C_M_RD) ||
-			    rdwr_pa[i].buf[0] < 1 ||
+			    rdwr_pa[i].len < 1 || rdwr_pa[i].buf[0] < 1 ||
 			    rdwr_pa[i].len < rdwr_pa[i].buf[0] +
 					     I2C_SMBUS_BLOCK_MAX) {
 				i++;
-- 
2.37.1


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

end of thread, other threads:[~2022-10-03  8:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-03  8:43 [PATCH 4.14 0/1] Fix NULL dereference in i2cdev_ioctl_rdwr() Harshit Mogalapalli
2022-10-03  8:43 ` [PATCH 4.14 1/1] i2c: dev: prevent ZERO_SIZE_PTR deref " Harshit Mogalapalli

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).