mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: ebiederm@xmission.com (Eric W. Biederman)
To: Andrew Morton <akpm@linux-foundation.org>
Cc: "Linux Kernel Mailing List" <linux-kernel@vger.kernel.org>,
	"Catalin Marinas" <catalin.marinas@gmail.com>,
	Stephen Smalley <sds@tycho.nsa.gov>
Subject: [PATCH 3/4] tty: Introduce no_tty and use it in selinux
Date: Sun, 18 Mar 2007 13:08:34 -0600	[thread overview]
Message-ID: <m1bqiqjrnx.fsf_-_@ebiederm.dsl.xmission.com> (raw)
In-Reply-To: <m1fy82jrwz.fsf_-_@ebiederm.dsl.xmission.com> (Eric W. Biederman's message of "Sun, 18 Mar 2007 13:03:08 -0600")


While researching the tty layer pid leaks I found a weird case in
selinux when we drop a controlling tty because of inadequate
permissions we don't do the normal hangup processing.  Which is a
problem if it happens the session  leader has exec'd something that
can no longer access the tty.

We already have code in the kernel to handle this case in the form of
the TIOCNOTTY ioctl.  So this patch factors out a helper function that
is the essence of that ioctl and calls it from the selinux code.

This removes the inconsistency in handling dropping of a controlling
tty and who knows it might even make some part of user space happy
because it received a SIGHUP it was expecting.

In addition since this removes the last user of proc_set_tty outside
of tty_io.c proc_set_tty is made static and removed from tty.h

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
---
 drivers/char/tty_io.c    |   19 +++++++++++++++----
 include/linux/tty.h      |    2 +-
 security/selinux/hooks.c |    7 +++----
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index 01fcfaf..0843fcb 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -156,6 +156,7 @@ int tty_ioctl(struct inode * inode, struct file * file,
 static int tty_fasync(int fd, struct file * filp, int on);
 static void release_tty(struct tty_struct *tty, int idx);
 static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
+static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
 
 /**
  *	alloc_tty_struct	-	allocate a tty object
@@ -1560,6 +1561,18 @@ void disassociate_ctty(int on_exit)
 	unlock_kernel();
 }
 
+/**
+ *
+ *	no_tty	- Ensure the current process does not have a controlling tty
+ */
+void no_tty(void)
+{
+	struct task_struct *tsk = current;
+	if (tsk->signal->leader)
+		disassociate_ctty(0);
+	proc_clear_tty(tsk);
+}
+
 
 /**
  *	stop_tty	-	propogate flow control
@@ -3282,9 +3295,7 @@ int tty_ioctl(struct inode * inode, struct file * file,
 		case TIOCNOTTY:
 			if (current->signal->tty != tty)
 				return -ENOTTY;
-			if (current->signal->leader)
-				disassociate_ctty(0);
-			proc_clear_tty(current);
+			no_tty();
 			return 0;
 		case TIOCSCTTY:
 			return tiocsctty(tty, arg);
@@ -3847,7 +3858,7 @@ static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
 	tsk->signal->tty_old_pgrp = NULL;
 }
 
-void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
+static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
 {
 	spin_lock_irq(&tsk->sighand->siglock);
 	__proc_set_tty(tsk, tty);
diff --git a/include/linux/tty.h b/include/linux/tty.h
index dee72b9..bb45760 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -313,6 +313,7 @@ extern int tty_hung_up_p(struct file * filp);
 extern void do_SAK(struct tty_struct *tty);
 extern void __do_SAK(struct tty_struct *tty);
 extern void disassociate_ctty(int priv);
+extern void no_tty(void);
 extern void tty_flip_buffer_push(struct tty_struct *tty);
 extern speed_t tty_get_baud_rate(struct tty_struct *tty);
 extern speed_t tty_termios_baud_rate(struct ktermios *termios);
@@ -333,7 +334,6 @@ extern int tty_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 
 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 struct tty_struct *get_current_tty(void);
 
 extern struct mutex tty_mutex;
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 19a385e..90da0e2 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -1758,12 +1758,11 @@ static inline void flush_unauthorized_files(struct files_struct * files)
 			}
 		}
 		file_list_unlock();
-
-		/* Reset controlling tty. */
-		if (drop_tty)
-			proc_set_tty(current, NULL);
 	}
 	mutex_unlock(&tty_mutex);
+	/* Reset controlling tty. */
+	if (drop_tty)
+		no_tty();
 
 	/* Revalidate access to inherited open files. */
 
-- 
1.5.0.g53756


  reply	other threads:[~2007-03-18 19:09 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-08 17:27 Possible "struct pid" leak from tty_io.c Catalin Marinas
2007-03-08 18:11 ` Eric W. Biederman
2007-03-09 10:53   ` Catalin Marinas
2007-03-09 16:13     ` Eric W. Biederman
2007-03-09 16:53       ` Catalin Marinas
2007-03-09 16:44   ` Catalin Marinas
2007-03-09 17:09     ` Eric W. Biederman
2007-03-12 15:07       ` Catalin Marinas
2007-03-12 16:12         ` Eric W. Biederman
2007-03-13 19:31         ` Eric W. Biederman
2007-03-14  9:59           ` Catalin Marinas
2007-03-14 14:40             ` Eric W. Biederman
2007-03-14 17:08               ` Catalin Marinas
2007-03-15 19:15                 ` Eric W. Biederman
2007-03-16 22:01                 ` Eric W. Biederman
2007-03-16 22:44                   ` Catalin Marinas
2007-03-18 18:45                     ` [PATCH] tty: Fix two reported pid leaks Eric W. Biederman
2007-03-18 18:52                       ` [PATCH 0/4] tty: small fixes and cleanups Eric W. Biederman
2007-03-18 18:57                         ` [PATCH 1/4] tty: Remove unnecessary export of proc_clear_tty Eric W. Biederman
2007-03-18 19:03                           ` [PATCH 2/4] tty: Simplify calling of put_pid Eric W. Biederman
2007-03-18 19:08                             ` Eric W. Biederman [this message]
2007-03-18 19:13                               ` [PATCH 4/4] tty: In tiocsctty when we steal a tty hang it up Eric W. Biederman
2007-03-18 20:55                               ` [PATCH 3/4] tty: Introduce no_tty and use it in selinux Alan Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m1bqiqjrnx.fsf_-_@ebiederm.dsl.xmission.com \
    --to=ebiederm@xmission.com \
    --cc=akpm@linux-foundation.org \
    --cc=catalin.marinas@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sds@tycho.nsa.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®