f731e5289f425f4c3d0d503ed4b7532a6a6d489d
3 * $Id: path.c,v 1.4 2001/01/25 22:16:02 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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 2001/01/25 22:16:02 mdw
33 * Make flags be unsigned.
35 * Revision 1.3 1999/05/21 22:07:20 mdw
36 * Take advantage of new dynamic string macros.
38 * Revision 1.2 1999/05/18 20:49:12 mdw
39 * Use a dynamic string for reading symlinks.
41 * Revision 1.1.1.1 1999/04/06 20:12:07 mdw
46 /*----- Header files ------------------------------------------------------*/
54 #include <sys/types.h>
61 #include <mLib/alloc.h>
62 #include <mLib/dstr.h>
66 /*----- Data structures ---------------------------------------------------*/
68 /* --- An item in the directory list --- *
70 * Each directory becomes an element on a list which is manipulated in a
75 struct elt
*e_link
; /* Pointer to the next one along */
76 size_t e_offset
; /* Offset of name in path string */
77 unsigned e_flags
; /* Various useful flags */
78 char e_name
[1]; /* Name of the directory */
81 #define f_sticky 1u /* Directory has sticky bit set */
83 #define f_last 1u /* This is the final item to check */
85 /*----- Static variables --------------------------------------------------*/
87 static struct elt rootnode
= { 0, 0, 0 }; /* Root of the list */
88 static struct elt
*sp
; /* Stack pointer for list */
89 static dstr d
= DSTR_INIT
; /* Current path string */
91 /*----- Main code ---------------------------------------------------------*/
93 /* --- @splitpath@ --- *
95 * Arguments: @const char *path@ = path string to break apart
96 * @struct elt *tail@ = tail block to attach to end of list
98 * Returns: Pointer to the new list head.
100 * Use: Breaks a path string into directories and adds each one
101 * as a node on the list, in the right order. These can then
102 * be pushed onto the directory stack as required.
105 static struct elt
*splitpath(const char *path
, struct elt
*tail
)
107 struct elt
*head
, **ee
= &head
, *e
;
112 /* --- Either a leading `/', or a doubled one --- *
114 * Either way, ignore it.
122 /* --- Skip to the next directory separator --- *
124 * Build a list element for it, and link it on.
127 n
= strcspn(path
, "/");
128 e
= xmalloc(sizeof(struct elt
) + n
+ 1);
129 memcpy(e
->e_name
, path
, n
);
149 * Use: Removes the top item from the directory stack.
152 static void pop(void)
155 struct elt
*e
= sp
->e_link
;
156 d
.len
= sp
->e_offset
;
162 /* --- @popall@ --- *
168 * Use: Removes all the items from the directory stack.
171 static void popall(void)
179 * Arguments: @struct elt *e@ = pointer to directory element
183 * Use: Pushes a new subdirectory onto the stack.
186 static void push(struct elt
*e
)
191 DPUTS(&d
, e
->e_name
);
195 /* --- @report@ --- *
197 * Arguments: @struct chkpath *cp@ = pointer to context
198 * @int what@ = what sort of report is this?
199 * @int verbose@ = how verbose is this?
200 * @const char *p@ = what path does it refer to?
201 * @const char *msg@ = the message to give to the user
205 * Use: Formats and presents messages to the client.
208 static void report(struct chkpath
*cp
, int what
, int verbose
,
209 const char *p
, const char *msg
, ...)
211 /* --- Decide whether to bin this message --- */
213 if (!cp
->cp_report
|| verbose
> cp
->cp_verbose
|| !(cp
->cp_what
& what
))
216 /* --- Format the message nicely --- */
218 if (cp
->cp_what
& CP_REPORT
) {
229 dstr_putf(&d
, "Path: %s: ", p
);
235 dstr_puts(&d
, strerror(e
));
238 uid_t u
= (uid_t
)va_arg(ap
, int);
239 struct passwd
*pw
= getpwuid(u
);
241 dstr_putf(&d
, "`%s'", pw
->pw_name
);
243 dstr_putf(&d
, "%i", (int)u
);
246 gid_t g
= (gid_t
)va_arg(ap
, int);
247 struct group
*gr
= getgrgid(g
);
249 dstr_putf(&d
, "`%s'", gr
->gr_name
);
251 dstr_putf(&d
, "%i", (int)g
);
254 const char *s
= va_arg(ap
, const char *);
275 cp
->cp_report(what
, verbose
, p
, d
.buf
, cp
->cp_arg
);
279 cp
->cp_report(what
, verbose
, p
, 0, cp
->cp_arg
);
282 /* --- @sanity@ --- *
284 * Arguments: @const char *p@ = name of directory to check
285 * @struct stat *st@ = pointer to @stat@(2) block for it
286 * @struct chkpath *cp@ = pointer to caller parameters
287 * @unsigned f@ = various flags
289 * Returns: Zero if everything's OK, else bitmask of problems.
291 * Use: Performs the main load of sanity-checking on a directory.
294 static int sanity(const char *p
, struct stat
*st
,
295 struct chkpath
*cp
, unsigned f
)
298 int sticky
= (cp
->cp_what
& CP_STICKYOK
) || !(f
& f_last
) ?
01000 : 0;
300 /* --- Check for world-writability --- */
302 if ((cp
->cp_what
& CP_WRWORLD
) &&
303 (st
->st_mode
& (0002 | sticky
)) == 0002) {
305 report(cp
, CP_WRWORLD
, 1, p
, "** world writable **");
308 /* --- Check for group-writability --- */
310 if ((cp
->cp_what
& (CP_WRGRP
| CP_WROTHGRP
)) &&
311 (st
->st_mode
& (0020 | sticky
)) == 0020) {
312 if (cp
->cp_what
& CP_WRGRP
) {
314 report(cp
, CP_WRGRP
, 1, p
, "writable by group %g", st
->st_gid
);
317 for (i
= 0; i
< cp
->cp_gids
; i
++) {
318 if (st
->st_gid
== cp
->cp_gid
[i
])
322 report(cp
, CP_WROTHGRP
, 1, p
, "writable by group %g", st
->st_gid
);
327 /* --- Check for user-writability --- */
329 if ((cp
->cp_what
& CP_WROTHUSR
) &&
330 st
->st_uid
!= cp
->cp_uid
&&
333 report(cp
, CP_WROTHUSR
, 1, p
, "owner is user %u", st
->st_uid
);
336 /* --- Done sanity check --- */
341 /* --- @path_check@ --- *
343 * Arguments: @const char *p@ = directory name which needs checking
344 * @struct chkpath *cp@ = caller parameters for the check
346 * Returns: Zero if all is well, otherwise bitmask of problems.
348 * Use: Scrutinises a directory path to see what evil things other
349 * users could do to it.
352 int path_check(const char *p
, struct chkpath
*cp
)
359 /* --- Initialize stack pointer and path string --- */
364 /* --- Try to find the current directory --- */
366 if (!getcwd(cwd
, sizeof(cwd
))) {
367 report(cp
, CP_ERROR
, 0, 0, "can't find current directory: %e");
371 /* --- Check that the root directory is OK --- */
373 if (stat("/", &st
)) {
374 report(cp
, CP_ERROR
, 0, 0, "can't stat root directory: %e");
378 report(cp
, CP_REPORT
, 3, p
, "begin scan");
379 bad
|= sanity("/", &st
, cp
, 0);
381 /* --- Get the initial list of things to process --- */
383 ee
= splitpath(p
, 0);
385 ee
= splitpath(cwd
, ee
);
387 /* --- While there are list items which still need doing --- */
392 /* --- Strip off simple `.' elements --- */
394 if (strcmp(ee
->e_name
, ".") == 0) {
400 /* --- Backtrack on `..' elements --- */
402 else if (strcmp(ee
->e_name
, "..") == 0) {
409 /* --- Everything else gets pushed on the end --- */
414 /* --- Find out what sort of a thing this is --- */
416 if (lstat(d
.buf
, &st
)) {
417 report(cp
, CP_ERROR
, 0, d
.buf
, "can't stat: %e");
422 /* --- Handle symbolic links specially --- */
424 if (S_ISLNK(st
.st_mode
)) {
425 dstr buf
= DSTR_INIT
;
428 /* --- Resolve the link --- */
430 dstr_ensure(&buf
, st
.st_size
+ 1);
431 if ((i
= readlink(d
.buf
, buf
.buf
, buf
.sz
)) < 0) {
432 report(cp
, CP_ERROR
, 0, d
.buf
, "can't readlink: %e");
437 report(cp
, CP_SYMLINK
, 2, d
.buf
, "symlink -> `%s'", buf
.buf
);
439 /* --- Handle sticky parents --- *
441 * If I make a symlink in a sticky directory, I can later modify it.
442 * However, nobody else can (except the owner of the directory, and
443 * we'll already have noticed that if we care).
446 if ((cp
->cp_what
& CP_WROTHUSR
) &&
447 (sp
->e_link
->e_flags
& f_sticky
) &&
448 st
.st_uid
!= cp
->cp_uid
&& st
.st_uid
!= 0) {
450 report(cp
, CP_WROTHUSR
, 1, d
.buf
,
451 "symlink modifiable by user %u", st
.st_uid
);
454 /* --- Sort out what to do from here --- */
456 if (buf
.buf
[0] == '/')
460 ee
= splitpath(buf
.buf
, ee
);
465 /* --- Run the sanity check on this path element --- */
467 bad
|= sanity(d
.buf
, &st
, cp
, ee ?
0 : f_last
);
469 if (S_ISDIR(st
.st_mode
)) {
470 if (st
.st_mode
& 01000)
471 sp
->e_flags
|= f_sticky
;
472 report(cp
, CP_REPORT
, 4, d
.buf
, "directory");
476 /* --- Something else I don't understand --- */
481 /* --- Check for leftover junk --- */
484 if (!(bad
& CP_ERROR
))
485 report(cp
, CP_ERROR
, 0, 0, "junk left over after reaching leaf");
497 /* --- @path_setids@ --- *
499 * Arguments: @struct chkpath *cp@ = pointer to block to fill in
501 * Returns: Zero if OK, else @-1@.
503 * Use: Fills in the user ids and things in the structure.
506 void path_setids(struct chkpath
*cp
)
511 cp
->cp_uid
= getuid();
512 n
= getgroups(sizeof(cp
->cp_gid
) / sizeof(cp
->cp_gid
[0]), cp
->cp_gid
);
514 for (i
= 0; i
< n
; i
++) {
515 if (cp
->cp_gid
[i
] == g
)
523 /*----- That's all, folks -------------------------------------------------*/