safety fix for glob's vla usage: disallow patterns longer than PATH_MAX
[musl] / src / regex / glob.c
1 #include <glob.h>
2 #include <fnmatch.h>
3 #include <sys/stat.h>
4 #include <dirent.h>
5 #include <limits.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <stddef.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include "libc.h"
13
14 struct match
15 {
16         struct match *next;
17         char name[1];
18 };
19
20 static int is_literal(const char *p, int useesc)
21 {
22         int bracket = 0;
23         for (; *p; p++) {
24                 switch (*p) {
25                 case '\\':
26                         if (!useesc) break;
27                 case '?':
28                 case '*':
29                         return 0;
30                 case '[':
31                         bracket = 1;
32                         break;
33                 case ']':
34                         if (bracket) return 0;
35                         break;
36                 }
37         }
38         return 1;
39 }
40
41 static int append(struct match **tail, const char *name, size_t len, int mark)
42 {
43         struct match *new = malloc(sizeof(struct match) + len + 1);
44         if (!new) return -1;
45         (*tail)->next = new;
46         new->next = NULL;
47         strcpy(new->name, name);
48         if (mark) strcat(new->name, "/");
49         *tail = new;
50         return 0;
51 }
52
53 static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
54 {
55         DIR *dir;
56         long long de_buf[(sizeof(struct dirent) + NAME_MAX + sizeof(long long))/sizeof(long long)];
57         struct dirent *de;
58         char pat[strlen(p)+1];
59         char *p2;
60         size_t l = strlen(d);
61         int literal;
62         int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | FNM_PERIOD;
63         int error;
64
65         if ((p2 = strchr(p, '/'))) {
66                 strcpy(pat, p);
67                 pat[p2-p] = 0;
68                 for (; *p2 == '/'; p2++);
69                 p = pat;
70         }
71         literal = is_literal(p, !(flags & GLOB_NOESCAPE));
72         if (*d == '/' && !*(d+1)) l = 0;
73
74         /* rely on opendir failing for nondirectory objects */
75         dir = opendir(*d ? d : ".");
76         error = errno;
77         if (!dir) {
78                 /* this is not an error -- we let opendir call stat for us */
79                 if (error == ENOTDIR) return 0;
80                 if (error == EACCES && !*p) {
81                         struct stat st;
82                         if (!stat(d, &st) && S_ISDIR(st.st_mode)) {
83                                 if (append(tail, d, l, l))
84                                         return GLOB_NOSPACE;
85                                 return 0;
86                         }
87                 }
88                 if (errfunc(d, error) || (flags & GLOB_ERR))
89                         return GLOB_ABORTED;
90                 return 0;
91         }
92         if (!*p) {
93                 error = append(tail, d, l, l) ? GLOB_NOSPACE : 0;
94                 closedir(dir);
95                 return error;
96         }
97         while (!(error = readdir_r(dir, (void *)de_buf, &de)) && de) {
98                 char namebuf[l+de->d_reclen+2], *name = namebuf;
99                 if (!literal && fnmatch(p, de->d_name, fnm_flags))
100                         continue;
101                 if (literal && strcmp(p, de->d_name))
102                         continue;
103                 if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
104                         continue;
105                 if (*d) {
106                         memcpy(name, d, l);
107                         name[l] = '/';
108                         strcpy(name+l+1, de->d_name);
109                 } else {
110                         name = de->d_name;
111                 }
112                 if (p2) {
113                         if ((error = match_in_dir(name, p2, flags, errfunc, tail))) {
114                                 closedir(dir);
115                                 return error;
116                         }
117                 } else {
118                         int mark = 0;
119                         if (flags & GLOB_MARK) {
120                                 if (de->d_type)
121                                         mark = S_ISDIR(de->d_type<<12);
122                                 else {
123                                         struct stat st;
124                                         stat(name, &st);
125                                         mark = S_ISDIR(st.st_mode);
126                                 }
127                         }
128                         if (append(tail, name, l+de->d_reclen+1, mark)) {
129                                 closedir(dir);
130                                 return GLOB_NOSPACE;
131                         }
132                 }
133         }
134         closedir(dir);
135         if (error && (errfunc(d, error) || (flags & GLOB_ERR)))
136                 return GLOB_ABORTED;
137         return 0;
138 }
139
140 static int ignore_err(const char *path, int err)
141 {
142         return 0;
143 }
144
145 static void freelist(struct match *head)
146 {
147         struct match *match, *next;
148         for (match=head->next; match; match=next) {
149                 next = match->next;
150                 free(match);
151         }
152 }
153
154 static int sort(const void *a, const void *b)
155 {
156         return strcmp(*(const char **)a, *(const char **)b);
157 }
158
159 int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g)
160 {
161         const char *p=pat, *d;
162         struct match head = { .next = NULL }, *tail = &head;
163         size_t cnt, i;
164         size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
165         int error = 0;
166         
167         if (*p == '/') {
168                 for (; *p == '/'; p++);
169                 d = "/";
170         } else {
171                 d = "";
172         }
173
174         if (strlen(p) > PATH_MAX) return GLOB_NOSPACE;
175
176         if (!errfunc) errfunc = ignore_err;
177
178         if (!(flags & GLOB_APPEND)) {
179                 g->gl_offs = offs;
180                 g->gl_pathc = 0;
181                 g->gl_pathv = NULL;
182         }
183
184         if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
185         if (error == GLOB_NOSPACE) {
186                 freelist(&head);
187                 return error;
188         }
189         
190         for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
191         if (!cnt) {
192                 if (flags & GLOB_NOCHECK) {
193                         tail = &head;
194                         if (append(&tail, pat, strlen(pat), 0))
195                                 return GLOB_NOSPACE;
196                         cnt++;
197                 } else
198                         return GLOB_NOMATCH;
199         }
200
201         if (flags & GLOB_APPEND) {
202                 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
203                 if (!pathv) {
204                         freelist(&head);
205                         return GLOB_NOSPACE;
206                 }
207                 g->gl_pathv = pathv;
208                 offs += g->gl_pathc;
209         } else {
210                 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
211                 if (!g->gl_pathv) {
212                         freelist(&head);
213                         return GLOB_NOSPACE;
214                 }
215                 for (i=0; i<offs; i++)
216                         g->gl_pathv[i] = NULL;
217         }
218         for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
219                 g->gl_pathv[offs + i] = tail->name;
220         g->gl_pathv[offs + i] = NULL;
221         g->gl_pathc += cnt;
222
223         if (!(flags & GLOB_NOSORT))
224                 qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
225         
226         return error;
227 }
228
229 void globfree(glob_t *g)
230 {
231         size_t i;
232         for (i=0; i<g->gl_pathc; i++)
233                 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
234         free(g->gl_pathv);
235         g->gl_pathc = 0;
236         g->gl_pathv = NULL;
237 }
238
239 LFS64(glob);
240 LFS64(globfree);