Skip to contentSkip to conversation

char: ppdev: check if ioctl argument is present and valid

PATCH In review Harshal Chaudhari 6 participants 11 messages Oct 8, 2020 – Oct 24, 2020 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 char: ppdev: check if ioctl argument is present and valid

Checking the argument passed to the ioctl is valid or not. if not then return -EINVAL.

---
 drivers/char/ppdev.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/char/ppdev.c +11 -1
index 38b46c7d1737..001392980202 100644
@@ -354,7 +354,7 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	unsigned int minor = iminor(file_inode(file));
 	struct pp_struct *pp = file->private_data;
 	struct parport *port;
-	void __user *argp = (void __user *)arg;
+	void __user *argp = NULL;
 	struct ieee1284_info *info;
 	unsigned char reg;
 	unsigned char mask;
@@ -364,6 +364,16 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	struct timespec64 ts;
 	int ret;
 
+	if (_IOC_TYPE(cmd) != PP_IOCTL)
+		return -ENOTTY;
+
+	/* check if ioctl argument is present and valid */
+	if (_IOC_DIR(cmd) != _IOC_NONE) {
+		argp = (void __user *)arg;
+		if (!argp)
+			return -EINVAL;
+	}
+
 	/* First handle the cases that don't take arguments. */
 	switch (cmd) {
 	case PPCLAIM:
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 14 lines · Harshal Chaudhari

Checking the argument passed to the ioctl is valid or not. if not then return -EINVAL.

---
 drivers/char/ppdev.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/char/ppdev.c +0 -0
index 38b46c7d1737..001392980202 100644

drivers/char/ppdev.c static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg) · line 360

Quoted 8 lines
drivers/char/ppdev.c +1 -1
@@ -354,7 +354,7 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        unsigned int minor = iminor(file_inode(file));
        struct pp_struct *pp = file->private_data;
        struct parport *port;
-       void __user *argp = (void __user *)arg;
+       void __user *argp = NULL;
        struct ieee1284_info *info;
        unsigned char reg;
        unsigned char mask;

Assigning to NULL serves no purpose here.

drivers/char/ppdev.c static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg) · line 368

Quoted 6 lines
drivers/char/ppdev.c +2 -0
@@ -364,6 +364,16 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        struct timespec64 ts;
        int ret;
 
+       if (_IOC_TYPE(cmd) != PP_IOCTL)
+               return -ENOTTY;

This looks correct but is normally done as a "default" case

drivers/char/ppdev.c static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg) · line 375

Quoted 6 lines
drivers/char/ppdev.c +6 -0
+       /* check if ioctl argument is present and valid */
+       if (_IOC_DIR(cmd) != _IOC_NONE) {
+               argp = (void __user *)arg;
+               if (!argp)
+                       return -EINVAL;
+       }

This is a change in behavior, it changes the return code from the correct -EFAULT to an unusual -EINVAL for the special case that the pointer is NULL compared to other invalid pointers.

Arnd

maintainer
#2 lore raw
In reply to Harshal Chaudhari
Quoted 2 lines · Harshal Chaudhari

Checking the argument passed to the ioctl is valid or not. if not then return -EINVAL.

Along the the comments that Arnd made, this is not the correct value to be returning from an ioctl when you don't pass in the correct command.

And it doesn't match what your patch says, please be consistent.

And do you have this device to be able to test your changes?

thanks,

greg k-h

maintainer
#3 lore raw
Quoted 11 lines · Greg KH
Quoted 2 lines · Harshal Chaudhari

Checking the argument passed to the ioctl is valid or not. if not then return -EINVAL.

Along the the comments that Arnd made, this is not the correct value to be returning from an ioctl when you don't pass in the correct command.

And it doesn't match what your patch says, please be consistent.

And do you have this device to be able to test your changes?

I will test this tomorrow. But from an initial look, its going to break ppdev. There are few ioctls which don't need any arguments.

Signature · 2 lines
Regards
Sudip
maintainer
#4 lore raw
Quoted 10 lines · Sudip Mukherjee
Quoted 8 lines · Greg KH
Quoted 2 lines · Harshal Chaudhari

Checking the argument passed to the ioctl is valid or not. if not then return -EINVAL.

Along the the comments that Arnd made, this is not the correct value to be returning from an ioctl when you don't pass in the correct command.

Quoted 6 lines
Quoted 3 lines

And it doesn't match what your patch says, please be consistent.

And do you have this device to be able to test your changes?

I will test this tomorrow. But from an initial look, its going to break ppdev. There are few ioctls which don't need any arguments.

No, sorry. I missed the check for _IOC_NONE.
Tested on a desktop which has a parallel port with a very basic test code of open->claim->write->release->close and it still works.

Signature · 2 lines
Regards
Sudip
#5 lore raw
Quoted 4 lines · Sudip Mukherjee
Quoted 1 line · Sudip Mukherjee
Quoted 8 lines
Quoted 8 lines · Greg KH
Quoted 7 lines · Harshal Chaudhari
Quoted 2 lines

Checking the argument passed to the ioctl is valid or not. if not then return -EINVAL.

Along the the comments that Arnd made, this is not the correct value to be returning from an ioctl when you don't pass in the correct command.

Thanks Greg for the comment. i am checking with value -EFAULT now, i will get back to you with changes as consideration of Arnd comments.

Quoted 1 line
Quoted 1 line
Quoted 1 line

And it doesn't match what your patch says, please be consistent.

I just want to perform the Argument check here only. back then i was trying with access_ok() as well, but access_ok() return success even if i passed a NULL pointer. so that's why i removed it from here.

Quoted 1 line
Quoted 1 line
Quoted 1 line

And do you have this device to be able to test your changes?

Yes I have a device and I tested these changes with the few ioctls.

Quoted 6 lines
Quoted 2 lines

I will test this tomorrow. But from an initial look, its going to break ppdev. There are few ioctls which don't need any arguments.

No, sorry. I missed the check for _IOC_NONE.
Tested on a desktop which has a parallel port with a very basic test code of open->claim->write->release->close and it still works.

Thanks a lot Sudip for your time.

Quoted 3 lines

--
Regards
Sudip

maintainer
#6 lore raw
Quoted 12 lines · harshal chaudhari
Quoted 10 lines · Sudip Mukherjee
Quoted 7 lines · Sudip Mukherjee
Quoted 5 lines · Greg KH
Quoted 2 lines · Harshal Chaudhari

Checking the argument passed to the ioctl is valid or not. if not then return -EINVAL.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Along the the comments that Arnd made, this is not the correct value to be returning from an ioctl when you don't pass in the correct command.

Thanks Greg for the comment. i am checking with value -EFAULT now, i will get back to you with changes as consideration of Arnd comments.

-EFAULT is only used if there really is a memory fault, which I strongly doubt is the case here.

Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

And it doesn't match what your patch says, please be consistent.

I just want to perform the Argument check here only. back then i was trying with access_ok() as well, but access_ok() return success even if i passed a NULL pointer. so that's why i removed it from here.

access_ok() should never be used in drivers anymore.

thanks,

greg k-h

#7 lore raw
In reply to harshal chaudhari

From: harshal chaudhari

Quoted 21 lines

Sent: 13 October 2020 10:01

Quoted 4 lines · Sudip Mukherjee
Quoted 1 line · Sudip Mukherjee
Quoted 8 lines
Quoted 8 lines · Greg KH
Quoted 7 lines · Harshal Chaudhari
Quoted 2 lines

Checking the argument passed to the ioctl is valid or not. if not then return -EINVAL.

Along the the comments that Arnd made, this is not the correct value to be returning from an ioctl when you don't pass in the correct command.

Thanks Greg for the comment. i am checking with value -EFAULT now, i will get back to you with changes as consideration of Arnd comments.

Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

And it doesn't match what your patch says, please be consistent.

I just want to perform the Argument check here only. back then i was trying with access_ok() as well, but access_ok() return success even if i passed a NULL pointer. so that's why i removed it from here.

Why bother.
You have to check the copy_from_user() whatever else you might have checked earlied.
So why optimise for the error case that never happens.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

A message that is not in the archive (sent off-list or not yet received): CAFEvwu=76mPtXSEgpwSoRC0rC0tkU5BiEx1X5O2VwVSPJ7m4Rw@mail.gmail.com

#9 lore raw
Quoted 19 lines · harshal chaudhari

On Tue, Oct 13, 2020 at 4:42 PM David Laight <[email protected]> wrote:

So I am a little bit confused about this check whether it's required or not Please could you point me in the right direction?

In any case, thanks for your help ...

Here is a driver source located in: linux/drivers/misc/xilinx_sdfec.c

static long xsdfec_dev_ioctl(struct file *fptr, unsigned int cmd, unsigned long data)
{
struct xsdfec_dev *xsdfec;
void __user *arg = NULL;
int rval = -EINVAL;

if (_IOC_TYPE(cmd) != XSDFEC_MAGIC)

               return -ENOTTY;
Quoted 8 lines

/* check if ioctl argument is present and valid */ if (_IOC_DIR(cmd) != _IOC_NONE)

{
        arg = (void __user *)data;
        if (!arg)
                   return rval;
}

All of this can be removed, and replaced with unconditional

     void __user *arg = (void __user *)data;
     int rval;

with an "rval = -ENOTTY" added in the 'default' case. This will make it behave more like other drivers, returning -ENOTTY for any unknown ioctl command, and returning -EFAULT for all invalid pointers, including NULL.

xsdfec_dev_compat_ioctl() can removed as well, with the file operations changed to an unconditional (removing the #ifdef)

.compat_ioctl = compat_ptr_ioctl(),

This will not affect behavior, it's just another simplification.

Arnd

#10 lore raw
Quoted 41 lines · Arnd Bergmann
Quoted 19 lines · harshal chaudhari

On Tue, Oct 13, 2020 at 4:42 PM David Laight <[email protected]> wrote:

So I am a little bit confused about this check whether it's required or not Please could you point me in the right direction?

In any case, thanks for your help ...

Here is a driver source located in: linux/drivers/misc/xilinx_sdfec.c

static long xsdfec_dev_ioctl(struct file *fptr, unsigned int cmd, unsigned long data)
{
struct xsdfec_dev *xsdfec;
void __user *arg = NULL;
int rval = -EINVAL;

if (_IOC_TYPE(cmd) != XSDFEC_MAGIC)

               return -ENOTTY;
Quoted 8 lines

/* check if ioctl argument is present and valid */ if (_IOC_DIR(cmd) != _IOC_NONE)

{
        arg = (void __user *)data;
        if (!arg)
                   return rval;
}

All of this can be removed, and replaced with unconditional

     void __user *arg = (void __user *)data;
     int rval;

with an "rval = -ENOTTY" added in the 'default' case. This will make it behave more like other drivers, returning -ENOTTY for any unknown ioctl command, and returning -EFAULT for all invalid pointers, including NULL.

Quoted 6 lines

xsdfec_dev_compat_ioctl() can removed as well, with the file operations changed to an unconditional (removing the #ifdef)

.compat_ioctl = compat_ptr_ioctl(),

This will not affect behavior, it's just another simplification.

Thanks a lot Arnd. Got the point. and i will make the change and i will send it to relevant maintainers of that driver file.

Once again thanks!

Quoted 1 line

Arnd

#11 lore raw
In reply to Arnd Bergmann

From: Arnd Bergmann

Quoted 38 lines

Sent: 24 October 2020 20:21
To: harshal chaudhari <[email protected]>
Cc: David Laight <[email protected]>; Greg KH <[email protected]>; Sudip Mukherjee <[email protected]>; linux-kernel <[email protected]>
Subject: Re: [PATCH] char: ppdev: check if ioctl argument is present and valid

Quoted 19 lines · harshal chaudhari

On Tue, Oct 13, 2020 at 4:42 PM David Laight <[email protected]> wrote:

So I am a little bit confused about this check whether it's required or not Please could you point me in the right direction?

In any case, thanks for your help ...

Here is a driver source located in: linux/drivers/misc/xilinx_sdfec.c

static long xsdfec_dev_ioctl(struct file *fptr, unsigned int cmd, unsigned long data)
{
struct xsdfec_dev *xsdfec;
void __user *arg = NULL;
int rval = -EINVAL;

if (_IOC_TYPE(cmd) != XSDFEC_MAGIC)

               return -ENOTTY;
Quoted 8 lines

/* check if ioctl argument is present and valid */ if (_IOC_DIR(cmd) != _IOC_NONE)

{
        arg = (void __user *)data;
        if (!arg)
                   return rval;
}

All of this can be removed, and replaced with unconditional

Quoted 7 lines
     void __user *arg = (void __user *)data;
     int rval;

with an "rval = -ENOTTY" added in the 'default' case. This will make it behave more like other drivers, returning -ENOTTY for any unknown ioctl command, and returning -EFAULT for all invalid pointers, including NULL.

Yep, the thing to remember is that even if you actually verified that the user buffer could be accessed on entry to the ioctl code another application thread could unmap the memory area before you do a later access.

What you may want to do is copy the user buffer into a kernel buffer at the top of the ioctl code and write it back at the bottom.
But do make absolutely sure you don't overflow the kernel buffer of access beyond its end.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)