ash: improve / fix glob expansion
authorFelix Fietkau <nbd@nbd.name>
Tue, 31 Jan 2017 20:58:55 +0000 (21:58 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 31 Jan 2017 20:58:55 +0000 (21:58 +0100)
When using musl libc glob() a very long string can cause glob() to fail,
which leads to an out of memory error being raised by ash.

This can happen easily if a very long quoted string contains *, even
though no glob expansion should ever be performed on it (since it's
quoted).

Fix this by properly parsing control characters and escaping and only
accept unquoted metacharacters. While we're at it, unify this check for
libc and built-in glob expansion

Signed-off-by: Felix Fietkau <nbd@nbd.name>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash.c

index e07b81c0556fa165f4b380f3542d92c86c02b10a..6be3ec6003fa828836eed4852d9a660b19dc40b8 100644 (file)
@@ -7160,6 +7160,57 @@ addfname(const char *name)
        exparg.lastp = &sp->next;
 }
 
+/* Avoid glob() (and thus, stat() et al) for words like "echo" */
+static int
+hasmeta(const char *p)
+{
+       static const char chars[] ALIGN1 = {
+               '*', '?', '[', '\\', CTLQUOTEMARK, CTLESC, 0
+       };
+
+       for (;;) {
+               p = strpbrk(p, chars);
+               if (!p)
+                       break;
+               switch ((unsigned char) *p) {
+               case CTLQUOTEMARK:
+                       for (;;) {
+                               p++;
+                               if (*p == CTLQUOTEMARK)
+                                       break;
+                               if (*p == CTLESC)
+                                       p++;
+                               if (*p == '\0') /* huh? */
+                                       return 0;
+                       }
+                       break;
+               case '\\':
+               case CTLESC:
+                       p++;
+                       if (*p == '\0')
+                               return 0;
+                       break;
+               case '[':
+                       if (!strchr(p + 1, ']')) {
+                               /* It's not a properly closed [] pattern,
+                                * but other metas may follow. Continue checking.
+                                * my[file* _is_ globbed by bash
+                                * and matches filenames like "my[file1".
+                                */
+                               break;
+                       }
+                       /* fallthrough */
+               default:
+               /* case '*': */
+               /* case '?': */
+                       return 1;
+               }
+               p++;
+       }
+
+       return 0;
+}
+
 /* If we want to use glob() from libc... */
 #if !ENABLE_ASH_INTERNAL_GLOB
 
@@ -7186,20 +7237,9 @@ expandmeta(struct strlist *str /*, int flag*/)
                if (fflag)
                        goto nometa;
 
-               /* Avoid glob() (and thus, stat() et al) for words like "echo" */
-               p = str->text;
-               while (*p) {
-                       if (*p == '*')
-                               goto need_glob;
-                       if (*p == '?')
-                               goto need_glob;
-                       if (*p == '[')
-                               goto need_glob;
-                       p++;
-               }
-               goto nometa;
+               if (!hasmeta(str->text))
+                       goto nometa;
 
- need_glob:
                INT_OFF;
                p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
 // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match
@@ -7436,9 +7476,6 @@ expsort(struct strlist *str)
 static void
 expandmeta(struct strlist *str /*, int flag*/)
 {
-       static const char metachars[] ALIGN1 = {
-               '*', '?', '[', 0
-       };
        /* TODO - EXP_REDIR */
 
        while (str) {
@@ -7449,7 +7486,7 @@ expandmeta(struct strlist *str /*, int flag*/)
 
                if (fflag)
                        goto nometa;
-               if (!strpbrk(str->text, metachars))
+               if (!hasmeta(str->text))
                        goto nometa;
                savelastp = exparg.lastp;