summaryrefslogtreecommitdiffstats
path: root/bin/sh/expand.c
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2015-09-30 21:32:29 +0000
committerjilles <jilles@FreeBSD.org>2015-09-30 21:32:29 +0000
commitaca221db9d8ee973f5ca86a03adfddf468891409 (patch)
treec0c8b623265338ef1866c1e7413af3622fae0154 /bin/sh/expand.c
parentbaa76ea3b344be8defab8d337442ca3b301a126c (diff)
downloadFreeBSD-src-aca221db9d8ee973f5ca86a03adfddf468891409.zip
FreeBSD-src-aca221db9d8ee973f5ca86a03adfddf468891409.tar.gz
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. Reviewed by: wblock (man page only) MFC after: 2 weeks Relnotes: yes Security: fixes command execution with wordexp(untrusted, WRDE_NOCMD)
Diffstat (limited to 'bin/sh/expand.c')
-rw-r--r--bin/sh/expand.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/bin/sh/expand.c b/bin/sh/expand.c
index 7411f1c..88d80ea 100644
--- a/bin/sh/expand.c
+++ b/bin/sh/expand.c
@@ -1656,3 +1656,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);
+}
OpenPOWER on IntegriCloud