| 1 |
/* |
| 2 |
* Copyright (c) 1999,2007,2019-21 Andrew G. Morgan <[email protected]> |
| 3 |
* |
| 4 |
* The purpose of this module is to enforce inheritable, bounding and |
| 5 |
* ambient capability sets for a specified user. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* #define PAM_DEBUG */ |
| 9 |
|
| 10 |
#ifndef _DEFAULT_SOURCE |
| 11 |
#define _DEFAULT_SOURCE |
| 12 |
#endif |
| 13 |
|
| 14 |
#include <errno.h> |
| 15 |
#include <fcntl.h> |
| 16 |
#include <grp.h> |
| 17 |
#include <limits.h> |
| 18 |
#include <pwd.h> |
| 19 |
#include <stdarg.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
#include <syslog.h> |
| 24 |
#include <sys/capability.h> |
| 25 |
#include <sys/prctl.h> |
| 26 |
#include <sys/stat.h> |
| 27 |
#include <sys/types.h> |
| 28 |
|
| 29 |
#include <security/pam_modules.h> |
| 30 |
#include <security/_pam_macros.h> |
| 31 |
|
| 32 |
#define USER_CAP_FILE "/etc/security/capability.conf" |
| 33 |
#define CAP_FILE_BUFFER_SIZE 4096 |
| 34 |
#define CAP_FILE_DELIMITERS " \t\n" |
| 35 |
|
| 36 |
/* |
| 37 |
* pam_cap_s is used to summarize argument values in a parsed form. |
| 38 |
*/ |
| 39 |
struct pam_cap_s { |
| 40 |
int debug; |
| 41 |
int keepcaps; |
| 42 |
int autoauth; |
| 43 |
int defer; |
| 44 |
const char *user; |
| 45 |
const char *conf_filename; |
| 46 |
const char *fallback; |
| 47 |
pam_handle_t *pamh; |
| 48 |
}; |
| 49 |
|
| 50 |
/* |
| 51 |
* load_groups obtains the list all of the groups associated with the |
| 52 |
* requested user: gid & supplemental groups. |
| 53 |
*/ |
| 54 |
static int load_groups(const char *user, char ***groups, int *groups_n) { |
| 55 |
struct passwd *pwd; |
| 56 |
gid_t grps[NGROUPS_MAX]; |
| 57 |
int ngrps = NGROUPS_MAX; |
| 58 |
|
| 59 |
*groups = NULL; |
| 60 |
*groups_n = 0; |
| 61 |
|
| 62 |
pwd = getpwnam(user); |
| 63 |
if (pwd == NULL) { |
| 64 |
return -1; |
| 65 |
} |
| 66 |
|
| 67 |
/* must include at least pwd->pw_gid, hence < 1 test. */ |
| 68 |
if (getgrouplist(user, pwd->pw_gid, grps, &ngrps) < 1) { |
| 69 |
return -1; |
| 70 |
} |
| 71 |
|
| 72 |
*groups = calloc(ngrps, sizeof(char *)); |
| 73 |
if (*groups == NULL) { |
| 74 |
return -1; |
| 75 |
} |
| 76 |
int g_n = 0, i; |
| 77 |
for (i = 0; i < ngrps; i++) { |
| 78 |
const struct group *g = getgrgid(grps[i]); |
| 79 |
if (g == NULL) { |
| 80 |
continue; |
| 81 |
} |
| 82 |
D(("noting [%s] is a member of [%s]", user, g->gr_name)); |
| 83 |
(*groups)[g_n++] = strdup(g->gr_name); |
| 84 |
} |
| 85 |
|
| 86 |
*groups_n = g_n; |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
/* obtain the desired IAB capabilities for the current user */ |
| 91 |
|
| 92 |
static char *read_capabilities_for_user(const char *user, const char *source) |
| 93 |
{ |
| 94 |
char *cap_string = NULL; |
| 95 |
char buffer[CAP_FILE_BUFFER_SIZE], *line; |
| 96 |
char **groups; |
| 97 |
int groups_n; |
| 98 |
FILE *cap_file; |
| 99 |
|
| 100 |
if (load_groups(user, &groups, &groups_n)) { |
| 101 |
D(("unknown user [%s]", user)); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
cap_file = fopen(source, "r"); |
| 106 |
if (cap_file == NULL) { |
| 107 |
D(("failed to open capability file")); |
| 108 |
goto defer; |
| 109 |
} |
| 110 |
/* |
| 111 |
* In all cases other than "/dev/null", the config file should not |
| 112 |
* be world writable. We do not check for ownership limitations or |
| 113 |
* group write restrictions as these represent legitimate local |
| 114 |
* administration choices. Especially in a system operating in |
| 115 |
* CAP_MODE_PURE1E. |
| 116 |
*/ |
| 117 |
if (strcmp(source, "/dev/null") != 0) { |
| 118 |
struct stat sb; |
| 119 |
D(("validate filehandle [for opened %s] does not point to a world" |
| 120 |
" writable file", source)); |
| 121 |
if (fstat(fileno(cap_file), &sb) != 0) { |
| 122 |
D(("unable to fstat config file: %d", errno)); |
| 123 |
goto close_out_file; |
| 124 |
} |
| 125 |
if ((sb.st_mode & S_IWOTH) != 0) { |
| 126 |
D(("open failed [%s] is world writable test: security hole", |
| 127 |
source)); |
| 128 |
goto close_out_file; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
int found_one = 0; |
| 133 |
while (!found_one && |
| 134 |
(line = fgets(buffer, CAP_FILE_BUFFER_SIZE, cap_file))) { |
| 135 |
const char *cap_text; |
| 136 |
|
| 137 |
char *next = NULL; |
| 138 |
cap_text = strtok_r(line, CAP_FILE_DELIMITERS, &next); |
| 139 |
|
| 140 |
if (cap_text == NULL) { |
| 141 |
D(("empty line")); |
| 142 |
continue; |
| 143 |
} |
| 144 |
if (*cap_text == '#') { |
| 145 |
D(("comment line")); |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
* Explore whether any of the ids are a match for the current |
| 151 |
* user. |
| 152 |
*/ |
| 153 |
while ((line = strtok_r(next, CAP_FILE_DELIMITERS, &next))) { |
| 154 |
if (strcmp("*", line) == 0) { |
| 155 |
D(("wildcard matched")); |
| 156 |
found_one = 1; |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
if (strcmp(user, line) == 0) { |
| 161 |
D(("exact match for user")); |
| 162 |
found_one = 1; |
| 163 |
break; |
| 164 |
} |
| 165 |
|
| 166 |
if (line[0] != '@') { |
| 167 |
D(("user [%s] is not [%s] - skipping", user, line)); |
| 168 |
continue; |
| 169 |
} |
| 170 |
|
| 171 |
int i; |
| 172 |
for (i=0; i < groups_n; i++) { |
| 173 |
const char *g = groups[i]; |
| 174 |
if (g != NULL && !strcmp(g, line+1)) { |
| 175 |
D(("user group matched [%s]", line)); |
| 176 |
found_one = 1; |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
if (found_one) { |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if (found_one) { |
| 186 |
cap_string = strdup(cap_text); |
| 187 |
D(("user [%s] matched - caps are [%s]", user, cap_string)); |
| 188 |
} |
| 189 |
|
| 190 |
cap_text = NULL; |
| 191 |
line = NULL; |
| 192 |
} |
| 193 |
|
| 194 |
close_out_file: |
| 195 |
fclose(cap_file); |
| 196 |
|
| 197 |
defer: |
| 198 |
memset(buffer, 0, CAP_FILE_BUFFER_SIZE); |
| 199 |
|
| 200 |
int i; |
| 201 |
for (i = 0; i < groups_n; i++) { |
| 202 |
char *g = groups[i]; |
| 203 |
memset(g, 0, strlen(g)); |
| 204 |
_pam_drop(g); |
| 205 |
} |
| 206 |
if (groups != NULL) { |
| 207 |
memset(groups, 0, groups_n * sizeof(char *)); |
| 208 |
_pam_drop(groups); |
| 209 |
} |
| 210 |
|
| 211 |
return cap_string; |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
* This is the "defer" cleanup function that actually applies the IAB |
| 216 |
* tuple. This happens really late in the PAM session, hopefully after |
| 217 |
* the application has performed its setuid() function. |
| 218 |
*/ |
| 219 |
static void iab_apply(pam_handle_t *pamh, void *data, int error_status) |
| 220 |
{ |
| 221 |
cap_iab_t iab = data; |
| 222 |
int retval = error_status & ~(PAM_DATA_REPLACE|PAM_DATA_SILENT); |
| 223 |
|
| 224 |
#ifdef PAM_DEBUG |
| 225 |
{ |
| 226 |
cap_t c = cap_get_proc(); |
| 227 |
cap_iab_t tu = cap_iab_get_proc(); |
| 228 |
char *tc, *ttu; |
| 229 |
tc = cap_to_text(c, NULL); |
| 230 |
ttu = cap_iab_to_text(tu); |
| 231 |
|
| 232 |
D(("iab_apply with uid=%d,euid=%d and error_status=0x%08x \"%s\", [%s]", |
| 233 |
getuid(), geteuid(), error_status, tc, ttu)); |
| 234 |
|
| 235 |
cap_free(ttu); |
| 236 |
cap_free(tc); |
| 237 |
cap_free(tu); |
| 238 |
cap_free(c); |
| 239 |
} |
| 240 |
#endif |
| 241 |
|
| 242 |
data = NULL; |
| 243 |
if (error_status & PAM_DATA_REPLACE) { |
| 244 |
goto done; |
| 245 |
} |
| 246 |
|
| 247 |
if (retval != PAM_SUCCESS || !(error_status & PAM_DATA_SILENT)) { |
| 248 |
goto done; |
| 249 |
} |
| 250 |
|
| 251 |
if (cap_iab_set_proc(iab) != 0) { |
| 252 |
D(("IAB setting failed")); |
| 253 |
} |
| 254 |
|
| 255 |
done: |
| 256 |
cap_free(iab); |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* Set capabilities for current process to match the current |
| 261 |
* permitted+executable sets combined with the configured inheritable |
| 262 |
* set. |
| 263 |
*/ |
| 264 |
static int set_capabilities(struct pam_cap_s *cs) |
| 265 |
{ |
| 266 |
cap_t cap_s; |
| 267 |
char *conf_caps; |
| 268 |
int ok = 0; |
| 269 |
cap_iab_t iab; |
| 270 |
|
| 271 |
cap_s = cap_get_proc(); |
| 272 |
if (cap_s == NULL) { |
| 273 |
D(("your kernel is capability challenged - upgrade: %s", |
| 274 |
strerror(errno))); |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
conf_caps = read_capabilities_for_user(cs->user, |
| 279 |
cs->conf_filename |
| 280 |
? cs->conf_filename:USER_CAP_FILE ); |
| 281 |
if (conf_caps == NULL) { |
| 282 |
D(("no capabilities found for user [%s]", cs->user)); |
| 283 |
if (cs->fallback == NULL) { |
| 284 |
goto cleanup_cap_s; |
| 285 |
} |
| 286 |
conf_caps = strdup(cs->fallback); |
| 287 |
if (conf_caps == NULL) { |
| 288 |
goto cleanup_cap_s; |
| 289 |
} |
| 290 |
D(("user [%s] received fallback caps [%s]", cs->user, conf_caps)); |
| 291 |
} |
| 292 |
|
| 293 |
ssize_t conf_caps_length = strlen(conf_caps); |
| 294 |
if (!strcmp(conf_caps, "all")) { |
| 295 |
/* |
| 296 |
* all here is interpreted as no change/pass through, which is |
| 297 |
* likely to be the same as none for sensible system defaults. |
| 298 |
*/ |
| 299 |
ok = 1; |
| 300 |
goto cleanup_conf; |
| 301 |
} |
| 302 |
|
| 303 |
if (!strcmp(conf_caps, "none")) { |
| 304 |
/* clearing CAP_INHERITABLE will also clear the ambient caps, |
| 305 |
* but for legacy reasons we do not alter the bounding set. */ |
| 306 |
cap_clear_flag(cap_s, CAP_INHERITABLE); |
| 307 |
if (!cap_set_proc(cap_s)) { |
| 308 |
ok = 1; |
| 309 |
} |
| 310 |
goto cleanup_conf; |
| 311 |
} |
| 312 |
|
| 313 |
iab = cap_iab_from_text(conf_caps); |
| 314 |
if (iab == NULL) { |
| 315 |
D(("unable to parse the IAB [%s] value", conf_caps)); |
| 316 |
goto cleanup_conf; |
| 317 |
} |
| 318 |
|
| 319 |
if (cs->defer) { |
| 320 |
D(("configured to delay applying IAB")); |
| 321 |
int ret = pam_set_data(cs->pamh, "pam_cap_iab", iab, iab_apply); |
| 322 |
if (ret != PAM_SUCCESS) { |
| 323 |
D(("unable to cache capabilities for delayed setting: %d", ret)); |
| 324 |
/* since ok=0, the module will return PAM_IGNORE */ |
| 325 |
cap_free(iab); |
| 326 |
} |
| 327 |
iab = NULL; |
| 328 |
} else if (!cap_iab_set_proc(iab)) { |
| 329 |
D(("able to set the IAB [%s] value", conf_caps)); |
| 330 |
ok = 1; |
| 331 |
} |
| 332 |
cap_free(iab); |
| 333 |
|
| 334 |
if (cs->keepcaps) { |
| 335 |
/* |
| 336 |
* Best effort to set keep caps - this may help work around |
| 337 |
* situations where applications are using a capabilities |
| 338 |
* unaware setuid() call. |
| 339 |
* |
| 340 |
* It isn't needed unless you want to support Ambient vector |
| 341 |
* values in the IAB. In this case, it will likely also |
| 342 |
* require you use the "defer" module argument. |
| 343 |
*/ |
| 344 |
D(("setting keepcaps")); |
| 345 |
(void) cap_prctlw(PR_SET_KEEPCAPS, 1, 0, 0, 0, 0); |
| 346 |
} |
| 347 |
|
| 348 |
cleanup_conf: |
| 349 |
memset(conf_caps, 0, conf_caps_length); |
| 350 |
_pam_drop(conf_caps); |
| 351 |
|
| 352 |
cleanup_cap_s: |
| 353 |
cap_free(cap_s); |
| 354 |
cap_s = NULL; |
| 355 |
|
| 356 |
return ok; |
| 357 |
} |
| 358 |
|
| 359 |
/* log errors */ |
| 360 |
|
| 361 |
static void _pam_log(int err, const char *format, ...) |
| 362 |
{ |
| 363 |
va_list args; |
| 364 |
|
| 365 |
va_start(args, format); |
| 366 |
openlog("pam_cap", LOG_CONS|LOG_PID, LOG_AUTH); |
| 367 |
vsyslog(err, format, args); |
| 368 |
va_end(args); |
| 369 |
closelog(); |
| 370 |
} |
| 371 |
|
| 372 |
static void parse_args(int argc, const char **argv, struct pam_cap_s *pcs) |
| 373 |
{ |
| 374 |
D(("parsing %d module arg(s)", argc)); |
| 375 |
|
| 376 |
memset(pcs, 0, sizeof(*pcs)); |
| 377 |
|
| 378 |
/* step through arguments */ |
| 379 |
for (; argc-- > 0; ++argv) { |
| 380 |
if (!strcmp(*argv, "debug")) { |
| 381 |
pcs->debug = 1; |
| 382 |
} else if (!strncmp(*argv, "config=", 7)) { |
| 383 |
pcs->conf_filename = 7 + *argv; |
| 384 |
} else if (!strcmp(*argv, "keepcaps")) { |
| 385 |
pcs->keepcaps = 1; |
| 386 |
} else if (!strcmp(*argv, "autoauth")) { |
| 387 |
pcs->autoauth = 1; |
| 388 |
} else if (!strncmp(*argv, "default=", 8)) { |
| 389 |
pcs->fallback = 8 + *argv; |
| 390 |
} else if (!strcmp(*argv, "defer")) { |
| 391 |
pcs->defer = 1; |
| 392 |
} else { |
| 393 |
_pam_log(LOG_ERR, "unknown option; %s", *argv); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
/* |
| 399 |
* pam_sm_authenticate parses the config file with respect to the user |
| 400 |
* being authenticated and determines if they are covered by any |
| 401 |
* capability inheritance rules. |
| 402 |
*/ |
| 403 |
int pam_sm_authenticate(pam_handle_t *pamh, int flags, |
| 404 |
int argc, const char **argv) |
| 405 |
{ |
| 406 |
int retval; |
| 407 |
struct pam_cap_s pcs; |
| 408 |
char *conf_caps; |
| 409 |
|
| 410 |
parse_args(argc, argv, &pcs); |
| 411 |
|
| 412 |
retval = pam_get_user(pamh, &pcs.user, NULL); |
| 413 |
if (retval == PAM_CONV_AGAIN) { |
| 414 |
D(("user conversation is not available yet")); |
| 415 |
memset(&pcs, 0, sizeof(pcs)); |
| 416 |
return PAM_INCOMPLETE; |
| 417 |
} |
| 418 |
|
| 419 |
if (pcs.autoauth) { |
| 420 |
D(("pam_sm_authenticate autoauth = success")); |
| 421 |
memset(&pcs, 0, sizeof(pcs)); |
| 422 |
return PAM_SUCCESS; |
| 423 |
} |
| 424 |
|
| 425 |
if (retval != PAM_SUCCESS) { |
| 426 |
D(("pam_get_user failed: pam error=%d", retval)); |
| 427 |
memset(&pcs, 0, sizeof(pcs)); |
| 428 |
return PAM_AUTH_ERR; |
| 429 |
} |
| 430 |
|
| 431 |
conf_caps = read_capabilities_for_user(pcs.user, |
| 432 |
pcs.conf_filename |
| 433 |
? pcs.conf_filename:USER_CAP_FILE ); |
| 434 |
memset(&pcs, 0, sizeof(pcs)); |
| 435 |
|
| 436 |
if (conf_caps) { |
| 437 |
D(("it appears that there are capabilities for this user [%s]", |
| 438 |
conf_caps)); |
| 439 |
|
| 440 |
/* We could also store this as a pam_[gs]et_data item for use |
| 441 |
by the setcred call to follow. However, this precludes |
| 442 |
using pam_cap as just a cred module, and requires that the |
| 443 |
'auth' component be called first. As it is, there is a |
| 444 |
small race associated with a redundant read of the |
| 445 |
config. */ |
| 446 |
|
| 447 |
memset(conf_caps, 0, strlen(conf_caps)); |
| 448 |
_pam_drop(conf_caps); |
| 449 |
|
| 450 |
return PAM_SUCCESS; |
| 451 |
} |
| 452 |
|
| 453 |
D(("there are no capabilities restrictions on this user")); |
| 454 |
return PAM_IGNORE; |
| 455 |
} |
| 456 |
|
| 457 |
/* |
| 458 |
* pam_sm_setcred optionally applies inheritable capabilities loaded |
| 459 |
* by the pam_sm_authenticate pass for the user. If it doesn't apply |
| 460 |
* them directly (because of the "defer" module argument), it caches |
| 461 |
* the cap_iab_t value for later use during the pam_end() call. |
| 462 |
*/ |
| 463 |
int pam_sm_setcred(pam_handle_t *pamh, int flags, |
| 464 |
int argc, const char **argv) |
| 465 |
{ |
| 466 |
int retval = 0; |
| 467 |
struct pam_cap_s pcs; |
| 468 |
|
| 469 |
if (!(flags & (PAM_ESTABLISH_CRED | PAM_REINITIALIZE_CRED))) { |
| 470 |
D(("we don't handle much in the way of credentials")); |
| 471 |
return PAM_IGNORE; |
| 472 |
} |
| 473 |
|
| 474 |
parse_args(argc, argv, &pcs); |
| 475 |
|
| 476 |
retval = pam_get_item(pamh, PAM_USER, (const void **)&pcs.user); |
| 477 |
if ((retval != PAM_SUCCESS) || (pcs.user == NULL) || !(pcs.user[0])) { |
| 478 |
D(("user's name is not set")); |
| 479 |
return PAM_AUTH_ERR; |
| 480 |
} |
| 481 |
|
| 482 |
pcs.pamh = pamh; |
| 483 |
retval = set_capabilities(&pcs); |
| 484 |
memset(&pcs, 0, sizeof(pcs)); |
| 485 |
|
| 486 |
return (retval ? PAM_SUCCESS:PAM_IGNORE); |
| 487 |
} |