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