Skip to contentSkip to conversation

Revert "open_init_pty: Do not make stdin and stdout non-blocking"

PATCH New Stephen Smalley 1 participant 2 messages Jul 18, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Revert "open_init_pty: Do not make stdin and stdout non-blocking"

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.

---
 policycoreutils/run_init/open_init_pty.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
policycoreutils/run_init/open_init_pty.c +3 -1
index b37ae4d..6e25ea3 100644
@@ -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) {
Signature · 1 line
2.9.4
#1 lore raw

PATCH 2/2 open_init_pty: restore stdin/stdout to blocking upon exit

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;'

---
 policycoreutils/run_init/open_init_pty.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
policycoreutils/run_init/open_init_pty.c +26 -0
index 6e25ea3..150cb45 100644
@@ -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) {
Signature · 1 line
2.9.4