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