mbox series

[0/3] scsi: qedf: sanitise uaccess

Message ID 20230728065819.139694-1-oleksandr@redhat.com
Headers show
Series scsi: qedf: sanitise uaccess | expand

Message

Oleksandr Natalenko July 28, 2023, 6:58 a.m. UTC
qedf driver, debugfs part of it specifically, touches __user pointers
directly for printing out info to userspace via sprintf(), which may
cause crash like this:

BUG: unable to handle kernel paging request at 00007ffd1d6b43a0
IP: [<ffffffffaa7a882a>] string.isra.7+0x6a/0xf0
Oops: 0003 [#1] SMP
Call Trace:
 [<ffffffffaa7a9f31>] vsnprintf+0x201/0x6a0
 [<ffffffffaa7aa556>] sprintf+0x56/0x80
 [<ffffffffc04227ed>] qedf_dbg_stop_io_on_error_cmd_read+0x6d/0x90 [qedf]
 [<ffffffffaa65bb2f>] vfs_read+0x9f/0x170
 [<ffffffffaa65cb82>] SyS_pread64+0x92/0xc0

Avoid this by preparing the info in a kernel buffer first, either
allocated on stack for small printouts, or via vmalloc() for big ones,
and then copying it to the userspace properly.

Previous submission is an RFC: [1]. There are no code changes since
then. The RFC prefix is dropped. The Tested-by tag from Laurence is
added.

There's similar submission from Saurav [2], but we agreed I could nack
it and proceed with my one.

[1] https://lore.kernel.org/linux-scsi/20230724120241.40495-1-oleksandr@redhat.com/
[2] https://lore.kernel.org/linux-scsi/20230726101236.11922-1-skashyap@marvell.com/

Oleksandr Natalenko (3):
  scsi: qedf: do not touch __user pointer in
    qedf_dbg_stop_io_on_error_cmd_read() directly
  scsi: qedf: do not touch __user pointer in qedf_dbg_debug_cmd_read()
    directly
  scsi: qedf: do not touch __user pointer in qedf_dbg_fp_int_cmd_read()
    directly

 drivers/scsi/qedf/qedf_dbg.h     |  2 ++
 drivers/scsi/qedf/qedf_debugfs.c | 35 +++++++++++++++++++-------------
 2 files changed, 23 insertions(+), 14 deletions(-)

Comments

Oleksandr Natalenko July 28, 2023, 7:05 a.m. UTC | #1
On pátek 28. července 2023 8:58:16 CEST Oleksandr Natalenko wrote:
> qedf driver, debugfs part of it specifically, touches __user pointers
> directly for printing out info to userspace via sprintf(), which may
> cause crash like this:
> 
> BUG: unable to handle kernel paging request at 00007ffd1d6b43a0
> IP: [<ffffffffaa7a882a>] string.isra.7+0x6a/0xf0
> Oops: 0003 [#1] SMP
> Call Trace:
>  [<ffffffffaa7a9f31>] vsnprintf+0x201/0x6a0
>  [<ffffffffaa7aa556>] sprintf+0x56/0x80
>  [<ffffffffc04227ed>] qedf_dbg_stop_io_on_error_cmd_read+0x6d/0x90 [qedf]
>  [<ffffffffaa65bb2f>] vfs_read+0x9f/0x170
>  [<ffffffffaa65cb82>] SyS_pread64+0x92/0xc0
> 
> Avoid this by preparing the info in a kernel buffer first, either
> allocated on stack for small printouts, or via vmalloc() for big ones,
> and then copying it to the userspace properly.
> 
> Previous submission is an RFC: [1]. There are no code changes since
> then. The RFC prefix is dropped. The Tested-by tag from Laurence is
> added.
> 
> There's similar submission from Saurav [2], but we agreed I could nack
> it and proceed with my one.
> 
> [1] https://lore.kernel.org/linux-scsi/20230724120241.40495-1-oleksandr@redhat.com/
> [2] https://lore.kernel.org/linux-scsi/20230726101236.11922-1-skashyap@marvell.com/
> 
> Oleksandr Natalenko (3):
>   scsi: qedf: do not touch __user pointer in
>     qedf_dbg_stop_io_on_error_cmd_read() directly
>   scsi: qedf: do not touch __user pointer in qedf_dbg_debug_cmd_read()
>     directly
>   scsi: qedf: do not touch __user pointer in qedf_dbg_fp_int_cmd_read()
>     directly
> 
>  drivers/scsi/qedf/qedf_dbg.h     |  2 ++
>  drivers/scsi/qedf/qedf_debugfs.c | 35 +++++++++++++++++++-------------
>  2 files changed, 23 insertions(+), 14 deletions(-)
> 
> 

Oops, I forgot to add:

Reviewed-by: Laurence Oberman <loberman@redhat.com>

as per [1].

My ask to the maintainer to add it if the submission is accepted, or let me know if I should do a v2 instead.

[1] https://lore.kernel.org/linux-scsi/4f35b02968a18e636e1689c9d52729ef63a438f9.camel@redhat.com/
Johannes Thumshirn July 28, 2023, 8:37 a.m. UTC | #2
Looks good,

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
David Laight July 28, 2023, 3:26 p.m. UTC | #3
From: Oleksandr Natalenko
> Sent: 28 July 2023 07:58
> 
> The qedf_dbg_debug_cmd_read() function invokes sprintf()
> directly on a __user pointer, which may crash the kernel.
                                      ^^^ will

> 
> Avoid doing that by using a small on-stack buffer for sprintf()
> and then calling simple_read_from_buffer() which does a proper
> copy_to_user() call.
...
> diff --git a/drivers/scsi/qedf/qedf_debugfs.c b/drivers/scsi/qedf/qedf_debugfs.c
> index 4d1b99569d490..f910af0029a2c 100644
> --- a/drivers/scsi/qedf/qedf_debugfs.c
> +++ b/drivers/scsi/qedf/qedf_debugfs.c
> @@ -138,15 +138,14 @@ qedf_dbg_debug_cmd_read(struct file *filp, char __user *buffer, size_t count,
>  			loff_t *ppos)
>  {
>  	int cnt;
> +	char cbuf[35];

Why 35?
I pick a multiple of 8 that if 'enough.

>  	struct qedf_dbg_ctx *qedf_dbg =
>  				(struct qedf_dbg_ctx *)filp->private_data;
> 
>  	QEDF_INFO(qedf_dbg, QEDF_LOG_DEBUGFS, "debug mask=0x%x\n", qedf_debug);
> -	cnt = sprintf(buffer, "debug mask = 0x%x\n", qedf_debug);
> +	cnt = sprintf(cbuf, "debug mask = 0x%x\n", qedf_debug);

Use scnprintf() to be sure it doesn't overflow.
Much safer if someone does a quick update or copies the code.

	David

> 
> -	cnt = min_t(int, count, cnt - *ppos);
> -	*ppos += cnt;
> -	return cnt;
> +	return simple_read_from_buffer(buffer, count, ppos, cbuf, cnt);
>  }
> 
>  static ssize_t
> --
> 2.41.0

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Oleksandr Natalenko July 31, 2023, 8:11 a.m. UTC | #4
Hello/

On pátek 28. července 2023 17:26:11 CEST David Laight wrote:
> From: Oleksandr Natalenko
> > Sent: 28 July 2023 07:58
> > 
> > The qedf_dbg_debug_cmd_read() function invokes sprintf()
> > directly on a __user pointer, which may crash the kernel.
>                                       ^^^ will

I don't think it is 100% guaranteed, but for sure this is not a correct behaviour.

> > 
> > Avoid doing that by using a small on-stack buffer for sprintf()
> > and then calling simple_read_from_buffer() which does a proper
> > copy_to_user() call.
> ...
> > diff --git a/drivers/scsi/qedf/qedf_debugfs.c b/drivers/scsi/qedf/qedf_debugfs.c
> > index 4d1b99569d490..f910af0029a2c 100644
> > --- a/drivers/scsi/qedf/qedf_debugfs.c
> > +++ b/drivers/scsi/qedf/qedf_debugfs.c
> > @@ -138,15 +138,14 @@ qedf_dbg_debug_cmd_read(struct file *filp, char __user *buffer, size_t count,
> >  			loff_t *ppos)
> >  {
> >  	int cnt;
> > +	char cbuf[35];
> 
> Why 35?
> I pick a multiple of 8 that if 'enough.

OK, I overestimated this, it should have been 27, but I'll make it 32 to be a multiple of 8.

Thanks.

> >  	struct qedf_dbg_ctx *qedf_dbg =
> >  				(struct qedf_dbg_ctx *)filp->private_data;
> > 
> >  	QEDF_INFO(qedf_dbg, QEDF_LOG_DEBUGFS, "debug mask=0x%x\n", qedf_debug);
> > -	cnt = sprintf(buffer, "debug mask = 0x%x\n", qedf_debug);
> > +	cnt = sprintf(cbuf, "debug mask = 0x%x\n", qedf_debug);
> 
> Use scnprintf() to be sure it doesn't overflow.
> Much safer if someone does a quick update or copies the code.
> 
> 	David
> 
> > 
> > -	cnt = min_t(int, count, cnt - *ppos);
> > -	*ppos += cnt;
> > -	return cnt;
> > +	return simple_read_from_buffer(buffer, count, ppos, cbuf, cnt);
> >  }
> > 
> >  static ssize_t
> > --
> > 2.41.0
> 
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
> 
>