Commit a7f61e89 authored by Jann Horn's avatar Jann Horn Committed by Al Viro

compat_ioctl: don't call do_ioctl under set_fs(KERNEL_DS)

This replaces all code in fs/compat_ioctl.c that translated
ioctl arguments into a in-kernel structure, then performed
do_ioctl under set_fs(KERNEL_DS), with code that allocates
data on the user stack and can call the VFS ioctl handler
under USER_DS.

This is done as a hardening measure because the caller
does not know what kind of ioctl handler will be invoked,
only that no corresponding compat_ioctl handler exists and
what the ioctl command number is. The accidental
invocation of an unlocked_ioctl handler that unexpectedly
calls copy_to_user could be a severe security issue.
Signed-off-by: default avatarJann Horn <jann@thejh.net>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 66cf191f
...@@ -117,6 +117,13 @@ ...@@ -117,6 +117,13 @@
#include <asm/fbio.h> #include <asm/fbio.h>
#endif #endif
#define convert_in_user(srcptr, dstptr) \
({ \
typeof(*srcptr) val; \
\
get_user(val, srcptr) || put_user(val, dstptr); \
})
static int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg) static int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{ {
int err; int err;
...@@ -131,16 +138,17 @@ static int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg) ...@@ -131,16 +138,17 @@ static int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
static int w_long(struct file *file, static int w_long(struct file *file,
unsigned int cmd, compat_ulong_t __user *argp) unsigned int cmd, compat_ulong_t __user *argp)
{ {
mm_segment_t old_fs = get_fs();
int err; int err;
unsigned long val; unsigned long __user *valp = compat_alloc_user_space(sizeof(*valp));
set_fs (KERNEL_DS); if (valp == NULL)
err = do_ioctl(file, cmd, (unsigned long)&val);
set_fs (old_fs);
if (!err && put_user(val, argp))
return -EFAULT; return -EFAULT;
return err; err = do_ioctl(file, cmd, (unsigned long)valp);
if (err)
return err;
if (convert_in_user(valp, argp))
return -EFAULT;
return 0;
} }
struct compat_video_event { struct compat_video_event {
...@@ -155,20 +163,20 @@ struct compat_video_event { ...@@ -155,20 +163,20 @@ struct compat_video_event {
static int do_video_get_event(struct file *file, static int do_video_get_event(struct file *file,
unsigned int cmd, struct compat_video_event __user *up) unsigned int cmd, struct compat_video_event __user *up)
{ {
struct video_event kevent; struct video_event __user *kevent =
mm_segment_t old_fs = get_fs(); compat_alloc_user_space(sizeof(*kevent));
int err; int err;
set_fs(KERNEL_DS); if (kevent == NULL)
err = do_ioctl(file, cmd, (unsigned long) &kevent); return -EFAULT;
set_fs(old_fs);
err = do_ioctl(file, cmd, (unsigned long)kevent);
if (!err) { if (!err) {
err = put_user(kevent.type, &up->type); err = convert_in_user(&kevent->type, &up->type);
err |= put_user(kevent.timestamp, &up->timestamp); err |= convert_in_user(&kevent->timestamp, &up->timestamp);
err |= put_user(kevent.u.size.w, &up->u.size.w); err |= convert_in_user(&kevent->u.size.w, &up->u.size.w);
err |= put_user(kevent.u.size.h, &up->u.size.h); err |= convert_in_user(&kevent->u.size.h, &up->u.size.h);
err |= put_user(kevent.u.size.aspect_ratio, err |= convert_in_user(&kevent->u.size.aspect_ratio,
&up->u.size.aspect_ratio); &up->u.size.aspect_ratio);
if (err) if (err)
err = -EFAULT; err = -EFAULT;
...@@ -528,10 +536,10 @@ struct mtpos32 { ...@@ -528,10 +536,10 @@ struct mtpos32 {
static int mt_ioctl_trans(struct file *file, static int mt_ioctl_trans(struct file *file,
unsigned int cmd, void __user *argp) unsigned int cmd, void __user *argp)
{ {
mm_segment_t old_fs = get_fs(); /* NULL initialization to make gcc shut up */
struct mtget get; struct mtget __user *get = NULL;
struct mtget32 __user *umget32; struct mtget32 __user *umget32;
struct mtpos pos; struct mtpos __user *pos = NULL;
struct mtpos32 __user *upos32; struct mtpos32 __user *upos32;
unsigned long kcmd; unsigned long kcmd;
void *karg; void *karg;
...@@ -540,32 +548,34 @@ static int mt_ioctl_trans(struct file *file, ...@@ -540,32 +548,34 @@ static int mt_ioctl_trans(struct file *file,
switch(cmd) { switch(cmd) {
case MTIOCPOS32: case MTIOCPOS32:
kcmd = MTIOCPOS; kcmd = MTIOCPOS;
karg = &pos; pos = compat_alloc_user_space(sizeof(*pos));
karg = pos;
break; break;
default: /* MTIOCGET32 */ default: /* MTIOCGET32 */
kcmd = MTIOCGET; kcmd = MTIOCGET;
karg = &get; get = compat_alloc_user_space(sizeof(*get));
karg = get;
break; break;
} }
set_fs (KERNEL_DS); if (karg == NULL)
return -EFAULT;
err = do_ioctl(file, kcmd, (unsigned long)karg); err = do_ioctl(file, kcmd, (unsigned long)karg);
set_fs (old_fs);
if (err) if (err)
return err; return err;
switch (cmd) { switch (cmd) {
case MTIOCPOS32: case MTIOCPOS32:
upos32 = argp; upos32 = argp;
err = __put_user(pos.mt_blkno, &upos32->mt_blkno); err = convert_in_user(&pos->mt_blkno, &upos32->mt_blkno);
break; break;
case MTIOCGET32: case MTIOCGET32:
umget32 = argp; umget32 = argp;
err = __put_user(get.mt_type, &umget32->mt_type); err = convert_in_user(&get->mt_type, &umget32->mt_type);
err |= __put_user(get.mt_resid, &umget32->mt_resid); err |= convert_in_user(&get->mt_resid, &umget32->mt_resid);
err |= __put_user(get.mt_dsreg, &umget32->mt_dsreg); err |= convert_in_user(&get->mt_dsreg, &umget32->mt_dsreg);
err |= __put_user(get.mt_gstat, &umget32->mt_gstat); err |= convert_in_user(&get->mt_gstat, &umget32->mt_gstat);
err |= __put_user(get.mt_erreg, &umget32->mt_erreg); err |= convert_in_user(&get->mt_erreg, &umget32->mt_erreg);
err |= __put_user(get.mt_fileno, &umget32->mt_fileno); err |= convert_in_user(&get->mt_fileno, &umget32->mt_fileno);
err |= __put_user(get.mt_blkno, &umget32->mt_blkno); err |= convert_in_user(&get->mt_blkno, &umget32->mt_blkno);
break; break;
} }
return err ? -EFAULT: 0; return err ? -EFAULT: 0;
...@@ -624,37 +634,36 @@ static int serial_struct_ioctl(struct file *file, ...@@ -624,37 +634,36 @@ static int serial_struct_ioctl(struct file *file,
{ {
typedef struct serial_struct32 SS32; typedef struct serial_struct32 SS32;
int err; int err;
struct serial_struct ss; struct serial_struct __user *ss = compat_alloc_user_space(sizeof(*ss));
mm_segment_t oldseg = get_fs();
__u32 udata; __u32 udata;
unsigned int base; unsigned int base;
unsigned char *iomem_base;
if (ss == NULL)
return -EFAULT;
if (cmd == TIOCSSERIAL) { if (cmd == TIOCSSERIAL) {
if (!access_ok(VERIFY_READ, ss32, sizeof(SS32))) if (copy_in_user(ss, ss32, offsetof(SS32, iomem_base)) ||
return -EFAULT; get_user(udata, &ss32->iomem_base))
if (__copy_from_user(&ss, ss32, offsetof(SS32, iomem_base)))
return -EFAULT; return -EFAULT;
if (__get_user(udata, &ss32->iomem_base)) iomem_base = compat_ptr(udata);
if (put_user(iomem_base, &ss->iomem_base) ||
convert_in_user(&ss32->iomem_reg_shift,
&ss->iomem_reg_shift) ||
convert_in_user(&ss32->port_high, &ss->port_high) ||
put_user(0UL, &ss->iomap_base))
return -EFAULT; return -EFAULT;
ss.iomem_base = compat_ptr(udata);
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 = do_ioctl(file, cmd, (unsigned long)ss);
err = do_ioctl(file, cmd, (unsigned long)&ss);
set_fs(oldseg);
if (cmd == TIOCGSERIAL && err >= 0) { if (cmd == TIOCGSERIAL && err >= 0) {
if (!access_ok(VERIFY_WRITE, ss32, sizeof(SS32))) if (copy_in_user(ss32, ss, offsetof(SS32, iomem_base)) ||
return -EFAULT; get_user(iomem_base, &ss->iomem_base))
if (__copy_to_user(ss32,&ss,offsetof(SS32,iomem_base)))
return -EFAULT; return -EFAULT;
base = (unsigned long)ss.iomem_base >> 32 ? base = (unsigned long)iomem_base >> 32 ?
0xffffffff : (unsigned)(unsigned long)ss.iomem_base; 0xffffffff : (unsigned)(unsigned long)iomem_base;
if (__put_user(base, &ss32->iomem_base) || if (put_user(base, &ss32->iomem_base) ||
__put_user(ss.iomem_reg_shift, &ss32->iomem_reg_shift) || convert_in_user(&ss->iomem_reg_shift,
__put_user(ss.port_high, &ss32->port_high)) &ss32->iomem_reg_shift) ||
convert_in_user(&ss->port_high, &ss32->port_high))
return -EFAULT; return -EFAULT;
} }
return err; return err;
...@@ -759,23 +768,20 @@ static int do_i2c_smbus_ioctl(struct file *file, ...@@ -759,23 +768,20 @@ static int do_i2c_smbus_ioctl(struct file *file,
static int rtc_ioctl(struct file *file, static int rtc_ioctl(struct file *file,
unsigned cmd, void __user *argp) unsigned cmd, void __user *argp)
{ {
mm_segment_t oldfs = get_fs(); unsigned long __user *valp = compat_alloc_user_space(sizeof(*valp));
compat_ulong_t val32;
unsigned long kval;
int ret; int ret;
if (valp == NULL)
return -EFAULT;
switch (cmd) { switch (cmd) {
case RTC_IRQP_READ32: case RTC_IRQP_READ32:
case RTC_EPOCH_READ32: case RTC_EPOCH_READ32:
set_fs(KERNEL_DS);
ret = do_ioctl(file, (cmd == RTC_IRQP_READ32) ? ret = do_ioctl(file, (cmd == RTC_IRQP_READ32) ?
RTC_IRQP_READ : RTC_EPOCH_READ, RTC_IRQP_READ : RTC_EPOCH_READ,
(unsigned long)&kval); (unsigned long)valp);
set_fs(oldfs);
if (ret) if (ret)
return ret; return ret;
val32 = kval; return convert_in_user(valp, (unsigned int __user *)argp);
return put_user(val32, (unsigned int __user *)argp);
case RTC_IRQP_SET32: case RTC_IRQP_SET32:
return do_ioctl(file, RTC_IRQP_SET, (unsigned long)argp); return do_ioctl(file, RTC_IRQP_SET, (unsigned long)argp);
case RTC_EPOCH_SET32: case RTC_EPOCH_SET32:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment