linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [scsi]  6aded12b10: kernel_BUG_at_mm/usercopy.c
       [not found] ` <20220323071409.GA25480@lst.de>
@ 2022-03-23 15:40   ` Kees Cook
  2022-03-23 15:42     ` Matthew Wilcox
                       ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Kees Cook @ 2022-03-23 15:40 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: kernel test robot, Martin K. Petersen, Bart Van Assche,
	John Garry, LKML, lkp, lkp, Matthew Wilcox (Oracle),
	linux-mm, linux-hardening

On Wed, Mar 23, 2022 at 08:14:10AM +0100, Christoph Hellwig wrote:
> The actual warning is;
> 
> [   34.496096][  T331] usercopy: Kernel memory overwrite attempt detected to spans multiple pages (off set 0, size 6)!
> 
> This is for the cmnd field in struct scsi_cmnd, which is allocated by
> the block layer as part of the request allocator.  So with a specific
> packing it can legitimately span pages.
> 
> Kees: how can we annotate that this is ok?

The main problem is that CONFIG_HARDENED_USERCOPY_PAGESPAN=y is broken
(and nothing should be setting it).

This series removes it:
https://lore.kernel.org/linux-hardening/20220110231530.665970-1-willy@infradead.org/

Matthew, what's the status of that series? Will it make the current
merge window?

As for the SCSI changes, I'm a bit worried about type confusion, as I
don't see anything actually validating types/sizes when converting:

static inline void *blk_mq_rq_to_pdu(struct request *rq)
{
        return rq + 1;
}

But I guess that ship has sailed. :P

Regardless, I'm concerned that disabling PAGESPAN will just uncover
further checks, though. Where is allocation happening? The check is here:

static int scsi_fill_sghdr_rq(struct scsi_device *sdev, struct request *rq,
                struct sg_io_hdr *hdr, fmode_t mode)
{
        struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);

        if (hdr->cmd_len < 6)
                return -EMSGSIZE;
        if (copy_from_user(scmd->cmnd, hdr->cmdp, hdr->cmd_len))
                return -EFAULT;
	...
}

I don't see any earlier marking for this copy_from_user(), so I assume
the old allocation was a plain kmalloc().

For comparision, a related marking can be seen for a copy_to_user() case
in commit 0afe76e88c57 ("scsi: Define usercopy region in scsi_sense_cache
slab cache")

I *think* the allocation is happening in scsi_ioctl_reset()? But that's
a plain kmalloc(), so I'm not sure why PAGESPAN would have tripped...
are there other allocation paths?

-- 
Kees Cook

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

* Re: [scsi]  6aded12b10: kernel_BUG_at_mm/usercopy.c
  2022-03-23 15:40   ` [scsi] 6aded12b10: kernel_BUG_at_mm/usercopy.c Kees Cook
@ 2022-03-23 15:42     ` Matthew Wilcox
  2022-03-23 22:30       ` Kees Cook
  2022-03-23 15:47     ` Christoph Hellwig
  2022-03-24  6:48     ` Christoph Hellwig
  2 siblings, 1 reply; 6+ messages in thread
From: Matthew Wilcox @ 2022-03-23 15:42 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christoph Hellwig, kernel test robot, Martin K. Petersen,
	Bart Van Assche, John Garry, LKML, lkp, lkp, linux-mm,
	linux-hardening

On Wed, Mar 23, 2022 at 08:40:30AM -0700, Kees Cook wrote:
> On Wed, Mar 23, 2022 at 08:14:10AM +0100, Christoph Hellwig wrote:
> > The actual warning is;
> > 
> > [   34.496096][  T331] usercopy: Kernel memory overwrite attempt detected to spans multiple pages (off set 0, size 6)!
> > 
> > This is for the cmnd field in struct scsi_cmnd, which is allocated by
> > the block layer as part of the request allocator.  So with a specific
> > packing it can legitimately span pages.
> > 
> > Kees: how can we annotate that this is ok?
> 
> The main problem is that CONFIG_HARDENED_USERCOPY_PAGESPAN=y is broken
> (and nothing should be setting it).
> 
> This series removes it:
> https://lore.kernel.org/linux-hardening/20220110231530.665970-1-willy@infradead.org/
> 
> Matthew, what's the status of that series? Will it make the current
> merge window?

I thought you were going to merge it!  I haven't put it in any of my
public trees.

> As for the SCSI changes, I'm a bit worried about type confusion, as I
> don't see anything actually validating types/sizes when converting:
> 
> static inline void *blk_mq_rq_to_pdu(struct request *rq)
> {
>         return rq + 1;
> }
> 
> But I guess that ship has sailed. :P
> 
> Regardless, I'm concerned that disabling PAGESPAN will just uncover
> further checks, though. Where is allocation happening? The check is here:
> 
> static int scsi_fill_sghdr_rq(struct scsi_device *sdev, struct request *rq,
>                 struct sg_io_hdr *hdr, fmode_t mode)
> {
>         struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
> 
>         if (hdr->cmd_len < 6)
>                 return -EMSGSIZE;
>         if (copy_from_user(scmd->cmnd, hdr->cmdp, hdr->cmd_len))
>                 return -EFAULT;
> 	...
> }
> 
> I don't see any earlier marking for this copy_from_user(), so I assume
> the old allocation was a plain kmalloc().
> 
> For comparision, a related marking can be seen for a copy_to_user() case
> in commit 0afe76e88c57 ("scsi: Define usercopy region in scsi_sense_cache
> slab cache")
> 
> I *think* the allocation is happening in scsi_ioctl_reset()? But that's
> a plain kmalloc(), so I'm not sure why PAGESPAN would have tripped...
> are there other allocation paths?
> 
> -- 
> Kees Cook
> 

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

* Re: [scsi]  6aded12b10: kernel_BUG_at_mm/usercopy.c
  2022-03-23 15:40   ` [scsi] 6aded12b10: kernel_BUG_at_mm/usercopy.c Kees Cook
  2022-03-23 15:42     ` Matthew Wilcox
@ 2022-03-23 15:47     ` Christoph Hellwig
  2022-03-23 22:33       ` Kees Cook
  2022-03-24  6:48     ` Christoph Hellwig
  2 siblings, 1 reply; 6+ messages in thread
From: Christoph Hellwig @ 2022-03-23 15:47 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christoph Hellwig, kernel test robot, Martin K. Petersen,
	Bart Van Assche, John Garry, LKML, lkp, lkp,
	Matthew Wilcox (Oracle),
	linux-mm, linux-hardening

On Wed, Mar 23, 2022 at 08:40:30AM -0700, Kees Cook wrote:
> Regardless, I'm concerned that disabling PAGESPAN will just uncover
> further checks, though. Where is allocation happening? The check is here:

blk_mq_alloc_rqs, using alloc_pages_node.  This hasn't actually changed
with this comment.  Just the size of the allocation shrunk, probably
leading to the span of pages.

> I *think* the allocation is happening in scsi_ioctl_reset()? But that's
> a plain kmalloc(), so I'm not sure why PAGESPAN would have tripped...
> are there other allocation paths?

scsi_ioctl_reset is the odd one out and does also allocate a request,
but that request is never used for user copies (and that whole hacky
side path needs to go away, there is a huge series that needs to be
finished to sort this out).

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

* Re: [scsi]  6aded12b10: kernel_BUG_at_mm/usercopy.c
  2022-03-23 15:42     ` Matthew Wilcox
@ 2022-03-23 22:30       ` Kees Cook
  0 siblings, 0 replies; 6+ messages in thread
From: Kees Cook @ 2022-03-23 22:30 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Christoph Hellwig, kernel test robot, Martin K. Petersen,
	Bart Van Assche, John Garry, LKML, lkp, lkp, linux-mm,
	linux-hardening

On Wed, Mar 23, 2022 at 03:42:34PM +0000, Matthew Wilcox wrote:
> On Wed, Mar 23, 2022 at 08:40:30AM -0700, Kees Cook wrote:
> > On Wed, Mar 23, 2022 at 08:14:10AM +0100, Christoph Hellwig wrote:
> > > The actual warning is;
> > > 
> > > [   34.496096][  T331] usercopy: Kernel memory overwrite attempt detected to spans multiple pages (off set 0, size 6)!
> > > 
> > > This is for the cmnd field in struct scsi_cmnd, which is allocated by
> > > the block layer as part of the request allocator.  So with a specific
> > > packing it can legitimately span pages.
> > > 
> > > Kees: how can we annotate that this is ok?
> > 
> > The main problem is that CONFIG_HARDENED_USERCOPY_PAGESPAN=y is broken
> > (and nothing should be setting it).
> > 
> > This series removes it:
> > https://lore.kernel.org/linux-hardening/20220110231530.665970-1-willy@infradead.org/
> > 
> > Matthew, what's the status of that series? Will it make the current
> > merge window?
> 
> I thought you were going to merge it!  I haven't put it in any of my
> public trees.

LOL. Okay, you'd mentioned another check, so I wasn't sure. I can go
ahead and snag it, but I'll likely wait until the next window and let it
live in -next for a while, unless you think it should get YOLOed in. :)

-Kees

-- 
Kees Cook

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

* Re: [scsi]  6aded12b10: kernel_BUG_at_mm/usercopy.c
  2022-03-23 15:47     ` Christoph Hellwig
@ 2022-03-23 22:33       ` Kees Cook
  0 siblings, 0 replies; 6+ messages in thread
From: Kees Cook @ 2022-03-23 22:33 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: kernel test robot, Martin K. Petersen, Bart Van Assche,
	John Garry, LKML, lkp, lkp, Matthew Wilcox (Oracle),
	linux-mm, linux-hardening

On Wed, Mar 23, 2022 at 04:47:39PM +0100, Christoph Hellwig wrote:
> On Wed, Mar 23, 2022 at 08:40:30AM -0700, Kees Cook wrote:
> > Regardless, I'm concerned that disabling PAGESPAN will just uncover
> > further checks, though. Where is allocation happening? The check is here:
> 
> blk_mq_alloc_rqs, using alloc_pages_node.  This hasn't actually changed
> with this comment.  Just the size of the allocation shrunk, probably
> leading to the span of pages.

Okay, the page allocator _should_ be fine for that. In the mean time,
lkp should probably just disable PAGESPAN.

> > I *think* the allocation is happening in scsi_ioctl_reset()? But that's
> > a plain kmalloc(), so I'm not sure why PAGESPAN would have tripped...
> > are there other allocation paths?
> 
> scsi_ioctl_reset is the odd one out and does also allocate a request,
> but that request is never used for user copies (and that whole hacky
> side path needs to go away, there is a huge series that needs to be
> finished to sort this out).

Gotcha!

-- 
Kees Cook

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

* Re: [scsi]  6aded12b10: kernel_BUG_at_mm/usercopy.c
  2022-03-23 15:40   ` [scsi] 6aded12b10: kernel_BUG_at_mm/usercopy.c Kees Cook
  2022-03-23 15:42     ` Matthew Wilcox
  2022-03-23 15:47     ` Christoph Hellwig
@ 2022-03-24  6:48     ` Christoph Hellwig
  2 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2022-03-24  6:48 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christoph Hellwig, kernel test robot, Martin K. Petersen,
	Bart Van Assche, John Garry, LKML, lkp, lkp,
	Matthew Wilcox (Oracle),
	linux-mm, linux-hardening

On Wed, Mar 23, 2022 at 08:40:30AM -0700, Kees Cook wrote:
> This series removes it:
> https://lore.kernel.org/linux-hardening/20220110231530.665970-1-willy@infradead.org/

If HARDENED_USERCOPY_PAGESPAN is so broken we really should remove it
ASAP independent of the other patches in the series.


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

end of thread, other threads:[~2022-03-24  6:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20220320143453.GD6208@xsang-OptiPlex-9020>
     [not found] ` <20220323071409.GA25480@lst.de>
2022-03-23 15:40   ` [scsi] 6aded12b10: kernel_BUG_at_mm/usercopy.c Kees Cook
2022-03-23 15:42     ` Matthew Wilcox
2022-03-23 22:30       ` Kees Cook
2022-03-23 15:47     ` Christoph Hellwig
2022-03-23 22:33       ` Kees Cook
2022-03-24  6:48     ` Christoph Hellwig

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