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>
47 #include "checkpath.h"
49 /*----- Data structures ---------------------------------------------------*/
51 /* --- An item in the directory list --- *
53 * Each directory becomes an element on a list which is manipulated in a
58 struct elt
*e_link
; /* Pointer to the next one along */
59 size_t e_offset
; /* Offset of name in path string */
60 unsigned e_flags
; /* Various useful flags */
61 char e_name
[1]; /* Name of the directory */
64 #define f_sticky 1u /* Directory has sticky bit set */
66 #define f_last 1u /* This is the final item to check */
68 /*----- Static variables --------------------------------------------------*/
70 static struct elt rootnode
= { 0, 0, 0 }; /* Root of the list */
71 static struct elt
*sp
; /* Stack pointer for list */
72 static dstr d
= DSTR_INIT
; /* Current path string */
74 /*----- Main code ---------------------------------------------------------*/
76 /* --- @splitpath@ --- *
78 * Arguments: @const char *path@ = path string to break apart
79 * @struct elt *tail@ = tail block to attach to end of list
81 * Returns: Pointer to the new list head.
83 * Use: Breaks a path string into directories and adds each one
84 * as a node on the list, in the right order. These can then
85 * be pushed onto the directory stack as required.
88 static struct elt
*splitpath(const char *path
, struct elt
*tail
)
90 struct elt
*head
, **ee
= &head
, *e
;
95 /* --- Either a leading `/', or a doubled one --- *
97 * Either way, ignore it.
105 /* --- Skip to the next directory separator --- *
107 * Build a list element for it, and link it on.
110 n
= strcspn(path
, "/");
111 e
= xmalloc(sizeof(struct elt
) + n
+ 1);
112 memcpy(e
->e_name
, path
, n
);
132 * Use: Removes the top item from the directory stack.
135 static void pop(void)
138 struct elt
*e
= sp
->e_link
;
139 d
.len
= sp
->e_offset
;
145 /* --- @popall@ --- *
151 * Use: Removes all the items from the directory stack.
154 static void popall(void)
155 { while (sp
->e_link
) pop(); }
159 * Arguments: @struct elt *e@ = pointer to directory element
163 * Use: Pushes a new subdirectory onto the stack.
166 static void push(struct elt
*e
)
171 DPUTS(&d
, e
->e_name
);
175 /* --- @report@ --- *
177 * Arguments: @const struct checkpath *cp@ = pointer to context
178 * @unsigned what@ = what sort of report is this?
179 * @int verbose@ = how verbose is this?
180 * @const char *p@ = what path does it refer to?
181 * @const char *msg@ = the message to give to the user
185 * Use: Formats and presents messages to the client.
188 static void report(const struct checkpath
*cp
, unsigned what
, int verbose
,
189 const char *p
, const char *msg
, ...)
202 /* --- Decide whether to bin this message --- */
204 if (!cp
->cp_report
|| verbose
> cp
->cp_verbose
|| !(cp
->cp_what
& what
))
207 /* --- If no reporting, do the easy thing --- */
209 if (!(cp
->cp_what
& CP_REPORT
)) {
210 cp
->cp_report(what
, verbose
, p
, 0, cp
->cp_arg
);
214 /* --- Format the message nicely --- */
220 dstr_putf(&d
, "Path: %s: ", p
);
226 dstr_puts(&d
, strerror(e
));
229 u
= (uid_t
)va_arg(ap
, int);
230 if ((pw
= getpwuid(u
)) != 0)
231 dstr_putf(&d
, "`%s'", pw
->pw_name
);
233 dstr_putf(&d
, "%i", (int)u
);
236 g
= (gid_t
)va_arg(ap
, int);
237 if ((gr
= getgrgid(g
)) != 0)
238 dstr_putf(&d
, "`%s'", gr
->gr_name
);
240 dstr_putf(&d
, "%i", (int)g
);
243 s
= va_arg(ap
, const char *);
264 cp
->cp_report(what
, verbose
, p
, d
.buf
, cp
->cp_arg
);
269 /* --- @sanity@ --- *
271 * Arguments: @const char *p@ = name of directory to check
272 * @struct stat *st@ = pointer to @stat@(2) block for it
273 * @const struct checkpath *cp@ = pointer to caller parameters
274 * @unsigned f@ = various flags
276 * Returns: Zero if everything's OK, else bitmask of problems.
278 * Use: Performs the main load of sanity-checking on a directory.
281 static unsigned sanity(const char *p
, struct stat
*st
,
282 const struct checkpath
*cp
, unsigned f
)
289 if (S_ISDIR(st
->st_mode
) &&
290 (!(f
& f_last
) || (cp
->cp_what
& CP_STICKYOK
)))
293 /* --- Check for world-writability --- */
295 if ((cp
->cp_what
& CP_WRWORLD
) &&
296 (st
->st_mode
& (0002 | stickyok
)) == 0002) {
298 report(cp
, CP_WRWORLD
, 1, p
, "** world writable **");
301 /* --- Check for group-writability --- */
303 if ((cp
->cp_what
& (CP_WRGRP
| CP_WROTHGRP
)) &&
304 (st
->st_mode
& (0020 | stickyok
)) == 0020) {
307 if (cp
->cp_what
& CP_WROTHGRP
) {
309 for (i
= 0; i
< cp
->cp_gids
; i
++) {
310 if (st
->st_gid
== cp
->cp_gid
[i
])
311 b
= cp
->cp_what
& CP_WRGRP
;
316 report(cp
, b
, 1, p
, "writable by %sgroup %g",
317 (b
== CP_WROTHGRP
) ?
"other " : "", st
->st_gid
);
321 /* --- Check for user-writability --- */
323 if ((cp
->cp_what
& CP_WROTHUSR
) &&
324 st
->st_uid
!= cp
->cp_uid
&&
327 report(cp
, CP_WROTHUSR
, 1, p
, "owner is user %u", st
->st_uid
);
330 /* --- Done sanity check --- */
335 /* --- @checkpath@ --- *
337 * Arguments: @const char *p@ = directory name which needs checking
338 * @const struct checkpath *cp@ = parameters for the check
340 * Returns: Zero if all is well, otherwise bitmask of problems.
342 * Use: Scrutinises a directory path to see what evil things other
343 * users could do to it.
346 unsigned checkpath(const char *p
, const struct checkpath
*cp
)
353 /* --- Initialize stack pointer and path string --- */
358 /* --- Try to find the current directory --- */
360 if (!getcwd(cwd
, sizeof(cwd
))) {
361 report(cp
, CP_ERROR
, 0, 0, "can't find current directory: %e");
365 /* --- Check that the root directory is OK --- */
367 if (stat("/", &st
)) {
368 report(cp
, CP_ERROR
, 0, 0, "can't stat root directory: %e");
372 report(cp
, CP_REPORT
, 3, p
, "begin scan");
373 bad
|= sanity("/", &st
, cp
, 0);
375 /* --- Get the initial list of things to process --- */
377 ee
= splitpath(p
, 0);
379 ee
= splitpath(cwd
, ee
);
381 /* --- While there are list items which still need doing --- */
386 /* --- Strip off simple `.' elements --- */
388 if (strcmp(ee
->e_name
, ".") == 0) {
394 /* --- Backtrack on `..' elements --- */
396 else if (strcmp(ee
->e_name
, "..") == 0) {
403 /* --- Everything else gets pushed on the end --- */
408 /* --- Find out what sort of a thing this is --- */
410 if (lstat(d
.buf
, &st
)) {
411 report(cp
, CP_ERROR
, 0, d
.buf
, "can't stat: %e");
416 /* --- Handle symbolic links specially --- */
418 if (S_ISLNK(st
.st_mode
)) {
419 dstr buf
= DSTR_INIT
;
422 /* --- Resolve the link --- */
424 dstr_ensure(&buf
, st
.st_size
+ 1);
425 if ((i
= readlink(d
.buf
, buf
.buf
, buf
.sz
)) < 0) {
426 report(cp
, CP_ERROR
, 0, d
.buf
, "can't readlink: %e");
431 report(cp
, CP_SYMLINK
, 2, d
.buf
, "symlink -> `%s'", buf
.buf
);
433 /* --- Handle sticky parents --- *
435 * If I make a symlink in a sticky directory, I can later modify it.
436 * However, nobody else can (except the owner of the directory, and
437 * we'll already have noticed that if we care).
440 if ((cp
->cp_what
& CP_WROTHUSR
) &&
441 (sp
->e_link
->e_flags
& f_sticky
) &&
442 st
.st_uid
!= cp
->cp_uid
&& st
.st_uid
!= 0) {
444 report(cp
, CP_WROTHUSR
, 1, d
.buf
,
445 "symlink modifiable by user %u", st
.st_uid
);
448 /* --- Sort out what to do from here --- */
450 if (buf
.buf
[0] == '/')
454 ee
= splitpath(buf
.buf
, ee
);
459 /* --- Run the sanity check on this path element --- */
461 bad
|= sanity(d
.buf
, &st
, cp
, ee ?
0 : f_last
);
463 if (S_ISDIR(st
.st_mode
)) {
464 if (st
.st_mode
& 01000)
465 sp
->e_flags
|= f_sticky
;
466 report(cp
, CP_REPORT
, 4, d
.buf
, "directory");
470 /* --- Something else I don't understand --- */
475 /* --- Check for leftover junk --- */
478 if (!(bad
& CP_ERROR
))
479 report(cp
, CP_ERROR
, 0, 0, "junk left over after reaching leaf");
491 /* --- @checkpath_setids@ --- *
493 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
497 * Use: Fills in the user ids and things in the structure.
500 void checkpath_setids(struct checkpath
*cp
)
505 cp
->cp_uid
= getuid();
506 n
= getgroups(sizeof(cp
->cp_gid
) / sizeof(cp
->cp_gid
[0]), cp
->cp_gid
);
508 for (i
= 0; i
< n
; i
++) {
509 if (cp
->cp_gid
[i
] == g
)
517 /*----- That's all, folks -------------------------------------------------*/