All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Revert "open_init_pty: Do not make stdin and stdout non-blocking"
@ 2017-07-18 13:04 Stephen Smalley
  2017-07-18 13:04 ` [PATCH 2/2] open_init_pty: restore stdin/stdout to blocking upon exit Stephen Smalley
  0 siblings, 1 reply; 2+ messages in thread
From: Stephen Smalley @ 2017-07-18 13:04 UTC (permalink / raw)
  To: selinux; +Cc: jason, bigon, Stephen Smalley

Making stdin/stdout non-blocking causes open_init_pty to hang if
they are closed, ala
./open_init_pty bash -c 'echo hello; exec >&- 2>&- <&-; sleep 1; '
and per
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=474956#10

This reverts commit fb081eb64b36a9de5a43f3d69d9e628b6eb1afc7.

Reported-by: Laurent Bigonville <bigon@debian.org>
Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov>
---
 policycoreutils/run_init/open_init_pty.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/policycoreutils/run_init/open_init_pty.c b/policycoreutils/run_init/open_init_pty.c
index b37ae4d..6e25ea3 100644
--- a/policycoreutils/run_init/open_init_pty.c
+++ b/policycoreutils/run_init/open_init_pty.c
@@ -276,8 +276,10 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	/* Non blocking mode for the pty master. */
+	/* Non blocking mode for all file descriptors. */
 	setfd_nonblock(pty_master);
+	setfd_nonblock(STDIN_FILENO);
+	setfd_nonblock(STDOUT_FILENO);
 
 	if (isatty(STDIN_FILENO)) {
 		if (tty_semi_raw(STDIN_FILENO) < 0) {
-- 
2.9.4

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] open_init_pty: restore stdin/stdout to blocking upon exit
  2017-07-18 13:04 [PATCH 1/2] Revert "open_init_pty: Do not make stdin and stdout non-blocking" Stephen Smalley
@ 2017-07-18 13:04 ` Stephen Smalley
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Smalley @ 2017-07-18 13:04 UTC (permalink / raw)
  To: selinux; +Cc: jason, bigon, Stephen Smalley

At exit, restore stdin and stdout to blocking.

Test: run_init id && run_init id
Test: open_init_pty bash -c 'echo hello; exec >&- 2>&- <&-; sleep 1;'

Fixes: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=863187
Fixes: https://bugs.gentoo.org/show_bug.cgi?id=621062
Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov>
---
 policycoreutils/run_init/open_init_pty.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/policycoreutils/run_init/open_init_pty.c b/policycoreutils/run_init/open_init_pty.c
index 6e25ea3..150cb45 100644
--- a/policycoreutils/run_init/open_init_pty.c
+++ b/policycoreutils/run_init/open_init_pty.c
@@ -191,6 +191,28 @@ static void setfd_nonblock(int fd)
 	}
 }
 
+static void setfd_block(int fd)
+{
+	int fsflags = fcntl(fd, F_GETFL);
+
+	if (fsflags < 0) {
+		fprintf(stderr, "fcntl(%d, F_GETFL): %s\n", fd, strerror(errno));
+		exit(EX_IOERR);
+	}
+
+	if (fcntl(fd, F_SETFL, fsflags & ~O_NONBLOCK) < 0) {
+		fprintf(stderr, "fcntl(%d, F_SETFL, ... & ~O_NONBLOCK): %s\n", fd, strerror(errno));
+		exit(EX_IOERR);
+	}
+}
+
+static void setfd_atexit(void)
+{
+	setfd_block(STDIN_FILENO);
+	setfd_block(STDOUT_FILENO);
+	return;
+}
+
 static void sigchld_handler(int asig __attribute__ ((unused)))
 {
 }
@@ -280,6 +302,10 @@ int main(int argc, char *argv[])
 	setfd_nonblock(pty_master);
 	setfd_nonblock(STDIN_FILENO);
 	setfd_nonblock(STDOUT_FILENO);
+	if (atexit(setfd_atexit) < 0) {
+		perror("atexit()");
+		exit(EXIT_FAILURE);
+	}
 
 	if (isatty(STDIN_FILENO)) {
 		if (tty_semi_raw(STDIN_FILENO) < 0) {
-- 
2.9.4

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-07-18 13:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-18 13:04 [PATCH 1/2] Revert "open_init_pty: Do not make stdin and stdout non-blocking" Stephen Smalley
2017-07-18 13:04 ` [PATCH 2/2] open_init_pty: restore stdin/stdout to blocking upon exit Stephen Smalley

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.