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 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 1988 AT&T 29 * All Rights Reserved 30 */ 31 32 /* 33 * PATH setup and search directory functions. 34 */ 35 36 #include <stdio.h> 37 #include <limits.h> 38 #include <fcntl.h> 39 #include <string.h> 40 #include <sys/systeminfo.h> 41 #include <debug.h> 42 #include <conv.h> 43 #include "_rtld.h" 44 #include "msg.h" 45 46 /* 47 * Given a search rule type, return a list of directories to search according 48 * to the specified rule. 49 */ 50 static Pnode * 51 get_dir_list(uchar_t rules, Rt_map *lmp, uint_t flags) 52 { 53 Pnode * dirlist = (Pnode *)0; 54 Lm_list * lml = LIST(lmp); 55 int search; 56 57 /* 58 * Determine whether ldd -s is in effect - ignore when we're searching 59 * for audit libraries as these will be added to their own link-map. 60 */ 61 if ((lml->lm_flags & LML_FLG_TRC_SEARCH) && 62 ((FLAGS1(lmp) & FL1_RT_LDDSTUB) == 0) && 63 ((flags & FLG_RT_AUDIT) == 0)) 64 search = 1; 65 else 66 search = 0; 67 68 switch (rules) { 69 case RPLENV: 70 /* 71 * Initialize the replaceable environment variable 72 * (LD_LIBRARY_PATH) search path list. Note, we always call 73 * Dbg_libs_path() so that every library lookup diagnostic can 74 * be preceded with the appropriate search path information. 75 */ 76 if (rpl_libpath) { 77 uint_t mode = (LA_SER_LIBPATH | PN_FLG_UNIQUE); 78 79 /* 80 * Note, this path may have originated from the users 81 * environment or from a configuration file. 82 */ 83 if (env_info & ENV_INF_PATHCFG) 84 mode |= LA_SER_CONFIG; 85 86 DBG_CALL(Dbg_libs_path(lml, rpl_libpath, mode, 87 config->c_name)); 88 89 /* 90 * For ldd(1) -s, indicate the search paths that'll 91 * be used. If this is a secure program then some 92 * search paths may be ignored, therefore reset the 93 * rpl_libdirs pointer each time so that the 94 * diagnostics related to these unsecure directories 95 * will be output for each image loaded. 96 */ 97 if (search) { 98 const char *fmt; 99 100 if (env_info & ENV_INF_PATHCFG) 101 fmt = MSG_INTL(MSG_LDD_PTH_LIBPATHC); 102 else 103 fmt = MSG_INTL(MSG_LDD_PTH_LIBPATH); 104 105 (void) printf(fmt, rpl_libpath, config->c_name); 106 } 107 if (rpl_libdirs && (rtld_flags & RT_FL_SECURE) && 108 (search || DBG_ENABLED)) { 109 free(rpl_libdirs); 110 rpl_libdirs = 0; 111 } 112 if (!rpl_libdirs) { 113 /* 114 * If this is a secure application we need to 115 * be selective over what directories we use. 116 */ 117 rpl_libdirs = expand_paths(lmp, rpl_libpath, 118 mode, PN_TKN_HWCAP); 119 } 120 dirlist = rpl_libdirs; 121 } 122 break; 123 case PRMENV: 124 /* 125 * Initialize the permanent (LD_LIBRARY_PATH) search path list. 126 * This can only originate from a configuration file. To be 127 * consistent with the debugging display of DEFENV (above), 128 * always call Dbg_libs_path(). 129 */ 130 if (prm_libpath) { 131 uint_t mode = 132 (LA_SER_LIBPATH | LA_SER_CONFIG | PN_FLG_UNIQUE); 133 134 DBG_CALL(Dbg_libs_path(lml, prm_libpath, mode, 135 config->c_name)); 136 137 /* 138 * For ldd(1) -s, indicate the search paths that'll 139 * be used. If this is a secure program then some 140 * search paths may be ignored, therefore reset the 141 * prm_libdirs pointer each time so that the 142 * diagnostics related to these unsecure directories 143 * will be output for each image loaded. 144 */ 145 if (search) 146 (void) printf(MSG_INTL(MSG_LDD_PTH_LIBPATHC), 147 prm_libpath, config->c_name); 148 if (prm_libdirs && (rtld_flags & RT_FL_SECURE) && 149 (search || DBG_ENABLED)) { 150 free(prm_libdirs); 151 prm_libdirs = 0; 152 } 153 if (!prm_libdirs) { 154 /* 155 * If this is a secure application we need to 156 * be selective over what directories we use. 157 */ 158 prm_libdirs = expand_paths(lmp, prm_libpath, 159 mode, PN_TKN_HWCAP); 160 } 161 dirlist = prm_libdirs; 162 } 163 break; 164 case RUNPATH: 165 /* 166 * Initialize the runpath search path list. To be consistent 167 * with the debugging display of DEFENV (above), always call 168 * Dbg_libs_path(). 169 */ 170 if (RPATH(lmp)) { 171 DBG_CALL(Dbg_libs_path(lml, RPATH(lmp), LA_SER_RUNPATH, 172 NAME(lmp))); 173 174 /* 175 * For ldd(1) -s, indicate the search paths that'll 176 * be used. If this is a secure program then some 177 * search paths may be ignored, therefore reset the 178 * runlist pointer each time so that the diagnostics 179 * related to these unsecure directories will be 180 * output for each image loaded. 181 */ 182 if (search) 183 (void) printf(MSG_INTL(MSG_LDD_PTH_RUNPATH), 184 RPATH(lmp), NAME(lmp)); 185 if (RLIST(lmp) && (rtld_flags & RT_FL_SECURE) && 186 (search || DBG_ENABLED)) { 187 free(RLIST(lmp)); 188 RLIST(lmp) = 0; 189 } 190 if (!(RLIST(lmp))) 191 /* 192 * If this is a secure application we need to 193 * be selective over what directories we use. 194 */ 195 RLIST(lmp) = expand_paths(lmp, RPATH(lmp), 196 LA_SER_RUNPATH, PN_TKN_HWCAP); 197 dirlist = RLIST(lmp); 198 } 199 break; 200 case DEFAULT: 201 if ((FLAGS1(lmp) & FL1_RT_NODEFLIB) == 0) { 202 if ((rtld_flags & RT_FL_SECURE) && 203 (flags & (FLG_RT_PRELOAD | FLG_RT_AUDIT))) 204 dirlist = LM_SECURE_DIRS(lmp); 205 else 206 dirlist = LM_DFLT_DIRS(lmp); 207 } 208 209 /* 210 * For ldd(1) -s, indicate the default paths that'll be used. 211 */ 212 if (dirlist && (search || DBG_ENABLED)) { 213 Pnode * pnp = dirlist; 214 int num = 0; 215 216 if (search) 217 (void) printf(MSG_INTL(MSG_LDD_PTH_BGNDFL)); 218 for (; pnp && pnp->p_name; pnp = pnp->p_next, num++) { 219 if (search) { 220 const char *fmt; 221 222 if (num) { 223 fmt = 224 MSG_ORIG(MSG_LDD_FMT_PATHN); 225 } else { 226 fmt = 227 MSG_ORIG(MSG_LDD_FMT_PATH1); 228 } 229 (void) printf(fmt, pnp->p_name); 230 } else 231 DBG_CALL(Dbg_libs_path(lml, pnp->p_name, 232 pnp->p_orig, config->c_name)); 233 } 234 /* BEGIN CSTYLED */ 235 if (search) { 236 if (dirlist->p_orig & LA_SER_CONFIG) 237 (void) printf( 238 MSG_INTL(MSG_LDD_PTH_ENDDFLC), 239 config->c_name); 240 else 241 (void) printf( 242 MSG_INTL(MSG_LDD_PTH_ENDDFL)); 243 } 244 /* END CSTYLED */ 245 } 246 break; 247 default: 248 break; 249 } 250 return (dirlist); 251 } 252 253 /* 254 * Get the next dir in the search rules path. 255 */ 256 Pnode * 257 get_next_dir(Pnode ** dirlist, Rt_map * lmp, uint_t flags) 258 { 259 static unsigned char *rules = NULL; 260 261 /* 262 * Search rules consist of one or more directories names. If this is a 263 * new search, then start at the beginning of the search rules. 264 * Otherwise traverse the list of directories that make up the rule. 265 */ 266 if (!*dirlist) { 267 rules = search_rules; 268 } else { 269 if ((*dirlist = (*dirlist)->p_next) != 0) 270 return (*dirlist); 271 else 272 rules++; 273 } 274 275 while (*rules) { 276 if ((*dirlist = get_dir_list(*rules, lmp, flags)) != 0) 277 return (*dirlist); 278 else 279 rules++; 280 } 281 282 /* 283 * If we got here, no more directories to search, return NULL. 284 */ 285 return (NULL); 286 } 287 288 /* 289 * Process a directory (runpath) or filename (needed or filter) string looking 290 * for tokens to expand. Allocate a new buffer for the string. 291 */ 292 uint_t 293 expand(char **name, size_t *len, char **list, uint_t orig, uint_t omit, 294 Rt_map *lmp) 295 { 296 char _name[PATH_MAX]; 297 char *token = 0, *oname, *optr, *_optr, *nptr, * _list; 298 size_t olen = 0, nlen = 0, _len; 299 int isaflag = 0; 300 uint_t flags = 0; 301 Lm_list *lml = LIST(lmp); 302 303 optr = _optr = oname = *name; 304 nptr = _name; 305 306 while ((olen < *len) && (nlen < PATH_MAX)) { 307 uint_t _flags; 308 309 if ((*optr != '$') || ((olen - *len) == 1)) { 310 /* 311 * When expanding paths while a configuration file 312 * exists that contains directory information, determine 313 * whether the path contains "./". If so, we'll resolve 314 * the path later to remove these relative entries. 315 */ 316 if ((rtld_flags & RT_FL_DIRCFG) && 317 (orig & LA_SER_MASK) && (*optr == '/') && 318 (optr != oname) && (*(optr - 1) == '.')) 319 flags |= TKN_DOTSLASH; 320 321 olen++, optr++; 322 continue; 323 } 324 325 /* 326 * Copy any string we've presently passed over to the new 327 * buffer. 328 */ 329 if ((_len = (optr - _optr)) != 0) { 330 if ((nlen += _len) < PATH_MAX) { 331 (void) strncpy(nptr, _optr, _len); 332 nptr = nptr + _len; 333 } else { 334 eprintf(lml, ERR_FATAL, 335 MSG_INTL(MSG_ERR_EXPAND1), NAME(lmp), 336 oname); 337 return (0); 338 } 339 } 340 341 /* 342 * Skip the token delimiter and determine if a reserved token 343 * match is found. 344 */ 345 olen++, optr++; 346 _flags = 0; 347 token = 0; 348 349 if (strncmp(optr, MSG_ORIG(MSG_TKN_ORIGIN), 350 MSG_TKN_ORIGIN_SIZE) == 0) { 351 token = (char *)MSG_ORIG(MSG_TKN_ORIGIN); 352 353 /* 354 * $ORIGIN expansion is required. Determine this 355 * objects basename. Expansion of $ORIGIN is allowed 356 * for secure applications but must be checked by the 357 * caller to insure the expanded path matches a 358 * registered secure name. 359 */ 360 if (((omit & PN_TKN_ORIGIN) == 0) && 361 (((_len = DIRSZ(lmp)) != 0) || 362 ((_len = fullpath(lmp, 0)) != 0))) { 363 if ((nlen += _len) < PATH_MAX) { 364 (void) strncpy(nptr, 365 ORIGNAME(lmp), _len); 366 nptr = nptr +_len; 367 olen += MSG_TKN_ORIGIN_SIZE; 368 optr += MSG_TKN_ORIGIN_SIZE; 369 _flags |= PN_TKN_ORIGIN; 370 } else { 371 eprintf(lml, ERR_FATAL, 372 MSG_INTL(MSG_ERR_EXPAND1), 373 NAME(lmp), oname); 374 return (0); 375 } 376 } 377 378 } else if (strncmp(optr, MSG_ORIG(MSG_TKN_PLATFORM), 379 MSG_TKN_PLATFORM_SIZE) == 0) { 380 token = (char *)MSG_ORIG(MSG_TKN_PLATFORM); 381 382 /* 383 * $PLATFORM expansion required. This would have been 384 * established from the AT_SUN_PLATFORM aux vector, but 385 * if not attempt to get it from sysconf(). 386 */ 387 if (((omit & PN_TKN_PLATFORM) == 0) && 388 ((platform == 0) && (platform_sz == 0))) { 389 char _info[SYS_NMLN]; 390 long _size; 391 392 _size = sysinfo(SI_PLATFORM, _info, SYS_NMLN); 393 if ((_size != -1) && 394 ((platform = malloc((size_t)_size)) != 0)) { 395 (void) strcpy(platform, _info); 396 platform_sz = (size_t)_size - 1; 397 } 398 } 399 if (((omit & PN_TKN_PLATFORM) == 0) && 400 (platform != 0)) { 401 if ((nlen += platform_sz) < PATH_MAX) { 402 (void) strncpy(nptr, platform, 403 platform_sz); 404 nptr = nptr + platform_sz; 405 olen += MSG_TKN_PLATFORM_SIZE; 406 optr += MSG_TKN_PLATFORM_SIZE; 407 _flags |= PN_TKN_PLATFORM; 408 } else { 409 eprintf(lml, ERR_FATAL, 410 MSG_INTL(MSG_ERR_EXPAND1), 411 NAME(lmp), oname); 412 return (0); 413 } 414 } 415 416 } else if (strncmp(optr, MSG_ORIG(MSG_TKN_OSNAME), 417 MSG_TKN_OSNAME_SIZE) == 0) { 418 token = (char *)MSG_ORIG(MSG_TKN_OSNAME); 419 420 /* 421 * $OSNAME expansion required. This is established 422 * from the sysname[] returned by uname(2). 423 */ 424 if (((omit & PN_TKN_OSNAME) == 0) && (uts == 0)) 425 uts = conv_uts(); 426 427 if (((omit & PN_TKN_OSNAME) == 0) && 428 (uts && uts->uts_osnamesz)) { 429 if ((nlen += uts->uts_osnamesz) < PATH_MAX) { 430 (void) strncpy(nptr, uts->uts_osname, 431 uts->uts_osnamesz); 432 nptr = nptr + uts->uts_osnamesz; 433 olen += MSG_TKN_OSNAME_SIZE; 434 optr += MSG_TKN_OSNAME_SIZE; 435 _flags |= PN_TKN_OSNAME; 436 } else { 437 eprintf(lml, ERR_FATAL, 438 MSG_INTL(MSG_ERR_EXPAND1), 439 NAME(lmp), oname); 440 return (0); 441 } 442 } 443 444 } else if (strncmp(optr, MSG_ORIG(MSG_TKN_OSREL), 445 MSG_TKN_OSREL_SIZE) == 0) { 446 token = (char *)MSG_ORIG(MSG_TKN_OSREL); 447 448 /* 449 * $OSREL expansion required. This is established 450 * from the release[] returned by uname(2). 451 */ 452 if (((omit & PN_TKN_OSREL) == 0) && (uts == 0)) 453 uts = conv_uts(); 454 455 if (((omit & PN_TKN_OSREL) == 0) && 456 (uts && uts->uts_osrelsz)) { 457 if ((nlen += uts->uts_osrelsz) < PATH_MAX) { 458 (void) strncpy(nptr, uts->uts_osrel, 459 uts->uts_osrelsz); 460 nptr = nptr + uts->uts_osrelsz; 461 olen += MSG_TKN_OSREL_SIZE; 462 optr += MSG_TKN_OSREL_SIZE; 463 _flags |= PN_TKN_OSREL; 464 } else { 465 eprintf(lml, ERR_FATAL, 466 MSG_INTL(MSG_ERR_EXPAND1), 467 NAME(lmp), oname); 468 return (0); 469 } 470 } 471 472 } else if ((strncmp(optr, MSG_ORIG(MSG_TKN_ISALIST), 473 MSG_TKN_ISALIST_SIZE) == 0)) { 474 int ok; 475 token = (char *)MSG_ORIG(MSG_TKN_ISALIST); 476 477 /* 478 * $ISALIST expansion required. When accompanied with 479 * a list pointer, this routine updates that pointer 480 * with the new list of potential candidates. Without 481 * this list pointer, only the first expansion is 482 * provided. NOTE, that two $ISLIST expansions within 483 * the same path aren't supported. 484 */ 485 if ((omit & PN_TKN_ISALIST) || isaflag++) 486 ok = 0; 487 else 488 ok = 1; 489 490 if (ok && (isa == 0)) 491 isa = conv_isalist(); 492 493 if (ok && isa && isa->isa_listsz) { 494 size_t no, mlen, tlen, hlen = olen - 1; 495 char *lptr; 496 Isa_opt *opt = isa->isa_opt; 497 498 if ((nlen += opt->isa_namesz) < PATH_MAX) { 499 (void) strncpy(nptr, opt->isa_name, 500 opt->isa_namesz); 501 nptr = nptr + opt->isa_namesz; 502 olen += MSG_TKN_ISALIST_SIZE; 503 optr += MSG_TKN_ISALIST_SIZE; 504 _flags |= PN_TKN_ISALIST; 505 } else { 506 eprintf(lml, ERR_FATAL, 507 MSG_INTL(MSG_ERR_EXPAND1), 508 NAME(lmp), oname); 509 return (0); 510 } 511 512 if (list) { 513 tlen = *len - olen; 514 mlen = ((hlen + tlen) * 515 (isa->isa_optno - 1)) + 516 isa->isa_listsz - opt->isa_namesz + 517 strlen(*list); 518 if ((_list = lptr = malloc(mlen)) == 0) 519 return (0); 520 521 for (no = 1, opt++; no < isa->isa_optno; 522 no++, opt++) { 523 (void) strncpy(lptr, *name, 524 hlen); 525 lptr = lptr + hlen; 526 (void) strncpy(lptr, 527 opt->isa_name, 528 opt->isa_namesz); 529 lptr = lptr + opt->isa_namesz; 530 (void) strncpy(lptr, optr, 531 tlen); 532 lptr = lptr + tlen; 533 *lptr++ = ':'; 534 } 535 if (**list) 536 (void) strcpy(lptr, *list); 537 else 538 *--lptr = '\0'; 539 } 540 } 541 542 } else if (strncmp(optr, MSG_ORIG(MSG_TKN_HWCAP), 543 MSG_TKN_HWCAP_SIZE) == 0) { 544 char *bptr = nptr - 1; 545 char *eptr = optr + MSG_TKN_HWCAP_SIZE; 546 token = (char *)MSG_ORIG(MSG_TKN_HWCAP); 547 548 /* 549 * $HWCAP expansion required. For compatibility with 550 * older environments, only expand this token when hard- 551 * ware capability information is available. This 552 * expansion is only allowed for non-simple pathnames 553 * (must contain a '/'), with the token itself being the 554 * last element of the path. Therefore, all we need do 555 * is test the existence of the string "/$HWCAP\0". 556 */ 557 if (((omit & PN_TKN_HWCAP) == 0) && 558 (rtld_flags2 & RT_FL2_HWCAP) && 559 ((bptr > _name) && (*bptr == '/') && 560 ((*eptr == '\0') || (*eptr == ':')))) { 561 /* 562 * Decrement the present pointer so that the 563 * directories trailing "/" gets nuked later. 564 */ 565 nptr--, nlen--; 566 olen += MSG_TKN_HWCAP_SIZE; 567 optr += MSG_TKN_HWCAP_SIZE; 568 _flags |= PN_TKN_HWCAP; 569 } 570 571 } else { 572 /* 573 * If reserved token was not found, copy the 574 * character. 575 */ 576 *nptr++ = '$'; 577 nlen++; 578 } 579 580 /* 581 * If reserved token was found, and could not be expanded, 582 * diagnose the error condition. 583 */ 584 if (token) { 585 if (_flags) 586 flags |= _flags; 587 else { 588 char buf[PATH_MAX], *str; 589 590 /* 591 * Note, the original string we're expanding 592 * might contain a number of ':' separated 593 * paths. Isolate the path we're processing to 594 * provide a more precise error diagnostic. 595 */ 596 if (str = strchr(oname, ':')) { 597 size_t slen = str - oname; 598 599 (void) strncpy(buf, oname, slen); 600 buf[slen] = '\0'; 601 str = buf; 602 } else 603 str = oname; 604 605 eprintf(lml, ERR_FATAL, 606 MSG_INTL(MSG_ERR_EXPAND2), NAME(lmp), 607 str, token); 608 return (0); 609 } 610 } 611 _optr = optr; 612 } 613 614 /* 615 * First make sure the current length is shorter than PATH_MAX. We may 616 * arrive here if the given path contains '$' characters which are not 617 * the lead of a reserved token. 618 */ 619 if (nlen >= PATH_MAX) { 620 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ERR_EXPAND1), NAME(lmp), 621 oname); 622 return (0); 623 } 624 625 /* 626 * If any ISALIST processing has occurred not only do we return the 627 * expanded node we're presently working on, but we can also update the 628 * remaining list so that it is effectively prepended with this node 629 * expanded to all remaining ISALIST options. Note that we can only 630 * handle one ISALIST per node. For more than one ISALIST to be 631 * processed we'd need a better algorithm than above to replace the 632 * newly generated list. Whether we want to encourage the number of 633 * pathname permutations this would provide is another question. So, for 634 * now if more than one ISALIST is encountered we return the original 635 * node untouched. 636 */ 637 if (isa && isaflag) { 638 if (isaflag == 1) { 639 if (list) 640 *list = _list; 641 } else { 642 flags &= ~PN_TKN_ISALIST; 643 644 if ((nptr = calloc(1, (*len + 1))) == 0) 645 return (0); 646 (void) strncpy(nptr, *name, *len); 647 *name = nptr; 648 649 return (TKN_NONE); 650 } 651 } 652 653 /* 654 * Copy any remaining string. Terminate the new string with a null as 655 * this string can be displayed via debugging diagnostics. 656 */ 657 if ((_len = (optr - _optr)) != 0) { 658 if ((nlen += _len) < PATH_MAX) { 659 (void) strncpy(nptr, _optr, _len); 660 nptr = nptr + _len; 661 } else { 662 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ERR_EXPAND1), 663 NAME(lmp), oname); 664 return (0); 665 } 666 } 667 *nptr = '\0'; 668 669 /* 670 * A path that has been expanded, is typically used to create full 671 * pathnames for objects that will be opened. The final pathname is 672 * resolved to simplify it, and set the stage for possible $ORIGIN 673 * processing. Therefore, it's usually unnecessary to resolve the path 674 * at this point. However, if a configuration file, containing 675 * directory information is in use, then we might need to lookup this 676 * path in the configuration file. To keep the number of pathname 677 * resolutions to a minimum, only resolve paths that contain "./". The 678 * use of "$ORIGIN/../lib" will probably only match a configuration file 679 * entry after resolution. 680 */ 681 if (list && ((rtld_flags & (RT_FL_DIRCFG | RT_FL_EXECNAME)) == 682 (RT_FL_DIRCFG | RT_FL_EXECNAME)) && (flags & TKN_DOTSLASH)) { 683 int len; 684 685 if ((len = resolvepath(_name, _name, (PATH_MAX - 1))) >= 0) { 686 nlen = (size_t)len; 687 _name[nlen] = '\0'; 688 } 689 } 690 691 /* 692 * Allocate permanent storage for the new string and return to the user. 693 */ 694 if ((nptr = malloc(nlen + 1)) == 0) 695 return (0); 696 (void) strcpy(nptr, _name); 697 *name = nptr; 698 *len = nlen; 699 700 /* 701 * Return an indication of any token expansion that may have occurred. 702 * Under security, any pathname expanded with the $ORIGIN token must be 703 * validated against any registered secure directories. 704 */ 705 return (flags ? flags : TKN_NONE); 706 } 707 708 /* 709 * Determine whether a pathname is secure. 710 */ 711 static int 712 is_path_secure(char *opath, Rt_map *clmp, uint_t info, uint_t flags) 713 { 714 Pnode *sdir = LM_SECURE_DIRS(LIST(clmp)->lm_head); 715 char buffer[PATH_MAX], *npath; 716 Lm_list *lml = LIST(clmp); 717 718 /* 719 * If a pathname originates from a configuration file, use it. The use 720 * of a configuration file is already validated for secure applications, 721 * so if we're using a configuration file, we must be able to use all 722 * that it defines. 723 */ 724 if (info & LA_SER_CONFIG) 725 return (1); 726 727 if ((info & LA_SER_MASK) == 0) { 728 char *str; 729 730 /* 731 * If the pathname specifies a file (rather than a directory), 732 * peel off the file before making the comparison. 733 */ 734 str = strrchr(opath, '/'); 735 736 /* 737 * Carry out some initial security checks. 738 * 739 * . a simple file name (one containing no "/") is fine, as 740 * this file name will be combined with search paths to 741 * determine the complete path. 742 * . a full path (one starting with "/") is fine, provided 743 * this path name isn't a preload/audit path. 744 * . provided $ORIGIN expansion has not been employed, the 745 * above categories of path are deemed secure. 746 */ 747 if (((str == 0) || ((*opath == '/') && (str != opath) && 748 ((info & PN_FLG_EXTLOAD) == 0))) && 749 ((flags & PN_TKN_ORIGIN) == 0)) 750 return (1); 751 752 /* 753 * Determine the directory name of the present path. 754 */ 755 if (str == opath) 756 npath = (char *)MSG_ORIG(MSG_STR_SLASH); 757 else { 758 size_t size; 759 760 if ((size = str - opath) >= PATH_MAX) 761 return (0); 762 763 (void) strncpy(buffer, opath, size); 764 buffer[size] = '\0'; 765 npath = buffer; 766 } 767 768 /* 769 * If $ORIGIN processing has been employed, then allow any 770 * directory that has already been used to satisfy other 771 * dependencies, to be used. 772 */ 773 if ((flags & PN_TKN_ORIGIN) && spavl_recorded(npath, 0)) { 774 DBG_CALL(Dbg_libs_insecure(lml, npath, 1)); 775 return (1); 776 } 777 778 } else { 779 /* 780 * A search path, i.e., RPATH, configuration file path, etc. is 781 * used as is. Exceptions to this are: 782 * 783 * . LD_LIBRARY_PATH. 784 * . any $ORIGIN expansion, unless used by a setuid ld.so.1 785 * to find its own dependencies, or the path name has 786 * already been used to find other dependencies. 787 * . any relative path. 788 */ 789 if (((info & LA_SER_LIBPATH) == 0) && (*opath == '/') && 790 ((flags & PN_TKN_ORIGIN) == 0)) 791 return (1); 792 793 /* 794 * If $ORIGIN processing is requested, allow a setuid ld.so.1 795 * to use this path for its own dependencies. Allow the 796 * application to use this path name only if the path name has 797 * already been used to locate other dependencies. 798 */ 799 if (flags & PN_TKN_ORIGIN) { 800 if ((lml->lm_flags & LML_FLG_RTLDLM) && 801 is_rtld_setuid()) 802 return (1); 803 else if (spavl_recorded(opath, 0)) { 804 DBG_CALL(Dbg_libs_insecure(lml, opath, 1)); 805 return (1); 806 } 807 } 808 npath = (char *)opath; 809 } 810 811 while (sdir) { 812 if (strcmp(npath, sdir->p_name) == 0) 813 return (1); 814 sdir = sdir->p_next; 815 } 816 817 /* 818 * The path is insecure, so depending on the caller, provide a 819 * diagnostic. Preloaded, or audit libraries generate a warning, as 820 * the process will run without them. 821 */ 822 if (info & PN_FLG_EXTLOAD) { 823 if (lml->lm_flags & LML_FLG_TRC_ENABLE) { 824 if ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0) 825 (void) printf(MSG_INTL(MSG_LDD_FIL_ILLEGAL), 826 opath); 827 } else 828 eprintf(lml, ERR_WARNING, MSG_INTL(MSG_SEC_ILLEGAL), 829 opath); 830 831 return (0); 832 } 833 834 /* 835 * Explicit file references are fatal. 836 */ 837 if ((info & LA_SER_MASK) == 0) { 838 if (lml->lm_flags & LML_FLG_TRC_ENABLE) { 839 /* BEGIN CSTYLED */ 840 if ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0) { 841 if (lml->lm_flags & 842 (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) 843 (void) printf( 844 MSG_INTL(MSG_LDD_FIL_FIND), 845 opath, NAME(clmp)); 846 847 if (((rtld_flags & RT_FL_SILENCERR) == 0) || 848 (lml->lm_flags & LML_FLG_TRC_VERBOSE)) 849 (void) printf( 850 MSG_INTL(MSG_LDD_FIL_ILLEGAL), 851 opath); 852 } 853 /* END CSTYLED */ 854 } else 855 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), opath, 856 strerror(EACCES)); 857 } else { 858 /* 859 * Search paths. 860 */ 861 DBG_CALL(Dbg_libs_insecure(lml, opath, 0)); 862 if ((lml->lm_flags & LML_FLG_TRC_SEARCH) && 863 ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) 864 (void) printf(MSG_INTL(MSG_LDD_PTH_IGNORE), opath); 865 } 866 return (0); 867 } 868 869 /* 870 * Determine whether a path already exists within the callers Pnode list. 871 */ 872 inline static uint_t 873 is_path_unique(Pnode *pnp, const char *path) 874 { 875 for (; pnp; pnp = pnp->p_next) { 876 if (pnp->p_len && (strcmp(pnp->p_name, path) == 0)) 877 return (PN_FLG_DUPLICAT); 878 } 879 return (0); 880 } 881 882 /* 883 * Expand one or more pathnames. This routine is called for all path strings, 884 * i.e., NEEDED, rpaths, default search paths, configuration file search paths, 885 * filtees, etc. The path may be a single pathname, or a colon separated list 886 * of pathnames. Each individual pathname is processed for possible reserved 887 * token expansion. All string nodes are maintained in allocated memory 888 * (regardless of whether they are constant (":"), or token expanded) to 889 * simplify pnode removal. 890 * 891 * The info argument passes in auxiliary information regarding the callers 892 * intended use of the pathnames. This information may be maintained in the 893 * pnode element produced to describe the pathname (i.e., LA_SER_LIBPATH etc.), 894 * or may be used to determine additional security or diagnostic processing. 895 */ 896 Pnode * 897 expand_paths(Rt_map *clmp, const char *list, uint_t orig, uint_t omit) 898 { 899 char *str, *olist = 0, *nlist = (char *)list; 900 Pnode *pnp, *npnp, *opnp; 901 int fnull = FALSE; /* TRUE if empty final path segment seen */ 902 uint_t unique = 0; 903 904 for (pnp = opnp = 0, str = nlist; *nlist || fnull; str = nlist) { 905 char *ostr; 906 size_t len, olen; 907 uint_t tkns = 0; 908 909 if (*nlist == ';') 910 ++nlist, ++str; 911 if ((*nlist == ':') || fnull) { 912 /* If not a final null segment, check following one */ 913 fnull = !(fnull || *(nlist + 1)); 914 915 if (*nlist) 916 nlist++; 917 918 /* 919 * When the shell sees a null PATH segment, it 920 * treats it as if it were the cwd (.). We mimic 921 * this behavior for LD_LIBRARY_PATH and runpaths 922 * (mainly for backwards compatibility with previous 923 * behavior). For other paths, this makes no sense, 924 * so we simply ignore the segment. 925 */ 926 if (!(orig & (LA_SER_LIBPATH | LA_SER_RUNPATH))) 927 continue; /* Process next segment */ 928 929 if ((str = strdup(MSG_ORIG(MSG_FMT_CWD))) == NULL) 930 return (NULL); 931 len = MSG_FMT_CWD_SIZE; 932 933 } else { 934 char *elist; 935 936 len = 0; 937 while (*nlist && (*nlist != ':') && (*nlist != ';')) { 938 nlist++, len++; 939 } 940 941 /* Check for a following final null segment */ 942 fnull = (*nlist == ':') && !*(nlist + 1); 943 944 if (*nlist) 945 nlist++; 946 947 /* 948 * Expand the captured string. Besides expanding the 949 * present path/file entry, we may have a new list to 950 * deal with (ISALIST expands to multiple new entries). 951 */ 952 elist = nlist; 953 ostr = str; 954 olen = len; 955 if ((tkns = expand(&str, &len, &elist, orig, omit, 956 clmp)) == 0) 957 continue; 958 959 if (elist != nlist) { 960 if (olist) 961 free(olist); 962 nlist = olist = elist; 963 } 964 } 965 966 /* 967 * If this a secure application, validation of the expanded 968 * pathname may be necessary. 969 */ 970 if (rtld_flags & RT_FL_SECURE) { 971 if (is_path_secure(str, clmp, orig, tkns) == 0) { 972 free(str); 973 continue; 974 } 975 } 976 977 /* 978 * If required, ensure that the string is unique. For search 979 * paths such as LD_LIBRARY_PATH, users often inherit multiple 980 * paths which result in unnecessary duplication. Note, if 981 * we're debugging, any duplicate entry is retained and flagged 982 * so that the entry can be diagnosed later as part of unused 983 * processing. 984 */ 985 if (orig & PN_FLG_UNIQUE) { 986 Word tracing; 987 988 tracing = LIST(clmp)->lm_flags & 989 (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED); 990 unique = is_path_unique(pnp, str); 991 992 /* 993 * Note, use the debug strings rpl_debug and prm_debug 994 * as an indicator that debugging has been requested, 995 * rather than DBG_ENABLE(), as the initial use of 996 * LD_LIBRARY_PATH occurs in preparation for loading 997 * our debugging library. 998 */ 999 if ((unique == PN_FLG_DUPLICAT) && (tracing == 0) && 1000 (rpl_debug == 0) && (prm_debug == 0)) { 1001 free(str); 1002 continue; 1003 } 1004 } 1005 1006 /* 1007 * Allocate a new Pnode for this string. 1008 */ 1009 if ((npnp = calloc(1, sizeof (Pnode))) == 0) { 1010 free(str); 1011 return (NULL); 1012 } 1013 if (opnp == 0) 1014 pnp = npnp; 1015 else 1016 opnp->p_next = npnp; 1017 1018 if (tkns & PN_TKN_MASK) { 1019 char *oname; 1020 1021 /* 1022 * If this is a pathname, and any token expansion 1023 * occurred, maintain the original string for possible 1024 * diagnostic use. 1025 */ 1026 if ((oname = malloc(olen + 1)) == 0) { 1027 free(str); 1028 return (NULL); 1029 } 1030 (void) strncpy(oname, ostr, olen); 1031 oname[olen] = '\0'; 1032 npnp->p_oname = oname; 1033 } 1034 npnp->p_name = str; 1035 npnp->p_len = len; 1036 npnp->p_orig = (orig & LA_SER_MASK) | unique | 1037 (tkns & PN_TKN_MASK); 1038 1039 opnp = npnp; 1040 } 1041 1042 if (olist) 1043 free(olist); 1044 1045 return (pnp); 1046 } 1047