Module Name:    src
Committed By:   kre
Date:           Mon May 29 22:21:00 UTC 2017

Modified Files:
        src/bin/sh: redir.c

Log Message:
Now that the shell is protecting its internal fds properly, moving
them whenever the user tries to step on one, we can change our behaviour
back to what the kernel considers to be that of a well behaved shell
(wrt file descriptor usage).  If our user causes problems, we will soon
move into recalcitrant process territory, but that should normally be
rare.  This should reduce kernel overheads a little.


To generate a diff of this commit:
cvs rdiff -u -r1.56 -r1.57 src/bin/sh/redir.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/redir.c
diff -u src/bin/sh/redir.c:1.56 src/bin/sh/redir.c:1.57
--- src/bin/sh/redir.c:1.56	Thu May 18 13:56:58 2017
+++ src/bin/sh/redir.c	Mon May 29 22:21:00 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: redir.c,v 1.56 2017/05/18 13:56:58 kre Exp $	*/
+/*	$NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)redir.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: redir.c,v 1.56 2017/05/18 13:56:58 kre Exp $");
+__RCSID("$NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -518,7 +518,7 @@ STATIC void
 find_big_fd(void)
 {
 	int i, fd;
-	static int last_start = 6;
+	static int last_start = 3; /* aim to keep sh fd's under 20 */
 
 	if (last_start < 10)
 		last_start++;
@@ -630,6 +630,15 @@ renumber_sh_fd(struct shell_fds *fp)
 #define	CLOEXEC(fd)
 #endif
 
+	/*
+	 * if we have had a collision, and the sh fd was a "big" one
+	 * try moving the sh fd base to a higher number (if possible)
+	 * so future sh fds are less likely to be in the user's sights
+	 * (incl this one when moved)
+	 */
+	if (fp->fd >= big_sh_fd)
+		find_big_fd();
+
 	to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd);
 	if (to == -1)
 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd/2);

Reply via email to