3 * Check a path for safety
5 * (c) 1999 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of chkpath.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
44 #include <mLib/alloc.h>
45 #include <mLib/dstr.h>
46 #include <mLib/macros.h>
47 #include <mLib/pool.h>
49 #include "checkpath.h"
51 /*----- Data structures ---------------------------------------------------*/
53 /* --- An item in the directory list --- *
55 * Each directory becomes an element on a list which is manipulated in a
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 #define EF_STICKY 1u /* Directory has sticky bit set */
64 char e_name[1]; /* Name of the directory */
68 pool *p; /* Allocation pool */
69 struct elt *sp; /* Stack pointer for list */
70 dstr path; /* Current path string */
71 dstr link; /* Symbolic link target string */
72 dstr msg; /* Message string */
75 /*----- Static variables --------------------------------------------------*/
77 static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
79 /*----- Main code ---------------------------------------------------------*/
81 /* --- @splitpath@ --- *
83 * Arguments: @struct state *state@ = pointer to state
84 * @const char *path@ = path string to break apart
85 * @struct elt *tail@ = tail block to attach to end of list
87 * Returns: Pointer to the new list head.
89 * Use: Breaks a path string into directories and adds each one
90 * as a node on the list, in the right order. These can then
91 * be pushed onto the directory stack as required.
94 static struct elt *splitpath(struct state *state,
95 const char *path, struct elt *tail)
97 struct elt *head, **ee = &head, *e;
102 /* --- Either a leading `/', or a doubled one --- *
104 * Either way, ignore it.
112 /* --- Skip to the next directory separator --- *
114 * Build a list element for it, and link it on.
117 n = strcspn(path, "/");
118 e = pool_alloc(state->p, offsetof(struct elt, e_name) + n + 1);
119 memcpy(e->e_name, path, n);
135 * Arguments: @struct state *state@ = working state
139 * Use: Removes the top item from the directory stack.
142 static void pop(struct state *state)
144 struct elt *sp = state->sp, *e;
148 state->path.len = sp->e_offset;
154 /* --- @popall@ --- *
156 * Arguments: @struct state *state@ = working state
160 * Use: Removes all the items from the directory stack.
163 static void popall(struct state *state)
164 { state->sp = (/*unconst*/ struct elt *)&rootnode; state->path.len = 0; }
168 * Arguments: @struct state *state@ = working state
169 * @struct elt *e@ = pointer to directory element
173 * Use: Pushes a new subdirectory onto the stack.
176 static void push(struct state *state, struct elt *e)
178 e->e_link = state->sp;
179 e->e_offset = state->path.len;
180 DPUTC(&state->path, '/');
181 DPUTS(&state->path, e->e_name);
185 /* --- @report@ --- *
187 * Arguments: @struct state *state@ = pointer to state
188 * @const struct checkpath *cp@ = pointer to query
189 * @unsigned what@ = what sort of report is this?
190 * @int verbose@ = how verbose is this?
191 * @const char *p@ = what path does it refer to?
192 * @const char *msg@ = the message to give to the user
196 * Use: Formats and presents messages to the client.
199 static void report(struct state *state, const struct checkpath *cp,
200 unsigned what, int verbose,
201 const char *p, const char *msg, ...)
213 /* --- Decide whether to bin this message --- */
215 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
218 /* --- If no reporting, do the easy thing --- */
220 if (!(cp->cp_what & CP_REPORT)) {
221 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
225 /* --- Format the message nicely --- */
227 dstr_reset(&state->msg);
230 dstr_puts(&state->msg, "[ ");
232 dstr_putf(&state->msg, "Path: %s: ", p);
238 dstr_puts(&state->msg, strerror(e));
241 u = (uid_t)va_arg(ap, long);
242 if ((pw = getpwuid(u)) != 0)
243 dstr_putf(&state->msg, "`%s'", pw->pw_name);
245 dstr_putf(&state->msg, "#%ld", (long)u);
248 g = (gid_t)va_arg(ap, long);
249 if ((gr = getgrgid(g)) != 0)
250 dstr_putf(&state->msg, "`%s'", gr->gr_name);
252 dstr_putf(&state->msg, "#%ld", (long)g);
255 s = va_arg(ap, const char *);
256 dstr_puts(&state->msg, s);
259 dstr_putc(&state->msg, '%');
262 dstr_putc(&state->msg, '%');
263 dstr_putc(&state->msg, *q);
269 DPUTM(&state->msg, q, n);
274 dstr_puts(&state->msg, " ]");
276 cp->cp_report(what, verbose, p, state->msg.buf, cp->cp_arg);
280 /* --- @sanity@ --- *
282 * Arguments: @const char *p@ = name of directory to check
283 * @struct stat *st@ = pointer to @stat@(2) block for it
284 * @struct state *state@ = pointer to state
285 * @const struct checkpath *cp@ = pointer to query
286 * @unsigned f@ = various flags (@SF_...@)
288 * Returns: Zero if everything's OK, else bitmask of problems.
290 * Use: Performs the main load of sanity-checking on a directory.
291 * If @SF_LAST@ is not set then sticky directories are always
295 #define SF_LAST 1u /* This is the final item to check */
297 static unsigned sanity(const char *p, struct stat *st,
298 struct state *state, const struct checkpath *cp,
306 if (S_ISDIR(st->st_mode) &&
307 (!(f & SF_LAST) || (cp->cp_what & CP_STICKYOK)))
310 /* --- Check for world-writability --- */
312 if ((cp->cp_what & CP_WRWORLD) &&
313 (st->st_mode & (S_IWOTH | stickyok)) == S_IWOTH) {
315 report(state, cp, CP_WRWORLD, 1, p, "** world writable **");
318 /* --- Check for group-writability --- */
320 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
321 (st->st_mode & (S_IWGRP | stickyok)) == S_IWGRP) {
324 if (cp->cp_what & CP_WROTHGRP) {
326 for (i = 0; i < cp->cp_gids; i++) {
327 if (st->st_gid == cp->cp_gid[i])
328 b = cp->cp_what & CP_WRGRP;
333 report(state, cp, b, 1, p, "writable by %sgroup %g",
334 (b == CP_WROTHGRP) ? "other " : "", (long)st->st_gid);
338 /* --- Check for user-writability --- */
340 if ((cp->cp_what & CP_WROTHUSR) &&
341 st->st_uid != cp->cp_uid &&
344 report(state, cp, CP_WROTHUSR, 1, p,
345 "owner is user %u", (long)st->st_uid);
348 /* --- Done sanity check --- */
353 /* --- @checkpath@ --- *
355 * Arguments: @const char *p@ = directory name which needs checking
356 * @const struct checkpath *cp@ = parameters for the check
358 * Returns: Zero if all is well, otherwise bitmask of problems.
360 * Use: Scrutinises a directory path to see what evil things other
361 * users could do to it.
364 unsigned checkpath(const char *p, const struct checkpath *cp)
374 /* --- Initialize the state --- */
376 pp = pool_create(arena_global);
377 state = pool_alloc(pp, sizeof(*state));
379 state->sp = (/*unconst*/ struct elt *)&rootnode;
380 dstr_create(&state->path);
381 dstr_create(&state->link);
382 dstr_create(&state->msg);
384 /* --- Try to find the current directory --- */
386 if (!getcwd(cwd, sizeof(cwd))) {
387 report(state, cp, CP_ERROR, 0, 0, "can't find current directory: %e");
391 /* --- Check that the root directory is OK --- */
393 if (stat("/", &st)) {
394 report(state, cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
398 report(state, cp, CP_REPORT, 3, p, "begin scan");
399 bad |= sanity("/", &st, state, cp, 0);
401 /* --- Get the initial list of things to process --- */
403 ee = splitpath(state, p, 0);
405 ee = splitpath(state, cwd, ee);
407 /* --- While there are list items which still need doing --- */
412 /* --- Strip off simple `.' elements --- */
414 if (strcmp(ee->e_name, ".") == 0) {
419 /* --- Backtrack on `..' elements --- */
421 else if (strcmp(ee->e_name, "..") == 0) {
427 /* --- Everything else gets pushed on the end --- */
432 /* --- Find out what sort of a thing this is --- */
434 if (lstat(state->path.buf, &st)) {
435 report(state, cp, CP_ERROR, 0, state->path.buf, "can't stat: %e");
440 /* --- Handle symbolic links specially --- */
442 if (S_ISLNK(st.st_mode)) {
444 /* --- Resolve the link --- */
446 dstr_reset(&state->link);
447 dstr_ensure(&state->link, st.st_size + 1);
448 if ((i = readlink(state->path.buf,
449 state->link.buf, state->link.sz)) < 0) {
450 report(state, cp, CP_ERROR, 0, state->path.buf,
451 "can't readlink: %e");
455 state->link.buf[i] = 0;
456 report(state, cp, CP_SYMLINK, 2, state->path.buf,
457 "symlink -> `%s'", state->link.buf);
459 /* --- Handle sticky parents --- *
461 * If I make a symlink in a sticky directory, I can later modify it.
462 * However, nobody else can (except the owner of the directory, and
463 * we'll already have noticed that if we care).
466 if ((cp->cp_what & CP_WROTHUSR) &&
467 (state->sp->e_link->e_flags & EF_STICKY) &&
468 st.st_uid != cp->cp_uid && st.st_uid != 0) {
470 report(state, cp, CP_WROTHUSR, 1, state->path.buf,
471 "symlink modifiable by user %u", (long)st.st_uid);
474 /* --- Sort out what to do from here --- */
476 if (state->link.buf[0] == '/')
480 ee = splitpath(state, state->link.buf, ee);
484 /* --- Run the sanity check on this path element --- */
486 bad |= sanity(state->path.buf, &st, state, cp, ee ? 0 : SF_LAST);
488 if (S_ISDIR(st.st_mode)) {
489 if (st.st_mode & S_ISVTX)
490 state->sp->e_flags |= EF_STICKY;
491 report(state, cp, CP_REPORT, 4, state->path.buf, "directory");
495 /* --- Something else I don't understand --- */
500 /* --- Check for leftover junk --- */
503 if (!(bad & CP_ERROR))
504 report(state, cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
511 dstr_destroy(&state->path);
512 dstr_destroy(&state->link);
513 dstr_destroy(&state->msg);
514 pool_destroy(state->p);
518 /* --- @checkpath_addgid@ --- *
520 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
521 * @gid_t g@ = group id to add
523 * Returns: Zero if successful, nonzero if the array is full.
525 * Use: Adds the group @g@ to the structure.
528 int checkpath_addgid(struct checkpath *cp, gid_t g)
532 for (i = 0; i < cp->cp_gids; i++) {
533 if (cp->cp_gid[i] == g)
536 if (cp->cp_gids >= N(cp->cp_gid))
538 cp->cp_gid[cp->cp_gids++] = g;
542 /* --- @checkpath_setuid@ --- *
544 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
548 * Use: Fills in the @cp_uid@ slot of the structure with the real uid
549 * of the current process.
552 void checkpath_setuid(struct checkpath *cp) { cp->cp_uid = getuid(); }
554 /* --- @checkpath_setgid@ --- *
556 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
558 * Returns: Zero if successful, nonzero if the array is full.
560 * Use: Adds the real gid of the current process to the @cp_gid@
564 int checkpath_setgid(struct checkpath *cp)
565 { return (checkpath_addgid(cp, getgid())); }
567 /* --- @checkpath_setgroups@ --- *
569 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
571 * Returns: Zero if successful, nonzero if the array is full.
573 * Use: Adds the current process's supplementary groups to the
577 int checkpath_setgroups(struct checkpath *cp)
580 gid_t gg[NGROUPS_MAX];
582 n = getgroups(N(gg), gg);
583 for (i = 0; i < n; i++) {
584 if (checkpath_addgid(cp, gg[i]))
590 /* --- @checkpath_setids@ --- *
592 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
596 * Use: Fills in the user ids and things in the structure. This is
597 * equivalent to setting @cp_gids = 0@ and then calling
598 * @_setuid@, @_setgid@ and @_setgroups@. It can't fail.
601 void checkpath_setids(struct checkpath *cp)
604 checkpath_setuid(cp);
605 checkpath_setgid(cp);
606 checkpath_setgroups(cp);
609 /*----- That's all, folks -------------------------------------------------*/