efa7a97b |
1 | /* -*-c-*- |
2 | * |
72462155 |
3 | * $Id: path.c,v 1.3 1999/05/21 22:07:20 mdw Exp $ |
efa7a97b |
4 | * |
5 | * Check a path for safety |
6 | * |
7 | * (c) 1999 Mark Wooding |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of chkpath. |
13 | * |
14 | * chkpath is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * chkpath is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with chkpath; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: path.c,v $ |
72462155 |
32 | * Revision 1.3 1999/05/21 22:07:20 mdw |
33 | * Take advantage of new dynamic string macros. |
34 | * |
558a9b22 |
35 | * Revision 1.2 1999/05/18 20:49:12 mdw |
36 | * Use a dynamic string for reading symlinks. |
37 | * |
38 | * Revision 1.1.1.1 1999/04/06 20:12:07 mdw |
39 | * Import new project. |
efa7a97b |
40 | * |
41 | */ |
42 | |
43 | /*----- Header files ------------------------------------------------------*/ |
44 | |
45 | #include <errno.h> |
46 | #include <stdarg.h> |
47 | #include <stdio.h> |
48 | #include <stdlib.h> |
49 | #include <string.h> |
50 | |
51 | #include <sys/types.h> |
52 | #include <sys/stat.h> |
53 | #include <unistd.h> |
54 | |
55 | #include <pwd.h> |
56 | #include <grp.h> |
57 | |
58 | #include <mLib/alloc.h> |
59 | #include <mLib/dstr.h> |
60 | |
61 | #include "path.h" |
62 | |
63 | /*----- Data structures ---------------------------------------------------*/ |
64 | |
65 | /* --- An item in the directory list --- * |
66 | * |
67 | * Each directory becomes an element on a list which is manipulated in a |
68 | * stack-like way. |
69 | */ |
70 | |
71 | struct elt { |
72 | struct elt *e_link; /* Pointer to the next one along */ |
73 | size_t e_offset; /* Offset of name in path string */ |
74 | unsigned e_flags; /* Various useful flags */ |
75 | char e_name[1]; /* Name of the directory */ |
76 | }; |
77 | |
78 | enum { |
79 | f_sticky = 1 /* Directory has sticky bit set */ |
80 | }; |
81 | |
82 | enum { |
83 | f_last = 1 /* This is the final item to check */ |
84 | }; |
85 | |
86 | /*----- Static variables --------------------------------------------------*/ |
87 | |
88 | static struct elt rootnode = { 0, 0, 0 }; /* Root of the list */ |
89 | static struct elt *sp; /* Stack pointer for list */ |
72462155 |
90 | static dstr d = DSTR_INIT; /* Current path string */ |
efa7a97b |
91 | |
92 | /*----- Main code ---------------------------------------------------------*/ |
93 | |
94 | /* --- @splitpath@ --- * |
95 | * |
96 | * Arguments: @const char *path@ = path string to break apart |
97 | * @struct elt *tail@ = tail block to attach to end of list |
98 | * |
99 | * Returns: Pointer to the new list head. |
100 | * |
101 | * Use: Breaks a path string into directories and adds each one |
102 | * as a node on the list, in the right order. These can then |
103 | * be pushed onto the directory stack as required. |
104 | */ |
105 | |
106 | static struct elt *splitpath(const char *path, struct elt *tail) |
107 | { |
108 | struct elt *head, **ee = &head, *e; |
109 | |
110 | while (*path) { |
111 | size_t n; |
112 | |
113 | /* --- Either a leading `/', or a doubled one --- * |
114 | * |
115 | * Either way, ignore it. |
116 | */ |
117 | |
118 | if (*path == '/') { |
119 | path++; |
120 | continue; |
121 | } |
122 | |
123 | /* --- Skip to the next directory separator --- * |
124 | * |
125 | * Build a list element for it, and link it on. |
126 | */ |
127 | |
128 | n = strcspn(path, "/"); |
129 | e = xmalloc(sizeof(struct elt) + n + 1); |
130 | memcpy(e->e_name, path, n); |
131 | e->e_name[n] = 0; |
132 | e->e_flags = 0; |
133 | *ee = e; |
134 | ee = &e->e_link; |
135 | path += n; |
136 | } |
137 | |
138 | /* --- Done --- */ |
139 | |
140 | *ee = tail; |
141 | return (head); |
142 | } |
143 | |
144 | /* --- @pop@ --- * |
145 | * |
146 | * Arguments: --- |
147 | * |
148 | * Returns: --- |
149 | * |
150 | * Use: Removes the top item from the directory stack. |
151 | */ |
152 | |
153 | static void pop(void) |
154 | { |
155 | if (sp->e_link) { |
156 | struct elt *e = sp->e_link; |
157 | d.len = sp->e_offset; |
158 | DPUTZ(&d); |
159 | sp = e; |
160 | } |
161 | } |
162 | |
163 | /* --- @popall@ --- * |
164 | * |
165 | * Arguments: --- |
166 | * |
167 | * Returns: --- |
168 | * |
169 | * Use: Removes all the items from the directory stack. |
170 | */ |
171 | |
172 | static void popall(void) |
173 | { |
174 | while (sp->e_link) |
175 | pop(); |
176 | } |
177 | |
178 | /* --- @push@ --- * |
179 | * |
180 | * Arguments: @struct elt *e@ = pointer to directory element |
181 | * |
182 | * Returns: --- |
183 | * |
184 | * Use: Pushes a new subdirectory onto the stack. |
185 | */ |
186 | |
187 | static void push(struct elt *e) |
188 | { |
189 | e->e_link = sp; |
190 | e->e_offset = d.len; |
191 | DPUTC(&d, '/'); |
192 | DPUTS(&d, e->e_name); |
193 | sp = e; |
194 | } |
195 | |
196 | /* --- @report@ --- * |
197 | * |
198 | * Arguments: @struct chkpath *cp@ = pointer to context |
199 | * @int what@ = what sort of report is this? |
200 | * @int verbose@ = how verbose is this? |
201 | * @const char *p@ = what path does it refer to? |
202 | * @const char *msg@ = the message to give to the user |
203 | * |
204 | * Returns: --- |
205 | * |
206 | * Use: Formats and presents messages to the client. |
207 | */ |
208 | |
209 | static void report(struct chkpath *cp, int what, int verbose, |
210 | const char *p, const char *msg, ...) |
211 | { |
212 | /* --- Decide whether to bin this message --- */ |
213 | |
214 | if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what)) |
215 | return; |
216 | |
217 | /* --- Format the message nicely --- */ |
218 | |
219 | if (cp->cp_what & CP_REPORT) { |
72462155 |
220 | dstr d = DSTR_INIT; |
efa7a97b |
221 | va_list ap; |
222 | const char *q = msg; |
223 | size_t n; |
224 | int e = errno; |
225 | |
efa7a97b |
226 | va_start(ap, msg); |
227 | if (verbose > 1) |
228 | dstr_puts(&d, "[ "); |
229 | if (p) |
230 | dstr_putf(&d, "Path: %s: ", p); |
231 | while (*q) { |
232 | if (*q == '%') { |
233 | q++; |
234 | switch (*q) { |
235 | case 'e': |
236 | dstr_puts(&d, strerror(e)); |
237 | break; |
238 | case 'u': { |
239 | uid_t u = (uid_t)va_arg(ap, int); |
240 | struct passwd *pw = getpwuid(u); |
241 | if (pw) |
242 | dstr_putf(&d, "`%s'", pw->pw_name); |
243 | else |
244 | dstr_putf(&d, "%i", (int)u); |
245 | } break; |
246 | case 'g': { |
247 | gid_t g = (gid_t)va_arg(ap, int); |
248 | struct group *gr = getgrgid(g); |
249 | if (gr) |
250 | dstr_putf(&d, "`%s'", gr->gr_name); |
251 | else |
252 | dstr_putf(&d, "%i", (int)g); |
253 | } break; |
254 | case 's': { |
255 | const char *s = va_arg(ap, const char *); |
256 | dstr_puts(&d, s); |
257 | } break; |
258 | case '%': |
259 | dstr_putc(&d, '%'); |
260 | break; |
261 | default: |
262 | dstr_putc(&d, '%'); |
263 | dstr_putc(&d, *q); |
264 | break; |
265 | } |
266 | q++; |
267 | } else { |
268 | n = strcspn(q, "%"); |
269 | DPUTM(&d, q, n); |
270 | q += n; |
271 | } |
272 | } |
273 | if (verbose > 1) |
274 | dstr_puts(&d, " ]"); |
275 | DPUTZ(&d); |
276 | cp->cp_report(what, verbose, p, d.buf, cp->cp_arg); |
277 | dstr_destroy(&d); |
278 | va_end(ap); |
279 | } else |
280 | cp->cp_report(what, verbose, p, 0, cp->cp_arg); |
281 | } |
282 | |
283 | /* --- @sanity@ --- * |
284 | * |
285 | * Arguments: @const char *p@ = name of directory to check |
286 | * @struct stat *st@ = pointer to @stat@(2) block for it |
287 | * @struct chkpath *cp@ = pointer to caller parameters |
288 | * @unsigned f@ = various flags |
289 | * |
290 | * Returns: Zero if everything's OK, else bitmask of problems. |
291 | * |
292 | * Use: Performs the main load of sanity-checking on a directory. |
293 | */ |
294 | |
295 | static int sanity(const char *p, struct stat *st, |
296 | struct chkpath *cp, unsigned f) |
297 | { |
298 | int bad = 0; |
299 | int sticky = (cp->cp_what & CP_STICKYOK) || !(f & f_last) ? 01000 : 0; |
300 | |
301 | /* --- Check for world-writability --- */ |
302 | |
303 | if ((cp->cp_what & CP_WRWORLD) && |
304 | (st->st_mode & (0002 | sticky)) == 0002) { |
305 | bad |= CP_WRWORLD; |
306 | report(cp, CP_WRWORLD, 1, p, "** world writable **"); |
307 | } |
308 | |
309 | /* --- Check for group-writability --- */ |
310 | |
311 | if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) && |
312 | (st->st_mode & (0020 | sticky)) == 0020) { |
313 | if (cp->cp_what & CP_WRGRP) { |
314 | bad |= CP_WRGRP; |
315 | report(cp, CP_WRGRP, 1, p, "writable by group %g", st->st_gid); |
316 | } else { |
317 | int i; |
318 | for (i = 0; i < cp->cp_gids; i++) { |
319 | if (st->st_gid == cp->cp_gid[i]) |
320 | goto good_gid; |
321 | } |
322 | bad |= CP_WROTHGRP; |
323 | report(cp, CP_WROTHGRP, 1, p, "writable by group %g", st->st_gid); |
324 | good_gid:; |
325 | } |
326 | } |
327 | |
328 | /* --- Check for user-writability --- */ |
329 | |
330 | if ((cp->cp_what & CP_WROTHUSR) && |
331 | st->st_uid != cp->cp_uid && |
332 | st->st_uid != 0) { |
333 | bad |= CP_WROTHUSR; |
334 | report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid); |
335 | } |
336 | |
337 | /* --- Done sanity check --- */ |
338 | |
339 | return (bad); |
340 | } |
341 | |
342 | /* --- @path_check@ --- * |
343 | * |
344 | * Arguments: @const char *p@ = directory name which needs checking |
345 | * @struct chkpath *cp@ = caller parameters for the check |
346 | * |
347 | * Returns: Zero if all is well, otherwise bitmask of problems. |
348 | * |
349 | * Use: Scrutinises a directory path to see what evil things other |
350 | * users could do to it. |
351 | */ |
352 | |
353 | int path_check(const char *p, struct chkpath *cp) |
354 | { |
355 | char cwd[PATH_MAX]; |
356 | struct elt *e, *ee; |
357 | struct stat st; |
358 | int bad = 0; |
359 | |
72462155 |
360 | /* --- Initialize stack pointer and path string --- */ |
efa7a97b |
361 | |
362 | sp = &rootnode; |
363 | dstr_destroy(&d); |
364 | |
365 | /* --- Try to find the current directory --- */ |
366 | |
367 | if (!getcwd(cwd, sizeof(cwd))) { |
368 | report(cp, CP_ERROR, 0, 0, "can't find current directory: %e"); |
369 | return (CP_ERROR); |
370 | } |
371 | |
372 | /* --- Check that the root directory is OK --- */ |
373 | |
374 | if (stat("/", &st)) { |
375 | report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e"); |
376 | return (CP_ERROR); |
377 | } |
378 | |
379 | report(cp, CP_REPORT, 3, p, "begin scan"); |
380 | bad |= sanity("/", &st, cp, 0); |
381 | |
382 | /* --- Get the initial list of things to process --- */ |
383 | |
384 | ee = splitpath(p, 0); |
385 | if (*p != '/') |
386 | ee = splitpath(cwd, ee); |
387 | |
388 | /* --- While there are list items which still need doing --- */ |
389 | |
390 | while (ee) { |
391 | e = ee->e_link; |
392 | |
393 | /* --- Strip off simple `.' elements --- */ |
394 | |
395 | if (strcmp(ee->e_name, ".") == 0) { |
396 | free(ee); |
397 | ee = e; |
398 | continue; |
399 | } |
400 | |
401 | /* --- Backtrack on `..' elements --- */ |
402 | |
403 | else if (strcmp(ee->e_name, "..") == 0) { |
404 | pop(); |
405 | free(ee); |
406 | ee = e; |
407 | continue; |
408 | } |
409 | |
410 | /* --- Everything else gets pushed on the end --- */ |
411 | |
412 | push(ee); |
413 | ee = e; |
414 | |
415 | /* --- Find out what sort of a thing this is --- */ |
416 | |
417 | if (lstat(d.buf, &st)) { |
418 | report(cp, CP_ERROR, 0, d.buf, "can't stat: %e"); |
419 | bad |= CP_ERROR; |
420 | break; |
421 | } |
422 | |
423 | /* --- Handle symbolic links specially --- */ |
424 | |
425 | if (S_ISLNK(st.st_mode)) { |
72462155 |
426 | dstr buf = DSTR_INIT; |
efa7a97b |
427 | int i; |
428 | |
429 | /* --- Resolve the link --- */ |
430 | |
558a9b22 |
431 | dstr_ensure(&buf, st.st_size + 1); |
432 | if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) { |
efa7a97b |
433 | report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e"); |
434 | bad |= CP_ERROR; |
435 | break; |
436 | } |
558a9b22 |
437 | buf.buf[i] = 0; |
438 | report(cp, CP_SYMLINK, 2, d.buf, "symlink -> `%s'", buf.buf); |
efa7a97b |
439 | |
440 | /* --- Handle sticky parents --- * |
441 | * |
442 | * If I make a symlink in a sticky directory, I can later modify it. |
443 | * However, nobody else can (except the owner of the directory, and |
444 | * we'll already have noticed that if we care). |
445 | */ |
446 | |
447 | if ((cp->cp_what & CP_WROTHUSR) && |
448 | (sp->e_link->e_flags & f_sticky) && |
449 | st.st_uid != cp->cp_uid && st.st_uid != 0) { |
450 | bad |= CP_WROTHUSR; |
451 | report(cp, CP_WROTHUSR, 1, d.buf, |
452 | "symlink modifiable by user %u", st.st_uid); |
453 | } |
454 | |
455 | /* --- Sort out what to do from here --- */ |
456 | |
558a9b22 |
457 | if (buf.buf[0] == '/') |
efa7a97b |
458 | popall(); |
459 | else |
460 | pop(); |
558a9b22 |
461 | ee = splitpath(buf.buf, ee); |
462 | dstr_destroy(&buf); |
efa7a97b |
463 | continue; |
464 | } |
465 | |
466 | /* --- Run the sanity check on this path element --- */ |
467 | |
468 | bad |= sanity(d.buf, &st, cp, ee ? 0 : f_last); |
469 | |
470 | if (S_ISDIR(st.st_mode)) { |
471 | if (st.st_mode & 01000) |
472 | sp->e_flags |= f_sticky; |
473 | report(cp, CP_REPORT, 4, d.buf, "directory"); |
474 | continue; |
475 | } |
476 | |
477 | /* --- Something else I don't understand --- */ |
478 | |
479 | break; |
480 | } |
481 | |
482 | /* --- Check for leftover junk --- */ |
483 | |
484 | if (ee) { |
485 | if (!(bad & CP_ERROR)) |
486 | report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf"); |
487 | while (ee) { |
488 | e = ee->e_link; |
489 | free(ee); |
490 | ee = e; |
491 | } |
492 | } |
493 | |
494 | popall(); |
495 | return (bad); |
496 | } |
497 | |
498 | /* --- @path_setids@ --- * |
499 | * |
500 | * Arguments: @struct chkpath *cp@ = pointer to block to fill in |
501 | * |
502 | * Returns: Zero if OK, else @-1@. |
503 | * |
504 | * Use: Fills in the user ids and things in the structure. |
505 | */ |
506 | |
507 | void path_setids(struct chkpath *cp) |
508 | { |
509 | int n, i; |
510 | gid_t g = getgid(); |
511 | |
512 | cp->cp_uid = getuid(); |
513 | n = getgroups(sizeof(cp->cp_gid) / sizeof(cp->cp_gid[0]), cp->cp_gid); |
514 | |
515 | for (i = 0; i < n; i++) { |
516 | if (cp->cp_gid[i] == g) |
517 | goto gid_ok; |
518 | } |
519 | cp->cp_gid[n++] = g; |
520 | gid_ok: |
521 | cp->cp_gids = n; |
522 | } |
523 | |
524 | /*----- That's all, folks -------------------------------------------------*/ |