All of lore.kernel.org
 help / color / mirror / Atom feed
From: Denis Efremov <efremov@linux.com>
To: Peilin Ye <yepeilin.cs@gmail.com>, Jens Axboe <axboe@kernel.dk>
Cc: Arnd Bergmann <arnd@arndb.de>,
	linux-kernel@vger.kernel.org, linux-block@vger.kernel.org,
	linux-kernel-mentees@lists.linuxfoundation.org,
	Dan Carpenter <dan.carpenter@oracle.com>
Subject: Re: [Linux-kernel-mentees] [PATCH] block/floppy: Prevent kernel-infoleak in raw_cmd_copyout()
Date: Wed, 29 Jul 2020 12:07:20 +0300	[thread overview]
Message-ID: <40446b2c-3885-1b30-c0b3-5f544a96ed78@linux.com> (raw)
In-Reply-To: <20200728141946.426245-1-yepeilin.cs@gmail.com>

Hi,

On 7/28/20 5:19 PM, Peilin Ye wrote:
> raw_cmd_copyout() is potentially copying uninitialized kernel stack memory
> since it is initializing `cmd` by assignment, which may cause the compiler
> to leave uninitialized holes in this structure. Fix it by using memcpy()
> instead.
> 
> Cc: stable@vger.kernel.org
> Fixes: 2145e15e0557 ("floppy: don't write kernel-only members to FDRAWCMD ioctl output")
> Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Signed-off-by: Peilin Ye <yepeilin.cs@gmail.com>

Reviewed-by: Denis Efremov <efremov@linux.com>

ptr comes from raw_cmd_copyin and it should be ok to use memcpy.

Jens, could you please take this one to your 5.9 branch?


> ---
> $ pahole -C "floppy_raw_cmd" drivers/block/floppy.o
> struct floppy_raw_cmd {
> 	unsigned int               flags;                /*     0     4 */
> 
> 	/* XXX 4 bytes hole, try to pack */
> 
> 	void *                     data;                 /*     8     8 */
> 	char *                     kernel_data;          /*    16     8 */
> 	struct floppy_raw_cmd *    next;                 /*    24     8 */
> 	long int                   length;               /*    32     8 */
> 	long int                   phys_length;          /*    40     8 */
> 	int                        buffer_length;        /*    48     4 */
> 	unsigned char              rate;                 /*    52     1 */
> 	unsigned char              cmd_count;            /*    53     1 */
> 	union {
> 		struct {
> 			unsigned char cmd[16];           /*    54    16 */
> 			/* --- cacheline 1 boundary (64 bytes) was 6 bytes ago --- */
> 			unsigned char reply_count;       /*    70     1 */
> 			unsigned char reply[16];         /*    71    16 */
> 		};                                       /*    54    33 */
> 		unsigned char      fullcmd[33];          /*    54    33 */
> 	};                                               /*    54    33 */
> 
> 	/* XXX 1 byte hole, try to pack */
> 
> 	/* --- cacheline 1 boundary (64 bytes) was 24 bytes ago --- */
> 	int                        track;                /*    88     4 */
> 	int                        resultcode;           /*    92     4 */
> 	int                        reserved1;            /*    96     4 */
> 	int                        reserved2;            /*   100     4 */
> 
> 	/* size: 104, cachelines: 2, members: 14 */
> 	/* sum members: 99, holes: 2, sum holes: 5 */
> 	/* last cacheline: 40 bytes */
> };
> 

It would be nice to add lkml links with discussion on the issue or
https://www.nccgroup.com/us/about-us/newsroom-and-events/blog/2019/october/padding-the-struct-how-a-compiler-optimization-can-disclose-stack-memory/
in addition to pahole output.

>  drivers/block/floppy.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index 09079aee8dc4..b8ea98f7a9cb 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -3126,7 +3126,9 @@ static int raw_cmd_copyout(int cmd, void __user *param,
>  	int ret;
>  
>  	while (ptr) {
> -		struct floppy_raw_cmd cmd = *ptr;
> +		struct floppy_raw_cmd cmd;
> +
> +		memcpy(&cmd, ptr, sizeof(cmd))>  		cmd.next = NULL;
>  		cmd.kernel_data = NULL;
>  		ret = copy_to_user(param, &cmd, sizeof(cmd));
> 

Thanks,
Denis

WARNING: multiple messages have this Message-ID (diff)
From: Denis Efremov <efremov@linux.com>
To: Peilin Ye <yepeilin.cs@gmail.com>, Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org,
	Dan Carpenter <dan.carpenter@oracle.com>,
	linux-kernel-mentees@lists.linuxfoundation.org,
	linux-kernel@vger.kernel.org, Arnd Bergmann <arnd@arndb.de>
Subject: Re: [Linux-kernel-mentees] [PATCH] block/floppy: Prevent kernel-infoleak in raw_cmd_copyout()
Date: Wed, 29 Jul 2020 12:07:20 +0300	[thread overview]
Message-ID: <40446b2c-3885-1b30-c0b3-5f544a96ed78@linux.com> (raw)
In-Reply-To: <20200728141946.426245-1-yepeilin.cs@gmail.com>

Hi,

On 7/28/20 5:19 PM, Peilin Ye wrote:
> raw_cmd_copyout() is potentially copying uninitialized kernel stack memory
> since it is initializing `cmd` by assignment, which may cause the compiler
> to leave uninitialized holes in this structure. Fix it by using memcpy()
> instead.
> 
> Cc: stable@vger.kernel.org
> Fixes: 2145e15e0557 ("floppy: don't write kernel-only members to FDRAWCMD ioctl output")
> Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Signed-off-by: Peilin Ye <yepeilin.cs@gmail.com>

Reviewed-by: Denis Efremov <efremov@linux.com>

ptr comes from raw_cmd_copyin and it should be ok to use memcpy.

Jens, could you please take this one to your 5.9 branch?


> ---
> $ pahole -C "floppy_raw_cmd" drivers/block/floppy.o
> struct floppy_raw_cmd {
> 	unsigned int               flags;                /*     0     4 */
> 
> 	/* XXX 4 bytes hole, try to pack */
> 
> 	void *                     data;                 /*     8     8 */
> 	char *                     kernel_data;          /*    16     8 */
> 	struct floppy_raw_cmd *    next;                 /*    24     8 */
> 	long int                   length;               /*    32     8 */
> 	long int                   phys_length;          /*    40     8 */
> 	int                        buffer_length;        /*    48     4 */
> 	unsigned char              rate;                 /*    52     1 */
> 	unsigned char              cmd_count;            /*    53     1 */
> 	union {
> 		struct {
> 			unsigned char cmd[16];           /*    54    16 */
> 			/* --- cacheline 1 boundary (64 bytes) was 6 bytes ago --- */
> 			unsigned char reply_count;       /*    70     1 */
> 			unsigned char reply[16];         /*    71    16 */
> 		};                                       /*    54    33 */
> 		unsigned char      fullcmd[33];          /*    54    33 */
> 	};                                               /*    54    33 */
> 
> 	/* XXX 1 byte hole, try to pack */
> 
> 	/* --- cacheline 1 boundary (64 bytes) was 24 bytes ago --- */
> 	int                        track;                /*    88     4 */
> 	int                        resultcode;           /*    92     4 */
> 	int                        reserved1;            /*    96     4 */
> 	int                        reserved2;            /*   100     4 */
> 
> 	/* size: 104, cachelines: 2, members: 14 */
> 	/* sum members: 99, holes: 2, sum holes: 5 */
> 	/* last cacheline: 40 bytes */
> };
> 

It would be nice to add lkml links with discussion on the issue or
https://www.nccgroup.com/us/about-us/newsroom-and-events/blog/2019/october/padding-the-struct-how-a-compiler-optimization-can-disclose-stack-memory/
in addition to pahole output.

>  drivers/block/floppy.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index 09079aee8dc4..b8ea98f7a9cb 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -3126,7 +3126,9 @@ static int raw_cmd_copyout(int cmd, void __user *param,
>  	int ret;
>  
>  	while (ptr) {
> -		struct floppy_raw_cmd cmd = *ptr;
> +		struct floppy_raw_cmd cmd;
> +
> +		memcpy(&cmd, ptr, sizeof(cmd))>  		cmd.next = NULL;
>  		cmd.kernel_data = NULL;
>  		ret = copy_to_user(param, &cmd, sizeof(cmd));
> 

Thanks,
Denis
_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

  reply	other threads:[~2020-07-29  9:07 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-28 14:19 [Linux-kernel-mentees] [PATCH] block/floppy: Prevent kernel-infoleak in raw_cmd_copyout() Peilin Ye
2020-07-28 14:19 ` Peilin Ye
2020-07-29  9:07 ` Denis Efremov [this message]
2020-07-29  9:07   ` Denis Efremov
2020-07-29  9:18 ` Denis Efremov
2020-07-29  9:18   ` Denis Efremov
2020-07-29  9:46   ` Peilin Ye
2020-07-29  9:46     ` Peilin Ye
2020-07-29 11:51 ` [Linux-kernel-mentees] [PATCH v2] " Peilin Ye
2020-07-29 11:51   ` Peilin Ye
2020-07-29 12:58   ` Dan Carpenter
2020-07-29 12:58     ` Dan Carpenter
2020-07-29 13:22     ` Denis Efremov
2020-07-29 13:22       ` Denis Efremov
2020-07-29 13:42       ` Dan Carpenter
2020-07-29 13:42         ` Dan Carpenter
2020-07-30  8:11       ` Arnd Bergmann
2020-07-30  8:11         ` Arnd Bergmann
2020-07-30 18:10         ` Kees Cook
2020-07-30 18:10           ` Kees Cook
2020-07-30 20:45           ` Arnd Bergmann
2020-07-30 20:45             ` Arnd Bergmann
2021-07-23 22:22             ` Kees Cook
2021-07-23 22:22               ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=40446b2c-3885-1b30-c0b3-5f544a96ed78@linux.com \
    --to=efremov@linux.com \
    --cc=arnd@arndb.de \
    --cc=axboe@kernel.dk \
    --cc=dan.carpenter@oracle.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel-mentees@lists.linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=yepeilin.cs@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.