xref: /freebsd/lib/libutil/_secure_path.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
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 
2256c04344SDavid Nugent #include <sys/types.h>
2356c04344SDavid Nugent #include <sys/stat.h>
24163ae670SBruce Evans 
2556c04344SDavid Nugent #include <errno.h>
2656c04344SDavid Nugent #include <libutil.h>
27163ae670SBruce Evans #include <stddef.h>
28163ae670SBruce Evans #include <syslog.h>
2956c04344SDavid Nugent 
3056c04344SDavid Nugent /*
3156c04344SDavid Nugent  * Check for common security problems on a given path
3256c04344SDavid Nugent  * It must be:
3356c04344SDavid Nugent  * 1. A regular file, and exists
349a5393acSRuslan Ermilov  * 2. Owned and writable only by root (or given owner)
3556c04344SDavid Nugent  * 3. Group ownership is given group or is non-group writable
3656c04344SDavid Nugent  *
3756c04344SDavid Nugent  * Returns:	-2 if file does not exist,
3856c04344SDavid Nugent  *		-1 if security test failure
3956c04344SDavid Nugent  *		0  otherwise
4056c04344SDavid Nugent  */
4156c04344SDavid Nugent 
4256c04344SDavid Nugent int
_secure_path(const char * path,uid_t uid,gid_t gid)4356c04344SDavid Nugent _secure_path(const char *path, uid_t uid, gid_t gid)
4456c04344SDavid Nugent {
4556c04344SDavid Nugent     int		r = -1;
4656c04344SDavid Nugent     struct stat	sb;
4756c04344SDavid Nugent     const char	*msg = NULL;
4856c04344SDavid Nugent 
4956c04344SDavid Nugent     if (lstat(path, &sb) < 0) {
5056c04344SDavid Nugent 	if (errno == ENOENT) /* special case */
5156c04344SDavid Nugent 	    r = -2;  /* if it is just missing, skip the log entry */
5256c04344SDavid Nugent 	else
5356c04344SDavid Nugent 	    msg = "%s: cannot stat %s: %m";
5456c04344SDavid Nugent     }
5556c04344SDavid Nugent     else if (!S_ISREG(sb.st_mode))
5656c04344SDavid Nugent     	msg = "%s: %s is not a regular file";
5756c04344SDavid Nugent     else if (sb.st_mode & S_IWOTH)
5856c04344SDavid Nugent     	msg = "%s: %s is world writable";
59547fa0d9SMark Murray     else if ((int)uid != -1 && sb.st_uid != uid && sb.st_uid != 0) {
6056c04344SDavid Nugent     	if (uid == 0)
6156c04344SDavid Nugent     		msg = "%s: %s is not owned by root";
6256c04344SDavid Nugent     	else
6356c04344SDavid Nugent     		msg = "%s: %s is not owned by uid %d";
64547fa0d9SMark Murray     } else if ((int)gid != -1 && sb.st_gid != gid && (sb.st_mode & S_IWGRP))
6556c04344SDavid Nugent     	msg = "%s: %s is group writeable by non-authorised groups";
6656c04344SDavid Nugent     else
6756c04344SDavid Nugent     	r = 0;
6856c04344SDavid Nugent     if (msg != NULL)
6956c04344SDavid Nugent 	syslog(LOG_ERR, msg, "_secure_path", path, uid);
7056c04344SDavid Nugent     return r;
7156c04344SDavid Nugent }
72