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>
48 #include "checkpath.h"
50 /*----- Data structures ---------------------------------------------------*/
52 /* --- An item in the directory list --- *
54 * Each directory becomes an element on a list which is manipulated in a
59 struct elt *e_link; /* Pointer to the next one along */
60 size_t e_offset; /* Offset of name in path string */
61 unsigned e_flags; /* Various useful flags */
62 #define EF_STICKY 1u /* Directory has sticky bit set */
63 char e_name[1]; /* Name of the directory */
67 struct elt *sp; /* Stack pointer for list */
68 dstr path; /* Current path string */
71 /*----- Static variables --------------------------------------------------*/
73 static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
75 /*----- Main code ---------------------------------------------------------*/
77 /* --- @splitpath@ --- *
79 * Arguments: @const char *path@ = path string to break apart
80 * @struct elt *tail@ = tail block to attach to end of list
82 * Returns: Pointer to the new list head.
84 * Use: Breaks a path string into directories and adds each one
85 * as a node on the list, in the right order. These can then
86 * be pushed onto the directory stack as required.
89 static struct elt *splitpath(const char *path, struct elt *tail)
91 struct elt *head, **ee = &head, *e;
96 /* --- Either a leading `/', or a doubled one --- *
98 * Either way, ignore it.
106 /* --- Skip to the next directory separator --- *
108 * Build a list element for it, and link it on.
111 n = strcspn(path, "/");
112 e = xmalloc(sizeof(struct elt) + n + 1);
113 memcpy(e->e_name, path, n);
129 * Arguments: @struct state *state@ = working state
133 * Use: Removes the top item from the directory stack.
136 static void pop(struct state *state)
138 struct elt *sp = state->sp, *e;
142 state->path.len = sp->e_offset;
144 xfree(state->sp); state->sp = e;
148 /* --- @popall@ --- *
150 * Arguments: @struct state *state@ = working state
154 * Use: Removes all the items from the directory stack.
157 static void popall(struct state *state)
158 { while (state->sp->e_link) pop(state); }
162 * Arguments: @struct state *state@ = working state
163 * @struct elt *e@ = pointer to directory element
167 * Use: Pushes a new subdirectory onto the stack.
170 static void push(struct state *state, struct elt *e)
172 e->e_link = state->sp;
173 e->e_offset = state->path.len;
174 DPUTC(&state->path, '/');
175 DPUTS(&state->path, e->e_name);
179 /* --- @report@ --- *
181 * Arguments: @const struct checkpath *cp@ = pointer to query
182 * @unsigned what@ = what sort of report is this?
183 * @int verbose@ = how verbose is this?
184 * @const char *p@ = what path does it refer to?
185 * @const char *msg@ = the message to give to the user
189 * Use: Formats and presents messages to the client.
192 static void report(const struct checkpath *cp, unsigned what, int verbose,
193 const char *p, const char *msg, ...)
206 /* --- Decide whether to bin this message --- */
208 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
211 /* --- If no reporting, do the easy thing --- */
213 if (!(cp->cp_what & CP_REPORT)) {
214 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
218 /* --- Format the message nicely --- */
224 dstr_putf(&d, "Path: %s: ", p);
230 dstr_puts(&d, strerror(e));
233 u = (uid_t)va_arg(ap, int);
234 if ((pw = getpwuid(u)) != 0)
235 dstr_putf(&d, "`%s'", pw->pw_name);
237 dstr_putf(&d, "%i", (int)u);
240 g = (gid_t)va_arg(ap, int);
241 if ((gr = getgrgid(g)) != 0)
242 dstr_putf(&d, "`%s'", gr->gr_name);
244 dstr_putf(&d, "%i", (int)g);
247 s = va_arg(ap, const char *);
268 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
273 /* --- @sanity@ --- *
275 * Arguments: @const char *p@ = name of directory to check
276 * @struct stat *st@ = pointer to @stat@(2) block for it
277 * @const struct checkpath *cp@ = pointer to query
278 * @unsigned f@ = various flags (@SF_...@)
280 * Returns: Zero if everything's OK, else bitmask of problems.
282 * Use: Performs the main load of sanity-checking on a directory.
283 * If @SF_LAST@ is not set then sticky directories are always
287 #define SF_LAST 1u /* This is the final item to check */
289 static unsigned sanity(const char *p, struct stat *st,
290 const struct checkpath *cp, unsigned f)
297 if (S_ISDIR(st->st_mode) &&
298 (!(f & SF_LAST) || (cp->cp_what & CP_STICKYOK)))
301 /* --- Check for world-writability --- */
303 if ((cp->cp_what & CP_WRWORLD) &&
304 (st->st_mode & (S_IWOTH | stickyok)) == S_IWOTH) {
306 report(cp, CP_WRWORLD, 1, p, "** world writable **");
309 /* --- Check for group-writability --- */
311 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
312 (st->st_mode & (S_IWGRP | stickyok)) == S_IWGRP) {
315 if (cp->cp_what & CP_WROTHGRP) {
317 for (i = 0; i < cp->cp_gids; i++) {
318 if (st->st_gid == cp->cp_gid[i])
319 b = cp->cp_what & CP_WRGRP;
324 report(cp, b, 1, p, "writable by %sgroup %g",
325 (b == CP_WROTHGRP) ? "other " : "", st->st_gid);
329 /* --- Check for user-writability --- */
331 if ((cp->cp_what & CP_WROTHUSR) &&
332 st->st_uid != cp->cp_uid &&
335 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
338 /* --- Done sanity check --- */
343 /* --- @checkpath@ --- *
345 * Arguments: @const char *p@ = directory name which needs checking
346 * @const struct checkpath *cp@ = parameters for the check
348 * Returns: Zero if all is well, otherwise bitmask of problems.
350 * Use: Scrutinises a directory path to see what evil things other
351 * users could do to it.
354 unsigned checkpath(const char *p, const struct checkpath *cp)
361 dstr buf = DSTR_INIT;
364 /* --- Initialize the state --- */
366 state.sp = (/*unconst*/ struct elt *)&rootnode;
367 dstr_create(&state.path);
369 /* --- Try to find the current directory --- */
371 if (!getcwd(cwd, sizeof(cwd))) {
372 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
376 /* --- Check that the root directory is OK --- */
378 if (stat("/", &st)) {
379 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
383 report(cp, CP_REPORT, 3, p, "begin scan");
384 bad |= sanity("/", &st, cp, 0);
386 /* --- Get the initial list of things to process --- */
388 ee = splitpath(p, 0);
390 ee = splitpath(cwd, ee);
392 /* --- While there are list items which still need doing --- */
397 /* --- Strip off simple `.' elements --- */
399 if (strcmp(ee->e_name, ".") == 0) {
405 /* --- Backtrack on `..' elements --- */
407 else if (strcmp(ee->e_name, "..") == 0) {
414 /* --- Everything else gets pushed on the end --- */
419 /* --- Find out what sort of a thing this is --- */
421 if (lstat(state.path.buf, &st)) {
422 report(cp, CP_ERROR, 0, state.path.buf, "can't stat: %e");
427 /* --- Handle symbolic links specially --- */
429 if (S_ISLNK(st.st_mode)) {
431 /* --- Resolve the link --- */
434 dstr_ensure(&buf, st.st_size + 1);
435 if ((i = readlink(state.path.buf, buf.buf, buf.sz)) < 0) {
436 report(cp, CP_ERROR, 0, state.path.buf, "can't readlink: %e");
441 report(cp, CP_SYMLINK, 2, state.path.buf, "symlink -> `%s'", buf.buf);
443 /* --- Handle sticky parents --- *
445 * If I make a symlink in a sticky directory, I can later modify it.
446 * However, nobody else can (except the owner of the directory, and
447 * we'll already have noticed that if we care).
450 if ((cp->cp_what & CP_WROTHUSR) &&
451 (state.sp->e_link->e_flags & EF_STICKY) &&
452 st.st_uid != cp->cp_uid && st.st_uid != 0) {
454 report(cp, CP_WROTHUSR, 1, state.path.buf,
455 "symlink modifiable by user %u", st.st_uid);
458 /* --- Sort out what to do from here --- */
460 if (buf.buf[0] == '/')
464 ee = splitpath(buf.buf, ee);
468 /* --- Run the sanity check on this path element --- */
470 bad |= sanity(state.path.buf, &st, cp, ee ? 0 : SF_LAST);
472 if (S_ISDIR(st.st_mode)) {
473 if (st.st_mode & S_ISVTX)
474 state.sp->e_flags |= EF_STICKY;
475 report(cp, CP_REPORT, 4, state.path.buf, "directory");
479 /* --- Something else I don't understand --- */
484 /* --- Check for leftover junk --- */
487 if (!(bad & CP_ERROR))
488 report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
497 dstr_destroy(&state.path);
502 /* --- @checkpath_addgid@ --- *
504 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
505 * @gid_t g@ = group id to add
507 * Returns: Zero if successful, nonzero if the array is full.
509 * Use: Adds the group @g@ to the structure.
512 int checkpath_addgid(struct checkpath *cp, gid_t g)
516 for (i = 0; i < cp->cp_gids; i++) {
517 if (cp->cp_gid[i] == g)
520 if (cp->cp_gids >= N(cp->cp_gid))
522 cp->cp_gid[cp->cp_gids++] = g;
526 /* --- @checkpath_setuid@ --- *
528 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
532 * Use: Fills in the @cp_uid@ slot of the structure with the real uid
533 * of the current process.
536 void checkpath_setuid(struct checkpath *cp) { cp->cp_uid = getuid(); }
538 /* --- @checkpath_setgid@ --- *
540 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
542 * Returns: Zero if successful, nonzero if the array is full.
544 * Use: Adds the real gid of the current process to the @cp_gid@
548 int checkpath_setgid(struct checkpath *cp)
549 { return (checkpath_addgid(cp, getgid())); }
551 /* --- @checkpath_setgroups@ --- *
553 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
555 * Returns: Zero if successful, nonzero if the array is full.
557 * Use: Adds the current process's supplementary groups to the
561 int checkpath_setgroups(struct checkpath *cp)
564 gid_t gg[NGROUPS_MAX];
566 n = getgroups(N(gg), gg);
567 for (i = 0; i < n; i++) {
568 if (checkpath_addgid(cp, gg[i]))
574 /* --- @checkpath_setids@ --- *
576 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
580 * Use: Fills in the user ids and things in the structure. This is
581 * equivalent to setting @cp_gids = 0@ and then calling
582 * @_setuid@, @_setgid@ and @_setgroups@. It can't fail.
585 void checkpath_setids(struct checkpath *cp)
588 checkpath_setuid(cp);
589 checkpath_setgid(cp);
590 checkpath_setgroups(cp);
593 /*----- That's all, folks -------------------------------------------------*/