Commit 24ec839c authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Linus Torvalds

[PATCH] tty: ->signal->tty locking

Fix the locking of signal->tty.

Use ->sighand->siglock to protect ->signal->tty; this lock is already used
by most other members of ->signal/->sighand.  And unless we are 'current'
or the tasklist_lock is held we need ->siglock to access ->signal anyway.

(NOTE: sys_unshare() is broken wrt ->sighand locking rules)

Note that tty_mutex is held over tty destruction, so while holding
tty_mutex any tty pointer remains valid.  Otherwise the lifetime of ttys
are governed by their open file handles.  This leaves some holes for tty
access from signal->tty (or any other non file related tty access).

It solves the tty SLAB scribbles we were seeing.

(NOTE: the change from group_send_sig_info to __group_send_sig_info needs to
       be examined by someone familiar with the security framework, I think
       it is safe given the SEND_SIG_PRIV from other __group_send_sig_info
       invocations)

[schwidefsky@de.ibm.com: 3270 fix]
[akpm@osdl.org: various post-viro fixes]
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
Acked-by: default avatarAlan Cox <alan@redhat.com>
Cc: Oleg Nesterov <oleg@tv-sign.ru>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Chris Wright <chrisw@sous-sol.org>
Cc: Roland McGrath <roland@redhat.com>
Cc: Stephen Smalley <sds@tycho.nsa.gov>
Cc: James Morris <jmorris@namei.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Jeff Dike <jdike@addtoit.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Jan Kara <jack@ucw.cz>
Signed-off-by: default avatarMartin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 562f9c57
......@@ -423,9 +423,7 @@ asmlinkage int solaris_procids(int cmd, s32 pid, s32 pgid)
Solaris setpgrp and setsid? */
ret = sys_setpgid(0, 0);
if (ret) return ret;
mutex_lock(&tty_mutex);
current->signal->tty = NULL;
mutex_unlock(&tty_mutex);
proc_clear_tty(current);
return process_group(current);
}
case 2: /* getsid */
......
......@@ -39,12 +39,13 @@ static long execve1(char *file, char __user * __user *argv,
char __user *__user *env)
{
long error;
struct tty_struct *tty;
#ifdef CONFIG_TTY_LOG
mutex_lock(&tty_mutex);
task_lock(current); /* FIXME: is this needed ? */
log_exec(argv, current->signal->tty);
task_unlock(current);
tty = get_current_tty();
if (tty)
log_exec(argv, tty);
mutex_unlock(&tty_mutex);
#endif
error = do_execve(file, argv, env, &current->thread.regs);
......
This diff is collapsed.
......@@ -424,11 +424,15 @@ fs3270_open(struct inode *inode, struct file *filp)
minor = iminor(filp->f_dentry->d_inode);
/* Check for minor 0 multiplexer. */
if (minor == 0) {
if (!current->signal->tty)
struct tty_struct *tty;
mutex_lock(&tty_mutex);
tty = get_current_tty();
if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
mutex_unlock(&tty_mutex);
return -ENODEV;
if (current->signal->tty->driver->major != IBM_TTY3270_MAJOR)
return -ENODEV;
minor = current->signal->tty->index + RAW3270_FIRSTMINOR;
}
minor = tty->index + RAW3270_FIRSTMINOR;
mutex_unlock(&tty_mutex);
}
/* Check if some other program is already using fullscreen mode. */
fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
......
......@@ -828,6 +828,7 @@ static inline int need_print_warning(struct dquot *dquot)
static void print_warning(struct dquot *dquot, const char warntype)
{
char *msg = NULL;
struct tty_struct *tty;
int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
......@@ -835,14 +836,15 @@ static void print_warning(struct dquot *dquot, const char warntype)
return;
mutex_lock(&tty_mutex);
if (!current->signal->tty)
tty = get_current_tty();
if (!tty)
goto out_lock;
tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
tty_write_message(tty, dquot->dq_sb->s_id);
if (warntype == ISOFTWARN || warntype == BSOFTWARN)
tty_write_message(current->signal->tty, ": warning, ");
tty_write_message(tty, ": warning, ");
else
tty_write_message(current->signal->tty, ": write failed, ");
tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
tty_write_message(tty, ": write failed, ");
tty_write_message(tty, quotatypes[dquot->dq_type]);
switch (warntype) {
case IHARDWARN:
msg = " file limit reached.\r\n";
......@@ -863,7 +865,7 @@ static void print_warning(struct dquot *dquot, const char warntype)
msg = " block quota exceeded.\r\n";
break;
}
tty_write_message(current->signal->tty, msg);
tty_write_message(tty, msg);
out_lock:
mutex_unlock(&tty_mutex);
}
......
......@@ -1087,6 +1087,7 @@ EXPORT_SYMBOL(sys_close);
asmlinkage long sys_vhangup(void)
{
if (capable(CAP_SYS_TTY_CONFIG)) {
/* XXX: this needs locking */
tty_vhangup(current->signal->tty);
return 0;
}
......
......@@ -309,6 +309,12 @@ extern void tty_ldisc_flush(struct tty_struct *tty);
extern int tty_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
unsigned long arg);
extern dev_t tty_devnum(struct tty_struct *tty);
extern void proc_clear_tty(struct task_struct *p);
extern void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
extern void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
extern struct tty_struct *get_current_tty(void);
extern struct mutex tty_mutex;
/* n_tty.c */
......@@ -335,10 +341,5 @@ extern void console_print(const char *);
extern int vt_ioctl(struct tty_struct *tty, struct file * file,
unsigned int cmd, unsigned long arg);
static inline dev_t tty_devnum(struct tty_struct *tty)
{
return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
}
#endif /* __KERNEL__ */
#endif
......@@ -428,6 +428,7 @@ static void do_acct_process(struct file *file)
u64 elapsed;
u64 run_time;
struct timespec uptime;
struct tty_struct *tty;
/*
* First check to see if there is enough free_space to continue
......@@ -485,12 +486,8 @@ static void do_acct_process(struct file *file)
#endif
mutex_lock(&tty_mutex);
/* FIXME: Whoever is responsible for current->signal locking needs
to use the same locking all over the kernel and document it */
read_lock(&tasklist_lock);
ac.ac_tty = current->signal->tty ?
old_encode_dev(tty_devnum(current->signal->tty)) : 0;
read_unlock(&tasklist_lock);
tty = get_current_tty();
ac.ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0;
mutex_unlock(&tty_mutex);
spin_lock_irq(&current->sighand->siglock);
......
......@@ -826,10 +826,12 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
context->return_code);
mutex_lock(&tty_mutex);
read_lock(&tasklist_lock);
if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
tty = tsk->signal->tty->name;
else
tty = "(none)";
read_unlock(&tasklist_lock);
audit_log_format(ab,
" a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
" ppid=%d pid=%d auid=%u uid=%u gid=%u"
......
......@@ -384,9 +384,7 @@ void daemonize(const char *name, ...)
exit_mm(current);
set_special_pids(1, 1);
mutex_lock(&tty_mutex);
current->signal->tty = NULL;
mutex_unlock(&tty_mutex);
proc_clear_tty(current);
/* Block and flush all signals */
sigfillset(&blocked);
......
......@@ -1484,7 +1484,6 @@ asmlinkage long sys_setsid(void)
pid_t session;
int err = -EPERM;
mutex_lock(&tty_mutex);
write_lock_irq(&tasklist_lock);
/* Fail if I am already a session leader */
......@@ -1504,12 +1503,15 @@ asmlinkage long sys_setsid(void)
group_leader->signal->leader = 1;
__set_special_pids(session, session);
spin_lock(&group_leader->sighand->siglock);
group_leader->signal->tty = NULL;
group_leader->signal->tty_old_pgrp = 0;
spin_unlock(&group_leader->sighand->siglock);
err = process_group(group_leader);
out:
write_unlock_irq(&tasklist_lock);
mutex_unlock(&tty_mutex);
return err;
}
......
......@@ -1695,9 +1695,10 @@ static inline void flush_unauthorized_files(struct files_struct * files)
struct tty_struct *tty;
struct fdtable *fdt;
long j = -1;
int drop_tty = 0;
mutex_lock(&tty_mutex);
tty = current->signal->tty;
tty = get_current_tty();
if (tty) {
file_list_lock();
file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list);
......@@ -1710,12 +1711,14 @@ static inline void flush_unauthorized_files(struct files_struct * files)
struct inode *inode = file->f_dentry->d_inode;
if (inode_has_perm(current, inode,
FILE__READ | FILE__WRITE, NULL)) {
/* Reset controlling tty. */
current->signal->tty = NULL;
current->signal->tty_old_pgrp = 0;
drop_tty = 1;
}
}
file_list_unlock();
/* Reset controlling tty. */
if (drop_tty)
proc_set_tty(current, NULL);
}
mutex_unlock(&tty_mutex);
......
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