1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <bsm/adt.h> 29 #include <bsm/adt_event.h> 30 #include <assert.h> 31 #include <bsm/audit.h> 32 #include <bsm/audit_record.h> 33 #include <bsm/libbsm.h> 34 #include <door.h> 35 #include <errno.h> 36 #include <generic.h> 37 #include <md5.h> 38 #include <sys/mkdev.h> 39 #include <netdb.h> 40 #include <nss_dbdefs.h> 41 #include <pwd.h> 42 #include <sys/stat.h> 43 #include <time.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <synch.h> 47 #include <sys/systeminfo.h> 48 #include <syslog.h> 49 #include <thread.h> 50 #include <unistd.h> 51 #include <adt_xlate.h> 52 #include <adt_ucred.h> 53 54 static int adt_selected(struct adt_event_state *, au_event_t, int); 55 static int adt_init(adt_internal_state_t *, int); 56 static int adt_import(adt_internal_state_t *, const adt_export_data_t *); 57 static m_label_t *adt_ucred_label(ucred_t *); 58 static void adt_setto_unaudited(adt_internal_state_t *); 59 60 #ifdef C2_DEBUG 61 #define DPRINTF(x) {printf x; } 62 #define DFLUSH fflush(stdout); 63 #else 64 #define DPRINTF(x) 65 #define DFLUSH 66 #endif 67 68 extern int _mutex_lock(mutex_t *); 69 extern int _mutex_unlock(mutex_t *); 70 71 static int auditstate = AUC_DISABLED; /* default state */ 72 73 /* 74 * adt_write_syslog 75 * 76 * errors that are not the user's fault (bugs or whatever in 77 * the underlying audit code are noted in syslog.) 78 * 79 * Avoid calling adt_write_syslog for things that can happen 80 * at high volume. 81 * 82 * syslog's open (openlog) and close (closelog) are interesting; 83 * openlog *may* create a file descriptor and is optional. closelog 84 * *will* close any open file descriptors and is also optional. 85 * 86 * Since syslog may also be used by the calling application, the 87 * choice is to avoid openlog, which sets some otherwise useful 88 * parameters, and to embed "Solaris_audit" in the log message. 89 */ 90 91 void 92 adt_write_syslog(const char *message, int err) 93 { 94 int save_errno; 95 int mask_priority; 96 97 save_errno = errno; 98 errno = err; 99 100 DPRINTF(("syslog called: %s\n", message)); 101 102 mask_priority = setlogmask(LOG_MASK(LOG_ALERT)); 103 syslog(LOG_ALERT, "Solaris_audit %s: %m", message, err); 104 (void) setlogmask(mask_priority); 105 errno = save_errno; 106 } 107 108 /* 109 * return true if audit is enabled. "Enabled" is any state 110 * other than AUC_DISABLED. 111 * 112 * states are 113 * AUC_INIT_AUDIT -- c2audit queuing enabled. 114 * AUC_AUDITING -- up and running 115 * AUC_DISABLED -- no audit subsystem loaded 116 * AUC_UNSET -- early boot state 117 * AUC_NOAUDIT -- subsystem loaded, turned off via 118 * auditon(A_SETCOND...) 119 * AUC_NOSPACE -- up and running, but log partitions are full 120 * 121 * For purpose of this API, anything but AUC_DISABLED or 122 * AUC_UNSET is enabled; however one never actually sees 123 * AUC_DISABLED since auditon returns EINVAL in that case. Any 124 * auditon error is considered the same as EINVAL for our 125 * purpose. auditstate is not changed by auditon if an error 126 * is returned. 127 */ 128 129 /* 130 * XXX this should probably be eliminated and adt_audit_state() replace it. 131 * All the legitimate uses are to not fork a waiting process for 132 * process exit processing, as in su, login, dtlogin. Other bogus 133 * users are zoneadmd and init. 134 * All but dtlogin are in ON, so we can do this without cross gate 135 * synchronization. 136 */ 137 138 boolean_t 139 adt_audit_enabled(void) 140 { 141 142 (void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate)); 143 144 return (auditstate != AUC_DISABLED); 145 } 146 147 /* 148 * See adt_audit_enabled() for state discussions. 149 * The state parameter is a hedge until all the uses become clear. 150 * Likely if adt_audit_enabled is brought internal to this file, 151 * it can take a parameter discussing the state. 152 */ 153 154 boolean_t 155 adt_audit_state(int state) 156 { 157 158 (void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate)); 159 160 return (auditstate == state); 161 } 162 163 /* 164 * The man page for getpwuid_r says the buffer must be big enough 165 * or ERANGE will be returned, but offers no guidance for how big 166 * the buffer should be or a way to calculate it. If you get 167 * ERANGE, double pwd_buff's size. 168 * 169 * This may be called even when auditing is off. 170 */ 171 172 #define NAFLAG_LEN 512 173 174 static int 175 adt_get_mask_from_user(uid_t uid, au_mask_t *mask) 176 { 177 struct passwd pwd; 178 char pwd_buff[NSS_BUFSIZ]; 179 char naflag_buf[NAFLAG_LEN]; 180 181 if (auditstate == AUC_DISABLED) { 182 mask->am_success = 0; 183 mask->am_failure = 0; 184 } else if (uid <= MAXUID) { 185 if (getpwuid_r(uid, &pwd, pwd_buff, NSS_BUFSIZ) == NULL) { 186 /* 187 * getpwuid_r returns NULL without setting 188 * errno if the user does not exist; only 189 * if the input is the wrong length does it 190 * set errno. 191 */ 192 if (errno != ERANGE) 193 errno = EINVAL; 194 return (-1); 195 } 196 if (au_user_mask(pwd.pw_name, mask)) { 197 errno = EFAULT; /* undetermined failure */ 198 return (-1); 199 } 200 } else if (getacna(naflag_buf, NAFLAG_LEN - 1) == 0) { 201 if (getauditflagsbin(naflag_buf, mask)) 202 return (-1); 203 } else { 204 return (-1); 205 } 206 return (0); 207 } 208 209 /* 210 * adt_get_unique_id -- generate a hopefully unique 32 bit value 211 * 212 * there will be a follow up to replace this with the use of /dev/random 213 * 214 * An MD5 hash is taken on a buffer of 215 * hostname . audit id . unix time . pid . count 216 * 217 * "count = noise++;" is subject to a race condition but I don't 218 * see a need to put a lock around it. 219 */ 220 221 au_id_t 222 adt_get_unique_id(au_id_t uid) 223 { 224 char hostname[MAXHOSTNAMELEN]; 225 union { 226 au_id_t v[4]; 227 unsigned char obuff[128/8]; 228 } output; 229 MD5_CTX context; 230 231 static int noise = 0; 232 233 int count = noise++; 234 time_t timebits = time(NULL); 235 pid_t pidbits = getpid(); 236 au_id_t retval = 0; 237 238 if (gethostname(hostname, MAXHOSTNAMELEN)) { 239 adt_write_syslog("gethostname call failed", errno); 240 (void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN); 241 } 242 243 while (retval == 0) { /* 0 is the only invalid result */ 244 MD5Init(&context); 245 246 MD5Update(&context, (unsigned char *)hostname, 247 (unsigned int) strlen((const char *)hostname)); 248 249 MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t)); 250 251 MD5Update(&context, 252 (unsigned char *) &timebits, sizeof (time_t)); 253 254 MD5Update(&context, (unsigned char *) &pidbits, 255 sizeof (pid_t)); 256 257 MD5Update(&context, (unsigned char *) &(count), sizeof (int)); 258 MD5Final(output.obuff, &context); 259 260 retval = output.v[count % 4]; 261 } 262 return (retval); 263 } 264 265 /* 266 * the following "port" function deals with the following issues: 267 * 268 * 1 the kernel and ucred deal with a dev_t as a 64 bit value made 269 * up from a 32 bit major and 32 bit minor. 270 * 2 User space deals with a dev_t as either the above 64 bit value 271 * or a 32 bit value made from a 14 bit major and an 18 bit minor. 272 * 3 The various audit interfaces (except ucred) pass the 32 or 273 * 64 bit version depending the architecture of the userspace 274 * application. If you get a port value from ucred and pass it 275 * to the kernel via auditon(), it must be squeezed into a 32 276 * bit value because the kernel knows the userspace app's bit 277 * size. 278 * 279 * The internal state structure for adt (adt_internal_state_t) uses 280 * dev_t, so adt converts data from ucred to fit. The import/export 281 * functions, however, can't know if they are importing/exporting 282 * from 64 or 32 bit applications, so they always send 64 bits and 283 * the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as 284 * appropriate. 285 */ 286 287 /* 288 * adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are 289 * both 64 bits). If lib is 32 bits, squeeze the two-int port into 290 * a 32 bit dev_t. A port fits in the "minor" part of au_port_t, 291 * so it isn't broken up into pieces. (When it goes to the kernel 292 * and back, however, it will have been split into major/minor 293 * pieces.) 294 */ 295 296 static void 297 adt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src) 298 { 299 #ifdef _LP64 300 (void) memcpy(dest, src, sizeof (au_tid_addr_t)); 301 #else 302 dest->at_type = src->at_type; 303 304 dest->at_port = src->at_port.at_minor & MAXMIN32; 305 dest->at_port |= (src->at_port.at_major & MAXMAJ32) << 306 NBITSMINOR32; 307 308 (void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t)); 309 #endif 310 } 311 312 /* 313 * adt_start_session -- create interface handle, create context 314 * 315 * The imported_state input is normally NULL, if not, it represents 316 * a continued session; its values obviate the need for a subsequent 317 * call to adt_set_user(). 318 * 319 * The flag is used to decide how to set the initial state of the session. 320 * If 0, the session is "no audit" until a call to adt_set_user; if 321 * ADT_USE_PROC_DATA, the session is built from the process audit 322 * characteristics obtained from the kernel. If imported_state is 323 * not NULL, the resulting audit mask is an OR of the current process 324 * audit mask and that passed in. 325 * 326 * The basic model is that the caller can use the pointer returned 327 * by adt_start_session whether or not auditing is enabled or an 328 * error was returned. The functions that take the session handle 329 * as input generally return without doing anything if auditing is 330 * disabled. 331 */ 332 333 int 334 adt_start_session(adt_session_data_t **new_session, 335 const adt_export_data_t *imported_state, adt_session_flags_t flags) 336 { 337 adt_internal_state_t *state; 338 adt_session_flags_t flgmask = ADT_FLAGS_ALL; 339 340 *new_session = NULL; /* assume failure */ 341 342 /* ensure that auditstate is set */ 343 (void) adt_audit_enabled(); 344 345 if ((flags & ~flgmask) != 0) { 346 errno = EINVAL; 347 goto return_err; 348 } 349 state = calloc(1, sizeof (adt_internal_state_t)); 350 351 if (state == NULL) 352 goto return_err; 353 354 if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0) 355 goto return_err_free; /* errno from adt_init() */ 356 357 /* 358 * The imported state overwrites the initial state if the 359 * imported state represents a valid audit trail 360 */ 361 362 if (imported_state != NULL) { 363 if (adt_import(state, imported_state) != 0) { 364 goto return_err_free; 365 } 366 } else if (flags & ADT_USE_PROC_DATA) { 367 state->as_session_model = ADT_PROCESS_MODEL; 368 } 369 state->as_flags = flags; 370 DPRINTF(("(%d) Starting session id = %08X\n", 371 getpid(), state->as_info.ai_asid)); 372 373 if (state->as_audit_enabled) { 374 *new_session = (adt_session_data_t *)state; 375 } else { 376 free(state); 377 } 378 379 return (0); 380 return_err_free: 381 free(state); 382 return_err: 383 adt_write_syslog("audit session create failed", errno); 384 return (-1); 385 } 386 387 /* 388 * adt_get_asid() and adt_set_asid() 389 * 390 * if you use this interface, you are responsible to insure that the 391 * rest of the session data is populated correctly before calling 392 * adt_proccess_attr() 393 * 394 * neither of these are intended for general use and will likely 395 * remain private interfaces for a long time. Forever is a long 396 * time. In the case of adt_set_asid(), you should have a very, 397 * very good reason for setting your own session id. The process 398 * audit characteristics are not changed by put, use adt_set_proc(). 399 * 400 * These are "volatile" (more changable than "evolving") and will 401 * probably change in the S10 period. 402 */ 403 404 void 405 adt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid) 406 { 407 408 if (session_data == NULL) { 409 *asid = 0; 410 } else { 411 assert(((adt_internal_state_t *)session_data)->as_check == 412 ADT_VALID); 413 414 *asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid; 415 } 416 } 417 418 void 419 adt_set_asid(const adt_session_data_t *session_data, const au_asid_t session_id) 420 { 421 422 if (session_data != NULL) { 423 assert(((adt_internal_state_t *)session_data)->as_check == 424 ADT_VALID); 425 426 ((adt_internal_state_t *)session_data)->as_have_user_data |= 427 ADT_HAVE_ASID; 428 ((adt_internal_state_t *)session_data)->as_info.ai_asid = 429 session_id; 430 } 431 } 432 433 /* 434 * adt_get_auid() and adt_set_auid() 435 * 436 * neither of these are intended for general use and will likely 437 * remain private interfaces for a long time. Forever is a long 438 * time. In the case of adt_set_auid(), you should have a very, 439 * very good reason for setting your own audit id. The process 440 * audit characteristics are not changed by put, use adt_set_proc(). 441 */ 442 443 void 444 adt_get_auid(const adt_session_data_t *session_data, au_id_t *auid) 445 { 446 447 if (session_data == NULL) { 448 *auid = AU_NOAUDITID; 449 } else { 450 assert(((adt_internal_state_t *)session_data)->as_check == 451 ADT_VALID); 452 453 *auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid; 454 } 455 } 456 457 void 458 adt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id) 459 { 460 461 if (session_data != NULL) { 462 assert(((adt_internal_state_t *)session_data)->as_check == 463 ADT_VALID); 464 465 ((adt_internal_state_t *)session_data)->as_have_user_data |= 466 ADT_HAVE_AUID; 467 ((adt_internal_state_t *)session_data)->as_info.ai_auid = 468 audit_id; 469 } 470 } 471 472 /* 473 * adt_get_termid(), adt_set_termid() 474 * 475 * if you use this interface, you are responsible to insure that the 476 * rest of the session data is populated correctly before calling 477 * adt_proccess_attr() 478 * 479 * The process audit characteristics are not changed by put, use 480 * adt_set_proc(). 481 */ 482 483 void 484 adt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid) 485 { 486 487 if (session_data == NULL) { 488 (void) memset(termid, 0, sizeof (au_tid_addr_t)); 489 termid->at_type = AU_IPv4; 490 } else { 491 assert(((adt_internal_state_t *)session_data)->as_check == 492 ADT_VALID); 493 494 *termid = 495 ((adt_internal_state_t *)session_data)->as_info.ai_termid; 496 } 497 } 498 499 void 500 adt_set_termid(const adt_session_data_t *session_data, 501 const au_tid_addr_t *termid) 502 { 503 504 if (session_data != NULL) { 505 assert(((adt_internal_state_t *)session_data)->as_check == 506 ADT_VALID); 507 508 ((adt_internal_state_t *)session_data)->as_info.ai_termid = 509 *termid; 510 511 ((adt_internal_state_t *)session_data)->as_have_user_data |= 512 ADT_HAVE_TID; 513 } 514 } 515 516 /* 517 * adt_get_mask(), adt_set_mask() 518 * 519 * if you use this interface, you are responsible to insure that the 520 * rest of the session data is populated correctly before calling 521 * adt_proccess_attr() 522 * 523 * The process audit characteristics are not changed by put, use 524 * adt_set_proc(). 525 */ 526 527 void 528 adt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask) 529 { 530 531 if (session_data == NULL) { 532 mask->am_success = 0; 533 mask->am_failure = 0; 534 } else { 535 assert(((adt_internal_state_t *)session_data)->as_check == 536 ADT_VALID); 537 538 *mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask; 539 } 540 } 541 542 void 543 adt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask) 544 { 545 546 if (session_data != NULL) { 547 assert(((adt_internal_state_t *)session_data)->as_check == 548 ADT_VALID); 549 550 ((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask; 551 552 ((adt_internal_state_t *)session_data)->as_have_user_data |= 553 ADT_HAVE_MASK; 554 } 555 } 556 557 /* 558 * helpers for adt_load_termid 559 */ 560 561 static void 562 adt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock, 563 au_tid_addr_t *termid) 564 { 565 566 termid->at_port = ((peer->sin6_port<<16) | (sock->sin6_port)); 567 termid->at_type = AU_IPv6; 568 (void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t)); 569 } 570 571 static void 572 adt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock, 573 au_tid_addr_t *termid) 574 { 575 576 termid->at_port = ((peer->sin_port<<16) | (sock->sin_port)); 577 578 termid->at_type = AU_IPv4; 579 termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr; 580 (void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t)); 581 } 582 583 /* 584 * adt_load_termid: convenience function; inputs file handle and 585 * outputs an au_tid_addr struct. 586 * 587 * This code was stolen from audit_settid.c; it differs from audit_settid() 588 * in that it does not write the terminal id to the process. 589 */ 590 591 int 592 adt_load_termid(int fd, adt_termid_t **termid) 593 { 594 au_tid_addr_t *p_term; 595 struct sockaddr_in6 peer; 596 struct sockaddr_in6 sock; 597 int peerlen = sizeof (peer); 598 int socklen = sizeof (sock); 599 600 *termid = NULL; 601 602 /* get peer name if its a socket, else assume local terminal */ 603 604 if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen) 605 < 0) { 606 if (errno == ENOTSOCK) 607 return (adt_load_hostname(NULL, termid)); 608 goto return_err; 609 } 610 611 if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) 612 goto return_err; 613 614 /* get sock name */ 615 if (getsockname(fd, (struct sockaddr *)&sock, 616 (socklen_t *)&socklen) < 0) 617 goto return_err_free; 618 619 if (peer.sin6_family == AF_INET6) { 620 adt_do_ipv6_address(&peer, &sock, p_term); 621 } else { 622 adt_do_ipv4_address((struct sockaddr_in *)&peer, 623 (struct sockaddr_in *)&sock, p_term); 624 } 625 *termid = (adt_termid_t *)p_term; 626 627 return (0); 628 629 return_err_free: 630 free(p_term); 631 return_err: 632 return (-1); 633 } 634 635 static boolean_t 636 adt_have_termid(au_tid_addr_t *dest) 637 { 638 struct auditinfo_addr audit_data; 639 640 if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) { 641 adt_write_syslog("getaudit failed", errno); 642 return (B_FALSE); 643 } 644 645 if ((audit_data.ai_termid.at_type == 0) || 646 (audit_data.ai_termid.at_addr[0] | 647 audit_data.ai_termid.at_addr[1] | 648 audit_data.ai_termid.at_addr[2] | 649 audit_data.ai_termid.at_addr[3]) == 0) 650 return (B_FALSE); 651 652 (void) memcpy(dest, &(audit_data.ai_termid), 653 sizeof (au_tid_addr_t)); 654 655 return (B_TRUE); 656 } 657 658 static int 659 adt_get_hostIP(const char *hostname, au_tid_addr_t *p_term) 660 { 661 struct addrinfo *ai; 662 void *p; 663 664 if (getaddrinfo(hostname, NULL, NULL, &ai) != 0) 665 return (-1); 666 667 switch (ai->ai_family) { 668 case AF_INET: 669 /* LINTED */ 670 p = &((struct sockaddr_in *)ai->ai_addr)->sin_addr; 671 (void) memcpy(p_term->at_addr, p, 672 sizeof (((struct sockaddr_in *)NULL)->sin_addr)); 673 p_term->at_type = AU_IPv4; 674 break; 675 case AF_INET6: 676 /* LINTED */ 677 p = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr, 678 (void) memcpy(p_term->at_addr, p, 679 sizeof (((struct sockaddr_in6 *)NULL)->sin6_addr)); 680 p_term->at_type = AU_IPv6; 681 break; 682 default: 683 return (-1); 684 } 685 686 freeaddrinfo(ai); 687 688 return (0); 689 } 690 691 /* 692 * adt_load_hostname() is called when the caller does not have a file 693 * handle that gives access to the socket info or any other way to 694 * pass in both port and ip address. The hostname input is ignored if 695 * the terminal id has already been set; instead it returns the 696 * existing terminal id. 697 * 698 * If audit is off and the hostname lookup fails, no error is 699 * returned, since an error may be interpreted by the caller 700 * as grounds for denying a login. Otherwise the caller would 701 * need to be aware of the audit state. 702 */ 703 704 int 705 adt_load_hostname(const char *hostname, adt_termid_t **termid) 706 { 707 char localhost[ADT_STRING_MAX + 1]; 708 au_tid_addr_t *p_term; 709 710 *termid = NULL; 711 712 if (!adt_audit_enabled()) 713 return (0); 714 715 if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) 716 goto return_err; 717 718 if (adt_have_termid(p_term)) { 719 *termid = (adt_termid_t *)p_term; 720 return (0); 721 } 722 p_term->at_port = 0; 723 724 if (hostname == NULL || *hostname == '\0') { 725 (void) sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX); 726 hostname = localhost; 727 } 728 if (adt_get_hostIP(hostname, p_term)) 729 goto return_err_free; 730 731 *termid = (adt_termid_t *)p_term; 732 return (0); 733 734 return_err_free: 735 free(p_term); 736 737 return_err: 738 if ((auditstate == AUC_DISABLED) || 739 (auditstate == AUC_NOAUDIT)) 740 return (0); 741 742 return (-1); 743 } 744 745 /* 746 * adt_load_ttyname() is called when the caller does not have a file 747 * handle that gives access to the local terminal or any other way 748 * of determining the device id. The ttyname input is ignored if 749 * the terminal id has already been set; instead it returns the 750 * existing terminal id. 751 * 752 * If audit is off and the ttyname lookup fails, no error is 753 * returned, since an error may be interpreted by the caller 754 * as grounds for denying a login. Otherwise the caller would 755 * need to be aware of the audit state. 756 */ 757 758 int 759 adt_load_ttyname(const char *ttyname, adt_termid_t **termid) 760 { 761 char localhost[ADT_STRING_MAX + 1]; 762 au_tid_addr_t *p_term; 763 struct stat stat_buf; 764 765 *termid = NULL; 766 767 if (!adt_audit_enabled()) 768 return (0); 769 770 if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL) 771 goto return_err; 772 773 if (adt_have_termid(p_term)) { 774 *termid = (adt_termid_t *)p_term; 775 return (0); 776 } 777 778 p_term->at_port = 0; 779 780 if (sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX) < 0) 781 goto return_err_free; /* errno from sysinfo */ 782 783 if (ttyname != NULL) { 784 if (stat(ttyname, &stat_buf) < 0) 785 goto return_err_free; 786 787 p_term->at_port = stat_buf.st_rdev; 788 } 789 790 if (adt_get_hostIP(localhost, p_term)) 791 goto return_err_free; 792 793 *termid = (adt_termid_t *)p_term; 794 return (0); 795 796 return_err_free: 797 free(p_term); 798 799 return_err: 800 if ((auditstate == AUC_DISABLED) || 801 (auditstate == AUC_NOAUDIT)) 802 return (0); 803 804 return (-1); 805 } 806 807 /* 808 * adt_get_session_id returns a stringified representation of 809 * the audit session id. See also adt_get_asid() for how to 810 * get the unexpurgated version. No guarantees as to how long 811 * the returned string will be or its general form; hex for now. 812 * 813 * An empty string is returned if auditing is off; length = 1 814 * and the pointer is valid. 815 * 816 * returns strlen + 1 if buffer is valid; else 0 and errno. 817 */ 818 819 size_t 820 adt_get_session_id(const adt_session_data_t *session_data, char **buff) 821 { 822 au_asid_t session_id; 823 size_t length; 824 /* 825 * output is 0x followed by 826 * two characters per byte 827 * plus terminator, 828 * except leading 0's are suppressed, so a few bytes may 829 * be unused. 830 */ 831 length = 2 + (2 * sizeof (session_id)) + 1; 832 *buff = malloc(length); 833 834 if (*buff == NULL) { 835 return (0); 836 } 837 if (session_data == NULL) { /* NULL is not an error */ 838 **buff = '\0'; 839 return (1); 840 } 841 adt_get_asid(session_data, &session_id); 842 843 length = snprintf(*buff, length, "0x%X", (int)session_id); 844 845 /* length < 1 is a bug: the session data type may have changed */ 846 assert(length > 0); 847 848 return (length); 849 } 850 851 /* 852 * adt_end_session -- close handle, clear context 853 * 854 * if as_check is invalid, no harm, no foul, EXCEPT that this could 855 * be an attempt to free data already free'd, so output to syslog 856 * to help explain why the process cored dumped. 857 */ 858 859 int 860 adt_end_session(adt_session_data_t *session_data) 861 { 862 adt_internal_state_t *state; 863 864 if (session_data != NULL) { 865 state = (adt_internal_state_t *)session_data; 866 if (state->as_check != ADT_VALID) { 867 adt_write_syslog("freeing invalid data", EINVAL); 868 } else { 869 state->as_check = 0; 870 m_label_free(state->as_label); 871 free(session_data); 872 } 873 } 874 /* no errors yet defined */ 875 return (0); 876 } 877 878 /* 879 * adt_dup_session -- copy the session data 880 */ 881 882 int 883 adt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest) 884 { 885 adt_internal_state_t *source_state; 886 adt_internal_state_t *dest_state = NULL; 887 int rc = 0; 888 889 if (source != NULL) { 890 source_state = (adt_internal_state_t *)source; 891 assert(source_state->as_check == ADT_VALID); 892 893 dest_state = malloc(sizeof (adt_internal_state_t)); 894 if (dest_state == NULL) { 895 rc = -1; 896 goto return_rc; 897 } 898 (void) memcpy(dest_state, source, 899 sizeof (struct adt_internal_state)); 900 901 if (source_state->as_label != NULL) { 902 dest_state->as_label = NULL; 903 if ((rc = m_label_dup(&dest_state->as_label, 904 source_state->as_label)) != 0) { 905 free(dest_state); 906 dest_state = NULL; 907 } 908 } 909 } 910 return_rc: 911 *dest = (adt_session_data_t *)dest_state; 912 return (rc); 913 } 914 915 /* 916 * from_export_format() 917 * read from a network order buffer into struct adt_session_data 918 */ 919 920 static size_t 921 adt_from_export_format(adt_internal_state_t *internal, 922 const adt_export_data_t *external) 923 { 924 struct export_header head; 925 struct export_link link; 926 adr_t context; 927 int32_t offset; 928 int32_t length; 929 int32_t version; 930 size_t label_len; 931 char *p = (char *)external; 932 933 adrm_start(&context, (char *)external); 934 adrm_int32(&context, (int *)&head, 4); 935 936 if ((internal->as_check = head.ax_check) != ADT_VALID) { 937 errno = EINVAL; 938 return (0); 939 } 940 offset = head.ax_link.ax_offset; 941 version = head.ax_link.ax_version; 942 length = head.ax_buffer_length; 943 944 /* 945 * Skip newer versions. 946 */ 947 while (version > PROTOCOL_VERSION_2) { 948 if (offset < 1) { 949 return (0); /* failed to match version */ 950 } 951 p += offset; /* point to next version # */ 952 953 if (p > (char *)external + length) { 954 return (0); 955 } 956 adrm_start(&context, p); 957 adrm_int32(&context, (int *)&link, 2); 958 offset = link.ax_offset; 959 version = link.ax_version; 960 assert(version != 0); 961 } 962 /* 963 * Adjust buffer pointer to the first data item (euid). 964 */ 965 if (p == (char *)external) { 966 adrm_start(&context, (char *)(p + sizeof (head))); 967 } else { 968 adrm_start(&context, (char *)(p + sizeof (link))); 969 } 970 /* 971 * if down rev version, neither pid nor label are included 972 * in v1 ax_size_of_tsol_data intentionally ignored 973 */ 974 if (version == PROTOCOL_VERSION_1) { 975 adrm_int32(&context, (int *)&(internal->as_euid), 1); 976 adrm_int32(&context, (int *)&(internal->as_ruid), 1); 977 adrm_int32(&context, (int *)&(internal->as_egid), 1); 978 adrm_int32(&context, (int *)&(internal->as_rgid), 1); 979 adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1); 980 adrm_int32(&context, 981 (int *)&(internal->as_info.ai_mask.am_success), 2); 982 adrm_int32(&context, 983 (int *)&(internal->as_info.ai_termid.at_port), 1); 984 adrm_int32(&context, 985 (int *)&(internal->as_info.ai_termid.at_type), 1); 986 adrm_int32(&context, 987 (int *)&(internal->as_info.ai_termid.at_addr[0]), 4); 988 adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1); 989 adrm_int32(&context, (int *)&(internal->as_audit_enabled), 1); 990 internal->as_pid = (pid_t)-1; 991 internal->as_label = NULL; 992 } else if (version == PROTOCOL_VERSION_2) { 993 adrm_int32(&context, (int *)&(internal->as_euid), 1); 994 adrm_int32(&context, (int *)&(internal->as_ruid), 1); 995 adrm_int32(&context, (int *)&(internal->as_egid), 1); 996 adrm_int32(&context, (int *)&(internal->as_rgid), 1); 997 adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1); 998 adrm_int32(&context, 999 (int *)&(internal->as_info.ai_mask.am_success), 2); 1000 adrm_int32(&context, 1001 (int *)&(internal->as_info.ai_termid.at_port), 1); 1002 adrm_int32(&context, 1003 (int *)&(internal->as_info.ai_termid.at_type), 1); 1004 adrm_int32(&context, 1005 (int *)&(internal->as_info.ai_termid.at_addr[0]), 4); 1006 adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1); 1007 adrm_int32(&context, (int *)&(internal->as_audit_enabled), 1); 1008 adrm_int32(&context, (int *)&(internal->as_pid), 1); 1009 adrm_int32(&context, (int *)&label_len, 1); 1010 if (label_len > 0) { 1011 /* read in and deal with different sized labels. */ 1012 size_t my_label_len = blabel_size(); 1013 1014 if ((internal->as_label = 1015 m_label_alloc(MAC_LABEL)) == NULL) { 1016 return (0); 1017 } 1018 if (label_len > my_label_len) { 1019 errno = EINVAL; 1020 m_label_free(internal->as_label); 1021 return (0); 1022 } 1023 (void) memset(internal->as_label, 0, my_label_len); 1024 adrm_int32(&context, (int *)(internal->as_label), 1025 label_len / sizeof (int32_t)); 1026 } else { 1027 internal->as_label = NULL; 1028 } 1029 } 1030 1031 return (length); 1032 } 1033 1034 /* 1035 * adt_to_export_format 1036 * read from struct adt_session_data into a network order buffer. 1037 * 1038 * (network order 'cause this data may be shared with a remote host.) 1039 */ 1040 1041 static size_t 1042 adt_to_export_format(adt_export_data_t *external, 1043 adt_internal_state_t *internal) 1044 { 1045 struct export_header head; 1046 struct export_link tail; 1047 adr_t context; 1048 size_t label_len = 0; 1049 1050 adrm_start(&context, (char *)external); 1051 1052 if (internal->as_label != NULL) { 1053 label_len = blabel_size(); 1054 } 1055 1056 head.ax_check = ADT_VALID; 1057 head.ax_buffer_length = sizeof (struct adt_export_data) + label_len; 1058 1059 /* version 2 first */ 1060 1061 head.ax_link.ax_version = PROTOCOL_VERSION_2; 1062 head.ax_link.ax_offset = sizeof (struct export_header) + 1063 sizeof (struct adt_export_v2) + label_len; 1064 1065 adrm_putint32(&context, (int *)&head, 4); 1066 1067 adrm_putint32(&context, (int *)&(internal->as_euid), 1); 1068 adrm_putint32(&context, (int *)&(internal->as_ruid), 1); 1069 adrm_putint32(&context, (int *)&(internal->as_egid), 1); 1070 adrm_putint32(&context, (int *)&(internal->as_rgid), 1); 1071 adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1); 1072 adrm_putint32(&context, 1073 (int *)&(internal->as_info.ai_mask.am_success), 2); 1074 adrm_putint32(&context, 1075 (int *)&(internal->as_info.ai_termid.at_port), 1); 1076 adrm_putint32(&context, 1077 (int *)&(internal->as_info.ai_termid.at_type), 1); 1078 adrm_putint32(&context, 1079 (int *)&(internal->as_info.ai_termid.at_addr[0]), 4); 1080 adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1); 1081 adrm_putint32(&context, (int *)&(internal->as_audit_enabled), 1); 1082 adrm_putint32(&context, (int *)&(internal->as_pid), 1); 1083 adrm_putint32(&context, (int *)&label_len, 1); 1084 if (internal->as_label != NULL) { 1085 /* serialize the label */ 1086 adrm_putint32(&context, (int *)(internal->as_label), 1087 (label_len / sizeof (int32_t))); 1088 } 1089 1090 /* now version 1 */ 1091 1092 tail.ax_version = PROTOCOL_VERSION_1; 1093 tail.ax_offset = 0; 1094 1095 adrm_putint32(&context, (int *)&tail, 2); 1096 1097 adrm_putint32(&context, (int *)&(internal->as_euid), 1); 1098 adrm_putint32(&context, (int *)&(internal->as_ruid), 1); 1099 adrm_putint32(&context, (int *)&(internal->as_egid), 1); 1100 adrm_putint32(&context, (int *)&(internal->as_rgid), 1); 1101 adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1); 1102 adrm_putint32(&context, 1103 (int *)&(internal->as_info.ai_mask.am_success), 2); 1104 adrm_putint32(&context, 1105 (int *)&(internal->as_info.ai_termid.at_port), 1); 1106 adrm_putint32(&context, 1107 (int *)&(internal->as_info.ai_termid.at_type), 1); 1108 adrm_putint32(&context, 1109 (int *)&(internal->as_info.ai_termid.at_addr[0]), 4); 1110 adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1); 1111 adrm_putint32(&context, (int *)&(internal->as_audit_enabled), 1); 1112 /* ignored in v1 */ 1113 adrm_putint32(&context, (int *)&label_len, 1); 1114 1115 /* finally terminator */ 1116 1117 tail.ax_version = 0; /* invalid version number */ 1118 tail.ax_offset = 0; 1119 1120 adrm_putint32(&context, (int *)&tail, 2); 1121 1122 return (head.ax_buffer_length); 1123 } 1124 1125 1126 /* 1127 * adt_import_proc() is used by a server acting on behalf 1128 * of a client which has connected via an ipc mechanism such as 1129 * a door. 1130 * 1131 * Since the interface is via ucred, the info.ap_termid.port 1132 * value is always the 64 bit version. What is stored depends 1133 * on how libbsm is compiled. 1134 */ 1135 1136 size_t 1137 adt_import_proc(pid_t pid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid, 1138 adt_export_data_t **external) 1139 { 1140 size_t length = 0; 1141 adt_internal_state_t *state; 1142 ucred_t *ucred; 1143 const au_tid64_addr_t *tid; 1144 1145 state = calloc(1, sizeof (adt_internal_state_t)); 1146 1147 if (state == NULL) 1148 return (0); 1149 1150 if (adt_init(state, 0) != 0) 1151 goto return_length_free; /* errno from adt_init() */ 1152 1153 /* 1154 * ucred_getauid() returns AU_NOAUDITID if audit is off, which 1155 * is the right answer for adt_import_proc(). 1156 * 1157 * Create a local context as near as possible. 1158 */ 1159 1160 ucred = ucred_get(pid); 1161 1162 if (ucred == NULL) 1163 goto return_length_free; 1164 1165 state->as_ruid = ruid != ADT_NO_CHANGE ? ruid : ucred_getruid(ucred); 1166 state->as_euid = euid != ADT_NO_CHANGE ? euid : ucred_geteuid(ucred); 1167 state->as_rgid = rgid != ADT_NO_CHANGE ? rgid : ucred_getrgid(ucred); 1168 state->as_egid = egid != ADT_NO_CHANGE ? egid : ucred_getegid(ucred); 1169 1170 state->as_info.ai_auid = ucred_getauid(ucred); 1171 1172 if (state->as_info.ai_auid == AU_NOAUDITID) { 1173 state->as_info.ai_asid = adt_get_unique_id(ruid); 1174 1175 if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask))) 1176 goto return_all_free; 1177 } else { 1178 const au_mask_t *mask = ucred_getamask(ucred); 1179 1180 if (mask != NULL) 1181 state->as_info.ai_mask = *mask; 1182 else 1183 goto return_all_free; 1184 1185 state->as_info.ai_asid = ucred_getasid(ucred); 1186 } 1187 1188 tid = ucred_getatid(ucred); 1189 1190 if (tid != NULL) { 1191 adt_cpy_tid(&(state->as_info.ai_termid), tid); 1192 } else { 1193 (void) memset((void *)&(state->as_info.ai_termid), 0, 1194 sizeof (au_tid_addr_t)); 1195 state->as_info.ai_termid.at_type = AU_IPv4; 1196 } 1197 1198 DPRINTF(("import_proc/asid = %X %u\n", state->as_info.ai_asid, 1199 state->as_info.ai_asid)); 1200 1201 DPRINTF(("import_proc/masks = %X %X\n", 1202 state->as_info.ai_mask.am_success, 1203 state->as_info.ai_mask.am_failure)); 1204 1205 if (state->as_label == NULL) { 1206 *external = malloc(sizeof (adt_export_data_t)); 1207 } else { 1208 *external = malloc(sizeof (adt_export_data_t) + blabel_size()); 1209 } 1210 1211 if (*external == NULL) 1212 goto return_all_free; 1213 1214 length = adt_to_export_format(*external, state); 1215 /* 1216 * yes, state is supposed to be free'd for both pass and fail 1217 */ 1218 return_all_free: 1219 ucred_free(ucred); 1220 return_length_free: 1221 free(state); 1222 return (length); 1223 } 1224 1225 /* 1226 * adt_ucred_label() -- if label is available, duplicate it. 1227 */ 1228 1229 static m_label_t * 1230 adt_ucred_label(ucred_t *uc) 1231 { 1232 m_label_t *ul = NULL; 1233 1234 if (ucred_getlabel(uc) != NULL) { 1235 (void) m_label_dup(&ul, ucred_getlabel(uc)); 1236 } 1237 1238 return (ul); 1239 } 1240 1241 /* 1242 * adt_import() -- convert from network order to machine-specific order 1243 */ 1244 1245 static int 1246 adt_import(adt_internal_state_t *internal, const adt_export_data_t *external) 1247 { 1248 au_mask_t mask; 1249 1250 /* save local audit enabled state */ 1251 int local_audit_enabled = internal->as_audit_enabled; 1252 1253 if (adt_from_export_format(internal, external) < 1) 1254 return (-1); /* errno from adt_from_export_format */ 1255 1256 /* 1257 * If audit isn't enabled on the remote, they were unable 1258 * to generate the audit mask, so generate it based on 1259 * local configuration. If the user id has changed, the 1260 * resulting mask may miss some subtleties that occurred 1261 * on the remote system. 1262 * 1263 * If the remote failed to generate a terminal id, it is not 1264 * recoverable. 1265 */ 1266 1267 if (!internal->as_audit_enabled) { 1268 if (adt_get_mask_from_user(internal->as_info.ai_auid, 1269 &(internal->as_info.ai_mask))) 1270 return (-1); 1271 if (internal->as_info.ai_auid != internal->as_ruid) { 1272 if (adt_get_mask_from_user(internal->as_info.ai_auid, 1273 &mask)) 1274 return (-1); 1275 internal->as_info.ai_mask.am_success |= 1276 mask.am_success; 1277 internal->as_info.ai_mask.am_failure |= 1278 mask.am_failure; 1279 } 1280 } 1281 internal->as_audit_enabled = local_audit_enabled; 1282 1283 DPRINTF(("(%d)imported asid = %X %u\n", getpid(), 1284 internal->as_info.ai_asid, 1285 internal->as_info.ai_asid)); 1286 1287 internal->as_have_user_data = ADT_HAVE_ALL; 1288 1289 return (0); 1290 } 1291 1292 /* 1293 * adt_export_session_data() 1294 * copies a adt_session_data struct into a network order buffer 1295 * 1296 * In a misconfigured network, the local host may have auditing 1297 * off while the destination may have auditing on, so if there 1298 * is sufficient memory, a buffer will be returned even in the 1299 * audit off case. 1300 */ 1301 1302 size_t 1303 adt_export_session_data(const adt_session_data_t *internal, 1304 adt_export_data_t **external) 1305 { 1306 size_t length = 0; 1307 1308 if ((internal != NULL) && 1309 ((adt_internal_state_t *)internal)->as_label != NULL) { 1310 length = blabel_size(); 1311 } 1312 1313 *external = malloc(sizeof (adt_export_data_t) + length); 1314 1315 if (*external == NULL) 1316 return (0); 1317 1318 if (internal == NULL) { 1319 adt_internal_state_t *dummy; 1320 1321 dummy = malloc(sizeof (adt_internal_state_t)); 1322 if (dummy == NULL) 1323 goto return_length_free; 1324 1325 if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */ 1326 free(dummy); 1327 goto return_length_free; 1328 } 1329 length = adt_to_export_format(*external, dummy); 1330 free(dummy); 1331 } else { 1332 length = adt_to_export_format(*external, 1333 (adt_internal_state_t *)internal); 1334 } 1335 return (length); 1336 1337 return_length_free: 1338 free(*external); 1339 *external = NULL; 1340 return (0); 1341 } 1342 1343 static void 1344 adt_setto_unaudited(adt_internal_state_t *state) 1345 { 1346 state->as_ruid = AU_NOAUDITID; 1347 state->as_euid = AU_NOAUDITID; 1348 state->as_rgid = AU_NOAUDITID; 1349 state->as_egid = AU_NOAUDITID; 1350 state->as_pid = (pid_t)-1; 1351 state->as_label = NULL; 1352 1353 if (state->as_audit_enabled) { 1354 state->as_info.ai_asid = 0; 1355 state->as_info.ai_auid = AU_NOAUDITID; 1356 1357 (void) memset((void *)&(state->as_info.ai_termid), 0, 1358 sizeof (au_tid_addr_t)); 1359 state->as_info.ai_termid.at_type = AU_IPv4; 1360 1361 (void) memset((void *)&(state->as_info.ai_mask), 0, 1362 sizeof (au_mask_t)); 1363 state->as_have_user_data = 0; 1364 } 1365 } 1366 1367 /* 1368 * adt_init -- set session context by copying the audit characteristics 1369 * from the proc and picking up current uid/tid information. 1370 * 1371 * By default, an audit session is based on the process; the default 1372 * is overriden by adt_set_user() 1373 */ 1374 1375 static int 1376 adt_init(adt_internal_state_t *state, int use_proc_data) 1377 { 1378 1379 state->as_audit_enabled = (auditstate == AUC_DISABLED) ? 0 : 1; 1380 1381 if (use_proc_data) { 1382 state->as_ruid = getuid(); 1383 state->as_euid = geteuid(); 1384 state->as_rgid = getgid(); 1385 state->as_egid = getegid(); 1386 state->as_pid = getpid(); 1387 1388 if (state->as_audit_enabled) { 1389 const au_tid64_addr_t *tid; 1390 const au_mask_t *mask; 1391 ucred_t *ucred = ucred_get(P_MYID); 1392 1393 /* 1394 * Even if the ucred is NULL, the underlying 1395 * credential may have a valid terminal id; if the 1396 * terminal id is set, then that's good enough. An 1397 * example of where this matters is failed login, 1398 * where rlogin/telnet sets the terminal id before 1399 * calling login; login does not load the credential 1400 * since auth failed. 1401 */ 1402 if (ucred == NULL) { 1403 if (!adt_have_termid( 1404 &(state->as_info.ai_termid))) 1405 return (-1); 1406 } else { 1407 mask = ucred_getamask(ucred); 1408 if (mask != NULL) { 1409 state->as_info.ai_mask = *mask; 1410 } else { 1411 ucred_free(ucred); 1412 return (-1); 1413 } 1414 tid = ucred_getatid(ucred); 1415 if (tid != NULL) { 1416 adt_cpy_tid(&(state->as_info.ai_termid), 1417 tid); 1418 } else { 1419 ucred_free(ucred); 1420 return (-1); 1421 } 1422 state->as_info.ai_asid = ucred_getasid(ucred); 1423 state->as_info.ai_auid = ucred_getauid(ucred); 1424 state->as_label = adt_ucred_label(ucred); 1425 ucred_free(ucred); 1426 } 1427 state->as_have_user_data = ADT_HAVE_ALL; 1428 } 1429 } else { 1430 adt_setto_unaudited(state); 1431 } 1432 state->as_session_model = ADT_SESSION_MODEL; /* default */ 1433 1434 if (state->as_audit_enabled && 1435 auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy), 1436 sizeof (state->as_kernel_audit_policy))) { 1437 return (-1); /* errno set by auditon */ 1438 } 1439 state->as_check = ADT_VALID; 1440 return (0); 1441 } 1442 1443 /* 1444 * adt_set_proc 1445 * 1446 * Copy the current session state to the process. If this function 1447 * is called, the model becomes a process model rather than a 1448 * session model. 1449 * 1450 * In the current implementation, the value state->as_have_user_data 1451 * must contain all of: ADT_HAVE_{AUID,MASK,TID,ASID}. These are all set 1452 * by adt_set_user() when the ADT_SETTID or ADT_NEW flag is passed in. 1453 * 1454 */ 1455 1456 int 1457 adt_set_proc(const adt_session_data_t *session_data) 1458 { 1459 int rc; 1460 adt_internal_state_t *state; 1461 1462 if (auditstate == AUC_DISABLED || (session_data == NULL)) 1463 return (0); 1464 1465 state = (adt_internal_state_t *)session_data; 1466 1467 assert(state->as_check == ADT_VALID); 1468 1469 if ((state->as_have_user_data & (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) != 1470 (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) { 1471 errno = EINVAL; 1472 goto return_err; 1473 } 1474 1475 rc = setaudit_addr((auditinfo_addr_t *)&(state->as_info), 1476 sizeof (auditinfo_addr_t)); 1477 1478 if (rc < 0) 1479 goto return_err; /* errno set by setaudit_addr() */ 1480 1481 state->as_session_model = ADT_PROCESS_MODEL; 1482 1483 return (0); 1484 1485 return_err: 1486 adt_write_syslog("failed to set process audit characteristics", errno); 1487 return (-1); 1488 } 1489 1490 static int 1491 adt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid) 1492 { 1493 au_tid_addr_t no_tid = {0, AU_IPv4, 0, 0, 0, 0}; 1494 au_mask_t no_mask = {0, 0}; 1495 1496 if (ruid == ADT_NO_AUDIT) { 1497 state->as_info.ai_auid = AU_NOAUDITID; 1498 state->as_info.ai_asid = 0; 1499 state->as_info.ai_termid = no_tid; 1500 state->as_info.ai_mask = no_mask; 1501 return (0); 1502 } 1503 state->as_info.ai_auid = ruid; 1504 state->as_info.ai_asid = adt_get_unique_id(ruid); 1505 if (termid != NULL) 1506 state->as_info.ai_termid = *termid; 1507 1508 if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask))) 1509 return (-1); 1510 1511 /* Assume intending to audit as this process */ 1512 1513 if (state->as_pid == (pid_t)-1) 1514 state->as_pid = getpid(); 1515 1516 if (is_system_labeled() && state->as_label == NULL) { 1517 ucred_t *ucred = ucred_get(P_MYID); 1518 1519 state->as_label = adt_ucred_label(ucred); 1520 ucred_free(ucred); 1521 } 1522 1523 return (0); 1524 } 1525 1526 static int 1527 adt_changeuser(adt_internal_state_t *state, uid_t ruid) 1528 { 1529 au_mask_t mask; 1530 1531 if (!(state->as_have_user_data & ADT_HAVE_AUID)) 1532 state->as_info.ai_auid = ruid; 1533 if (!(state->as_have_user_data & ADT_HAVE_ASID)) 1534 state->as_info.ai_asid = adt_get_unique_id(ruid); 1535 1536 if (ruid <= MAXEPHUID) { 1537 if (adt_get_mask_from_user(ruid, &mask)) 1538 return (-1); 1539 1540 state->as_info.ai_mask.am_success |= mask.am_success; 1541 state->as_info.ai_mask.am_failure |= mask.am_failure; 1542 } 1543 DPRINTF(("changed mask to %08X/%08X for ruid=%d\n", 1544 state->as_info.ai_mask.am_success, 1545 state->as_info.ai_mask.am_failure, 1546 ruid)); 1547 return (0); 1548 } 1549 1550 /* 1551 * adt_set_user -- see also adt_set_from_ucred() 1552 * 1553 * ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or 1554 * "unattributed." If ruid, change the model to session. 1555 * 1556 * ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value" 1557 * only valid with ADT_UPDATE. 1558 * 1559 * ADT_NO_AUDIT is the external equivalent to AU_NOAUDITID -- there 1560 * isn't a good reason to call adt_set_user() with it unless you don't 1561 * have a good value yet and intend to replace it later; auid will be 1562 * AU_NOAUDITID. 1563 * 1564 * adt_set_user should be called even if auditing is not enabled 1565 * so that adt_export_session_data() will have useful stuff to 1566 * work with. 1567 * 1568 * See the note preceding adt_set_proc() about the use of ADT_HAVE_TID 1569 * and ADT_HAVE_ALL. 1570 */ 1571 1572 int 1573 adt_set_user(const adt_session_data_t *session_data, uid_t euid, gid_t egid, 1574 uid_t ruid, gid_t rgid, const adt_termid_t *termid, 1575 enum adt_user_context user_context) 1576 { 1577 adt_internal_state_t *state; 1578 int rc; 1579 1580 if (session_data == NULL) /* no session exists to audit */ 1581 return (0); 1582 1583 state = (adt_internal_state_t *)session_data; 1584 assert(state->as_check == ADT_VALID); 1585 1586 switch (user_context) { 1587 case ADT_NEW: 1588 if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE || 1589 rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) { 1590 errno = EINVAL; 1591 return (-1); 1592 } 1593 if ((rc = adt_newuser(state, ruid, 1594 (au_tid_addr_t *)termid)) != 0) 1595 return (rc); 1596 1597 state->as_have_user_data = ADT_HAVE_ALL; 1598 break; 1599 case ADT_UPDATE: 1600 if (state->as_have_user_data != ADT_HAVE_ALL) { 1601 errno = EINVAL; 1602 return (-1); 1603 } 1604 1605 if (ruid != ADT_NO_CHANGE) 1606 if ((rc = adt_changeuser(state, ruid)) != 0) 1607 return (rc); 1608 break; 1609 case ADT_USER: 1610 if (state->as_have_user_data != ADT_HAVE_ALL) { 1611 errno = EINVAL; 1612 return (-1); 1613 } 1614 break; 1615 case ADT_SETTID: 1616 assert(termid != NULL); 1617 state->as_info.ai_termid = *((au_tid_addr_t *)termid); 1618 /* avoid fooling pam_setcred()... */ 1619 state->as_info.ai_auid = AU_NOAUDITID; 1620 state->as_info.ai_asid = 0; 1621 state->as_info.ai_mask.am_failure = 0; 1622 state->as_info.ai_mask.am_success = 0; 1623 state->as_have_user_data = ADT_HAVE_TID | 1624 ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK; 1625 return (0); 1626 default: 1627 errno = EINVAL; 1628 return (-1); 1629 } 1630 1631 if (ruid == ADT_NO_AUDIT) { 1632 state->as_ruid = AU_NOAUDITID; 1633 state->as_euid = AU_NOAUDITID; 1634 state->as_rgid = AU_NOAUDITID; 1635 state->as_egid = AU_NOAUDITID; 1636 } else { 1637 if (ruid != ADT_NO_CHANGE) 1638 state->as_ruid = ruid; 1639 if (euid != ADT_NO_CHANGE) 1640 state->as_euid = euid; 1641 if (rgid != ADT_NO_CHANGE) 1642 state->as_rgid = rgid; 1643 if (egid != ADT_NO_CHANGE) 1644 state->as_egid = egid; 1645 } 1646 1647 if (ruid == ADT_NO_ATTRIB) { 1648 state->as_session_model = ADT_SESSION_MODEL; 1649 } 1650 1651 return (0); 1652 } 1653 1654 /* 1655 * adt_set_from_ucred() 1656 * 1657 * an alternate to adt_set_user that fills the same role but uses 1658 * a pointer to a ucred rather than a list of id's. If the ucred 1659 * pointer is NULL, use the credential from the this process. 1660 * 1661 * A key difference is that for ADT_NEW, adt_set_from_ucred() does 1662 * not overwrite the asid and auid unless auid has not been set. 1663 * ADT_NEW differs from ADT_UPDATE in that it does not OR together 1664 * the incoming audit mask with the one that already exists. 1665 * 1666 * adt_set_from_ucred should be called even if auditing is not enabled 1667 * so that adt_export_session_data() will have useful stuff to 1668 * work with. 1669 */ 1670 1671 int 1672 adt_set_from_ucred(const adt_session_data_t *session_data, const ucred_t *uc, 1673 enum adt_user_context user_context) 1674 { 1675 adt_internal_state_t *state; 1676 int rc = -1; 1677 const au_tid64_addr_t *tid64; 1678 au_tid_addr_t termid, *tid; 1679 ucred_t *ucred = (ucred_t *)uc; 1680 boolean_t local_uc = B_FALSE; 1681 1682 if (session_data == NULL) /* no session exists to audit */ 1683 return (0); 1684 1685 state = (adt_internal_state_t *)session_data; 1686 assert(state->as_check == ADT_VALID); 1687 1688 if (ucred == NULL) { 1689 ucred = ucred_get(P_MYID); 1690 1691 if (ucred == NULL) 1692 goto return_rc; 1693 local_uc = B_TRUE; 1694 } 1695 1696 switch (user_context) { 1697 case ADT_NEW: 1698 tid64 = ucred_getatid(ucred); 1699 if (tid64 != NULL) { 1700 adt_cpy_tid(&termid, tid64); 1701 tid = &termid; 1702 } else { 1703 tid = NULL; 1704 } 1705 if (ucred_getauid(ucred) == AU_NOAUDITID) { 1706 adt_setto_unaudited(state); 1707 state->as_have_user_data = ADT_HAVE_ALL; 1708 rc = 0; 1709 goto return_rc; 1710 } else { 1711 state->as_info.ai_auid = ucred_getauid(ucred); 1712 state->as_info.ai_asid = ucred_getasid(ucred); 1713 state->as_info.ai_mask = *ucred_getamask(ucred); 1714 state->as_info.ai_termid = *tid; 1715 } 1716 state->as_have_user_data = ADT_HAVE_ALL; 1717 break; 1718 case ADT_UPDATE: 1719 if (state->as_have_user_data != ADT_HAVE_ALL) { 1720 errno = EINVAL; 1721 goto return_rc; 1722 } 1723 1724 if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0) 1725 goto return_rc; 1726 break; 1727 case ADT_USER: 1728 if (state->as_have_user_data != ADT_HAVE_ALL) { 1729 errno = EINVAL; 1730 goto return_rc; 1731 } 1732 break; 1733 default: 1734 errno = EINVAL; 1735 goto return_rc; 1736 } 1737 rc = 0; 1738 1739 state->as_ruid = ucred_getruid(ucred); 1740 state->as_euid = ucred_geteuid(ucred); 1741 state->as_rgid = ucred_getrgid(ucred); 1742 state->as_egid = ucred_getegid(ucred); 1743 state->as_pid = ucred_getpid(ucred); 1744 state->as_label = adt_ucred_label(ucred); 1745 1746 return_rc: 1747 if (local_uc) { 1748 ucred_free(ucred); 1749 } 1750 return (rc); 1751 } 1752 1753 /* 1754 * adt_alloc_event() returns a pointer to allocated memory 1755 * 1756 */ 1757 1758 adt_event_data_t 1759 *adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id) 1760 { 1761 struct adt_event_state *event_state; 1762 adt_internal_state_t *session_state; 1763 adt_event_data_t *return_event = NULL; 1764 /* 1765 * need to return a valid event pointer even if audit is 1766 * off, else the caller will end up either (1) keeping its 1767 * own flags for on/off or (2) writing to a NULL pointer. 1768 * If auditing is on, the session data must be valid; otherwise 1769 * we don't care. 1770 */ 1771 if (session_data != NULL) { 1772 session_state = (adt_internal_state_t *)session_data; 1773 assert(session_state->as_check == ADT_VALID); 1774 } 1775 event_state = calloc(1, sizeof (struct adt_event_state)); 1776 if (event_state == NULL) 1777 goto return_ptr; 1778 1779 event_state->ae_check = ADT_VALID; 1780 1781 event_state->ae_event_id = event_id; 1782 event_state->ae_session = (struct adt_internal_state *)session_data; 1783 1784 return_event = (adt_event_data_t *)&(event_state->ae_event_data); 1785 1786 /* 1787 * preload data so the adt_au_*() functions can detect un-supplied 1788 * values (0 and NULL are free via calloc()). 1789 */ 1790 adt_preload(event_id, return_event); 1791 1792 return_ptr: 1793 return (return_event); 1794 } 1795 1796 /* 1797 * adt_getXlateTable -- look up translation table address for event id 1798 */ 1799 1800 static struct translation * 1801 adt_getXlateTable(au_event_t event_id) 1802 { 1803 /* xlate_table is global in adt_xlate.c */ 1804 struct translation **p_xlate = &xlate_table[0]; 1805 struct translation *p_event; 1806 1807 while (*p_xlate != NULL) { 1808 p_event = *p_xlate; 1809 if (event_id == p_event->tx_external_event) 1810 return (p_event); 1811 p_xlate++; 1812 } 1813 return (NULL); 1814 } 1815 1816 /* 1817 * adt_calcOffsets 1818 * 1819 * the call to this function is surrounded by a mutex. 1820 * 1821 * i walks down the table picking up next_token. j walks again to 1822 * calculate the offset to the input data. k points to the next 1823 * token's row. Finally, l, is used to sum the values in the 1824 * datadef array. 1825 * 1826 * What's going on? The entry array is in the order of the input 1827 * fields but the processing of array entries is in the order of 1828 * the output (see next_token). Calculating the offset to the 1829 * "next" input can't be done in the outer loop (i) since i doesn't 1830 * point to the current entry and it can't be done with the k index 1831 * because it doesn't represent the order of input fields. 1832 * 1833 * While the resulting algorithm is n**2, it is only done once per 1834 * event type. 1835 */ 1836 1837 /* 1838 * adt_calcOffsets is only called once per event type, but it uses 1839 * the address alignment of memory allocated for that event as if it 1840 * were the same for all subsequently allocated memory. This is 1841 * guaranteed by calloc/malloc. Arrays take special handling since 1842 * what matters for figuring out the correct alignment is the size 1843 * of the array element. 1844 */ 1845 1846 static void 1847 adt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data) 1848 { 1849 int i, j; 1850 size_t this_size, prev_size; 1851 void *struct_start = p_data; 1852 1853 for (i = 0; i < tablesize; i++) { 1854 if (p_entry[i].en_type_def == NULL) { 1855 p_entry[i].en_offset = 0; 1856 continue; 1857 } 1858 prev_size = 0; 1859 p_entry[i].en_offset = (char *)p_data - (char *)struct_start; 1860 1861 for (j = 0; j < p_entry[i].en_count_types; j++) { 1862 if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG) 1863 this_size = sizeof (enum adt_generic); 1864 else 1865 this_size = 1866 p_entry[i].en_type_def[j].dd_input_size; 1867 1868 /* adj for first entry */ 1869 if (prev_size == 0) 1870 prev_size = this_size; 1871 1872 if (p_entry[i].en_type_def[j].dd_datatype == 1873 ADT_UINT32ARRAY) { 1874 p_data = (char *)adt_adjust_address(p_data, 1875 prev_size, sizeof (uint32_t)) + 1876 this_size - sizeof (uint32_t); 1877 1878 prev_size = sizeof (uint32_t); 1879 } else { 1880 p_data = adt_adjust_address(p_data, prev_size, 1881 this_size); 1882 prev_size = this_size; 1883 } 1884 } 1885 } 1886 } 1887 1888 /* 1889 * adt_generate_event 1890 * generate event record from external struct. The order is based on 1891 * the output tokens, allowing for the possibility that the input data 1892 * is in a different order. 1893 * 1894 */ 1895 1896 static void 1897 adt_generate_event(const adt_event_data_t *p_extdata, 1898 struct adt_event_state *p_event, 1899 struct translation *p_xlate) 1900 { 1901 struct entry *p_entry; 1902 static mutex_t lock = DEFAULTMUTEX; 1903 1904 p_entry = p_xlate->tx_first_entry; 1905 assert(p_entry != NULL); 1906 1907 p_event->ae_internal_id = p_xlate->tx_internal_event; 1908 adt_token_open(p_event); 1909 1910 /* 1911 * offsets are not pre-calculated; the initial offsets are all 1912 * 0; valid offsets are >= 0. Offsets for no-input tokens such 1913 * as subject are set to -1 by adt_calcOffset() 1914 */ 1915 if (p_xlate->tx_offsetsCalculated == 0) { 1916 (void) _mutex_lock(&lock); 1917 p_xlate->tx_offsetsCalculated = 1; 1918 1919 adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries, 1920 (void *)p_extdata); 1921 (void) _mutex_unlock(&lock); 1922 } 1923 while (p_entry != NULL) { 1924 adt_generate_token(p_entry, (char *)p_extdata, 1925 p_event); 1926 1927 p_entry = p_entry->en_next_token; 1928 } 1929 adt_token_close(p_event); 1930 } 1931 1932 /* 1933 * adt_put_event -- main event generation function. 1934 * The input "event" is the address of the struct containing 1935 * event-specific data. 1936 * 1937 * However if auditing is off or the session handle 1938 * is NULL, no attempt to write a record is made. 1939 */ 1940 1941 int 1942 adt_put_event(const adt_event_data_t *event, int status, int return_val) 1943 { 1944 struct adt_event_state *event_state; 1945 struct translation *xlate; 1946 int rc = 0; 1947 1948 if (event == NULL) { 1949 errno = EINVAL; 1950 rc = -1; 1951 goto return_rc; 1952 } 1953 event_state = (struct adt_event_state *)event; 1954 1955 /* if audit off or this is a broken session, exit */ 1956 if (auditstate == AUC_DISABLED || (event_state->ae_session == NULL)) 1957 goto return_rc; 1958 1959 assert(event_state->ae_check == ADT_VALID); 1960 1961 event_state->ae_rc = status; 1962 event_state->ae_type = return_val; 1963 1964 /* look up the event */ 1965 1966 xlate = adt_getXlateTable(event_state->ae_event_id); 1967 1968 if (xlate == NULL) { 1969 errno = EINVAL; 1970 rc = -1; 1971 goto return_rc; 1972 } 1973 DPRINTF(("got event %d\n", xlate->tx_internal_event)); 1974 1975 if (adt_selected(event_state, xlate->tx_internal_event, status)) 1976 adt_generate_event(event, event_state, xlate); 1977 1978 return_rc: 1979 return (rc); 1980 } 1981 1982 /* 1983 * adt_free_event -- invalidate and free 1984 */ 1985 1986 void 1987 adt_free_event(adt_event_data_t *event) 1988 { 1989 struct adt_event_state *event_state; 1990 1991 if (event == NULL) 1992 return; 1993 1994 event_state = (struct adt_event_state *)event; 1995 1996 assert(event_state->ae_check == ADT_VALID); 1997 1998 event_state->ae_check = 0; 1999 2000 free(event_state); 2001 } 2002 2003 /* 2004 * adt_is_selected -- helper to adt_selected(), below. 2005 * 2006 * "sorf" is "success or fail" status; au_preselect compares 2007 * that with success, fail, or both. 2008 */ 2009 2010 static int 2011 adt_is_selected(au_event_t e, au_mask_t *m, int sorf) 2012 { 2013 int prs_sorf; 2014 2015 if (sorf == 0) 2016 prs_sorf = AU_PRS_SUCCESS; 2017 else 2018 prs_sorf = AU_PRS_FAILURE; 2019 2020 return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD)); 2021 } 2022 2023 /* 2024 * selected -- see if this event is preselected. 2025 * 2026 * if errors are encountered trying to check a preselection mask 2027 * or look up a user name, the event is selected. Otherwise, the 2028 * preselection mask is used for the job. 2029 */ 2030 2031 static int 2032 adt_selected(struct adt_event_state *event, au_event_t actual_id, int status) 2033 { 2034 adt_internal_state_t *sp; 2035 au_mask_t namask; 2036 2037 sp = event->ae_session; 2038 2039 if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) { 2040 adt_write_syslog("No user data available", EINVAL); 2041 return (1); /* default is "selected" */ 2042 } 2043 2044 /* non-attributable? */ 2045 if ((sp->as_info.ai_auid == AU_NOAUDITID) || 2046 (sp->as_info.ai_auid == ADT_NO_AUDIT)) { 2047 if (auditon(A_GETKMASK, (caddr_t)&namask, 2048 sizeof (namask)) != 0) { 2049 adt_write_syslog("auditon failure", errno); 2050 return (1); 2051 } 2052 return (adt_is_selected(actual_id, &namask, status)); 2053 } else { 2054 return (adt_is_selected(actual_id, &(sp->as_info.ai_mask), 2055 status)); 2056 } 2057 } 2058