KMSAN: kernel-infoleak in scsi_cmd_ioctl

24 views
Skip to first unread message

syzbot

unread,
Aug 31, 2020, 6:28:23 AM8/31/20
to ax...@kernel.dk, gli...@google.com, linux...@vger.kernel.org, linux-...@vger.kernel.org, syzkall...@googlegroups.com
Hello,

syzbot found the following issue on:

HEAD commit: 3b3ea602 x86: add failure injection to get/put/clear_user
git tree: https://github.com/google/kmsan.git master
console output: https://syzkaller.appspot.com/x/log.txt?x=14d89966900000
kernel config: https://syzkaller.appspot.com/x/.config?x=3afe005fb99591f
dashboard link: https://syzkaller.appspot.com/bug?extid=85433a479a646a064ab3
compiler: clang version 10.0.0 (https://github.com/llvm/llvm-project/ c2443155a0fb245c8f17f2c1c72b6ea391e86e81)
userspace arch: i386

Unfortunately, I don't have any reproducer for this issue yet.

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+85433a...@syzkaller.appspotmail.com

=====================================================
BUG: KMSAN: kernel-infoleak in kmsan_copy_to_user+0x81/0x90 mm/kmsan/kmsan_hooks.c:253
CPU: 1 PID: 12272 Comm: syz-executor.3 Not tainted 5.8.0-rc5-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x21c/0x280 lib/dump_stack.c:118
kmsan_report+0xf7/0x1e0 mm/kmsan/kmsan_report.c:121
kmsan_internal_check_memory+0x238/0x3d0 mm/kmsan/kmsan.c:423
kmsan_copy_to_user+0x81/0x90 mm/kmsan/kmsan_hooks.c:253
instrument_copy_to_user include/linux/instrumented.h:91 [inline]
_copy_to_user+0x18e/0x260 lib/usercopy.c:33
scsi_put_cdrom_generic_arg include/linux/uaccess.h:170 [inline]
scsi_cdrom_send_packet block/scsi_ioctl.c:770 [inline]
scsi_cmd_ioctl+0x2422/0x25a0 block/scsi_ioctl.c:827
scsi_cmd_blk_ioctl+0x1f6/0x240 block/scsi_ioctl.c:876
sd_ioctl_common+0x50a/0x5c0 drivers/scsi/sd.c:1531
sd_compat_ioctl+0xc5/0x220 drivers/scsi/sd.c:1733
compat_blkdev_ioctl+0x74b/0x1200 block/ioctl.c:691
__do_compat_sys_ioctl fs/ioctl.c:847 [inline]
__se_compat_sys_ioctl+0x55f/0x1100 fs/ioctl.c:798
__ia32_compat_sys_ioctl+0x4a/0x70 fs/ioctl.c:798
do_syscall_32_irqs_on arch/x86/entry/common.c:430 [inline]
__do_fast_syscall_32+0x2af/0x480 arch/x86/entry/common.c:477
do_fast_syscall_32+0x6b/0xd0 arch/x86/entry/common.c:505
do_SYSENTER_32+0x73/0x90 arch/x86/entry/common.c:554
entry_SYSENTER_compat_after_hwframe+0x4d/0x5c
RIP: 0023:0xf7fae549
Code: Bad RIP value.
RSP: 002b:00000000f55a80cc EFLAGS: 00000296 ORIG_RAX: 0000000000000036
RAX: ffffffffffffffda RBX: 0000000000000005 RCX: 0000000000005393
RDX: 0000000020002000 RSI: 0000000000000000 RDI: 0000000000000000
RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000
R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000

Local variable ----cgc32.i42.i@scsi_cmd_ioctl created at:
scsi_put_cdrom_generic_arg block/scsi_ioctl.c:695 [inline]
scsi_cdrom_send_packet block/scsi_ioctl.c:770 [inline]
scsi_cmd_ioctl+0x2257/0x25a0 block/scsi_ioctl.c:827
scsi_put_cdrom_generic_arg block/scsi_ioctl.c:695 [inline]
scsi_cdrom_send_packet block/scsi_ioctl.c:770 [inline]
scsi_cmd_ioctl+0x2257/0x25a0 block/scsi_ioctl.c:827

Bytes 29-31 of 44 are uninitialized
Memory access of size 44 starts at ffff8881c5bfbaa0
Data copied to user address 0000000020002000
=====================================================


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzk...@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.

Peilin Ye

unread,
Oct 2, 2020, 9:49:56 AM10/2/20
to syzbot, ax...@kernel.dk, gli...@google.com, linux...@vger.kernel.org, linux-...@vger.kernel.org, syzkall...@googlegroups.com, Anant Thazhemadam, Greg Kroah-Hartman
On Mon, Aug 31, 2020 at 03:28:22AM -0700, syzbot wrote:
> BUG: KMSAN: kernel-infoleak in kmsan_copy_to_user+0x81/0x90 mm/kmsan/kmsan_hooks.c:253
> CPU: 1 PID: 12272 Comm: syz-executor.3 Not tainted 5.8.0-rc5-syzkaller #0
> Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
> Call Trace:
> __dump_stack lib/dump_stack.c:77 [inline]
> dump_stack+0x21c/0x280 lib/dump_stack.c:118
> kmsan_report+0xf7/0x1e0 mm/kmsan/kmsan_report.c:121
> kmsan_internal_check_memory+0x238/0x3d0 mm/kmsan/kmsan.c:423
> kmsan_copy_to_user+0x81/0x90 mm/kmsan/kmsan_hooks.c:253
> instrument_copy_to_user include/linux/instrumented.h:91 [inline]
> _copy_to_user+0x18e/0x260 lib/usercopy.c:33
> scsi_put_cdrom_generic_arg include/linux/uaccess.h:170 [inline]

+ Cc: Greg Kroah-Hartman
+ Cc: Anant Thazhemadam

Hi all,

In looking at the report, I guess this patch should fix the issue, there's
a 3-byte hole in `struct compat_cdrom_generic_command`:

[PATCH v3] block/scsi-ioctl: Prevent kernel-infoleak in scsi_put_cdrom_generic_arg()
https://lore.kernel.org/lkml/20200909095057.121...@gmail.com/

But I cannot verify it, since syzbot doesn't have a reproducer for it.
The patch adds a 3-byte padding field to `struct
compat_cdrom_generic_command`. It hasn't been accepted yet.

> Local variable ----cgc32.i42.i@scsi_cmd_ioctl created at:
> scsi_put_cdrom_generic_arg block/scsi_ioctl.c:695 [inline]

#ifdef CONFIG_COMPAT
if (in_compat_syscall()) {
struct compat_cdrom_generic_command
[...]

$ # before
$ pahole -C "compat_cdrom_generic_command" !$
pahole -C "compat_cdrom_generic_command" block/scsi_ioctl.o
struct compat_cdrom_generic_command {
unsigned char cmd[12]; /* 0 12 */
compat_caddr_t buffer; /* 12 4 */
compat_uint_t buflen; /* 16 4 */
compat_int_t stat; /* 20 4 */
compat_caddr_t sense; /* 24 4 */
unsigned char data_direction; /* 28 1 */

/* XXX 3 bytes hole, try to pack */

compat_int_t quiet; /* 32 4 */
compat_int_t timeout; /* 36 4 */
compat_caddr_t reserved[1]; /* 40 4 */

/* size: 44, cachelines: 1, members: 9 */
/* sum members: 41, holes: 1, sum holes: 3 */
/* last cacheline: 44 bytes */
};
$ # after
$ pahole -C "compat_cdrom_generic_command" block/scsi_ioctl.o
struct compat_cdrom_generic_command {
unsigned char cmd[12]; /* 0 12 */
compat_caddr_t buffer; /* 12 4 */
compat_uint_t buflen; /* 16 4 */
compat_int_t stat; /* 20 4 */
compat_caddr_t sense; /* 24 4 */
unsigned char data_direction; /* 28 1 */
unsigned char pad[3]; /* 29 3 */
compat_int_t quiet; /* 32 4 */
compat_int_t timeout; /* 36 4 */
compat_caddr_t reserved[1]; /* 40 4 */

/* size: 44, cachelines: 1, members: 10 */
/* last cacheline: 44 bytes */
};
$ _

Thank you,
Peilin Ye

Greg Kroah-Hartman

unread,
Oct 2, 2020, 10:01:01 AM10/2/20
to Peilin Ye, syzbot, ax...@kernel.dk, gli...@google.com, linux...@vger.kernel.org, linux-...@vger.kernel.org, syzkall...@googlegroups.com, Anant Thazhemadam
On Fri, Oct 02, 2020 at 09:49:44AM -0400, Peilin Ye wrote:
> On Mon, Aug 31, 2020 at 03:28:22AM -0700, syzbot wrote:
> > BUG: KMSAN: kernel-infoleak in kmsan_copy_to_user+0x81/0x90 mm/kmsan/kmsan_hooks.c:253
> > CPU: 1 PID: 12272 Comm: syz-executor.3 Not tainted 5.8.0-rc5-syzkaller #0
> > Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
> > Call Trace:
> > __dump_stack lib/dump_stack.c:77 [inline]
> > dump_stack+0x21c/0x280 lib/dump_stack.c:118
> > kmsan_report+0xf7/0x1e0 mm/kmsan/kmsan_report.c:121
> > kmsan_internal_check_memory+0x238/0x3d0 mm/kmsan/kmsan.c:423
> > kmsan_copy_to_user+0x81/0x90 mm/kmsan/kmsan_hooks.c:253
> > instrument_copy_to_user include/linux/instrumented.h:91 [inline]
> > _copy_to_user+0x18e/0x260 lib/usercopy.c:33
> > scsi_put_cdrom_generic_arg include/linux/uaccess.h:170 [inline]
>
> + Cc: Greg Kroah-Hartman
> + Cc: Anant Thazhemadam
>
> Hi all,
>
> In looking at the report, I guess this patch should fix the issue, there's
> a 3-byte hole in `struct compat_cdrom_generic_command`:
>
> [PATCH v3] block/scsi-ioctl: Prevent kernel-infoleak in scsi_put_cdrom_generic_arg()
> https://lore.kernel.org/lkml/20200909095057.121...@gmail.com/
>
> But I cannot verify it, since syzbot doesn't have a reproducer for it.
> The patch adds a 3-byte padding field to `struct
> compat_cdrom_generic_command`. It hasn't been accepted yet.

Please resend it, it looks like it hasn't been taken yet :(

thanks,

greg k-h

Peilin Ye

unread,
Oct 2, 2020, 10:23:35 AM10/2/20
to Jens Axboe, syzbot, Peilin Ye, Dan Carpenter, Arnd Bergmann, Greg Kroah-Hartman, gli...@google.com, Anant Thazhemadam, linux-kern...@lists.linuxfoundation.org, linux...@vger.kernel.org, syzkall...@googlegroups.com, linux-...@vger.kernel.org
scsi_put_cdrom_generic_arg() is copying uninitialized stack memory to
userspace, since the compiler may leave a 3-byte hole in the middle of
`cgc32`. Fix it by adding a padding field to `struct
compat_cdrom_generic_command`.

Cc: sta...@vger.kernel.org
Fixes: f3ee6e63a9df ("compat_ioctl: move CDROM_SEND_PACKET handling into scsi")
Suggested-by: Dan Carpenter <dan.ca...@oracle.com>
Suggested-by: Arnd Bergmann <ar...@arndb.de>
Reported-by: syzbot+85433a...@syzkaller.appspotmail.com
Signed-off-by: Peilin Ye <yepei...@gmail.com>
---
v3: https://lore.kernel.org/lkml/20200909095057.121...@gmail.com/

Changes in v4:
- Change "Prevent" in the title to "Fix", and improve the commit
message, since syzbot has reported it as a "real" bug.
- Add syzbot Reported-by: tag.

Changes in v3:
- Improve commit message. scsi_put_cdrom_generic_arg() does not
*always* leak kernel information. It is compiler dependent.
Reference: https://www.nccgroup.com/us/about-us/newsroom-and-events/blog/2019/october/padding-the-struct-how-a-compiler-optimization-can-disclose-stack-memory/
- Base the patch against mainline 5.9-rc4.

Change in v2:
- Add a padding field to `struct compat_cdrom_generic_command`,
instead of doing memset() on `cgc32`. (Suggested by Jens Axboe
<ax...@kernel.dk>)
block/scsi_ioctl.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c
index ef722f04f88a..72108404718f 100644
--- a/block/scsi_ioctl.c
+++ b/block/scsi_ioctl.c
@@ -651,6 +651,7 @@ struct compat_cdrom_generic_command {
compat_int_t stat;
compat_caddr_t sense;
unsigned char data_direction;
+ unsigned char pad[3];
compat_int_t quiet;
compat_int_t timeout;
compat_caddr_t reserved[1];
--
2.25.1

Jens Axboe

unread,
Oct 2, 2020, 2:02:05 PM10/2/20
to Peilin Ye, syzbot, Dan Carpenter, Arnd Bergmann, Greg Kroah-Hartman, gli...@google.com, Anant Thazhemadam, linux-kern...@lists.linuxfoundation.org, linux...@vger.kernel.org, syzkall...@googlegroups.com, linux-...@vger.kernel.org
On 10/2/20 8:22 AM, Peilin Ye wrote:
> scsi_put_cdrom_generic_arg() is copying uninitialized stack memory to
> userspace, since the compiler may leave a 3-byte hole in the middle of
> `cgc32`. Fix it by adding a padding field to `struct
> compat_cdrom_generic_command`.

Applied, thanks.

--
Jens Axboe

Reply all
Reply to author
Forward
0 new messages