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 */
74 /*----- Static variables --------------------------------------------------*/
76 static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
78 /*----- Main code ---------------------------------------------------------*/
80 /* --- @splitpath@ --- *
82 * Arguments: @struct state *state@ = pointer to state
83 * @const char *path@ = path string to break apart
84 * @struct elt *tail@ = tail block to attach to end of list
86 * Returns: Pointer to the new list head.
88 * Use: Breaks a path string into directories and adds each one
89 * as a node on the list, in the right order. These can then
90 * be pushed onto the directory stack as required.
93 static struct elt *splitpath(struct state *state,
94 const char *path, struct elt *tail)
96 struct elt *head, **ee = &head, *e;
101 /* --- Either a leading `/', or a doubled one --- *
103 * Either way, ignore it.
111 /* --- Skip to the next directory separator --- *
113 * Build a list element for it, and link it on.
116 n = strcspn(path, "/");
117 e = pool_alloc(state->p, offsetof(struct elt, e_name) + n + 1);
118 memcpy(e->e_name, path, n);
134 * Arguments: @struct state *state@ = working state
138 * Use: Removes the top item from the directory stack.
141 static void pop(struct state *state)
143 struct elt *sp = state->sp, *e;
147 state->path.len = sp->e_offset;
153 /* --- @popall@ --- *
155 * Arguments: @struct state *state@ = working state
159 * Use: Removes all the items from the directory stack.
162 static void popall(struct state *state)
163 { state->sp = (/*unconst*/ struct elt *)&rootnode; state->path.len = 0; }
167 * Arguments: @struct state *state@ = working state
168 * @struct elt *e@ = pointer to directory element
172 * Use: Pushes a new subdirectory onto the stack.
175 static void push(struct state *state, struct elt *e)
177 e->e_link = state->sp;
178 e->e_offset = state->path.len;
179 DPUTC(&state->path, '/');
180 DPUTS(&state->path, e->e_name);
184 /* --- @report@ --- *
186 * Arguments: @const struct checkpath *cp@ = pointer to query
187 * @unsigned what@ = what sort of report is this?
188 * @int verbose@ = how verbose is this?
189 * @const char *p@ = what path does it refer to?
190 * @const char *msg@ = the message to give to the user
194 * Use: Formats and presents messages to the client.
197 static void report(const struct checkpath *cp, unsigned what, int verbose,
198 const char *p, const char *msg, ...)
211 /* --- Decide whether to bin this message --- */
213 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
216 /* --- If no reporting, do the easy thing --- */
218 if (!(cp->cp_what & CP_REPORT)) {
219 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
223 /* --- Format the message nicely --- */
229 dstr_putf(&d, "Path: %s: ", p);
235 dstr_puts(&d, strerror(e));
238 u = (uid_t)va_arg(ap, int);
239 if ((pw = getpwuid(u)) != 0)
240 dstr_putf(&d, "`%s'", pw->pw_name);
242 dstr_putf(&d, "%i", (int)u);
245 g = (gid_t)va_arg(ap, int);
246 if ((gr = getgrgid(g)) != 0)
247 dstr_putf(&d, "`%s'", gr->gr_name);
249 dstr_putf(&d, "%i", (int)g);
252 s = va_arg(ap, const char *);
273 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
278 /* --- @sanity@ --- *
280 * Arguments: @const char *p@ = name of directory to check
281 * @struct stat *st@ = pointer to @stat@(2) block for it
282 * @const struct checkpath *cp@ = pointer to query
283 * @unsigned f@ = various flags (@SF_...@)
285 * Returns: Zero if everything's OK, else bitmask of problems.
287 * Use: Performs the main load of sanity-checking on a directory.
288 * If @SF_LAST@ is not set then sticky directories are always
292 #define SF_LAST 1u /* This is the final item to check */
294 static unsigned sanity(const char *p, struct stat *st,
295 const struct checkpath *cp, unsigned f)
302 if (S_ISDIR(st->st_mode) &&
303 (!(f & SF_LAST) || (cp->cp_what & CP_STICKYOK)))
306 /* --- Check for world-writability --- */
308 if ((cp->cp_what & CP_WRWORLD) &&
309 (st->st_mode & (S_IWOTH | stickyok)) == S_IWOTH) {
311 report(cp, CP_WRWORLD, 1, p, "** world writable **");
314 /* --- Check for group-writability --- */
316 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
317 (st->st_mode & (S_IWGRP | stickyok)) == S_IWGRP) {
320 if (cp->cp_what & CP_WROTHGRP) {
322 for (i = 0; i < cp->cp_gids; i++) {
323 if (st->st_gid == cp->cp_gid[i])
324 b = cp->cp_what & CP_WRGRP;
329 report(cp, b, 1, p, "writable by %sgroup %g",
330 (b == CP_WROTHGRP) ? "other " : "", st->st_gid);
334 /* --- Check for user-writability --- */
336 if ((cp->cp_what & CP_WROTHUSR) &&
337 st->st_uid != cp->cp_uid &&
340 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
343 /* --- Done sanity check --- */
348 /* --- @checkpath@ --- *
350 * Arguments: @const char *p@ = directory name which needs checking
351 * @const struct checkpath *cp@ = parameters for the check
353 * Returns: Zero if all is well, otherwise bitmask of problems.
355 * Use: Scrutinises a directory path to see what evil things other
356 * users could do to it.
359 unsigned checkpath(const char *p, const struct checkpath *cp)
369 /* --- Initialize the state --- */
371 pp = pool_create(arena_global);
372 state = pool_alloc(pp, sizeof(*state));
374 state->sp = (/*unconst*/ struct elt *)&rootnode;
375 dstr_create(&state->path);
376 dstr_create(&state->link);
378 /* --- Try to find the current directory --- */
380 if (!getcwd(cwd, sizeof(cwd))) {
381 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
385 /* --- Check that the root directory is OK --- */
387 if (stat("/", &st)) {
388 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
392 report(cp, CP_REPORT, 3, p, "begin scan");
393 bad |= sanity("/", &st, cp, 0);
395 /* --- Get the initial list of things to process --- */
397 ee = splitpath(state, p, 0);
399 ee = splitpath(state, cwd, ee);
401 /* --- While there are list items which still need doing --- */
406 /* --- Strip off simple `.' elements --- */
408 if (strcmp(ee->e_name, ".") == 0) {
413 /* --- Backtrack on `..' elements --- */
415 else if (strcmp(ee->e_name, "..") == 0) {
421 /* --- Everything else gets pushed on the end --- */
426 /* --- Find out what sort of a thing this is --- */
428 if (lstat(state->path.buf, &st)) {
429 report(cp, CP_ERROR, 0, state->path.buf, "can't stat: %e");
434 /* --- Handle symbolic links specially --- */
436 if (S_ISLNK(st.st_mode)) {
438 /* --- Resolve the link --- */
440 dstr_reset(&state->link);
441 dstr_ensure(&state->link, st.st_size + 1);
442 if ((i = readlink(state->path.buf,
443 state->link.buf, state->link.sz)) < 0) {
444 report(cp, CP_ERROR, 0, state->path.buf, "can't readlink: %e");
448 state->link.buf[i] = 0;
449 report(cp, CP_SYMLINK, 2, state->path.buf,
450 "symlink -> `%s'", state->link.buf);
452 /* --- Handle sticky parents --- *
454 * If I make a symlink in a sticky directory, I can later modify it.
455 * However, nobody else can (except the owner of the directory, and
456 * we'll already have noticed that if we care).
459 if ((cp->cp_what & CP_WROTHUSR) &&
460 (state->sp->e_link->e_flags & EF_STICKY) &&
461 st.st_uid != cp->cp_uid && st.st_uid != 0) {
463 report(cp, CP_WROTHUSR, 1, state->path.buf,
464 "symlink modifiable by user %u", st.st_uid);
467 /* --- Sort out what to do from here --- */
469 if (state->link.buf[0] == '/')
473 ee = splitpath(state, state->link.buf, ee);
477 /* --- Run the sanity check on this path element --- */
479 bad |= sanity(state->path.buf, &st, cp, ee ? 0 : SF_LAST);
481 if (S_ISDIR(st.st_mode)) {
482 if (st.st_mode & S_ISVTX)
483 state->sp->e_flags |= EF_STICKY;
484 report(cp, CP_REPORT, 4, state->path.buf, "directory");
488 /* --- Something else I don't understand --- */
493 /* --- Check for leftover junk --- */
496 if (!(bad & CP_ERROR))
497 report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
504 dstr_destroy(&state->path);
505 dstr_destroy(&state->link);
506 pool_destroy(state->p);
510 /* --- @checkpath_addgid@ --- *
512 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
513 * @gid_t g@ = group id to add
515 * Returns: Zero if successful, nonzero if the array is full.
517 * Use: Adds the group @g@ to the structure.
520 int checkpath_addgid(struct checkpath *cp, gid_t g)
524 for (i = 0; i < cp->cp_gids; i++) {
525 if (cp->cp_gid[i] == g)
528 if (cp->cp_gids >= N(cp->cp_gid))
530 cp->cp_gid[cp->cp_gids++] = g;
534 /* --- @checkpath_setuid@ --- *
536 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
540 * Use: Fills in the @cp_uid@ slot of the structure with the real uid
541 * of the current process.
544 void checkpath_setuid(struct checkpath *cp) { cp->cp_uid = getuid(); }
546 /* --- @checkpath_setgid@ --- *
548 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
550 * Returns: Zero if successful, nonzero if the array is full.
552 * Use: Adds the real gid of the current process to the @cp_gid@
556 int checkpath_setgid(struct checkpath *cp)
557 { return (checkpath_addgid(cp, getgid())); }
559 /* --- @checkpath_setgroups@ --- *
561 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
563 * Returns: Zero if successful, nonzero if the array is full.
565 * Use: Adds the current process's supplementary groups to the
569 int checkpath_setgroups(struct checkpath *cp)
572 gid_t gg[NGROUPS_MAX];
574 n = getgroups(N(gg), gg);
575 for (i = 0; i < n; i++) {
576 if (checkpath_addgid(cp, gg[i]))
582 /* --- @checkpath_setids@ --- *
584 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
588 * Use: Fills in the user ids and things in the structure. This is
589 * equivalent to setting @cp_gids = 0@ and then calling
590 * @_setuid@, @_setgid@ and @_setgroups@. It can't fail.
593 void checkpath_setids(struct checkpath *cp)
596 checkpath_setuid(cp);
597 checkpath_setgid(cp);
598 checkpath_setgroups(cp);
601 /*----- That's all, folks -------------------------------------------------*/