3 * $Id: checkpath.c,v 1.6 2004/04/08 01:36:22 mdw Exp $
5 * Check a path for safety
7 * (c) 1999 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of chkpath.
14 * chkpath is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * chkpath is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with chkpath; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
46 #include <mLib/alloc.h>
47 #include <mLib/dstr.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 char e_name[1]; /* Name of the directory */
66 #define f_sticky 1u /* Directory has sticky bit set */
68 #define f_last 1u /* This is the final item to check */
70 /*----- Static variables --------------------------------------------------*/
72 static struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
73 static struct elt *sp; /* Stack pointer for list */
74 static dstr d = DSTR_INIT; /* Current path string */
76 /*----- Main code ---------------------------------------------------------*/
78 /* --- @splitpath@ --- *
80 * Arguments: @const char *path@ = path string to break apart
81 * @struct elt *tail@ = tail block to attach to end of list
83 * Returns: Pointer to the new list head.
85 * Use: Breaks a path string into directories and adds each one
86 * as a node on the list, in the right order. These can then
87 * be pushed onto the directory stack as required.
90 static struct elt *splitpath(const char *path, struct elt *tail)
92 struct elt *head, **ee = &head, *e;
97 /* --- Either a leading `/', or a doubled one --- *
99 * Either way, ignore it.
107 /* --- Skip to the next directory separator --- *
109 * Build a list element for it, and link it on.
112 n = strcspn(path, "/");
113 e = xmalloc(sizeof(struct elt) + n + 1);
114 memcpy(e->e_name, path, n);
134 * Use: Removes the top item from the directory stack.
137 static void pop(void)
140 struct elt *e = sp->e_link;
141 d.len = sp->e_offset;
147 /* --- @popall@ --- *
153 * Use: Removes all the items from the directory stack.
156 static void popall(void)
164 * Arguments: @struct elt *e@ = pointer to directory element
168 * Use: Pushes a new subdirectory onto the stack.
171 static void push(struct elt *e)
176 DPUTS(&d, e->e_name);
180 /* --- @report@ --- *
182 * Arguments: @const struct checkpath *cp@ = pointer to context
183 * @unsigned what@ = what sort of report is this?
184 * @int verbose@ = how verbose is this?
185 * @const char *p@ = what path does it refer to?
186 * @const char *msg@ = the message to give to the user
190 * Use: Formats and presents messages to the client.
193 static void report(const struct checkpath *cp, unsigned what, int verbose,
194 const char *p, const char *msg, ...)
196 /* --- Decide whether to bin this message --- */
198 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
201 /* --- Format the message nicely --- */
203 if (cp->cp_what & CP_REPORT) {
214 dstr_putf(&d, "Path: %s: ", p);
220 dstr_puts(&d, strerror(e));
223 uid_t u = (uid_t)va_arg(ap, int);
224 struct passwd *pw = getpwuid(u);
226 dstr_putf(&d, "`%s'", pw->pw_name);
228 dstr_putf(&d, "%i", (int)u);
231 gid_t g = (gid_t)va_arg(ap, int);
232 struct group *gr = getgrgid(g);
234 dstr_putf(&d, "`%s'", gr->gr_name);
236 dstr_putf(&d, "%i", (int)g);
239 const char *s = va_arg(ap, const char *);
260 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
264 cp->cp_report(what, verbose, p, 0, 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)
285 if (S_ISDIR(st->st_mode) &&
286 (!(f & f_last) || (cp->cp_what & CP_STICKYOK)))
289 /* --- Check for world-writability --- */
291 if ((cp->cp_what & CP_WRWORLD) &&
292 (st->st_mode & (0002 | stickyok)) == 0002) {
294 report(cp, CP_WRWORLD, 1, p, "** world writable **");
297 /* --- Check for group-writability --- */
299 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
300 (st->st_mode & (0020 | stickyok)) == 0020) {
302 unsigned b = CP_WRGRP;
304 if (cp->cp_what & CP_WROTHGRP) {
306 for (i = 0; i < cp->cp_gids; i++) {
307 if (st->st_gid == cp->cp_gid[i])
308 b = cp->cp_what & CP_WRGRP;
313 report(cp, b, 1, p, "writable by %sgroup %g",
314 (b == CP_WROTHGRP) ? "other " : "", st->st_gid);
318 /* --- Check for user-writability --- */
320 if ((cp->cp_what & CP_WROTHUSR) &&
321 st->st_uid != cp->cp_uid &&
324 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
327 /* --- Done sanity check --- */
332 /* --- @checkpath@ --- *
334 * Arguments: @const char *p@ = directory name which needs checking
335 * @const struct checkpath *cp@ = parameters for the check
337 * Returns: Zero if all is well, otherwise bitmask of problems.
339 * Use: Scrutinises a directory path to see what evil things other
340 * users could do to it.
343 unsigned checkpath(const char *p, const struct checkpath *cp)
350 /* --- Initialize stack pointer and path string --- */
355 /* --- Try to find the current directory --- */
357 if (!getcwd(cwd, sizeof(cwd))) {
358 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
362 /* --- Check that the root directory is OK --- */
364 if (stat("/", &st)) {
365 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
369 report(cp, CP_REPORT, 3, p, "begin scan");
370 bad |= sanity("/", &st, cp, 0);
372 /* --- Get the initial list of things to process --- */
374 ee = splitpath(p, 0);
376 ee = splitpath(cwd, ee);
378 /* --- While there are list items which still need doing --- */
383 /* --- Strip off simple `.' elements --- */
385 if (strcmp(ee->e_name, ".") == 0) {
391 /* --- Backtrack on `..' elements --- */
393 else if (strcmp(ee->e_name, "..") == 0) {
400 /* --- Everything else gets pushed on the end --- */
405 /* --- Find out what sort of a thing this is --- */
407 if (lstat(d.buf, &st)) {
408 report(cp, CP_ERROR, 0, d.buf, "can't stat: %e");
413 /* --- Handle symbolic links specially --- */
415 if (S_ISLNK(st.st_mode)) {
416 dstr buf = DSTR_INIT;
419 /* --- Resolve the link --- */
421 dstr_ensure(&buf, st.st_size + 1);
422 if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) {
423 report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e");
428 report(cp, CP_SYMLINK, 2, d.buf, "symlink -> `%s'", buf.buf);
430 /* --- Handle sticky parents --- *
432 * If I make a symlink in a sticky directory, I can later modify it.
433 * However, nobody else can (except the owner of the directory, and
434 * we'll already have noticed that if we care).
437 if ((cp->cp_what & CP_WROTHUSR) &&
438 (sp->e_link->e_flags & f_sticky) &&
439 st.st_uid != cp->cp_uid && st.st_uid != 0) {
441 report(cp, CP_WROTHUSR, 1, d.buf,
442 "symlink modifiable by user %u", st.st_uid);
445 /* --- Sort out what to do from here --- */
447 if (buf.buf[0] == '/')
451 ee = splitpath(buf.buf, ee);
456 /* --- Run the sanity check on this path element --- */
458 bad |= sanity(d.buf, &st, cp, ee ? 0 : f_last);
460 if (S_ISDIR(st.st_mode)) {
461 if (st.st_mode & 01000)
462 sp->e_flags |= f_sticky;
463 report(cp, CP_REPORT, 4, d.buf, "directory");
467 /* --- Something else I don't understand --- */
472 /* --- Check for leftover junk --- */
475 if (!(bad & CP_ERROR))
476 report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
488 /* --- @checkpath_setids@ --- *
490 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
494 * Use: Fills in the user ids and things in the structure.
497 void checkpath_setids(struct checkpath *cp)
502 cp->cp_uid = getuid();
503 n = getgroups(sizeof(cp->cp_gid) / sizeof(cp->cp_gid[0]), cp->cp_gid);
505 for (i = 0; i < n; i++) {
506 if (cp->cp_gid[i] == g)
514 /*----- That's all, folks -------------------------------------------------*/