Skip to contentSkip to conversation

- catch ignored copy_*_user() - fs/compat_ioctl.c

PATCH In review Jeremy Huddleston 2 participants 2 messages Dec 25, 2004 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 - catch ignored copy_*_user() - fs/compat_ioctl.c

And one more...

Catch and handle some previously ignored copy_*_user() calls

fs/compat_ioctl.c +28 -25
diff -Naurp linux-2.6.10-rc3-bk8/fs/compat_ioctl.c linux-2.6.10-rc3-bk8.edit/fs/compat_ioctl.c
@@ -645,8 +645,9 @@ int siocdevprivate_ioctl(unsigned int fd
 	/* Don't check these user accesses, just let that get trapped
 	 * in the ioctl handler instead.
 	 */
-	copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0], IFNAMSIZ);
-	__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data);
+	if(copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0], IFNAMSIZ) ||
+	   __put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
+		return -EFAULT;
 
 	return sys_ioctl(fd, cmd, (unsigned long) u_ifreq64);
 }
@@ -2334,17 +2335,19 @@ static int mtd_rw_oob(unsigned int fd, u
 static long
 put_dirent32 (struct dirent *d, struct compat_dirent __user *d32)
 {
-        int ret;
+	int ret;
 
-        if ((ret = verify_area(VERIFY_WRITE, d32,
-                               sizeof(struct compat_dirent))))
-                return ret;
+	if ((ret = verify_area(VERIFY_WRITE, d32,
+	                       sizeof(struct compat_dirent))))
+		return ret;
+
+	if(__put_user(d->d_ino, &d32->d_ino) ||
+	   __put_user(d->d_off, &d32->d_off) ||
+	   __put_user(d->d_reclen, &d32->d_reclen) ||
+	   __copy_to_user(d32->d_name, d->d_name, d->d_reclen) )
+		return -EFAULT;
 
-        __put_user(d->d_ino, &d32->d_ino);
-        __put_user(d->d_off, &d32->d_off);
-        __put_user(d->d_reclen, &d32->d_reclen);
-        __copy_to_user(d32->d_name, d->d_name, d->d_reclen);
-        return ret;
+	return ret;
 }
 
 static int vfat_ioctl32(unsigned fd, unsigned cmd, unsigned long arg)
@@ -2484,28 +2487,28 @@ static int serial_struct_ioctl(unsigned 
         __u32 udata;
 
         if (cmd == TIOCSSERIAL) {
-                if (verify_area(VERIFY_READ, ss32, sizeof(SS32)))
-                        return -EFAULT;
-                __copy_from_user(&ss, ss32, offsetof(SS32, iomem_base));
-                __get_user(udata, &ss32->iomem_base);
+                if (verify_area(VERIFY_READ, ss32, sizeof(SS32)) ||
+                    __copy_from_user(&ss, ss32, offsetof(SS32, iomem_base)) ||
+                    __get_user(udata, &ss32->iomem_base) )
+			return -EFAULT;
                 ss.iomem_base = compat_ptr(udata);
-                __get_user(ss.iomem_reg_shift, &ss32->iomem_reg_shift);
-                __get_user(ss.port_high, &ss32->port_high);
+                if (__get_user(ss.iomem_reg_shift, &ss32->iomem_reg_shift) ||
+                    __get_user(ss.port_high, &ss32->port_high) )
+			return -EFAULT;
                 ss.iomap_base = 0UL;
         }
         set_fs(KERNEL_DS);
                 err = sys_ioctl(fd,cmd,(unsigned long)(&ss));
         set_fs(oldseg);
         if (cmd == TIOCGSERIAL && err >= 0) {
-                if (verify_area(VERIFY_WRITE, ss32, sizeof(SS32)))
+                if (verify_area(VERIFY_WRITE, ss32, sizeof(SS32)) ||
+                    __copy_to_user(ss32,&ss,offsetof(SS32,iomem_base)) ||
+                    __put_user((unsigned long)ss.iomem_base  >> 32 ?
+                               0xffffffff : (unsigned)(unsigned long)ss.iomem_base,
+                                &ss32->iomem_base) ||
+                    __put_user(ss.iomem_reg_shift, &ss32->iomem_reg_shift) ||
+                    __put_user(ss.port_high, &ss32->port_high) )
                         return -EFAULT;
-                __copy_to_user(ss32,&ss,offsetof(SS32,iomem_base));
-                __put_user((unsigned long)ss.iomem_base  >> 32 ?
-                            0xffffffff : (unsigned)(unsigned long)ss.iomem_base,
-                            &ss32->iomem_base);
-                __put_user(ss.iomem_reg_shift, &ss32->iomem_reg_shift);
-                __put_user(ss.port_high, &ss32->port_high);
-
         }
         return err;
 }
#1 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Jeremy,

Quoted 1 line · Jeremy Huddleston

And one more...

Patch attached not inlined, so you have to figure yourself :-(

Could you do the "binary OR trick"?

Such that

if (__put_user() || __put_user() || __put_user()) return -EFAULT;

becomes:

err = __put_user();
err |= __put_user();
err |= __put_user();
if (err) return -EFAULT;

This saves branches, which speeds up code for the common case and reduces size. Downside is more faults, but this hurts only for broken software, which deserves this behavior ;-)

This optimization works only if:
- function returns either zero or exact the same value.
- we don't mind having all these other calls executed, even if the first failed already.

Which is true for __put_user() and __get_user(), but works the other way around for __copy_{from,to}_user(). But since 0 is succes on __copy_{from,to}_user(), you can even do this trick there and have only ONE branch instead of 3-6 in your code.

You'll find this pattern in many places in the Kernel.

Regards

Ingo Oeser

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBzbLGU56oYWuOrkARAlGkAJ4nm909AVDY96Q9PKE92tbGKbAUygCfT+64 oRC6L0uwFbL14o0ThhGnMY0=
=Mj1F
-----END PGP SIGNATURE-----