2020-07-27 18:49:36

by Peilin Ye

[permalink] [raw]
Subject: [Linux-kernel-mentees] [PATCH] block/scsi-ioctl: Prevent kernel-infoleak in scsi_put_cdrom_generic_arg()

scsi_put_cdrom_generic_arg() is copying uninitialized stack memory to
userspace due to the compiler not initializing holes in statically
allocated structures. Fix it by initializing `cgc32` using memset().

Cc: [email protected]
Fixes: f3ee6e63a9df ("compat_ioctl: move CDROM_SEND_PACKET handling into scsi")
Suggested-by: Dan Carpenter <[email protected]>
Suggested-by: Arnd Bergmann <[email protected]>
Signed-off-by: Peilin Ye <[email protected]>
---
block/scsi_ioctl.c | 23 +++++++++++++----------
1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c
index ef722f04f88a..1b7f85634751 100644
--- a/block/scsi_ioctl.c
+++ b/block/scsi_ioctl.c
@@ -692,16 +692,19 @@ static int scsi_put_cdrom_generic_arg(const struct cdrom_generic_command *cgc,
{
#ifdef CONFIG_COMPAT
if (in_compat_syscall()) {
- struct compat_cdrom_generic_command cgc32 = {
- .buffer = (uintptr_t)(cgc->buffer),
- .buflen = cgc->buflen,
- .stat = cgc->stat,
- .sense = (uintptr_t)(cgc->sense),
- .data_direction = cgc->data_direction,
- .quiet = cgc->quiet,
- .timeout = cgc->timeout,
- .reserved[0] = (uintptr_t)(cgc->reserved[0]),
- };
+ struct compat_cdrom_generic_command cgc32;
+
+ memset(&cgc32, 0, sizeof(cgc32));
+
+ cgc32.buffer = (uintptr_t)(cgc->buffer);
+ cgc32.buflen = cgc->buflen;
+ cgc32.stat = cgc->stat;
+ cgc32.sense = (uintptr_t)(cgc->sense);
+ cgc32.data_direction = cgc->data_direction;
+ cgc32.quiet = cgc->quiet;
+ cgc32.timeout = cgc->timeout;
+ cgc32.reserved[0] = (uintptr_t)(cgc->reserved[0]);
+
memcpy(&cgc32.cmd, &cgc->cmd, CDROM_PACKET_SIZE);

if (copy_to_user(arg, &cgc32, sizeof(cgc32)))
--
2.25.1


2020-07-27 18:51:45

by Jens Axboe

[permalink] [raw]
Subject: Re: [Linux-kernel-mentees] [PATCH] block/scsi-ioctl: Prevent kernel-infoleak in scsi_put_cdrom_generic_arg()

On 7/27/20 10:19 AM, Peilin Ye wrote:
> scsi_put_cdrom_generic_arg() is copying uninitialized stack memory to
> userspace due to the compiler not initializing holes in statically
> allocated structures. Fix it by initializing `cgc32` using memset().

Could also just add the appropriate pad, so the compiler does the
right thing.


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];

--
Jens Axboe

2020-07-27 19:31:52

by Peilin Ye

[permalink] [raw]
Subject: [Linux-kernel-mentees] [PATCH v2] block/scsi-ioctl: Prevent kernel-infoleak in scsi_put_cdrom_generic_arg()

scsi_put_cdrom_generic_arg() is copying uninitialized stack memory to
userspace due to the compiler not initializing holes in statically
allocated structures. Fix it by adding a padding field to `struct
compat_cdrom_generic_command`.

Cc: [email protected]
Fixes: f3ee6e63a9df ("compat_ioctl: move CDROM_SEND_PACKET handling into scsi")
Suggested-by: Dan Carpenter <[email protected]>
Suggested-by: Arnd Bergmann <[email protected]>
Signed-off-by: Peilin Ye <[email protected]>
---
Change in v2:
- Add a padding field to `struct compat_cdrom_generic_command`,
instead of doing memset() on `cgc32`. (Suggested by Jens Axboe
<[email protected]>)

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

2020-09-09 09:54:48

by Peilin Ye

[permalink] [raw]
Subject: [Linux-kernel-mentees] [PATCH v3] block/scsi-ioctl: Prevent kernel-infoleak in scsi_put_cdrom_generic_arg()

scsi_put_cdrom_generic_arg() is potentially copying uninitialized stack
memory to userspace, since the compiler may leave a 3-byte hole in the
middle of `cgc32`. Prevent it by adding a padding field to `struct
compat_cdrom_generic_command`.

Cc: [email protected]
Fixes: f3ee6e63a9df ("compat_ioctl: move CDROM_SEND_PACKET handling into scsi")
Suggested-by: Dan Carpenter <[email protected]>
Suggested-by: Arnd Bergmann <[email protected]>
Signed-off-by: Peilin Ye <[email protected]>
---
Change 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
<[email protected]>)

$ # 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 */
};
$ _

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