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: Alan Cox <alan@linux.intel.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Alan Cox 2009-06-11 14:03:13 +01:00 committed by Linus Torvalds
parent 0b91421857
commit 26a2e20f4a
1 changed files with 46 additions and 29 deletions

View File

@ -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,
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,
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,
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,
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,
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))