| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: checkpath.c,v 1.6 2004/04/08 01:36:22 mdw Exp $ |
| 4 | * |
| 5 | * Check a path for safety |
| 6 | * |
| 7 | * (c) 1999 Mark Wooding |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of chkpath. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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. |
| 27 | */ |
| 28 | |
| 29 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <stdarg.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | #include <pwd.h> |
| 42 | #include <grp.h> |
| 43 | |
| 44 | #include <mLib/alloc.h> |
| 45 | #include <mLib/dstr.h> |
| 46 | |
| 47 | #include "checkpath.h" |
| 48 | |
| 49 | /*----- Data structures ---------------------------------------------------*/ |
| 50 | |
| 51 | /* --- An item in the directory list --- * |
| 52 | * |
| 53 | * Each directory becomes an element on a list which is manipulated in a |
| 54 | * stack-like way. |
| 55 | */ |
| 56 | |
| 57 | struct elt { |
| 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 */ |
| 62 | }; |
| 63 | |
| 64 | #define f_sticky 1u /* Directory has sticky bit set */ |
| 65 | |
| 66 | #define f_last 1u /* This is the final item to check */ |
| 67 | |
| 68 | /*----- Static variables --------------------------------------------------*/ |
| 69 | |
| 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 */ |
| 73 | |
| 74 | /*----- Main code ---------------------------------------------------------*/ |
| 75 | |
| 76 | /* --- @splitpath@ --- * |
| 77 | * |
| 78 | * Arguments: @const char *path@ = path string to break apart |
| 79 | * @struct elt *tail@ = tail block to attach to end of list |
| 80 | * |
| 81 | * Returns: Pointer to the new list head. |
| 82 | * |
| 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. |
| 86 | */ |
| 87 | |
| 88 | static struct elt *splitpath(const char *path, struct elt *tail) |
| 89 | { |
| 90 | struct elt *head, **ee = &head, *e; |
| 91 | |
| 92 | while (*path) { |
| 93 | size_t n; |
| 94 | |
| 95 | /* --- Either a leading `/', or a doubled one --- * |
| 96 | * |
| 97 | * Either way, ignore it. |
| 98 | */ |
| 99 | |
| 100 | if (*path == '/') { |
| 101 | path++; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | /* --- Skip to the next directory separator --- * |
| 106 | * |
| 107 | * Build a list element for it, and link it on. |
| 108 | */ |
| 109 | |
| 110 | n = strcspn(path, "/"); |
| 111 | e = xmalloc(sizeof(struct elt) + n + 1); |
| 112 | memcpy(e->e_name, path, n); |
| 113 | e->e_name[n] = 0; |
| 114 | e->e_flags = 0; |
| 115 | *ee = e; |
| 116 | ee = &e->e_link; |
| 117 | path += n; |
| 118 | } |
| 119 | |
| 120 | /* --- Done --- */ |
| 121 | |
| 122 | *ee = tail; |
| 123 | return (head); |
| 124 | } |
| 125 | |
| 126 | /* --- @pop@ --- * |
| 127 | * |
| 128 | * Arguments: --- |
| 129 | * |
| 130 | * Returns: --- |
| 131 | * |
| 132 | * Use: Removes the top item from the directory stack. |
| 133 | */ |
| 134 | |
| 135 | static void pop(void) |
| 136 | { |
| 137 | if (sp->e_link) { |
| 138 | struct elt *e = sp->e_link; |
| 139 | d.len = sp->e_offset; |
| 140 | DPUTZ(&d); |
| 141 | sp = e; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* --- @popall@ --- * |
| 146 | * |
| 147 | * Arguments: --- |
| 148 | * |
| 149 | * Returns: --- |
| 150 | * |
| 151 | * Use: Removes all the items from the directory stack. |
| 152 | */ |
| 153 | |
| 154 | static void popall(void) |
| 155 | { |
| 156 | while (sp->e_link) |
| 157 | pop(); |
| 158 | } |
| 159 | |
| 160 | /* --- @push@ --- * |
| 161 | * |
| 162 | * Arguments: @struct elt *e@ = pointer to directory element |
| 163 | * |
| 164 | * Returns: --- |
| 165 | * |
| 166 | * Use: Pushes a new subdirectory onto the stack. |
| 167 | */ |
| 168 | |
| 169 | static void push(struct elt *e) |
| 170 | { |
| 171 | e->e_link = sp; |
| 172 | e->e_offset = d.len; |
| 173 | DPUTC(&d, '/'); |
| 174 | DPUTS(&d, e->e_name); |
| 175 | sp = e; |
| 176 | } |
| 177 | |
| 178 | /* --- @report@ --- * |
| 179 | * |
| 180 | * Arguments: @const struct checkpath *cp@ = pointer to context |
| 181 | * @unsigned what@ = what sort of report is this? |
| 182 | * @int verbose@ = how verbose is this? |
| 183 | * @const char *p@ = what path does it refer to? |
| 184 | * @const char *msg@ = the message to give to the user |
| 185 | * |
| 186 | * Returns: --- |
| 187 | * |
| 188 | * Use: Formats and presents messages to the client. |
| 189 | */ |
| 190 | |
| 191 | static void report(const struct checkpath *cp, unsigned what, int verbose, |
| 192 | const char *p, const char *msg, ...) |
| 193 | { |
| 194 | /* --- Decide whether to bin this message --- */ |
| 195 | |
| 196 | if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what)) |
| 197 | return; |
| 198 | |
| 199 | /* --- Format the message nicely --- */ |
| 200 | |
| 201 | if (cp->cp_what & CP_REPORT) { |
| 202 | dstr d = DSTR_INIT; |
| 203 | va_list ap; |
| 204 | const char *q = msg; |
| 205 | size_t n; |
| 206 | int e = errno; |
| 207 | |
| 208 | va_start(ap, msg); |
| 209 | if (verbose > 1) |
| 210 | dstr_puts(&d, "[ "); |
| 211 | if (p) |
| 212 | dstr_putf(&d, "Path: %s: ", p); |
| 213 | while (*q) { |
| 214 | if (*q == '%') { |
| 215 | q++; |
| 216 | switch (*q) { |
| 217 | case 'e': |
| 218 | dstr_puts(&d, strerror(e)); |
| 219 | break; |
| 220 | case 'u': { |
| 221 | uid_t u = (uid_t)va_arg(ap, int); |
| 222 | struct passwd *pw = getpwuid(u); |
| 223 | if (pw) |
| 224 | dstr_putf(&d, "`%s'", pw->pw_name); |
| 225 | else |
| 226 | dstr_putf(&d, "%i", (int)u); |
| 227 | } break; |
| 228 | case 'g': { |
| 229 | gid_t g = (gid_t)va_arg(ap, int); |
| 230 | struct group *gr = getgrgid(g); |
| 231 | if (gr) |
| 232 | dstr_putf(&d, "`%s'", gr->gr_name); |
| 233 | else |
| 234 | dstr_putf(&d, "%i", (int)g); |
| 235 | } break; |
| 236 | case 's': { |
| 237 | const char *s = va_arg(ap, const char *); |
| 238 | dstr_puts(&d, s); |
| 239 | } break; |
| 240 | case '%': |
| 241 | dstr_putc(&d, '%'); |
| 242 | break; |
| 243 | default: |
| 244 | dstr_putc(&d, '%'); |
| 245 | dstr_putc(&d, *q); |
| 246 | break; |
| 247 | } |
| 248 | q++; |
| 249 | } else { |
| 250 | n = strcspn(q, "%"); |
| 251 | DPUTM(&d, q, n); |
| 252 | q += n; |
| 253 | } |
| 254 | } |
| 255 | if (verbose > 1) |
| 256 | dstr_puts(&d, " ]"); |
| 257 | DPUTZ(&d); |
| 258 | cp->cp_report(what, verbose, p, d.buf, cp->cp_arg); |
| 259 | dstr_destroy(&d); |
| 260 | va_end(ap); |
| 261 | } else |
| 262 | cp->cp_report(what, verbose, p, 0, cp->cp_arg); |
| 263 | } |
| 264 | |
| 265 | /* --- @sanity@ --- * |
| 266 | * |
| 267 | * Arguments: @const char *p@ = name of directory to check |
| 268 | * @struct stat *st@ = pointer to @stat@(2) block for it |
| 269 | * @const struct checkpath *cp@ = pointer to caller parameters |
| 270 | * @unsigned f@ = various flags |
| 271 | * |
| 272 | * Returns: Zero if everything's OK, else bitmask of problems. |
| 273 | * |
| 274 | * Use: Performs the main load of sanity-checking on a directory. |
| 275 | */ |
| 276 | |
| 277 | static unsigned sanity(const char *p, struct stat *st, |
| 278 | const struct checkpath *cp, unsigned f) |
| 279 | { |
| 280 | unsigned bad = 0; |
| 281 | int stickyok = 0; |
| 282 | |
| 283 | if (S_ISDIR(st->st_mode) && |
| 284 | (!(f & f_last) || (cp->cp_what & CP_STICKYOK))) |
| 285 | stickyok = 01000; |
| 286 | |
| 287 | /* --- Check for world-writability --- */ |
| 288 | |
| 289 | if ((cp->cp_what & CP_WRWORLD) && |
| 290 | (st->st_mode & (0002 | stickyok)) == 0002) { |
| 291 | bad |= CP_WRWORLD; |
| 292 | report(cp, CP_WRWORLD, 1, p, "** world writable **"); |
| 293 | } |
| 294 | |
| 295 | /* --- Check for group-writability --- */ |
| 296 | |
| 297 | if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) && |
| 298 | (st->st_mode & (0020 | stickyok)) == 0020) { |
| 299 | int i; |
| 300 | unsigned b = CP_WRGRP; |
| 301 | |
| 302 | if (cp->cp_what & CP_WROTHGRP) { |
| 303 | b = CP_WROTHGRP; |
| 304 | for (i = 0; i < cp->cp_gids; i++) { |
| 305 | if (st->st_gid == cp->cp_gid[i]) |
| 306 | b = cp->cp_what & CP_WRGRP; |
| 307 | } |
| 308 | } |
| 309 | if (b) { |
| 310 | bad |= b; |
| 311 | report(cp, b, 1, p, "writable by %sgroup %g", |
| 312 | (b == CP_WROTHGRP) ? "other " : "", st->st_gid); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* --- Check for user-writability --- */ |
| 317 | |
| 318 | if ((cp->cp_what & CP_WROTHUSR) && |
| 319 | st->st_uid != cp->cp_uid && |
| 320 | st->st_uid != 0) { |
| 321 | bad |= CP_WROTHUSR; |
| 322 | report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid); |
| 323 | } |
| 324 | |
| 325 | /* --- Done sanity check --- */ |
| 326 | |
| 327 | return (bad); |
| 328 | } |
| 329 | |
| 330 | /* --- @checkpath@ --- * |
| 331 | * |
| 332 | * Arguments: @const char *p@ = directory name which needs checking |
| 333 | * @const struct checkpath *cp@ = parameters for the check |
| 334 | * |
| 335 | * Returns: Zero if all is well, otherwise bitmask of problems. |
| 336 | * |
| 337 | * Use: Scrutinises a directory path to see what evil things other |
| 338 | * users could do to it. |
| 339 | */ |
| 340 | |
| 341 | unsigned checkpath(const char *p, const struct checkpath *cp) |
| 342 | { |
| 343 | char cwd[PATH_MAX]; |
| 344 | struct elt *e, *ee; |
| 345 | struct stat st; |
| 346 | unsigned bad = 0; |
| 347 | |
| 348 | /* --- Initialize stack pointer and path string --- */ |
| 349 | |
| 350 | sp = &rootnode; |
| 351 | dstr_destroy(&d); |
| 352 | |
| 353 | /* --- Try to find the current directory --- */ |
| 354 | |
| 355 | if (!getcwd(cwd, sizeof(cwd))) { |
| 356 | report(cp, CP_ERROR, 0, 0, "can't find current directory: %e"); |
| 357 | return (CP_ERROR); |
| 358 | } |
| 359 | |
| 360 | /* --- Check that the root directory is OK --- */ |
| 361 | |
| 362 | if (stat("/", &st)) { |
| 363 | report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e"); |
| 364 | return (CP_ERROR); |
| 365 | } |
| 366 | |
| 367 | report(cp, CP_REPORT, 3, p, "begin scan"); |
| 368 | bad |= sanity("/", &st, cp, 0); |
| 369 | |
| 370 | /* --- Get the initial list of things to process --- */ |
| 371 | |
| 372 | ee = splitpath(p, 0); |
| 373 | if (*p != '/') |
| 374 | ee = splitpath(cwd, ee); |
| 375 | |
| 376 | /* --- While there are list items which still need doing --- */ |
| 377 | |
| 378 | while (ee) { |
| 379 | e = ee->e_link; |
| 380 | |
| 381 | /* --- Strip off simple `.' elements --- */ |
| 382 | |
| 383 | if (strcmp(ee->e_name, ".") == 0) { |
| 384 | free(ee); |
| 385 | ee = e; |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | /* --- Backtrack on `..' elements --- */ |
| 390 | |
| 391 | else if (strcmp(ee->e_name, "..") == 0) { |
| 392 | pop(); |
| 393 | free(ee); |
| 394 | ee = e; |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | /* --- Everything else gets pushed on the end --- */ |
| 399 | |
| 400 | push(ee); |
| 401 | ee = e; |
| 402 | |
| 403 | /* --- Find out what sort of a thing this is --- */ |
| 404 | |
| 405 | if (lstat(d.buf, &st)) { |
| 406 | report(cp, CP_ERROR, 0, d.buf, "can't stat: %e"); |
| 407 | bad |= CP_ERROR; |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | /* --- Handle symbolic links specially --- */ |
| 412 | |
| 413 | if (S_ISLNK(st.st_mode)) { |
| 414 | dstr buf = DSTR_INIT; |
| 415 | int i; |
| 416 | |
| 417 | /* --- Resolve the link --- */ |
| 418 | |
| 419 | dstr_ensure(&buf, st.st_size + 1); |
| 420 | if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) { |
| 421 | report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e"); |
| 422 | bad |= CP_ERROR; |
| 423 | break; |
| 424 | } |
| 425 | buf.buf[i] = 0; |
| 426 | report(cp, CP_SYMLINK, 2, d.buf, "symlink -> `%s'", buf.buf); |
| 427 | |
| 428 | /* --- Handle sticky parents --- * |
| 429 | * |
| 430 | * If I make a symlink in a sticky directory, I can later modify it. |
| 431 | * However, nobody else can (except the owner of the directory, and |
| 432 | * we'll already have noticed that if we care). |
| 433 | */ |
| 434 | |
| 435 | if ((cp->cp_what & CP_WROTHUSR) && |
| 436 | (sp->e_link->e_flags & f_sticky) && |
| 437 | st.st_uid != cp->cp_uid && st.st_uid != 0) { |
| 438 | bad |= CP_WROTHUSR; |
| 439 | report(cp, CP_WROTHUSR, 1, d.buf, |
| 440 | "symlink modifiable by user %u", st.st_uid); |
| 441 | } |
| 442 | |
| 443 | /* --- Sort out what to do from here --- */ |
| 444 | |
| 445 | if (buf.buf[0] == '/') |
| 446 | popall(); |
| 447 | else |
| 448 | pop(); |
| 449 | ee = splitpath(buf.buf, ee); |
| 450 | dstr_destroy(&buf); |
| 451 | continue; |
| 452 | } |
| 453 | |
| 454 | /* --- Run the sanity check on this path element --- */ |
| 455 | |
| 456 | bad |= sanity(d.buf, &st, cp, ee ? 0 : f_last); |
| 457 | |
| 458 | if (S_ISDIR(st.st_mode)) { |
| 459 | if (st.st_mode & 01000) |
| 460 | sp->e_flags |= f_sticky; |
| 461 | report(cp, CP_REPORT, 4, d.buf, "directory"); |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | /* --- Something else I don't understand --- */ |
| 466 | |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | /* --- Check for leftover junk --- */ |
| 471 | |
| 472 | if (ee) { |
| 473 | if (!(bad & CP_ERROR)) |
| 474 | report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf"); |
| 475 | while (ee) { |
| 476 | e = ee->e_link; |
| 477 | free(ee); |
| 478 | ee = e; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | popall(); |
| 483 | return (bad); |
| 484 | } |
| 485 | |
| 486 | /* --- @checkpath_setids@ --- * |
| 487 | * |
| 488 | * Arguments: @struct checkpath *cp@ = pointer to block to fill in |
| 489 | * |
| 490 | * Returns: --- |
| 491 | * |
| 492 | * Use: Fills in the user ids and things in the structure. |
| 493 | */ |
| 494 | |
| 495 | void checkpath_setids(struct checkpath *cp) |
| 496 | { |
| 497 | int n, i; |
| 498 | gid_t g = getgid(); |
| 499 | |
| 500 | cp->cp_uid = getuid(); |
| 501 | n = getgroups(sizeof(cp->cp_gid) / sizeof(cp->cp_gid[0]), cp->cp_gid); |
| 502 | |
| 503 | for (i = 0; i < n; i++) { |
| 504 | if (cp->cp_gid[i] == g) |
| 505 | goto gid_ok; |
| 506 | } |
| 507 | cp->cp_gid[n++] = g; |
| 508 | gid_ok: |
| 509 | cp->cp_gids = n; |
| 510 | } |
| 511 | |
| 512 | /*----- That's all, folks -------------------------------------------------*/ |