]> git.ipfire.org Git - people/arne_f/kernel.git/commitdiff
mtd: properly check all write ioctls for permissions
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 16 Jul 2020 11:53:46 +0000 (13:53 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 21 Aug 2020 07:47:58 +0000 (09:47 +0200)
commit f7e6b19bc76471ba03725fe58e0c218a3d6266c3 upstream.

When doing a "write" ioctl call, properly check that we have permissions
to do so before copying anything from userspace or anything else so we
can "fail fast".  This includes also covering the MEMWRITE ioctl which
previously missed checking for this.

Cc: Miquel Raynal <miquel.raynal@bootlin.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Vignesh Raghavendra <vigneshr@ti.com>
Cc: stable <stable@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[rw: Fixed locking issue]
Signed-off-by: Richard Weinberger <richard@nod.at>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/mtd/mtdchar.c

index fa4d12217652b542fdd5683a30b6c9cbbbfd9522..18dd333f2d4075054f50a90f123b399533915e62 100644 (file)
@@ -372,9 +372,6 @@ static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
        uint32_t retlen;
        int ret = 0;
 
-       if (!(file->f_mode & FMODE_WRITE))
-               return -EPERM;
-
        if (length > 4096)
                return -EINVAL;
 
@@ -681,6 +678,48 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
                        return -EFAULT;
        }
 
+       /*
+        * Check the file mode to require "dangerous" commands to have write
+        * permissions.
+        */
+       switch (cmd) {
+       /* "safe" commands */
+       case MEMGETREGIONCOUNT:
+       case MEMGETREGIONINFO:
+       case MEMGETINFO:
+       case MEMREADOOB:
+       case MEMREADOOB64:
+       case MEMLOCK:
+       case MEMUNLOCK:
+       case MEMISLOCKED:
+       case MEMGETOOBSEL:
+       case MEMGETBADBLOCK:
+       case MEMSETBADBLOCK:
+       case OTPSELECT:
+       case OTPGETREGIONCOUNT:
+       case OTPGETREGIONINFO:
+       case OTPLOCK:
+       case ECCGETLAYOUT:
+       case ECCGETSTATS:
+       case MTDFILEMODE:
+       case BLKPG:
+       case BLKRRPART:
+               break;
+
+       /* "dangerous" commands */
+       case MEMERASE:
+       case MEMERASE64:
+       case MEMWRITEOOB:
+       case MEMWRITEOOB64:
+       case MEMWRITE:
+               if (!(file->f_mode & FMODE_WRITE))
+                       return -EPERM;
+               break;
+
+       default:
+               return -ENOTTY;
+       }
+
        switch (cmd) {
        case MEMGETREGIONCOUNT:
                if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
@@ -728,9 +767,6 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
        {
                struct erase_info *erase;
 
-               if(!(file->f_mode & FMODE_WRITE))
-                       return -EPERM;
-
                erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
                if (!erase)
                        ret = -ENOMEM;
@@ -1051,9 +1087,6 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
                ret = 0;
                break;
        }
-
-       default:
-               ret = -ENOTTY;
        }
 
        return ret;
@@ -1097,6 +1130,11 @@ static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
                struct mtd_oob_buf32 buf;
                struct mtd_oob_buf32 __user *buf_user = argp;
 
+               if (!(file->f_mode & FMODE_WRITE)) {
+                       ret = -EPERM;
+                       break;
+               }
+
                if (copy_from_user(&buf, argp, sizeof(buf)))
                        ret = -EFAULT;
                else