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