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 */
73 /*----- Static variables --------------------------------------------------*/
75 static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
77 /*----- Main code ---------------------------------------------------------*/
79 /* --- @splitpath@ --- *
81 * Arguments: @const char *path@ = path string to break apart
82 * @struct elt *tail@ = tail block to attach to end of list
84 * Returns: Pointer to the new list head.
86 * Use: Breaks a path string into directories and adds each one
87 * as a node on the list, in the right order. These can then
88 * be pushed onto the directory stack as required.
91 static struct elt *splitpath(const char *path, struct elt *tail)
93 struct elt *head, **ee = &head, *e;
98 /* --- Either a leading `/', or a doubled one --- *
100 * Either way, ignore it.
108 /* --- Skip to the next directory separator --- *
110 * Build a list element for it, and link it on.
113 n = strcspn(path, "/");
114 e = xmalloc(sizeof(struct elt) + n + 1);
115 memcpy(e->e_name, path, n);
131 * Arguments: @struct state *state@ = working state
135 * Use: Removes the top item from the directory stack.
138 static void pop(struct state *state)
140 struct elt *sp = state->sp, *e;
144 state->path.len = sp->e_offset;
146 xfree(state->sp); state->sp = e;
150 /* --- @popall@ --- *
152 * Arguments: @struct state *state@ = working state
156 * Use: Removes all the items from the directory stack.
159 static void popall(struct state *state)
160 { while (state->sp->e_link) pop(state); }
164 * Arguments: @struct state *state@ = working state
165 * @struct elt *e@ = pointer to directory element
169 * Use: Pushes a new subdirectory onto the stack.
172 static void push(struct state *state, struct elt *e)
174 e->e_link = state->sp;
175 e->e_offset = state->path.len;
176 DPUTC(&state->path, '/');
177 DPUTS(&state->path, e->e_name);
181 /* --- @report@ --- *
183 * Arguments: @const struct checkpath *cp@ = pointer to query
184 * @unsigned what@ = what sort of report is this?
185 * @int verbose@ = how verbose is this?
186 * @const char *p@ = what path does it refer to?
187 * @const char *msg@ = the message to give to the user
191 * Use: Formats and presents messages to the client.
194 static void report(const struct checkpath *cp, unsigned what, int verbose,
195 const char *p, const char *msg, ...)
208 /* --- Decide whether to bin this message --- */
210 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
213 /* --- If no reporting, do the easy thing --- */
215 if (!(cp->cp_what & CP_REPORT)) {
216 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
220 /* --- Format the message nicely --- */
226 dstr_putf(&d, "Path: %s: ", p);
232 dstr_puts(&d, strerror(e));
235 u = (uid_t)va_arg(ap, int);
236 if ((pw = getpwuid(u)) != 0)
237 dstr_putf(&d, "`%s'", pw->pw_name);
239 dstr_putf(&d, "%i", (int)u);
242 g = (gid_t)va_arg(ap, int);
243 if ((gr = getgrgid(g)) != 0)
244 dstr_putf(&d, "`%s'", gr->gr_name);
246 dstr_putf(&d, "%i", (int)g);
249 s = va_arg(ap, const char *);
270 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
275 /* --- @sanity@ --- *
277 * Arguments: @const char *p@ = name of directory to check
278 * @struct stat *st@ = pointer to @stat@(2) block for it
279 * @const struct checkpath *cp@ = pointer to query
280 * @unsigned f@ = various flags (@SF_...@)
282 * Returns: Zero if everything's OK, else bitmask of problems.
284 * Use: Performs the main load of sanity-checking on a directory.
285 * If @SF_LAST@ is not set then sticky directories are always
289 #define SF_LAST 1u /* This is the final item to check */
291 static unsigned sanity(const char *p, struct stat *st,
292 const struct checkpath *cp, unsigned f)
299 if (S_ISDIR(st->st_mode) &&
300 (!(f & SF_LAST) || (cp->cp_what & CP_STICKYOK)))
303 /* --- Check for world-writability --- */
305 if ((cp->cp_what & CP_WRWORLD) &&
306 (st->st_mode & (S_IWOTH | stickyok)) == S_IWOTH) {
308 report(cp, CP_WRWORLD, 1, p, "** world writable **");
311 /* --- Check for group-writability --- */
313 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
314 (st->st_mode & (S_IWGRP | stickyok)) == S_IWGRP) {
317 if (cp->cp_what & CP_WROTHGRP) {
319 for (i = 0; i < cp->cp_gids; i++) {
320 if (st->st_gid == cp->cp_gid[i])
321 b = cp->cp_what & CP_WRGRP;
326 report(cp, b, 1, p, "writable by %sgroup %g",
327 (b == CP_WROTHGRP) ? "other " : "", st->st_gid);
331 /* --- Check for user-writability --- */
333 if ((cp->cp_what & CP_WROTHUSR) &&
334 st->st_uid != cp->cp_uid &&
337 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
340 /* --- Done sanity check --- */
345 /* --- @checkpath@ --- *
347 * Arguments: @const char *p@ = directory name which needs checking
348 * @const struct checkpath *cp@ = parameters for the check
350 * Returns: Zero if all is well, otherwise bitmask of problems.
352 * Use: Scrutinises a directory path to see what evil things other
353 * users could do to it.
356 unsigned checkpath(const char *p, const struct checkpath *cp)
364 dstr buf = DSTR_INIT;
367 /* --- Initialize the state --- */
369 pp = pool_create(arena_global);
370 state = pool_alloc(pp, sizeof(*state));
372 state->sp = (/*unconst*/ struct elt *)&rootnode;
373 dstr_create(&state->path);
375 /* --- Try to find the current directory --- */
377 if (!getcwd(cwd, sizeof(cwd))) {
378 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
382 /* --- Check that the root directory is OK --- */
384 if (stat("/", &st)) {
385 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
389 report(cp, CP_REPORT, 3, p, "begin scan");
390 bad |= sanity("/", &st, cp, 0);
392 /* --- Get the initial list of things to process --- */
394 ee = splitpath(p, 0);
396 ee = splitpath(cwd, ee);
398 /* --- While there are list items which still need doing --- */
403 /* --- Strip off simple `.' elements --- */
405 if (strcmp(ee->e_name, ".") == 0) {
411 /* --- Backtrack on `..' elements --- */
413 else if (strcmp(ee->e_name, "..") == 0) {
420 /* --- Everything else gets pushed on the end --- */
425 /* --- Find out what sort of a thing this is --- */
427 if (lstat(state->path.buf, &st)) {
428 report(cp, CP_ERROR, 0, state->path.buf, "can't stat: %e");
433 /* --- Handle symbolic links specially --- */
435 if (S_ISLNK(st.st_mode)) {
437 /* --- Resolve the link --- */
440 dstr_ensure(&buf, st.st_size + 1);
441 if ((i = readlink(state->path.buf, buf.buf, buf.sz)) < 0) {
442 report(cp, CP_ERROR, 0, state->path.buf, "can't readlink: %e");
447 report(cp, CP_SYMLINK, 2, state->path.buf, "symlink -> `%s'", buf.buf);
449 /* --- Handle sticky parents --- *
451 * If I make a symlink in a sticky directory, I can later modify it.
452 * However, nobody else can (except the owner of the directory, and
453 * we'll already have noticed that if we care).
456 if ((cp->cp_what & CP_WROTHUSR) &&
457 (state->sp->e_link->e_flags & EF_STICKY) &&
458 st.st_uid != cp->cp_uid && st.st_uid != 0) {
460 report(cp, CP_WROTHUSR, 1, state->path.buf,
461 "symlink modifiable by user %u", st.st_uid);
464 /* --- Sort out what to do from here --- */
466 if (buf.buf[0] == '/')
470 ee = splitpath(buf.buf, ee);
474 /* --- Run the sanity check on this path element --- */
476 bad |= sanity(state->path.buf, &st, cp, ee ? 0 : SF_LAST);
478 if (S_ISDIR(st.st_mode)) {
479 if (st.st_mode & S_ISVTX)
480 state->sp->e_flags |= EF_STICKY;
481 report(cp, CP_REPORT, 4, state->path.buf, "directory");
485 /* --- Something else I don't understand --- */
490 /* --- Check for leftover junk --- */
493 if (!(bad & CP_ERROR))
494 report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
503 dstr_destroy(&state->path);
505 pool_destroy(state->p);
509 /* --- @checkpath_addgid@ --- *
511 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
512 * @gid_t g@ = group id to add
514 * Returns: Zero if successful, nonzero if the array is full.
516 * Use: Adds the group @g@ to the structure.
519 int checkpath_addgid(struct checkpath *cp, gid_t g)
523 for (i = 0; i < cp->cp_gids; i++) {
524 if (cp->cp_gid[i] == g)
527 if (cp->cp_gids >= N(cp->cp_gid))
529 cp->cp_gid[cp->cp_gids++] = g;
533 /* --- @checkpath_setuid@ --- *
535 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
539 * Use: Fills in the @cp_uid@ slot of the structure with the real uid
540 * of the current process.
543 void checkpath_setuid(struct checkpath *cp) { cp->cp_uid = getuid(); }
545 /* --- @checkpath_setgid@ --- *
547 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
549 * Returns: Zero if successful, nonzero if the array is full.
551 * Use: Adds the real gid of the current process to the @cp_gid@
555 int checkpath_setgid(struct checkpath *cp)
556 { return (checkpath_addgid(cp, getgid())); }
558 /* --- @checkpath_setgroups@ --- *
560 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
562 * Returns: Zero if successful, nonzero if the array is full.
564 * Use: Adds the current process's supplementary groups to the
568 int checkpath_setgroups(struct checkpath *cp)
571 gid_t gg[NGROUPS_MAX];
573 n = getgroups(N(gg), gg);
574 for (i = 0; i < n; i++) {
575 if (checkpath_addgid(cp, gg[i]))
581 /* --- @checkpath_setids@ --- *
583 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
587 * Use: Fills in the user ids and things in the structure. This is
588 * equivalent to setting @cp_gids = 0@ and then calling
589 * @_setuid@, @_setgid@ and @_setgroups@. It can't fail.
592 void checkpath_setids(struct checkpath *cp)
595 checkpath_setuid(cp);
596 checkpath_setgid(cp);
597 checkpath_setgroups(cp);
600 /*----- That's all, folks -------------------------------------------------*/