156c04344SDavid Nugent /*- 256c04344SDavid Nugent * Based on code copyright (c) 1995,1997 by 356c04344SDavid Nugent * Berkeley Software Design, Inc. 456c04344SDavid Nugent * All rights reserved. 556c04344SDavid Nugent * 656c04344SDavid Nugent * Redistribution and use in source and binary forms, with or without 756c04344SDavid Nugent * modification, is permitted provided that the following conditions 856c04344SDavid Nugent * are met: 956c04344SDavid Nugent * 1. Redistributions of source code must retain the above copyright 1056c04344SDavid Nugent * notice immediately at the beginning of the file, without modification, 1156c04344SDavid Nugent * this list of conditions, and the following disclaimer. 1256c04344SDavid Nugent * 2. Redistributions in binary form must reproduce the above copyright 1356c04344SDavid Nugent * notice, this list of conditions and the following disclaimer in the 1456c04344SDavid Nugent * documentation and/or other materials provided with the distribution. 1556c04344SDavid Nugent * 3. This work was done expressly for inclusion into FreeBSD. Other use 1656c04344SDavid Nugent * is permitted provided this notation is included. 1756c04344SDavid Nugent * 4. Absolutely no warranty of function or purpose is made by the authors. 1856c04344SDavid Nugent * 5. Modifications may be freely made to this file providing the above 1956c04344SDavid Nugent * conditions are met. 2056c04344SDavid Nugent */ 2156c04344SDavid Nugent 228719c58fSMatthew Dillon #include <sys/cdefs.h> 238719c58fSMatthew Dillon __FBSDID("$FreeBSD$"); 2456c04344SDavid Nugent 2556c04344SDavid Nugent #include <sys/types.h> 2656c04344SDavid Nugent #include <sys/stat.h> 27163ae670SBruce Evans 2856c04344SDavid Nugent #include <errno.h> 2956c04344SDavid Nugent #include <libutil.h> 30163ae670SBruce Evans #include <stddef.h> 31163ae670SBruce Evans #include <syslog.h> 3256c04344SDavid Nugent 3356c04344SDavid Nugent /* 3456c04344SDavid Nugent * Check for common security problems on a given path 3556c04344SDavid Nugent * It must be: 3656c04344SDavid Nugent * 1. A regular file, and exists 3756c04344SDavid Nugent * 2. Owned and writaable only by root (or given owner) 3856c04344SDavid Nugent * 3. Group ownership is given group or is non-group writable 3956c04344SDavid Nugent * 4056c04344SDavid Nugent * Returns: -2 if file does not exist, 4156c04344SDavid Nugent * -1 if security test failure 4256c04344SDavid Nugent * 0 otherwise 4356c04344SDavid Nugent */ 4456c04344SDavid Nugent 4556c04344SDavid Nugent int 4656c04344SDavid Nugent _secure_path(const char *path, uid_t uid, gid_t gid) 4756c04344SDavid Nugent { 4856c04344SDavid Nugent int r = -1; 4956c04344SDavid Nugent struct stat sb; 5056c04344SDavid Nugent const char *msg = NULL; 5156c04344SDavid Nugent 5256c04344SDavid Nugent if (lstat(path, &sb) < 0) { 5356c04344SDavid Nugent if (errno == ENOENT) /* special case */ 5456c04344SDavid Nugent r = -2; /* if it is just missing, skip the log entry */ 5556c04344SDavid Nugent else 5656c04344SDavid Nugent msg = "%s: cannot stat %s: %m"; 5756c04344SDavid Nugent } 5856c04344SDavid Nugent else if (!S_ISREG(sb.st_mode)) 5956c04344SDavid Nugent msg = "%s: %s is not a regular file"; 6056c04344SDavid Nugent else if (sb.st_mode & S_IWOTH) 6156c04344SDavid Nugent msg = "%s: %s is world writable"; 62547fa0d9SMark Murray else if ((int)uid != -1 && sb.st_uid != uid && sb.st_uid != 0) { 6356c04344SDavid Nugent if (uid == 0) 6456c04344SDavid Nugent msg = "%s: %s is not owned by root"; 6556c04344SDavid Nugent else 6656c04344SDavid Nugent msg = "%s: %s is not owned by uid %d"; 67547fa0d9SMark Murray } else if ((int)gid != -1 && sb.st_gid != gid && (sb.st_mode & S_IWGRP)) 6856c04344SDavid Nugent msg = "%s: %s is group writeable by non-authorised groups"; 6956c04344SDavid Nugent else 7056c04344SDavid Nugent r = 0; 7156c04344SDavid Nugent if (msg != NULL) 7256c04344SDavid Nugent syslog(LOG_ERR, msg, "_secure_path", path, uid); 7356c04344SDavid Nugent return r; 7456c04344SDavid Nugent } 75