[PATCH] char: ppdev: check if ioctl argument is present and valid

From: Harshal Chaudhari
Date: Thu Oct 08 2020 - 14:27:32 EST


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

Signed-off-by: Harshal Chaudhari <harshalchau04@xxxxxxxxx>
---
drivers/char/ppdev.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/char/ppdev.c b/drivers/char/ppdev.c
index 38b46c7d1737..001392980202 100644
--- a/drivers/char/ppdev.c
+++ b/drivers/char/ppdev.c
@@ -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:
--
2.17.1