diff mbox series

[trusty/linux,xenial/linux,artful/linux,bionic/linux,1/1] floppy: Do not copy a kernel pointer to user memory in FDGETPRM ioctl

Message ID 20180529133827.12544-2-apw@canonical.com
State New
Headers show
Series [trusty/linux,xenial/linux,artful/linux,bionic/linux,1/1] floppy: Do not copy a kernel pointer to user memory in FDGETPRM ioctl | expand

Commit Message

Andy Whitcroft May 29, 2018, 1:38 p.m. UTC
The final field of a floppy_struct is the field "name", which is a pointer
to a string in kernel memory.  The kernel pointer should not be copied to
user memory.  The FDGETPRM ioctl copies a floppy_struct to user memory,
including this "name" field.  This pointer cannot be used by the user
and it will leak a kernel address to user-space, which will reveal the
location of kernel code and data and undermine KASLR protection.

Model this code after the compat ioctl which copies the returned data
to a previously cleared temporary structure on the stack (excluding the
name pointer) and copy out to userspace from there.  As we already have
an inparam union with an appropriate member and that memory is already
cleared even for read only calls make use of that as a temporary store.

Based on an initial patch by Brian Belleville.

CVE-2018-7755
Signed-off-by: Andy Whitcroft <apw@canonical.com>
---
 drivers/block/floppy.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Thadeu Lima de Souza Cascardo June 4, 2018, 12:28 p.m. UTC | #1
On Tue, May 29, 2018 at 02:38:27PM +0100, Andy Whitcroft wrote:
> The final field of a floppy_struct is the field "name", which is a pointer
> to a string in kernel memory.  The kernel pointer should not be copied to
> user memory.  The FDGETPRM ioctl copies a floppy_struct to user memory,
> including this "name" field.  This pointer cannot be used by the user
> and it will leak a kernel address to user-space, which will reveal the
> location of kernel code and data and undermine KASLR protection.
> 
> Model this code after the compat ioctl which copies the returned data
> to a previously cleared temporary structure on the stack (excluding the
> name pointer) and copy out to userspace from there.  As we already have
> an inparam union with an appropriate member and that memory is already
> cleared even for read only calls make use of that as a temporary store.
> 
> Based on an initial patch by Brian Belleville.
> 
> CVE-2018-7755
> Signed-off-by: Andy Whitcroft <apw@canonical.com>
> ---
>  drivers/block/floppy.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index eae484acfbbc..0a860603a5b6 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -3470,6 +3470,8 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
>  					  (struct floppy_struct **)&outparam);
>  		if (ret)
>  			return ret;
> +		memcpy(&inparam.g, outparam, offsetof(struct floppy_struct, name));
> +		outparam = &inparam.g;
>  		break;
>  	case FDMSGON:
>  		UDP->flags |= FTD_MSG;
> -- 
> 2.17.0
> 

Code seems fine.

Acked-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
Kleber Sacilotto de Souza June 4, 2018, 8:46 p.m. UTC | #2
On 05/29/18 06:38, Andy Whitcroft wrote:
> The final field of a floppy_struct is the field "name", which is a pointer
> to a string in kernel memory.  The kernel pointer should not be copied to
> user memory.  The FDGETPRM ioctl copies a floppy_struct to user memory,
> including this "name" field.  This pointer cannot be used by the user
> and it will leak a kernel address to user-space, which will reveal the
> location of kernel code and data and undermine KASLR protection.
> 
> Model this code after the compat ioctl which copies the returned data
> to a previously cleared temporary structure on the stack (excluding the
> name pointer) and copy out to userspace from there.  As we already have
> an inparam union with an appropriate member and that memory is already
> cleared even for read only calls make use of that as a temporary store.
> 
> Based on an initial patch by Brian Belleville.
> 
> CVE-2018-7755
> Signed-off-by: Andy Whitcroft <apw@canonical.com>
> ---
>  drivers/block/floppy.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index eae484acfbbc..0a860603a5b6 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -3470,6 +3470,8 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
>  					  (struct floppy_struct **)&outparam);
>  		if (ret)
>  			return ret;
> +		memcpy(&inparam.g, outparam, offsetof(struct floppy_struct, name));
> +		outparam = &inparam.g;
>  		break;
>  	case FDMSGON:
>  		UDP->flags |= FTD_MSG;
> 

Looks good.

Acked-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
Khalid Elmously June 4, 2018, 9:35 p.m. UTC | #3
On 2018-05-29 14:38:27 , Andy Whitcroft wrote:
> The final field of a floppy_struct is the field "name", which is a pointer
> to a string in kernel memory.  The kernel pointer should not be copied to
> user memory.  The FDGETPRM ioctl copies a floppy_struct to user memory,
> including this "name" field.  This pointer cannot be used by the user
> and it will leak a kernel address to user-space, which will reveal the
> location of kernel code and data and undermine KASLR protection.
> 
> Model this code after the compat ioctl which copies the returned data
> to a previously cleared temporary structure on the stack (excluding the
> name pointer) and copy out to userspace from there.  As we already have
> an inparam union with an appropriate member and that memory is already
> cleared even for read only calls make use of that as a temporary store.
> 
> Based on an initial patch by Brian Belleville.
> 
> CVE-2018-7755
> Signed-off-by: Andy Whitcroft <apw@canonical.com>
> ---
>  drivers/block/floppy.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index eae484acfbbc..0a860603a5b6 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -3470,6 +3470,8 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
>  					  (struct floppy_struct **)&outparam);
>  		if (ret)
>  			return ret;
> +		memcpy(&inparam.g, outparam, offsetof(struct floppy_struct, name));
> +		outparam = &inparam.g;
>  		break;
>  	case FDMSGON:
>  		UDP->flags |= FTD_MSG;


Not important, but I personally would have thought that just zeroing out the char * from outparam would be "cheaper" than copying everything _but_ the char pointer:

memset(outparam + offsetof(struct floppy_struct, name), 
       0, 
       sizeof(struct floppy_struct) - offsetof(struct floppy_struct, name));


Doesn't look like the savings would be significant though.
Andy Whitcroft June 5, 2018, 1:36 p.m. UTC | #4
On Mon, Jun 04, 2018 at 05:35:05PM -0400, Khaled Elmously wrote:
> On 2018-05-29 14:38:27 , Andy Whitcroft wrote:
> > The final field of a floppy_struct is the field "name", which is a pointer
> > to a string in kernel memory.  The kernel pointer should not be copied to
> > user memory.  The FDGETPRM ioctl copies a floppy_struct to user memory,
> > including this "name" field.  This pointer cannot be used by the user
> > and it will leak a kernel address to user-space, which will reveal the
> > location of kernel code and data and undermine KASLR protection.
> > 
> > Model this code after the compat ioctl which copies the returned data
> > to a previously cleared temporary structure on the stack (excluding the
> > name pointer) and copy out to userspace from there.  As we already have
> > an inparam union with an appropriate member and that memory is already
> > cleared even for read only calls make use of that as a temporary store.
> > 
> > Based on an initial patch by Brian Belleville.
> > 
> > CVE-2018-7755
> > Signed-off-by: Andy Whitcroft <apw@canonical.com>
> > ---
> >  drivers/block/floppy.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> > index eae484acfbbc..0a860603a5b6 100644
> > --- a/drivers/block/floppy.c
> > +++ b/drivers/block/floppy.c
> > @@ -3470,6 +3470,8 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
> >  					  (struct floppy_struct **)&outparam);
> >  		if (ret)
> >  			return ret;
> > +		memcpy(&inparam.g, outparam, offsetof(struct floppy_struct, name));
> > +		outparam = &inparam.g;
> >  		break;
> >  	case FDMSGON:
> >  		UDP->flags |= FTD_MSG;
> 
> 
> Not important, but I personally would have thought that just zeroing out the char * from outparam would be "cheaper" than copying everything _but_ the char pointer:
> 
> memset(outparam + offsetof(struct floppy_struct, name), 
>        0, 
>        sizeof(struct floppy_struct) - offsetof(struct floppy_struct, name));
> 
> 
> Doesn't look like the savings would be significant though.

We can't do that because outparam as returned is pointing to the kernel
internal store for the original data.  If we clear name in outparam we
are clearing it for the kernel en-toto and would lose the name.

-apw
Juerg Haefliger June 5, 2018, 2:08 p.m. UTC | #5
And applied to artful, bionic and cosmic master-next.

...Juerg

On 05/29/2018 03:38 PM, Andy Whitcroft wrote:
> The final field of a floppy_struct is the field "name", which is a pointer
> to a string in kernel memory.  The kernel pointer should not be copied to
> user memory.  The FDGETPRM ioctl copies a floppy_struct to user memory,
> including this "name" field.  This pointer cannot be used by the user
> and it will leak a kernel address to user-space, which will reveal the
> location of kernel code and data and undermine KASLR protection.
> 
> Model this code after the compat ioctl which copies the returned data
> to a previously cleared temporary structure on the stack (excluding the
> name pointer) and copy out to userspace from there.  As we already have
> an inparam union with an appropriate member and that memory is already
> cleared even for read only calls make use of that as a temporary store.
> 
> Based on an initial patch by Brian Belleville.
> 
> CVE-2018-7755
> Signed-off-by: Andy Whitcroft <apw@canonical.com>
> ---
>  drivers/block/floppy.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index eae484acfbbc..0a860603a5b6 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -3470,6 +3470,8 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
>  					  (struct floppy_struct **)&outparam);
>  		if (ret)
>  			return ret;
> +		memcpy(&inparam.g, outparam, offsetof(struct floppy_struct, name));
> +		outparam = &inparam.g;
>  		break;
>  	case FDMSGON:
>  		UDP->flags |= FTD_MSG;
>
Khalid Elmously June 5, 2018, 3:48 p.m. UTC | #6
On 2018-06-05 14:36:14 , Andy Whitcroft wrote:
> On Mon, Jun 04, 2018 at 05:35:05PM -0400, Khaled Elmously wrote:
> > On 2018-05-29 14:38:27 , Andy Whitcroft wrote:
> > > The final field of a floppy_struct is the field "name", which is a pointer
> > > to a string in kernel memory.  The kernel pointer should not be copied to
> > > user memory.  The FDGETPRM ioctl copies a floppy_struct to user memory,
> > > including this "name" field.  This pointer cannot be used by the user
> > > and it will leak a kernel address to user-space, which will reveal the
> > > location of kernel code and data and undermine KASLR protection.
> > > 
> > > Model this code after the compat ioctl which copies the returned data
> > > to a previously cleared temporary structure on the stack (excluding the
> > > name pointer) and copy out to userspace from there.  As we already have
> > > an inparam union with an appropriate member and that memory is already
> > > cleared even for read only calls make use of that as a temporary store.
> > > 
> > > Based on an initial patch by Brian Belleville.
> > > 
> > > CVE-2018-7755
> > > Signed-off-by: Andy Whitcroft <apw@canonical.com>
> > > ---
> > >  drivers/block/floppy.c | 2 ++
> > >  1 file changed, 2 insertions(+)
> > > 
> > > diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> > > index eae484acfbbc..0a860603a5b6 100644
> > > --- a/drivers/block/floppy.c
> > > +++ b/drivers/block/floppy.c
> > > @@ -3470,6 +3470,8 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
> > >  					  (struct floppy_struct **)&outparam);
> > >  		if (ret)
> > >  			return ret;
> > > +		memcpy(&inparam.g, outparam, offsetof(struct floppy_struct, name));
> > > +		outparam = &inparam.g;
> > >  		break;
> > >  	case FDMSGON:
> > >  		UDP->flags |= FTD_MSG;
> > 
> > 
> > Not important, but I personally would have thought that just zeroing out the char * from outparam would be "cheaper" than copying everything _but_ the char pointer:
> > 
> > memset(outparam + offsetof(struct floppy_struct, name), 
> >        0, 
> >        sizeof(struct floppy_struct) - offsetof(struct floppy_struct, name));
> > 
> > 
> > Doesn't look like the savings would be significant though.
> 
> We can't do that because outparam as returned is pointing to the kernel
> internal store for the original data.  If we clear name in outparam we
> are clearing it for the kernel en-toto and would lose the name.

I see what you mean - thanks for explaining that :)

> 
> -apw
diff mbox series

Patch

diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index eae484acfbbc..0a860603a5b6 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -3470,6 +3470,8 @@  static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
 					  (struct floppy_struct **)&outparam);
 		if (ret)
 			return ret;
+		memcpy(&inparam.g, outparam, offsetof(struct floppy_struct, name));
+		outparam = &inparam.g;
 		break;
 	case FDMSGON:
 		UDP->flags |= FTD_MSG;