Commit 26a2e20f authored by Alan Cox's avatar Alan Cox Committed by Linus Torvalds

tty: Untangle termios and mm mutex dependencies

Although this doesn't cause any problems it could potentially do so for
future mmap using devices. No real work is needed to sort it out so untangle
it before it causes problems
Signed-off-by: default avatarAlan Cox <alan@linux.intel.com>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 0b914218
...@@ -626,9 +626,25 @@ static int set_termios(struct tty_struct *tty, void __user *arg, int opt) ...@@ -626,9 +626,25 @@ static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
return 0; return 0;
} }
static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
{
mutex_lock(&tty->termios_mutex);
memcpy(kterm, tty->termios, sizeof(struct ktermios));
mutex_unlock(&tty->termios_mutex);
}
static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
{
mutex_lock(&tty->termios_mutex);
memcpy(kterm, tty->termios_locked, sizeof(struct ktermios));
mutex_unlock(&tty->termios_mutex);
}
static int get_termio(struct tty_struct *tty, struct termio __user *termio) static int get_termio(struct tty_struct *tty, struct termio __user *termio)
{ {
if (kernel_termios_to_user_termio(termio, tty->termios)) struct ktermios kterm;
copy_termios(tty, &kterm);
if (kernel_termios_to_user_termio(termio, &kterm))
return -EFAULT; return -EFAULT;
return 0; return 0;
} }
...@@ -930,6 +946,8 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file, ...@@ -930,6 +946,8 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
struct tty_struct *real_tty; struct tty_struct *real_tty;
void __user *p = (void __user *)arg; void __user *p = (void __user *)arg;
int ret = 0; int ret = 0;
struct ktermios kterm;
struct termiox ktermx;
if (tty->driver->type == TTY_DRIVER_TYPE_PTY && if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
tty->driver->subtype == PTY_TYPE_MASTER) tty->driver->subtype == PTY_TYPE_MASTER)
...@@ -965,23 +983,20 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file, ...@@ -965,23 +983,20 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
return set_termios(real_tty, p, TERMIOS_OLD); return set_termios(real_tty, p, TERMIOS_OLD);
#ifndef TCGETS2 #ifndef TCGETS2
case TCGETS: case TCGETS:
mutex_lock(&real_tty->termios_mutex); copy_termios(real_tty, &kterm);
if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios)) if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
ret = -EFAULT; ret = -EFAULT;
mutex_unlock(&real_tty->termios_mutex);
return ret; return ret;
#else #else
case TCGETS: case TCGETS:
mutex_lock(&real_tty->termios_mutex); copy_termios(real_tty, &kterm);
if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios)) if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
ret = -EFAULT; ret = -EFAULT;
mutex_unlock(&real_tty->termios_mutex);
return ret; return ret;
case TCGETS2: case TCGETS2:
mutex_lock(&real_tty->termios_mutex); copy_termios(real_tty, &kterm);
if (kernel_termios_to_user_termios((struct termios2 __user *)arg, real_tty->termios)) if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
ret = -EFAULT; ret = -EFAULT;
mutex_unlock(&real_tty->termios_mutex);
return ret; return ret;
case TCSETSF2: case TCSETSF2:
return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT); return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
...@@ -1000,34 +1015,36 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file, ...@@ -1000,34 +1015,36 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
return set_termios(real_tty, p, TERMIOS_TERMIO); return set_termios(real_tty, p, TERMIOS_TERMIO);
#ifndef TCGETS2 #ifndef TCGETS2
case TIOCGLCKTRMIOS: case TIOCGLCKTRMIOS:
mutex_lock(&real_tty->termios_mutex); copy_termios_locked(real_tty, &kterm);
if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked)) if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
ret = -EFAULT; ret = -EFAULT;
mutex_unlock(&real_tty->termios_mutex);
return ret; return ret;
case TIOCSLCKTRMIOS: case TIOCSLCKTRMIOS:
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
return -EPERM; return -EPERM;
mutex_lock(&real_tty->termios_mutex); copy_termios_locked(real_tty, &kterm);
if (user_termios_to_kernel_termios(real_tty->termios_locked, if (user_termios_to_kernel_termios(&kterm,
(struct termios __user *) arg)) (struct termios __user *) arg))
ret = -EFAULT; return -EFAULT;
mutex_lock(&real_tty->termios_mutex);
memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
mutex_unlock(&real_tty->termios_mutex); mutex_unlock(&real_tty->termios_mutex);
return ret; return 0;
#else #else
case TIOCGLCKTRMIOS: case TIOCGLCKTRMIOS:
mutex_lock(&real_tty->termios_mutex); copy_termios_locked(real_tty, &kterm);
if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios_locked)) if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
ret = -EFAULT; ret = -EFAULT;
mutex_unlock(&real_tty->termios_mutex);
return ret; return ret;
case TIOCSLCKTRMIOS: case TIOCSLCKTRMIOS:
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
ret = -EPERM; return -EPERM;
mutex_lock(&real_tty->termios_mutex); copy_termios_locked(real_tty, &kterm);
if (user_termios_to_kernel_termios_1(real_tty->termios_locked, if (user_termios_to_kernel_termios_1(&kterm,
(struct termios __user *) arg)) (struct termios __user *) arg))
ret = -EFAULT; return -EFAULT;
mutex_lock(&real_tty->termios_mutex);
memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
mutex_unlock(&real_tty->termios_mutex); mutex_unlock(&real_tty->termios_mutex);
return ret; return ret;
#endif #endif
...@@ -1036,9 +1053,10 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file, ...@@ -1036,9 +1053,10 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
if (real_tty->termiox == NULL) if (real_tty->termiox == NULL)
return -EINVAL; return -EINVAL;
mutex_lock(&real_tty->termios_mutex); mutex_lock(&real_tty->termios_mutex);
if (copy_to_user(p, real_tty->termiox, sizeof(struct termiox))) memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
ret = -EFAULT;
mutex_unlock(&real_tty->termios_mutex); mutex_unlock(&real_tty->termios_mutex);
if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
ret = -EFAULT;
return ret; return ret;
case TCSETX: case TCSETX:
return set_termiox(real_tty, p, 0); return set_termiox(real_tty, p, 0);
...@@ -1048,10 +1066,9 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file, ...@@ -1048,10 +1066,9 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
return set_termiox(real_tty, p, TERMIOS_FLUSH); return set_termiox(real_tty, p, TERMIOS_FLUSH);
#endif #endif
case TIOCGSOFTCAR: case TIOCGSOFTCAR:
mutex_lock(&real_tty->termios_mutex); copy_termios(real_tty, &kterm);
ret = put_user(C_CLOCAL(real_tty) ? 1 : 0, ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
(int __user *)arg); (int __user *)arg);
mutex_unlock(&real_tty->termios_mutex);
return ret; return ret;
case TIOCSSOFTCAR: case TIOCSSOFTCAR:
if (get_user(arg, (unsigned int __user *) arg)) if (get_user(arg, (unsigned int __user *) arg))
......
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