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 ------------------------------------------------------*/
35 #include <sys/types.h>
42 #include <mLib/alloc.h>
43 #include <mLib/dstr.h>
45 #include "checkpath.h"
47 /*----- Data structures ---------------------------------------------------*/
49 /* --- An item in the directory list --- *
51 * Each directory becomes an element on a list which is manipulated in a
56 struct elt *e_link; /* Pointer to the next one along */
57 size_t e_offset; /* Offset of name in path string */
58 unsigned e_flags; /* Various useful flags */
59 char e_name[1]; /* Name of the directory */
62 #define f_sticky 1u /* Directory has sticky bit set */
64 #define f_last 1u /* This is the final item to check */
66 /*----- Static variables --------------------------------------------------*/
68 static struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
69 static struct elt *sp; /* Stack pointer for list */
70 static dstr d = DSTR_INIT; /* Current path string */
72 /*----- Main code ---------------------------------------------------------*/
74 /* --- @splitpath@ --- *
76 * Arguments: @const char *path@ = path string to break apart
77 * @struct elt *tail@ = tail block to attach to end of list
79 * Returns: Pointer to the new list head.
81 * Use: Breaks a path string into directories and adds each one
82 * as a node on the list, in the right order. These can then
83 * be pushed onto the directory stack as required.
86 static struct elt *splitpath(const char *path, struct elt *tail)
88 struct elt *head, **ee = &head, *e;
93 /* --- Either a leading `/', or a doubled one --- *
95 * Either way, ignore it.
103 /* --- Skip to the next directory separator --- *
105 * Build a list element for it, and link it on.
108 n = strcspn(path, "/");
109 e = xmalloc(sizeof(struct elt) + n + 1);
110 memcpy(e->e_name, path, n);
130 * Use: Removes the top item from the directory stack.
133 static void pop(void)
136 struct elt *e = sp->e_link;
137 d.len = sp->e_offset;
143 /* --- @popall@ --- *
149 * Use: Removes all the items from the directory stack.
152 static void popall(void)
153 { while (sp->e_link) pop(); }
157 * Arguments: @struct elt *e@ = pointer to directory element
161 * Use: Pushes a new subdirectory onto the stack.
164 static void push(struct elt *e)
169 DPUTS(&d, e->e_name);
173 /* --- @report@ --- *
175 * Arguments: @const struct checkpath *cp@ = pointer to context
176 * @unsigned what@ = what sort of report is this?
177 * @int verbose@ = how verbose is this?
178 * @const char *p@ = what path does it refer to?
179 * @const char *msg@ = the message to give to the user
183 * Use: Formats and presents messages to the client.
186 static void report(const struct checkpath *cp, unsigned what, int verbose,
187 const char *p, const char *msg, ...)
200 /* --- Decide whether to bin this message --- */
202 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
205 /* --- If no reporting, do the easy thing --- */
207 if (!(cp->cp_what & CP_REPORT)) {
208 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
212 /* --- Format the message nicely --- */
218 dstr_putf(&d, "Path: %s: ", p);
224 dstr_puts(&d, strerror(e));
227 u = (uid_t)va_arg(ap, int);
228 if ((pw = getpwuid(u)) != 0)
229 dstr_putf(&d, "`%s'", pw->pw_name);
231 dstr_putf(&d, "%i", (int)u);
234 g = (gid_t)va_arg(ap, int);
235 if ((gr = getgrgid(g)) != 0)
236 dstr_putf(&d, "`%s'", gr->gr_name);
238 dstr_putf(&d, "%i", (int)g);
241 s = va_arg(ap, const char *);
262 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
267 /* --- @sanity@ --- *
269 * Arguments: @const char *p@ = name of directory to check
270 * @struct stat *st@ = pointer to @stat@(2) block for it
271 * @const struct checkpath *cp@ = pointer to caller parameters
272 * @unsigned f@ = various flags
274 * Returns: Zero if everything's OK, else bitmask of problems.
276 * Use: Performs the main load of sanity-checking on a directory.
279 static unsigned sanity(const char *p, struct stat *st,
280 const struct checkpath *cp, unsigned f)
287 if (S_ISDIR(st->st_mode) &&
288 (!(f & f_last) || (cp->cp_what & CP_STICKYOK)))
291 /* --- Check for world-writability --- */
293 if ((cp->cp_what & CP_WRWORLD) &&
294 (st->st_mode & (0002 | stickyok)) == 0002) {
296 report(cp, CP_WRWORLD, 1, p, "** world writable **");
299 /* --- Check for group-writability --- */
301 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
302 (st->st_mode & (0020 | stickyok)) == 0020) {
305 if (cp->cp_what & CP_WROTHGRP) {
307 for (i = 0; i < cp->cp_gids; i++) {
308 if (st->st_gid == cp->cp_gid[i])
309 b = cp->cp_what & CP_WRGRP;
314 report(cp, b, 1, p, "writable by %sgroup %g",
315 (b == CP_WROTHGRP) ? "other " : "", st->st_gid);
319 /* --- Check for user-writability --- */
321 if ((cp->cp_what & CP_WROTHUSR) &&
322 st->st_uid != cp->cp_uid &&
325 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
328 /* --- Done sanity check --- */
333 /* --- @checkpath@ --- *
335 * Arguments: @const char *p@ = directory name which needs checking
336 * @const struct checkpath *cp@ = parameters for the check
338 * Returns: Zero if all is well, otherwise bitmask of problems.
340 * Use: Scrutinises a directory path to see what evil things other
341 * users could do to it.
344 unsigned checkpath(const char *p, const struct checkpath *cp)
351 /* --- Initialize stack pointer and path string --- */
356 /* --- Try to find the current directory --- */
358 if (!getcwd(cwd, sizeof(cwd))) {
359 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
363 /* --- Check that the root directory is OK --- */
365 if (stat("/", &st)) {
366 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
370 report(cp, CP_REPORT, 3, p, "begin scan");
371 bad |= sanity("/", &st, cp, 0);
373 /* --- Get the initial list of things to process --- */
375 ee = splitpath(p, 0);
377 ee = splitpath(cwd, ee);
379 /* --- While there are list items which still need doing --- */
384 /* --- Strip off simple `.' elements --- */
386 if (strcmp(ee->e_name, ".") == 0) {
392 /* --- Backtrack on `..' elements --- */
394 else if (strcmp(ee->e_name, "..") == 0) {
401 /* --- Everything else gets pushed on the end --- */
406 /* --- Find out what sort of a thing this is --- */
408 if (lstat(d.buf, &st)) {
409 report(cp, CP_ERROR, 0, d.buf, "can't stat: %e");
414 /* --- Handle symbolic links specially --- */
416 if (S_ISLNK(st.st_mode)) {
417 dstr buf = DSTR_INIT;
420 /* --- Resolve the link --- */
422 dstr_ensure(&buf, st.st_size + 1);
423 if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) {
424 report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e");
429 report(cp, CP_SYMLINK, 2, d.buf, "symlink -> `%s'", buf.buf);
431 /* --- Handle sticky parents --- *
433 * If I make a symlink in a sticky directory, I can later modify it.
434 * However, nobody else can (except the owner of the directory, and
435 * we'll already have noticed that if we care).
438 if ((cp->cp_what & CP_WROTHUSR) &&
439 (sp->e_link->e_flags & f_sticky) &&
440 st.st_uid != cp->cp_uid && st.st_uid != 0) {
442 report(cp, CP_WROTHUSR, 1, d.buf,
443 "symlink modifiable by user %u", st.st_uid);
446 /* --- Sort out what to do from here --- */
448 if (buf.buf[0] == '/')
452 ee = splitpath(buf.buf, ee);
457 /* --- Run the sanity check on this path element --- */
459 bad |= sanity(d.buf, &st, cp, ee ? 0 : f_last);
461 if (S_ISDIR(st.st_mode)) {
462 if (st.st_mode & 01000)
463 sp->e_flags |= f_sticky;
464 report(cp, CP_REPORT, 4, d.buf, "directory");
468 /* --- Something else I don't understand --- */
473 /* --- Check for leftover junk --- */
476 if (!(bad & CP_ERROR))
477 report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
489 /* --- @checkpath_setids@ --- *
491 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
495 * Use: Fills in the user ids and things in the structure.
498 void checkpath_setids(struct checkpath *cp)
503 cp->cp_uid = getuid();
504 n = getgroups(sizeof(cp->cp_gid) / sizeof(cp->cp_gid[0]), cp->cp_gid);
506 for (i = 0; i < n; i++) {
507 if (cp->cp_gid[i] == g)
515 /*----- That's all, folks -------------------------------------------------*/