@@ -355,9 +355,6 @@ static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
355
355
uint32_t retlen ;
356
356
int ret = 0 ;
357
357
358
- if (!(file -> f_mode & FMODE_WRITE ))
359
- return - EPERM ;
360
-
361
358
if (length > 4096 )
362
359
return - EINVAL ;
363
360
@@ -643,6 +640,48 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
643
640
644
641
pr_debug ("MTD_ioctl\n" );
645
642
643
+ /*
644
+ * Check the file mode to require "dangerous" commands to have write
645
+ * permissions.
646
+ */
647
+ switch (cmd ) {
648
+ /* "safe" commands */
649
+ case MEMGETREGIONCOUNT :
650
+ case MEMGETREGIONINFO :
651
+ case MEMGETINFO :
652
+ case MEMREADOOB :
653
+ case MEMREADOOB64 :
654
+ case MEMLOCK :
655
+ case MEMUNLOCK :
656
+ case MEMISLOCKED :
657
+ case MEMGETOOBSEL :
658
+ case MEMGETBADBLOCK :
659
+ case MEMSETBADBLOCK :
660
+ case OTPSELECT :
661
+ case OTPGETREGIONCOUNT :
662
+ case OTPGETREGIONINFO :
663
+ case OTPLOCK :
664
+ case ECCGETLAYOUT :
665
+ case ECCGETSTATS :
666
+ case MTDFILEMODE :
667
+ case BLKPG :
668
+ case BLKRRPART :
669
+ break ;
670
+
671
+ /* "dangerous" commands */
672
+ case MEMERASE :
673
+ case MEMERASE64 :
674
+ case MEMWRITEOOB :
675
+ case MEMWRITEOOB64 :
676
+ case MEMWRITE :
677
+ if (!(file -> f_mode & FMODE_WRITE ))
678
+ return - EPERM ;
679
+ break ;
680
+
681
+ default :
682
+ return - ENOTTY ;
683
+ }
684
+
646
685
switch (cmd ) {
647
686
case MEMGETREGIONCOUNT :
648
687
if (copy_to_user (argp , & (mtd -> numeraseregions ), sizeof (int )))
@@ -690,9 +729,6 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
690
729
{
691
730
struct erase_info * erase ;
692
731
693
- if (!(file -> f_mode & FMODE_WRITE ))
694
- return - EPERM ;
695
-
696
732
erase = kzalloc (sizeof (struct erase_info ),GFP_KERNEL );
697
733
if (!erase )
698
734
ret = - ENOMEM ;
@@ -985,9 +1021,6 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
985
1021
ret = 0 ;
986
1022
break ;
987
1023
}
988
-
989
- default :
990
- ret = - ENOTTY ;
991
1024
}
992
1025
993
1026
return ret ;
@@ -1031,6 +1064,11 @@ static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
1031
1064
struct mtd_oob_buf32 buf ;
1032
1065
struct mtd_oob_buf32 __user * buf_user = argp ;
1033
1066
1067
+ if (!(file -> f_mode & FMODE_WRITE )) {
1068
+ ret = - EPERM ;
1069
+ break ;
1070
+ }
1071
+
1034
1072
if (copy_from_user (& buf , argp , sizeof (buf )))
1035
1073
ret = - EFAULT ;
1036
1074
else
0 commit comments