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 /*----- Static variables --------------------------------------------------*/
69 static const struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
70 static struct elt *sp; /* Stack pointer for list */
71 static dstr d = DSTR_INIT; /* Current path string */
73 /*----- Main code ---------------------------------------------------------*/
75 /* --- @splitpath@ --- *
77 * Arguments: @const char *path@ = path string to break apart
78 * @struct elt *tail@ = tail block to attach to end of list
80 * Returns: Pointer to the new list head.
82 * Use: Breaks a path string into directories and adds each one
83 * as a node on the list, in the right order. These can then
84 * be pushed onto the directory stack as required.
87 static struct elt *splitpath(const char *path, struct elt *tail)
89 struct elt *head, **ee = &head, *e;
94 /* --- Either a leading `/', or a doubled one --- *
96 * Either way, ignore it.
104 /* --- Skip to the next directory separator --- *
106 * Build a list element for it, and link it on.
109 n = strcspn(path, "/");
110 e = xmalloc(sizeof(struct elt) + n + 1);
111 memcpy(e->e_name, path, n);
131 * Use: Removes the top item from the directory stack.
134 static void pop(void)
137 struct elt *e = sp->e_link;
138 d.len = sp->e_offset;
144 /* --- @popall@ --- *
150 * Use: Removes all the items from the directory stack.
153 static void popall(void)
154 { while (sp->e_link) pop(); }
158 * Arguments: @struct elt *e@ = pointer to directory element
162 * Use: Pushes a new subdirectory onto the stack.
165 static void push(struct elt *e)
170 DPUTS(&d, e->e_name);
174 /* --- @report@ --- *
176 * Arguments: @const struct checkpath *cp@ = pointer to context
177 * @unsigned what@ = what sort of report is this?
178 * @int verbose@ = how verbose is this?
179 * @const char *p@ = what path does it refer to?
180 * @const char *msg@ = the message to give to the user
184 * Use: Formats and presents messages to the client.
187 static void report(const struct checkpath *cp, unsigned what, int verbose,
188 const char *p, const char *msg, ...)
201 /* --- Decide whether to bin this message --- */
203 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
206 /* --- If no reporting, do the easy thing --- */
208 if (!(cp->cp_what & CP_REPORT)) {
209 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
213 /* --- Format the message nicely --- */
219 dstr_putf(&d, "Path: %s: ", p);
225 dstr_puts(&d, strerror(e));
228 u = (uid_t)va_arg(ap, int);
229 if ((pw = getpwuid(u)) != 0)
230 dstr_putf(&d, "`%s'", pw->pw_name);
232 dstr_putf(&d, "%i", (int)u);
235 g = (gid_t)va_arg(ap, int);
236 if ((gr = getgrgid(g)) != 0)
237 dstr_putf(&d, "`%s'", gr->gr_name);
239 dstr_putf(&d, "%i", (int)g);
242 s = va_arg(ap, const char *);
263 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
268 /* --- @sanity@ --- *
270 * Arguments: @const char *p@ = name of directory to check
271 * @struct stat *st@ = pointer to @stat@(2) block for it
272 * @const struct checkpath *cp@ = pointer to caller parameters
273 * @unsigned f@ = various flags (@SF_...@)
275 * Returns: Zero if everything's OK, else bitmask of problems.
277 * Use: Performs the main load of sanity-checking on a directory.
278 * If @SF_LAST@ is not set then sticky directories are always
282 #define SF_LAST 1u /* This is the final item to check */
284 static unsigned sanity(const char *p, struct stat *st,
285 const struct checkpath *cp, unsigned f)
292 if (S_ISDIR(st->st_mode) &&
293 (!(f & SF_LAST) || (cp->cp_what & CP_STICKYOK)))
296 /* --- Check for world-writability --- */
298 if ((cp->cp_what & CP_WRWORLD) &&
299 (st->st_mode & (S_IWOTH | stickyok)) == S_IWOTH) {
301 report(cp, CP_WRWORLD, 1, p, "** world writable **");
304 /* --- Check for group-writability --- */
306 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
307 (st->st_mode & (S_IWGRP | stickyok)) == S_IWGRP) {
310 if (cp->cp_what & CP_WROTHGRP) {
312 for (i = 0; i < cp->cp_gids; i++) {
313 if (st->st_gid == cp->cp_gid[i])
314 b = cp->cp_what & CP_WRGRP;
319 report(cp, b, 1, p, "writable by %sgroup %g",
320 (b == CP_WROTHGRP) ? "other " : "", st->st_gid);
324 /* --- Check for user-writability --- */
326 if ((cp->cp_what & CP_WROTHUSR) &&
327 st->st_uid != cp->cp_uid &&
330 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
333 /* --- Done sanity check --- */
338 /* --- @checkpath@ --- *
340 * Arguments: @const char *p@ = directory name which needs checking
341 * @const struct checkpath *cp@ = parameters for the check
343 * Returns: Zero if all is well, otherwise bitmask of problems.
345 * Use: Scrutinises a directory path to see what evil things other
346 * users could do to it.
349 unsigned checkpath(const char *p, const struct checkpath *cp)
356 /* --- Initialize stack pointer and path string --- */
358 sp = (/*unconst*/ struct elt *)&rootnode;
361 /* --- Try to find the current directory --- */
363 if (!getcwd(cwd, sizeof(cwd))) {
364 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
368 /* --- Check that the root directory is OK --- */
370 if (stat("/", &st)) {
371 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
375 report(cp, CP_REPORT, 3, p, "begin scan");
376 bad |= sanity("/", &st, cp, 0);
378 /* --- Get the initial list of things to process --- */
380 ee = splitpath(p, 0);
382 ee = splitpath(cwd, ee);
384 /* --- While there are list items which still need doing --- */
389 /* --- Strip off simple `.' elements --- */
391 if (strcmp(ee->e_name, ".") == 0) {
397 /* --- Backtrack on `..' elements --- */
399 else if (strcmp(ee->e_name, "..") == 0) {
406 /* --- Everything else gets pushed on the end --- */
411 /* --- Find out what sort of a thing this is --- */
413 if (lstat(d.buf, &st)) {
414 report(cp, CP_ERROR, 0, d.buf, "can't stat: %e");
419 /* --- Handle symbolic links specially --- */
421 if (S_ISLNK(st.st_mode)) {
422 dstr buf = DSTR_INIT;
425 /* --- Resolve the link --- */
427 dstr_ensure(&buf, st.st_size + 1);
428 if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) {
429 report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e");
434 report(cp, CP_SYMLINK, 2, d.buf, "symlink -> `%s'", buf.buf);
436 /* --- Handle sticky parents --- *
438 * If I make a symlink in a sticky directory, I can later modify it.
439 * However, nobody else can (except the owner of the directory, and
440 * we'll already have noticed that if we care).
443 if ((cp->cp_what & CP_WROTHUSR) &&
444 (sp->e_link->e_flags & EF_STICKY) &&
445 st.st_uid != cp->cp_uid && st.st_uid != 0) {
447 report(cp, CP_WROTHUSR, 1, d.buf,
448 "symlink modifiable by user %u", st.st_uid);
451 /* --- Sort out what to do from here --- */
453 if (buf.buf[0] == '/')
457 ee = splitpath(buf.buf, ee);
462 /* --- Run the sanity check on this path element --- */
464 bad |= sanity(d.buf, &st, cp, ee ? 0 : SF_LAST);
466 if (S_ISDIR(st.st_mode)) {
467 if (st.st_mode & S_ISVTX)
468 sp->e_flags |= EF_STICKY;
469 report(cp, CP_REPORT, 4, d.buf, "directory");
473 /* --- Something else I don't understand --- */
478 /* --- Check for leftover junk --- */
481 if (!(bad & CP_ERROR))
482 report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
494 /* --- @checkpath_addgid@ --- *
496 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
497 * @gid_t g@ = group id to add
499 * Returns: Zero if successful, nonzero if the array is full.
501 * Use: Adds the group @g@ to the structure.
504 int checkpath_addgid(struct checkpath *cp, gid_t g)
508 for (i = 0; i < cp->cp_gids; i++) {
509 if (cp->cp_gid[i] == g)
512 if (cp->cp_gids >= N(cp->cp_gid))
514 cp->cp_gid[cp->cp_gids++] = g;
518 /* --- @checkpath_setuid@ --- *
520 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
524 * Use: Fills in the @cp_uid@ slot of the structure with the real uid
525 * of the current process.
528 void checkpath_setuid(struct checkpath *cp) { cp->cp_uid = getuid(); }
530 /* --- @checkpath_setgid@ --- *
532 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
534 * Returns: Zero if successful, nonzero if the array is full.
536 * Use: Adds the real gid of the current process to the @cp_gid@
540 int checkpath_setgid(struct checkpath *cp)
541 { return (checkpath_addgid(cp, getgid())); }
543 /* --- @checkpath_setgroups@ --- *
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 current process's supplementary groups to the
553 int checkpath_setgroups(struct checkpath *cp)
556 gid_t gg[NGROUPS_MAX];
558 n = getgroups(N(gg), gg);
559 for (i = 0; i < n; i++) {
560 if (checkpath_addgid(cp, gg[i]))
566 /* --- @checkpath_setids@ --- *
568 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
572 * Use: Fills in the user ids and things in the structure. This is
573 * equivalent to setting @cp_gids = 0@ and then calling
574 * @_setuid@, @_setgid@ and @_setgroups@. It can't fail.
577 void checkpath_setids(struct checkpath *cp)
580 checkpath_setuid(cp);
581 checkpath_setgid(cp);
582 checkpath_setgroups(cp);
585 /*----- That's all, folks -------------------------------------------------*/