How to lock current->signal->tty
Subsystems (5)
The biggest crawly horror I've found so far in auditing the tty locking is current->signal->tty. The tty layer currently and explicitly protects this using tty_mutex. The core kernel likewise knows about this.
Unfortunately:
SELinux doesn't do any locking at all
Dquot passes the tty to tty_write_message without locking audit_log_exit doesn't do any locking at all
acct.c thinks tasklist_lock protects it (wrong) drivers/char/sx misuses it unlocked in debug info fs/proc/array thinks tasklist_lock will save it (also wrong) fs3270 does fascinating things with it which don't look safe ebtables remote debugging (#if 0 thankfully) does no locking
and just for fun calls the tty driver directly with no driver locking either.
voyager_thread sets up a thread and then touches ->tty unlocked
(and it seems daemonize already fixed it)
Sparc solaris_procids sets it to NULL without locking arch/ia64/kernel/unanligned seems to write to it without locking arch/um/kernel/exec.c appears to believe task_lock is used
The semantics are actually as follows
signal->tty must not be changed without holding tty_mutex signal->tty must not be used unless tty_mutex is held from before reading it to completing using it
Simple if(signal->tty == NULL) type checks are ok
I'm looking longer term at tty ref counting and the like but for now and current distributions it might be an idea to fix the existing problems.
Alan
Quoted 7 lines · Alan Cox
The biggest crawly horror I've found so far in auditing the tty locking is
current->signal->tty. The tty layer currently and explicitly protects this usingtty_mutex. The core kernel likewise knows about this.Unfortunately:
Dquot passes the tty totty_write_messagewithout lockingarch/ia64/kernel/unanlignedseems to write to it without locking
these two have absolutely no business at all using anything tty.... they should just use printk with KERN_EMERG or whatever
Ar Maw, 2006-08-08 am 08:10 -0700, ysgrifennodd Arjan van de Ven:
Quoted 6 lines
Quoted 3 lines
Unfortunately:
Dquot passes the tty totty_write_messagewithout lockingarch/ia64/kernel/unanlignedseems to write to it without lockingthese two have absolutely no business at all using anything tty.... they should just use printk with
KERN_EMERGor whatever
Dquot does - it writes to the controlling tty of the process exceeding quota . That is standard Unix behaviour
Quoted 10 lines · Alan Cox
Ar Maw, 2006-08-08 am 08:10 -0700, ysgrifennodd Arjan van de Ven:
Quoted 6 lines
Quoted 3 lines
Unfortunately:
Dquot passes the tty totty_write_messagewithout lockingarch/ia64/kernel/unanlignedseems to write to it without lockingthese two have absolutely no business at all using anything tty.... they should just use printk with
KERN_EMERGor whateverDquot does - it writes to the controlling tty of the process exceeding quota . That is standard Unix behaviour
IA-64 is also trying to be helpful by putting the message where the user may actually see it (following the dquot precedent). But the whole subject of whether we should print any messages for unaligned accesses at all is rather controversial. So its a 50-50 shot whether I'll fix it by adding the mutex_lock/mutex_unlock around the use of current->signal->tty, or just rip this out and just leave the printk().
-Tony
Ar Maw, 2006-08-08 am 09:41 -0700, ysgrifennodd Luck, Tony:
Quoted 4 lines
unaligned accesses at all is rather controversial. So its a 50-50 shot whether I'll fix it by adding the mutex_lock/mutex_unlock around the use of
current->signal->tty, or just rip this out and just leave theprintk().
Personally I'd just rip it out full stop. Its trivial to use kprobes and friends to audit such things if there is a performance concern.
Alan
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Alan" == Alan Cox <[email protected]> writes:
Alan> Ar Maw, 2006-08-08 am 09:41 -0700, ysgrifennodd Luck, Tony:
Quoted 4 lines
Quoted 4 lines
unaligned accesses at all is rather controversial. So its a 50-50 shot whether I'll fix it by adding the mutex_lock/mutex_unlock around the use of
current->signal->tty, or just rip this out and just leave theprintk().
Alan> Personally I'd just rip it out full stop. Its trivial to use Alan> kprobes and friends to audit such things if there is a Alan> performance concern.
Personally I don't like the current approach. However, I believe the philosophy behind it is that users rarely look in dmesg and they should be notified (and beaten with a stick) when their badly written app spawns unaligned accesses which end up being emulated by the kernel.
These messages are normally caused by userland code, so kprobes probably wont do much good :)
Cheers,
Jes
Ar Mer, 2006-08-09 am 04:09 -0400, ysgrifennodd Jes Sorensen:
Quoted 5 lines
Personally I don't like the current approach. However, I believe the philosophy behind it is that users rarely look in dmesg and they should be notified (and beaten with a stick) when their badly written app spawns unaligned accesses which end up being emulated by the kernel.
The users won't seem the anyway, they are hidden behind the GUI.
Quoted 2 lines
These messages are normally caused by userland code, so kprobes probably wont do much good :)
Jes, read up on kprobes a little if you think its of no use in these kind of situations. A systemtap script to count/measure alignment fault rates and see who is causing the load isn't very hard to write.
Alan
Quoted 8 lines · Alan Cox
Ar Mer, 2006-08-09 am 04:09 -0400, ysgrifennodd Jes Sorensen:
Quoted 5 lines
Personally I don't like the current approach. However, I believe the philosophy behind it is that users rarely look in dmesg and they should be notified (and beaten with a stick) when their badly written app spawns unaligned accesses which end up being emulated by the kernel.
The users won't seem the anyway, they are hidden behind the GUI.
Regularly see complaints from users in the HPC space over this ..... clearly it's the kernel thats broken, not their buggy code.
Jes
Quoted 2 lines · Alan Cox
The users won't see them anyway, they are hidden behind the GUI.
I think that the majority of IA-64 users are connected to the target machine via ssh login, rather than a directly connected VGA screen. So they should see the message on their pty.
Quoted 6 lines
Quoted 2 lines
These messages are normally caused by userland code, so kprobes probably wont do much good :)
Jes, read up on kprobes a little if you think its of no use in these kind of situations. A systemtap script to count/measure alignment fault rates and see who is causing the load isn't very hard to write.
But this does make sense ... the system administrator of a mainframe or super-computer can (and arguably should) be monitoring resource utilization and providing feedback to users on how to get the best performance from their applications.
-Tony (currently at 90% rip out this message, 10% add the mutex lock/unlock).
Quoted 9 lines · Tony
Quoted 3 lines · Alan Cox
Jes, read up on kprobes a little if you think its of no use in these kind of situations. A systemtap script to count/measure alignment fault rates and see who is causing the load isn't very hard to write.
But this does make sense ... the system administrator of a mainframe or super-computer can (and arguably should) be monitoring resource utilization and providing feedback to users on how to get the best performance from their applications.
Actually perfmon would probably be better suited for this on ia64 than kprobes/systemtap. You can do it on hackish way with a [jk]probe, but it isn't going to be all that pretty.
Quoted 1 line
-Tony (currently at 90% rip out this message, 10% add the mutex lock/unlock).
Nuke it!
Jes
Jan Kara
reviewerQuoted 10 lines
Ar Maw, 2006-08-08 am 08:10 -0700, ysgrifennodd Arjan van de Ven:
Quoted 6 lines
Quoted 3 lines
Unfortunately:
Dquot passes the tty totty_write_messagewithout lockingarch/ia64/kernel/unanlignedseems to write to it without lockingthese two have absolutely no business at all using anything tty.... they should just use printk with
KERN_EMERGor whateverDquot does - it writes to the controlling tty of the process exceeding quota . That is standard Unix behaviour
Yes. On one hand I find this behaviour kind of inconsistent (Unix usually
just returns an error code and that's it) but on the other hand there is no other good way of informing user about e.g. exceeded quota soft limit.
So I'll just fix up the tty locking ;).
Honza
Signature · 2 lines
Jan Kara <[email protected]> SuSE CR Labs
Quoted 23 lines · Alan Cox
The biggest crawly horror I've found so far in auditing the tty locking is
current->signal->tty. The tty layer currently and explicitly protects this usingtty_mutex. The core kernel likewise knows about this.Unfortunately:
SELinux doesn't do any locking at all
Dquot passes the tty totty_write_messagewithout lockingaudit_log_exitdoesn't do any locking at all
acct.cthinkstasklist_lockprotects it (wrong)drivers/char/sxmisuses it unlocked in debug infofs/proc/arraythinkstasklist_lockwill save it (also wrong) fs3270 does fascinating things with it which don't look safe ebtables remote debugging (#if 0 thankfully) does no locking
and just for fun calls the tty driver directly with no driver locking either.
voyager_threadsets up a thread and then touches ->tty unlocked
(and it seems daemonize already fixed it)
Sparcsolaris_procidssets it to NULL without lockingarch/ia64/kernel/unanlignedseems to write to it without lockingarch/um/kernel/exec.cappears to believetask_lockis usedThe semantics are actually as follows
Quoted 7 lines
signal->ttymust not be changed without holdingtty_mutexsignal->ttymust not be used unlesstty_mutexis held from before reading it to completing using it
Simpleif(signal->tty == NULL)type checks are okI'm looking longer term at tty ref counting and the like but for now and current distributions it might be an idea to fix the existing problems.
Does this look sane? Or do we need a common helper factored from disassociate_ctty()? Why is the locking different for TIOCNOTTY in the non-leader case?
---
selinux: fix tty locking
Take tty_mutex when accessing ->signal->tty.
Noted by Alan Cox.
---
security/selinux/hooks.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletion(-)
security/selinux/hooks.c +4 -1
@@ -1711,10 +1711,12 @@ static inline void flush_unauthorized_fi {struct avc_audit_data ad;struct file *file, *devnull =NULL ; -struct tty_struct *tty = current->signal->tty; +struct tty_struct *tty;struct fdtable *fdt;long j =-1 ; +mutex_lock (&tty_mutex); + tty = current->signal->tty;if (tty) {file_list_lock (); file =list_entry (tty->tty_files.next,typeof (*file), f_u.fu_list);
@@ -1734,6 +1736,7 @@ static inline void flush_unauthorized_fi }file_list_unlock (); } +mutex_unlock (&tty_mutex);/* Revalidate access to inherited open files. */
Signature · 2 lines
Stephen Smalley National Security Agency
Ar Maw, 2006-08-08 am 13:11 -0400, ysgrifennodd Stephen Smalley:
Quoted 3 lines
Does this look sane? Or do we need a common helper factored from
disassociate_ctty()? Why is the locking different for TIOCNOTTY in the non-leader case?
The non-leader case for TIOCNOTTY in the base kernel is different because it is wrong and I've fixed that one.
If you can factor disassociate_ctty out to do what you need I'd prefer that path so the tty locking actually ends up in the tty layer.
security/selinux/hooks.c static inline void flush_unauthorized_fi · line 1721
Quoted 4 lines
security/selinux/hooks.c +2
-0+mutex_lock (&tty_mutex); + tty = current->signal->tty;if (tty) {file_list_lock ();
Looks sane and the lock ordering matches vhangup() which may actually also do what you want - I'm not 100% sure I follow what SELinux tries to do here.
Quoted 11 lines · Alan Cox
Ar Maw, 2006-08-08 am 13:11 -0400, ysgrifennodd Stephen Smalley:
Quoted 3 lines
Does this look sane? Or do we need a common helper factored from
disassociate_ctty()? Why is the locking different for TIOCNOTTY in the non-leader case?The non-leader case for TIOCNOTTY in the base kernel is different because it is wrong and I've fixed that one.
If you can factor
disassociate_cttyout to do what you need I'd prefer that path so the tty locking actually ends up in the tty layer.
Quoted 8 lines
security/selinux/hooks.c static inline void flush_unauthorized_fi · line 1721
Quoted 4 lines
security/selinux/hooks.c +2
-0+mutex_lock (&tty_mutex); + tty = current->signal->tty;if (tty) {file_list_lock ();Looks sane and the lock ordering matches
vhangup()which may actually also do what you want - I'm not 100% sure I follow what SELinux tries to do here.
SELinux is just revalidating access to the tty when the task changes contexts upon execve, and resetting the tty if the task is no longer allowed to use it. Likewise with the open file descriptors that would be inherited. No clearing of the ttys of other tasks required as far as SELinux is concerned, although that might not fit with normal semantics.
Signature · 2 lines
Stephen Smalley National Security Agency
Ar Maw, 2006-08-08 am 13:44 -0400, ysgrifennodd Stephen Smalley:
Quoted 5 lines
SELinux is just revalidating access to the tty when the task changes contexts upon execve, and resetting the tty if the task is no longer allowed to use it. Likewise with the open file descriptors that would be inherited. No clearing of the ttys of other tasks required as far as SELinux is concerned, although that might not fit with normal semantics.
The kernel requires you end up with a session leader etc so an exec that loses rights by the session leader does indeed match disassociate_ctty I guess. The ctty is a bit of an odd thing in the Unix world and perhaps something that shouldn't have happened in that it gives you ability to do things even if you have no fd to it.
Alan
Quoted 10 lines · Alan Cox
Ar Maw, 2006-08-08 am 13:11 -0400, ysgrifennodd Stephen Smalley:
Quoted 3 lines
Does this look sane? Or do we need a common helper factored from
disassociate_ctty()? Why is the locking different for TIOCNOTTY in the non-leader case?The non-leader case for TIOCNOTTY in the base kernel is different because it is wrong and I've fixed that one.
If you can factor
disassociate_cttyout to do what you need I'd prefer that path so the tty locking actually ends up in the tty layer.
Possible patch below, but not yet tested. This splits the handling between the tty layer and SELinux. Not sure about the !leader case, should be consistent with whatever you end up using for TIOCNOTTY. This changes behavior for SELinux in the leader case, since it will now perform a full disassociate. Not sure whether the use of ->tty_files should also be moved into the tty layer, with the tty layer then just passing the file struct to the security hook instead of the tty struct.
---
drivers/char/tty_io.c | 30 ++++++++++++++++++++++++++- include/linux/security.h | 20 ++++++++++++++++++ include/linux/tty.h | 1 security/dummy.c | 6 +++++ security/selinux/hooks.c | 51 ++++++++++++++++++++++++++++------------------- 5 files changed, 87 insertions(+), 21 deletions(-)
drivers/char/tty_io.c +29 -1
@@ -94,6 +94,7 @@ #include <linux/idr.h>#include <linux/wait.h> #include <linux/bitops.h> #include <linux/delay.h> +#include <linux/security.h> #include <asm/uaccess.h> #include <asm/system.h>
@@ -1175,8 +1176,11 @@ EXPORT_SYMBOL(tty_hung_up_p);* * The argument on_exit is set to 1 if called when a process is * exiting; it is 0 if called by the ioctl TIOCNOTTY. +* The argument cond_sec is set to 1 if called by a security module +* to revalidate access to the ctty and conditionally disassociate +* if access is no longer permitted. */ -void disassociate_ctty (int on_exit) +static void disassociate_ctty_core (int on_exit,int cond_sec) {struct tty_struct *tty;struct task_struct *p;
@@ -1186,6 +1190,13 @@ void disassociate_ctty(int on_exit)mutex_lock (&tty_mutex); tty = current->signal->tty; +if (tty && cond_sec) { +if (!security_tty_disassociate (tty)) { +mutex_unlock (&tty_mutex); +unlock_kernel (); +return ; + } + }if (tty) { tty_pgrp = tty->pgrp;mutex_unlock (&tty_mutex);
@@ -1222,6 +1233,23 @@ void disassociate_ctty(int on_exit)unlock_kernel (); } +void disassociate_ctty (int on_exit) +{ +disassociate_ctty_core (on_exit,0 ); +} + +void revalidate_ctty (void ) +{ +if (current->signal->leader) { +disassociate_ctty_core (0 ,1 ); +return ; + } +mutex_lock (&tty_mutex); +if (security_tty_disassociate (current->signal->tty)) + current->signal->tty =NULL ; +mutex_unlock (&tty_mutex); +} +void stop_tty (struct tty_struct *tty) {if (tty->stopped)
include/linux/security.h +20 -0
@@ -500,6 +500,14 @@ #ifdef CONFIG_SECURITY* @file contains the file structure being received. * Return 0 if permission is granted. * +* Security hooks for tty operations. +* +* @tty_disassociate: +* This hook allows security modules to control whether the tty +* should be disassociated. +* @tty contains the tty structure. +* Return 0 if permission is granted to the tty, or <0 to disassociate. +* * Security hooks for task operations. * * @task_create:
@@ -1227,6 +1235,8 @@ struct security_operations { struct fown_struct * fown, int sig);int (*file_receive) (struct file * file); +int (*tty_disassociate) (struct tty_struct *tty); +int (*task_create) (unsigned long clone_flags);int (*task_alloc_security) (struct task_struct * p);void (*task_free_security) (struct task_struct * p);
@@ -1813,6 +1823,11 @@ static inline int security_file_receivereturn security_ops->file_receive (file); } +static inline int security_tty_disassociate (struct tty_struct *tty) +{ +return security_ops->tty_disassociate (tty); +} +static inline int security_task_create (unsigned long clone_flags) {return security_ops->task_create (clone_flags);
@@ -2488,6 +2503,11 @@ static inline int security_file_receivereturn 0 ; } +static inline int security_tty_disassociate (struct tty_struct *tty) +{ +return 0 ; +} +static inline int security_task_create (unsigned long clone_flags) {return 0 ;
include/linux/tty.h +1 -0
@@ -292,6 +292,7 @@ extern void tty_vhangup(struct tty_strucextern void tty_unhangup (struct file *filp);extern int tty_hung_up_p (struct file * filp);extern void do_SAK (struct tty_struct *tty); +extern void revalidate_ctty (void );extern void disassociate_ctty (int priv);extern void tty_flip_buffer_push (struct tty_struct *tty);extern int tty_get_baud_rate (struct tty_struct *tty);
security/dummy.c +6 -0
@@ -459,6 +459,11 @@ static int dummy_file_receive (struct fireturn 0 ; } +static int dummy_tty_disassociate (struct tty_struct *tty) +{ +return 0 ; +} +static int dummy_task_create (unsigned long clone_flags) {return 0 ;
@@ -987,6 +992,7 @@ void security_fixup_ops (struct securityset_to_dummy_if_null (ops, file_set_fowner);set_to_dummy_if_null (ops, file_send_sigiotask);set_to_dummy_if_null (ops, file_receive); +set_to_dummy_if_null (ops, tty_disassociate);set_to_dummy_if_null (ops, task_create);set_to_dummy_if_null (ops, task_alloc_security);set_to_dummy_if_null (ops, task_free_security);
security/selinux/hooks.c +31 -20
72 lines not on this page · raw message
Signature · 2 lines
Stephen Smalley National Security Agency
Jan Kara
reviewerQuoted 7 lines
The biggest crawly horror I've found so far in auditing the tty locking is
current->signal->tty. The tty layer currently and explicitly protects this usingtty_mutex. The core kernel likewise knows about this.Unfortunately:
SELinux doesn't do any locking at all
Dquot passes the tty totty_write_messagewithout locking
Ok, is something like attached patch fine?
Honza
Add proper locking when using current->signal->tty.
fs/dquot.c +5 -0
@@ -834,6 +834,9 @@ static void print_warning(struct dquot *if (!need_print_warning (dquot) || (flag &&test_and_set_bit (flag, &dquot->dq_flags)))return ; +mutex_lock (&tty_mutex); +if (!current->signal->tty) + goto out_lock;tty_write_message (current->signal->tty, dquot->dq_sb->s_id);if (warntype ==ISOFTWARN || warntype ==BSOFTWARN )tty_write_message (current->signal->tty,": warning, " );
@@ -861,6 +864,8 @@ static void print_warning(struct dquot *break ; }tty_write_message (current->signal->tty, msg); +out_lock: +mutex_unlock (&tty_mutex); }static inline void flush_warnings (struct dquot **dquots,char *warntype)