From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: fd used for saving redirected fds leaked to child processes
Date: Mon, 14 Aug 2017 20:19:39 +0100 [thread overview]
Message-ID: <20170814201939.22ba0dcb@ntlworld.com> (raw)
In-Reply-To: <CAH+w=7bUMdLYFd0aBE=xFCizZvkK6Msj93fOAFanF5vZO7QsKg@mail.gmail.com>
On Mon, 14 Aug 2017 07:49:52 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Mon, Aug 14, 2017 at 2:09 AM, Peter Stephenson
> <p.stephenson@samsung.com> wrote:
> >
> > We need to expose the entire hierarchy for this particular case, but
> > that looks like a stack or a linked list, which seems a little
> > heavyweight for this case.
>
> Another flag in the fdtable array?
This does that --- this looks easy. It didn't seem convenient to
optimise, since once one set of saved fds is restored, exposing any in
an enclosing scope, there's no way of working out which the new highest
number is without an exhaustive search.
pws
diff --git a/Src/exec.c b/Src/exec.c
index f339dd6..9996dff 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -972,7 +972,7 @@ enum {
static void
entersubsh(int flags)
{
- int sig, monitor, job_control_ok;
+ int i, sig, monitor, job_control_ok;
if (!(flags & ESUB_KEEPTRAP))
for (sig = 0; sig < SIGCOUNT; sig++)
@@ -1083,6 +1083,14 @@ entersubsh(int flags)
opts[MONITOR] = 0;
opts[USEZLE] = 0;
zleactive = 0;
+ /*
+ * If we've saved fd's for later restoring, we're never going
+ * to restore them now, so just close them.
+ */
+ for (i = 10; i <= max_zsh_fd; i++) {
+ if (fdtable[i] & FDT_SAVED_MASK)
+ zclose(i);
+ }
if (flags & ESUB_PGRP)
clearjobtab(monitor);
get_usage();
@@ -2318,6 +2326,9 @@ addfd(int forked, int *save, struct multio **mfds, int fd1, int fd2, int rflag,
return;
}
save[fd1] = fdN;
+ DPUTS(fdtable[fdN] != FDT_INTERNAL,
+ "Saved file descriptor not marked as internal");
+ fdtable[fdN] |= FDT_SAVED_MASK;
}
}
}
@@ -3575,7 +3586,8 @@ execcmd_exec(Estate state, Execcmd_params eparams,
}
if (!bad && fn->fd1 <= max_zsh_fd) {
if (fn->fd1 >= 10 &&
- fdtable[fn->fd1] == FDT_INTERNAL)
+ (fdtable[fn->fd1] & FDT_TYPE_MASK) ==
+ FDT_INTERNAL)
bad = 3;
}
}
@@ -4270,7 +4282,7 @@ closem(int how)
for (i = 10; i <= max_zsh_fd; i++)
if (fdtable[i] != FDT_UNUSED &&
- (how == FDT_UNUSED || fdtable[i] == how)) {
+ (how == FDT_UNUSED || (fdtable[i] & FDT_TYPE_MASK) == how)) {
if (i == SHTTY)
SHTTY = -1;
zclose(i);
diff --git a/Src/zsh.h b/Src/zsh.h
index ccd11db..f42ada7 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -456,6 +456,18 @@ enum {
#define FDT_PROC_SUBST 7
#endif
+/*
+ * Mask to get the basic FDT type.
+ */
+#define FDT_TYPE_MASK 15
+
+/*
+ * Bit flag that fd is saved for later restoration.
+ * Currently this is only use with FDT_INTERNAL. We use this fact so as
+ * not to have to mask checks against other types.
+ */
+#define FDT_SAVED_MASK 16
+
/* Flags for input stack */
#define INP_FREE (1<<0) /* current buffer can be free'd */
#define INP_ALIAS (1<<1) /* expanding alias or history */
next prev parent reply other threads:[~2017-08-16 14:51 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-13 16:12 Stephane Chazelas
2017-08-13 18:49 ` Bart Schaefer
2017-08-13 18:49 ` Peter Stephenson
2017-08-13 21:45 ` Bart Schaefer
2017-08-14 9:09 ` Peter Stephenson
2017-08-14 14:49 ` Bart Schaefer
2017-08-14 15:24 ` Peter Stephenson
2017-08-14 15:31 ` Bart Schaefer
2017-08-14 19:19 ` Peter Stephenson [this message]
2017-09-29 15:06 ` Daniel Shahaf
2017-09-29 15:18 ` Peter Stephenson
2017-08-15 18:42 ` Peter Stephenson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170814201939.22ba0dcb@ntlworld.com \
--to=p.w.stephenson@ntlworld.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).