summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2015-10-25 17:17:50 +0000
committerjilles <jilles@FreeBSD.org>2015-10-25 17:17:50 +0000
commitaff97c8f1f5dfb2c92a3497ef0dfdc9f5068361b (patch)
tree549fa8604538046131b5b363cc06210ecaa0270b /bin
parent8bb27e0d32095ceac091c0ececaf0ac91fbdb1ee (diff)
downloadFreeBSD-src-aff97c8f1f5dfb2c92a3497ef0dfdc9f5068361b.zip
FreeBSD-src-aff97c8f1f5dfb2c92a3497ef0dfdc9f5068361b.tar.gz
MFC r288430: wordexp: Rewrite to make WRDE_NOCMD reliable.
Shell syntax is too complicated to detect command substitution and unquoted operators reliably without implementing much of sh's parser. Therefore, have sh do this detection. While changing sh's support anyway, also read input from a pipe instead of arguments to avoid {ARG_MAX} limits and improve privacy, and output count and length using 16 instead of 8 digits. The basic concept is: execl("/bin/sh", "sh", "-c", "freebsd_wordexp ${1:+\"$1\"} -f "$2", "", flags & WRDE_NOCMD ? "-p" : "", <pipe with words>); The WRDE_BADCHAR error is still implemented in libc. POSIX requires us to fail strings containing unquoted braces with code WRDE_BADCHAR. Since this is normally not a syntax error in sh, there is still a need for checking code in libc, we_check(). The new we_check() is an optimistic check that all the characters <newline> | & ; < > ( ) { } are quoted. To avoid duplicating too much sh logic, such characters are permitted when quoting characters are seen, even if the quoting characters may themselves be quoted. This code reports all WRDE_BADCHAR errors; bad characters that get past it and are a syntax error in sh return WRDE_SYNTAX. Although many implementations of WRDE_NOCMD erroneously allow some command substitutions (and ours even documented this), there appears to be code that relies on its security (codesearch.debian.net shows quite a few uses). Passing untrusted data to wordexp() still exposes a denial of service possibility and a fairly large attack surface. This is also a MFC of r286830 to reduce conflicts. I changed the code somewhat to avoid changes from r286941; in particular, WRDE_BADVAL can still only be returned if WRDE_UNDEF was passed. Relnotes: yes Security: fixes command execution with wordexp(untrusted, WRDE_NOCMD)
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/builtins.def1
-rw-r--r--bin/sh/expand.c54
-rw-r--r--bin/sh/parser.c33
-rw-r--r--bin/sh/parser.h1
4 files changed, 89 insertions, 0 deletions
diff --git a/bin/sh/builtins.def b/bin/sh/builtins.def
index 1cbeea9..8807347 100644
--- a/bin/sh/builtins.def
+++ b/bin/sh/builtins.def
@@ -65,6 +65,7 @@ exportcmd -s export -s readonly
#exprcmd expr
falsecmd false
fgcmd -j fg
+freebsd_wordexpcmd freebsd_wordexp
getoptscmd getopts
hashcmd hash
histcmd -h fc
diff --git a/bin/sh/expand.c b/bin/sh/expand.c
index 84e342d..1d86698 100644
--- a/bin/sh/expand.c
+++ b/bin/sh/expand.c
@@ -1660,3 +1660,57 @@ wordexpcmd(int argc, char **argv)
outbin(argv[i], strlen(argv[i]) + 1, out1);
return (0);
}
+
+/*
+ * Do most of the work for wordexp(3), new version.
+ */
+
+int
+freebsd_wordexpcmd(int argc __unused, char **argv __unused)
+{
+ struct arglist arglist;
+ union node *args, *n;
+ struct strlist *sp;
+ size_t count, len;
+ int ch;
+ int protected = 0;
+ int fd = -1;
+
+ while ((ch = nextopt("f:p")) != '\0') {
+ switch (ch) {
+ case 'f':
+ fd = number(shoptarg);
+ break;
+ case 'p':
+ protected = 1;
+ break;
+ }
+ }
+ if (*argptr != NULL)
+ error("wrong number of arguments");
+ if (fd < 0)
+ error("missing fd");
+ INTOFF;
+ setinputfd(fd, 1);
+ INTON;
+ args = parsewordexp();
+ popfile(); /* will also close fd */
+ if (protected)
+ for (n = args; n != NULL; n = n->narg.next) {
+ if (n->narg.backquote != NULL) {
+ outcslow('C', out1);
+ error("command substitution disabled");
+ }
+ }
+ outcslow(' ', out1);
+ arglist.lastp = &arglist.list;
+ for (n = args; n != NULL; n = n->narg.next)
+ expandarg(n, &arglist, EXP_FULL | EXP_TILDE);
+ *arglist.lastp = NULL;
+ for (sp = arglist.list, count = len = 0; sp; sp = sp->next)
+ count++, len += strlen(sp->text);
+ out1fmt("%016zx %016zx", count, len);
+ for (sp = arglist.list; sp; sp = sp->next)
+ outbin(sp->text, strlen(sp->text) + 1, out1);
+ return (0);
+}
diff --git a/bin/sh/parser.c b/bin/sh/parser.c
index b577a8a..cb4f1ec0 100644
--- a/bin/sh/parser.c
+++ b/bin/sh/parser.c
@@ -229,6 +229,39 @@ parsecmd(int interact)
}
+/*
+ * Read and parse words for wordexp.
+ * Returns a list of NARG nodes; NULL if there are no words.
+ */
+union node *
+parsewordexp(void)
+{
+ union node *n, *first = NULL, **pnext;
+ int t;
+
+ /* This assumes the parser is not re-entered,
+ * which could happen if we add command substitution on PS1/PS2.
+ */
+ parser_temp_free_all();
+ heredoclist = NULL;
+
+ tokpushback = 0;
+ checkkwd = 0;
+ doprompt = 0;
+ setprompt(0);
+ needprompt = 0;
+ pnext = &first;
+ while ((t = readtoken()) != TEOF) {
+ if (t != TWORD)
+ synexpect(TWORD);
+ n = makename();
+ *pnext = n;
+ pnext = &n->narg.next;
+ }
+ return first;
+}
+
+
static union node *
list(int nlflag)
{
diff --git a/bin/sh/parser.h b/bin/sh/parser.h
index 5982594..0c3cd88 100644
--- a/bin/sh/parser.h
+++ b/bin/sh/parser.h
@@ -76,6 +76,7 @@ extern const char *const parsekwd[];
union node *parsecmd(int);
+union node *parsewordexp(void);
void forcealias(void);
void fixredir(union node *, const char *, int);
int goodname(const char *);
OpenPOWER on IntegriCloud