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