Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Keep track of the biggest fd used by, or available to...



details:   https://anonhg.NetBSD.org/src/rev/3fca5fdae995
branches:  trunk
changeset: 823425:3fca5fdae995
user:      kre <kre%NetBSD.org@localhost>
date:      Sat Apr 22 16:02:39 2017 +0000

description:
Keep track of the biggest fd used by, or available to, the user/script
and use that to control which fd's are examined by a (bare) fdflags
(with no fd args).

Usually this will mean that fdflags will no longer show the shell's
internal use fds, only user fds.

This is only a partial fix however, a user can easily discover the
shell's fd usage (eg: using fstat) and can then still use fdflags to
manipulate those fds (or even send output to them).

The shell needs to monitor its own fd usage better, and keep out of
the way of user fds - coming sometime later...

diffstat:

 bin/sh/main.c  |  10 ++++++++--
 bin/sh/redir.c |  15 ++++++---------
 bin/sh/redir.h |   4 +++-
 3 files changed, 17 insertions(+), 12 deletions(-)

diffs (110 lines):

diff -r e53096e9469e -r 3fca5fdae995 bin/sh/main.c
--- a/bin/sh/main.c     Sat Apr 22 15:54:53 2017 +0000
+++ b/bin/sh/main.c     Sat Apr 22 16:02:39 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.67 2016/05/09 21:03:10 kre Exp $    */
+/*     $NetBSD: main.c,v 1.68 2017/04/22 16:02:39 kre Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.7 (Berkeley) 7/19/95";
 #else
-__RCSID("$NetBSD: main.c,v 1.67 2016/05/09 21:03:10 kre Exp $");
+__RCSID("$NetBSD: main.c,v 1.68 2017/04/22 16:02:39 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -77,11 +77,13 @@
 #include "mystring.h"
 #include "exec.h"
 #include "cd.h"
+#include "redir.h"
 
 #define PROFILE 0
 
 int rootpid;
 int rootshell;
+int max_user_fd;
 #if PROFILE
 short profile_buf[16384];
 extern int etext();
@@ -111,6 +113,10 @@
        uid = getuid();
        gid = getgid();
 
+       max_user_fd = fcntl(0, F_MAXFD);
+       if (max_user_fd < 2)
+               max_user_fd = 2;
+
        setlocale(LC_ALL, "");
 
        posix = getenv("POSIXLY_CORRECT") != NULL;
diff -r e53096e9469e -r 3fca5fdae995 bin/sh/redir.c
--- a/bin/sh/redir.c    Sat Apr 22 15:54:53 2017 +0000
+++ b/bin/sh/redir.c    Sat Apr 22 16:02:39 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: redir.c,v 1.52 2017/04/22 15:54:53 kre Exp $   */
+/*     $NetBSD: redir.c,v 1.53 2017/04/22 16:02:39 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.52 2017/04/22 15:54:53 kre Exp $");
+__RCSID("$NetBSD: redir.c,v 1.53 2017/04/22 16:02:39 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -189,6 +189,8 @@
        }
        for (n = redir ; n ; n = n->nfile.next) {
                fd = n->nfile.fd;
+               if (fd > max_user_fd)
+                       max_user_fd = fd;
                if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
                    n->ndup.dupfd == fd) {
                        /* redirect from/to same file descriptor */
@@ -738,22 +740,17 @@
                parseflags(setflags, &pos, &neg);
 
        if (argc == 0) {
-               int maxfd;
+               int i;
 
                if (setflags)
                        goto msg;
 
                /*
-                * XXX  this has to go, maxfd might be 700 (or something)
-                *
                 * XXX  we should only ever operate on user defined fds
                 * XXX  not on sh internal fds that might be open.
                 * XXX  but for that we need to know their range (later)
                 */
-               maxfd = fcntl(0, F_MAXFD);
-               if (maxfd == -1)
-                       error("Can't get max fd (%s)", strerror(errno));
-               for (int i = 0; i <= maxfd; i++)
+               for (i = 0; i <= max_user_fd; i++)
                        printone(i, 0, verbose, 1);
                return 0;
        }
diff -r e53096e9469e -r 3fca5fdae995 bin/sh/redir.h
--- a/bin/sh/redir.h    Sat Apr 22 15:54:53 2017 +0000
+++ b/bin/sh/redir.h    Sat Apr 22 16:02:39 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: redir.h,v 1.21 2016/05/12 13:31:37 kre Exp $   */
+/*     $NetBSD: redir.h,v 1.22 2017/04/22 16:02:39 kre Exp $   */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -47,3 +47,5 @@
 void clearredir(int);
 int movefd(int, int);
 int to_upper_fd(int);
+
+int max_user_fd;               /* highest fd used by user */



Home | Main Index | Thread Index | Old Index