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...
This commit is contained in:
kre 2017-04-22 16:02:39 +00:00
parent 9df68bad0e
commit c1cbf1992b
3 changed files with 17 additions and 12 deletions

View File

@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
#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 @@ __RCSID("$NetBSD: main.c,v 1.67 2016/05/09 21:03:10 kre Exp $");
#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 @@ main(int argc, char **argv)
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;

View File

@ -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 @@ redirect(union node *redir, int flags)
}
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 @@ fdflagscmd(int argc, char *argv[])
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;
}

View File

@ -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 @@ int fd0_redirected_p(void);
void clearredir(int);
int movefd(int, int);
int to_upper_fd(int);
int max_user_fd; /* highest fd used by user */