1 /* $NetBSD: openpam_check_owner_perms.c,v 1.4 2023/06/30 21:46:20 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 Dag-Erling Smørgrav 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote 16 * products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifdef HAVE_CONFIG_H 33 # include "config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 __RCSID("$NetBSD: openpam_check_owner_perms.c,v 1.4 2023/06/30 21:46:20 christos Exp $"); 38 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 42 #include <errno.h> 43 #include <limits.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #include <security/pam_appl.h> 49 50 #include "openpam_impl.h" 51 52 /* 53 * OpenPAM internal 54 * 55 * Verify that the file or directory referenced by the given descriptor is 56 * owned by either root or the arbitrator and that it is not writable by 57 * group or other. 58 */ 59 60 int 61 openpam_check_desc_owner_perms(const char *name, int fd) 62 { 63 uid_t root, arbitrator; 64 struct stat sb; 65 int serrno; 66 67 root = 0; 68 arbitrator = geteuid(); 69 if (fstat(fd, &sb) != 0) { 70 serrno = errno; 71 openpam_log(PAM_LOG_ERROR, "%s: %s", name, strerror(errno)); 72 errno = serrno; 73 return (-1); 74 } 75 if (!S_ISREG(sb.st_mode)) { 76 openpam_log(PAM_LOG_ERROR, 77 "%s: not a regular file", name); 78 errno = EINVAL; 79 return (-1); 80 } 81 if ((sb.st_uid != root && sb.st_uid != arbitrator) || 82 (sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) { 83 openpam_log(PAM_LOG_ERROR, 84 "%s: insecure ownership or permissions", name); 85 errno = EPERM; 86 return (-1); 87 } 88 return (0); 89 } 90 91 /* 92 * OpenPAM internal 93 * 94 * Verify that a file or directory and all components of the path leading 95 * up to it are owned by either root or the arbitrator and that they are 96 * not writable by group or other. 97 * 98 * Note that openpam_check_desc_owner_perms() should be used instead if 99 * possible to avoid a race between the ownership / permission check and 100 * the actual open(). 101 */ 102 103 int 104 openpam_check_path_owner_perms(const char *path) 105 { 106 uid_t root, arbitrator; 107 char pathbuf[PATH_MAX]; 108 struct stat sb; 109 size_t len; 110 int serrno, tip; 111 112 tip = 1; 113 root = 0; 114 arbitrator = geteuid(); 115 if (realpath(path, pathbuf) == NULL) 116 return (-1); 117 len = strlen(pathbuf); 118 while (len > 0) { 119 if (stat(pathbuf, &sb) != 0) { 120 if (errno != ENOENT) { 121 serrno = errno; 122 openpam_log(PAM_LOG_ERROR, "%s: %m", pathbuf); 123 errno = serrno; 124 } 125 return (-1); 126 } 127 if (tip && !S_ISREG(sb.st_mode)) { 128 openpam_log(PAM_LOG_ERROR, 129 "%s: not a regular file", pathbuf); 130 errno = EINVAL; 131 return (-1); 132 } 133 if ((sb.st_uid != root && sb.st_uid != arbitrator) || 134 (sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) { 135 openpam_log(PAM_LOG_ERROR, 136 "%s: insecure ownership or permissions", pathbuf); 137 errno = EPERM; 138 return (-1); 139 } 140 while (--len > 0 && pathbuf[len] != '/') 141 pathbuf[len] = '\0'; 142 tip = 0; 143 } 144 return (0); 145 } 146 147 /* 148 * NOPARSE 149 */ 150