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: @struct state *state@ = pointer to state
82 * @const char *path@ = path string to break apart
83 * @struct elt *tail@ = tail block to attach to end of list
85 * Returns: Pointer to the new list head.
87 * Use: Breaks a path string into directories and adds each one
88 * as a node on the list, in the right order. These can then
89 * be pushed onto the directory stack as required.
92 static struct elt *splitpath(struct state *state,
93 const char *path, struct elt *tail)
95 struct elt *head, **ee = &head, *e;
100 /* --- Either a leading `/', or a doubled one --- *
102 * Either way, ignore it.
110 /* --- Skip to the next directory separator --- *
112 * Build a list element for it, and link it on.
115 n = strcspn(path, "/");
116 e = pool_alloc(state->p, offsetof(struct elt, e_name) + n + 1);
117 memcpy(e->e_name, path, n);
133 * Arguments: @struct state *state@ = working state
137 * Use: Removes the top item from the directory stack.
140 static void pop(struct state *state)
142 struct elt *sp = state->sp, *e;
146 state->path.len = sp->e_offset;
152 /* --- @popall@ --- *
154 * Arguments: @struct state *state@ = working state
158 * Use: Removes all the items from the directory stack.
161 static void popall(struct state *state)
162 { state->sp = (/*unconst*/ struct elt *)&rootnode; state->path.len = 0; }
166 * Arguments: @struct state *state@ = working state
167 * @struct elt *e@ = pointer to directory element
171 * Use: Pushes a new subdirectory onto the stack.
174 static void push(struct state *state, struct elt *e)
176 e->e_link = state->sp;
177 e->e_offset = state->path.len;
178 DPUTC(&state->path, '/');
179 DPUTS(&state->path, e->e_name);
183 /* --- @report@ --- *
185 * Arguments: @const struct checkpath *cp@ = pointer to query
186 * @unsigned what@ = what sort of report is this?
187 * @int verbose@ = how verbose is this?
188 * @const char *p@ = what path does it refer to?
189 * @const char *msg@ = the message to give to the user
193 * Use: Formats and presents messages to the client.
196 static void report(const struct checkpath *cp, unsigned what, int verbose,
197 const char *p, const char *msg, ...)
210 /* --- Decide whether to bin this message --- */
212 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
215 /* --- If no reporting, do the easy thing --- */
217 if (!(cp->cp_what & CP_REPORT)) {
218 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
222 /* --- Format the message nicely --- */
228 dstr_putf(&d, "Path: %s: ", p);
234 dstr_puts(&d, strerror(e));
237 u = (uid_t)va_arg(ap, int);
238 if ((pw = getpwuid(u)) != 0)
239 dstr_putf(&d, "`%s'", pw->pw_name);
241 dstr_putf(&d, "%i", (int)u);
244 g = (gid_t)va_arg(ap, int);
245 if ((gr = getgrgid(g)) != 0)
246 dstr_putf(&d, "`%s'", gr->gr_name);
248 dstr_putf(&d, "%i", (int)g);
251 s = va_arg(ap, const char *);
272 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
277 /* --- @sanity@ --- *
279 * Arguments: @const char *p@ = name of directory to check
280 * @struct stat *st@ = pointer to @stat@(2) block for it
281 * @const struct checkpath *cp@ = pointer to query
282 * @unsigned f@ = various flags (@SF_...@)
284 * Returns: Zero if everything's OK, else bitmask of problems.
286 * Use: Performs the main load of sanity-checking on a directory.
287 * If @SF_LAST@ is not set then sticky directories are always
291 #define SF_LAST 1u /* This is the final item to check */
293 static unsigned sanity(const char *p, struct stat *st,
294 const struct checkpath *cp, unsigned f)
301 if (S_ISDIR(st->st_mode) &&
302 (!(f & SF_LAST) || (cp->cp_what & CP_STICKYOK)))
305 /* --- Check for world-writability --- */
307 if ((cp->cp_what & CP_WRWORLD) &&
308 (st->st_mode & (S_IWOTH | stickyok)) == S_IWOTH) {
310 report(cp, CP_WRWORLD, 1, p, "** world writable **");
313 /* --- Check for group-writability --- */
315 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
316 (st->st_mode & (S_IWGRP | stickyok)) == S_IWGRP) {
319 if (cp->cp_what & CP_WROTHGRP) {
321 for (i = 0; i < cp->cp_gids; i++) {
322 if (st->st_gid == cp->cp_gid[i])
323 b = cp->cp_what & CP_WRGRP;
328 report(cp, b, 1, p, "writable by %sgroup %g",
329 (b == CP_WROTHGRP) ? "other " : "", st->st_gid);
333 /* --- Check for user-writability --- */
335 if ((cp->cp_what & CP_WROTHUSR) &&
336 st->st_uid != cp->cp_uid &&
339 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
342 /* --- Done sanity check --- */
347 /* --- @checkpath@ --- *
349 * Arguments: @const char *p@ = directory name which needs checking
350 * @const struct checkpath *cp@ = parameters for the check
352 * Returns: Zero if all is well, otherwise bitmask of problems.
354 * Use: Scrutinises a directory path to see what evil things other
355 * users could do to it.
358 unsigned checkpath(const char *p, const struct checkpath *cp)
366 dstr buf = DSTR_INIT;
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);
377 /* --- Try to find the current directory --- */
379 if (!getcwd(cwd, sizeof(cwd))) {
380 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
384 /* --- Check that the root directory is OK --- */
386 if (stat("/", &st)) {
387 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
391 report(cp, CP_REPORT, 3, p, "begin scan");
392 bad |= sanity("/", &st, cp, 0);
394 /* --- Get the initial list of things to process --- */
396 ee = splitpath(state, p, 0);
398 ee = splitpath(state, cwd, ee);
400 /* --- While there are list items which still need doing --- */
405 /* --- Strip off simple `.' elements --- */
407 if (strcmp(ee->e_name, ".") == 0) {
412 /* --- Backtrack on `..' elements --- */
414 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(state, 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");
501 dstr_destroy(&state->path);
503 pool_destroy(state->p);
507 /* --- @checkpath_addgid@ --- *
509 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
510 * @gid_t g@ = group id to add
512 * Returns: Zero if successful, nonzero if the array is full.
514 * Use: Adds the group @g@ to the structure.
517 int checkpath_addgid(struct checkpath *cp, gid_t g)
521 for (i = 0; i < cp->cp_gids; i++) {
522 if (cp->cp_gid[i] == g)
525 if (cp->cp_gids >= N(cp->cp_gid))
527 cp->cp_gid[cp->cp_gids++] = g;
531 /* --- @checkpath_setuid@ --- *
533 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
537 * Use: Fills in the @cp_uid@ slot of the structure with the real uid
538 * of the current process.
541 void checkpath_setuid(struct checkpath *cp) { cp->cp_uid = getuid(); }
543 /* --- @checkpath_setgid@ --- *
545 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
547 * Returns: Zero if successful, nonzero if the array is full.
549 * Use: Adds the real gid of the current process to the @cp_gid@
553 int checkpath_setgid(struct checkpath *cp)
554 { return (checkpath_addgid(cp, getgid())); }
556 /* --- @checkpath_setgroups@ --- *
558 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
560 * Returns: Zero if successful, nonzero if the array is full.
562 * Use: Adds the current process's supplementary groups to the
566 int checkpath_setgroups(struct checkpath *cp)
569 gid_t gg[NGROUPS_MAX];
571 n = getgroups(N(gg), gg);
572 for (i = 0; i < n; i++) {
573 if (checkpath_addgid(cp, gg[i]))
579 /* --- @checkpath_setids@ --- *
581 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
585 * Use: Fills in the user ids and things in the structure. This is
586 * equivalent to setting @cp_gids = 0@ and then calling
587 * @_setuid@, @_setgid@ and @_setgroups@. It can't fail.
590 void checkpath_setids(struct checkpath *cp)
593 checkpath_setuid(cp);
594 checkpath_setgid(cp);
595 checkpath_setgroups(cp);
598 /*----- That's all, folks -------------------------------------------------*/