xref: /titanic_50/usr/src/cmd/sgs/rtld/common/cap.c (revision 532877c46d04a2d0b254f9b5797720078adcea07)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include	"_synonyms.h"
29 
30 #include	<sys/types.h>
31 #include	<sys/mman.h>
32 #include	<dirent.h>
33 #include	<stdio.h>
34 #include	<string.h>
35 #include	<limits.h>
36 #include	<debug.h>
37 #include	<conv.h>
38 #include	"_rtld.h"
39 #include	"_audit.h"
40 #include	"msg.h"
41 
42 /*
43  * qsort(3c) comparison function.
44  */
45 static int
46 compare(const void *fdesc1, const void *fdesc2)
47 {
48 	ulong_t	hwcap1 = ((Fdesc *)fdesc1)->fd_fmap.fm_hwptr;
49 	ulong_t	hwcap2 = ((Fdesc *)fdesc2)->fd_fmap.fm_hwptr;
50 
51 	if (hwcap1 && (hwcap2 == 0))
52 		return (-1);
53 	if ((hwcap1 == 0) && hwcap2)
54 		return (1);
55 	if ((hwcap1 == 0) && (hwcap2 == 0))
56 		return (0);
57 
58 	if (hwcap1 > hwcap2)
59 		return (-1);
60 	if (hwcap1 < hwcap2)
61 		return (1);
62 	return (0);
63 }
64 
65 /*
66  * If this object defines a set of hardware capability requirements, insure the
67  * kernal can cope with them.
68  */
69 int
70 hwcap_check(Rej_desc *rej, Ehdr *ehdr)
71 {
72 	Cap	*cptr;
73 	Phdr	*phdr;
74 	int	cnt;
75 
76 	/* LINTED */
77 	phdr = (Phdr *)((char *)ehdr + ehdr->e_phoff);
78 	for (cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) {
79 		Lword	val;
80 
81 		if (phdr->p_type != PT_SUNWCAP)
82 			continue;
83 
84 		/* LINTED */
85 		cptr = (Cap *)((char *)ehdr + phdr->p_offset);
86 		while (cptr->c_tag != CA_SUNW_NULL) {
87 			if (cptr->c_tag == CA_SUNW_HW_1)
88 				break;
89 			cptr++;
90 		}
91 		if (cptr->c_tag == CA_SUNW_NULL)
92 			break;
93 
94 		if ((val = (cptr->c_un.c_val & ~hwcap)) != 0) {
95 			rej->rej_type = SGS_REJ_HWCAP_1;
96 			rej->rej_str = conv_cap_val_hw1(val, M_MACH);
97 			return (0);
98 		}
99 
100 		/*
101 		 * Retain this hardware capabilities pointer for possible later
102 		 * inspection should this object be processed as a filtee.
103 		 */
104 		fmap->fm_hwptr = cptr->c_un.c_val;
105 	}
106 	return (1);
107 }
108 
109 static void
110 remove_fdesc(Fdesc *fdp)
111 {
112 #if	defined(MAP_ALIGN)
113 	if (fdp->fd_fmap.fm_maddr &&
114 	    ((fdp->fd_fmap.fm_mflags & MAP_ALIGN) == 0)) {
115 #else
116 	if (fdp->fd_fmap.fm_maddr) {
117 #endif
118 		(void) munmap(fdp->fd_fmap.fm_maddr, fdp->fd_fmap.fm_msize);
119 
120 		/*
121 		 * Note, this file descriptor might be duplicating information
122 		 * from the global fmap descriptor.  If so, clean up the global
123 		 * descriptor to prevent a duplicate (unnecessary) unmap.
124 		 */
125 		if (fmap->fm_maddr == fdp->fd_fmap.fm_maddr) {
126 			fmap->fm_maddr = 0;
127 			fmap_setup();
128 		}
129 	}
130 	if (fdp->fd_fd)
131 		(void) close(fdp->fd_fd);
132 	if (fdp->fd_pname && (fdp->fd_pname != fdp->fd_nname))
133 		free((void *)fdp->fd_pname);
134 	if (fdp->fd_nname)
135 		free((void *)fdp->fd_nname);
136 }
137 
138 /*
139  * When $HWCAP is used to represent dependencies, take the associated directory
140  * and analyze all the files it contains.
141  */
142 int
143 hwcap_dir(Alist **fdalpp, Lm_list *lml, const char *name, Rt_map *clmp,
144     uint_t flags, Rej_desc *rej)
145 {
146 	char		path[PATH_MAX], *dst;
147 	const char	*src;
148 	DIR		*dir;
149 	struct dirent	*dirent;
150 	Aliste		off;
151 	Alist		*fdalp = 0;
152 	Fdesc		*fdp;
153 	int		error = 0;
154 
155 	/*
156 	 * Access the directory in preparation for reading its entries.  If
157 	 * successful, establish the initial pathname.
158 	 */
159 	if ((dir = opendir(name)) == 0) {
160 		Rej_desc	_rej = { 0 };
161 
162 		_rej.rej_type = SGS_REJ_STR;
163 		_rej.rej_name = name;
164 		_rej.rej_str = strerror(errno);
165 		DBG_CALL(Dbg_file_rejected(lml, &_rej));
166 		rejection_inherit(rej, &_rej, 0);
167 		return (0);
168 	}
169 
170 	for (dst = path, src = name; *src; dst++, src++)
171 		*dst = *src;
172 	*dst++ = '/';
173 
174 	/*
175 	 * Read each entry from the directory and determine whether it is a
176 	 * valid ELF file.
177 	 */
178 	while ((dirent = readdir(dir)) != NULL) {
179 		const char	*file = dirent->d_name;
180 		char		*_dst;
181 		Fdesc		fdesc = { 0 };
182 		Rej_desc	_rej = { 0 };
183 
184 		/*
185 		 * Ignore "." and ".." entries.
186 		 */
187 		if ((file[0] == '.') && ((file[1] == '\0') ||
188 		    ((file[1] == '.') && (file[2] == '\0'))))
189 			continue;
190 
191 		/*
192 		 * Complete the full pathname, and verify its usability.
193 		 */
194 		for (_dst = dst, src = file, file = dst; *src; _dst++, src++)
195 			*_dst = *src;
196 		*_dst = '\0';
197 
198 		if ((name = strdup(path)) == 0) {
199 			error = 1;
200 			break;
201 		}
202 		if ((name = load_trace(lml, name, clmp)) == 0)
203 			continue;
204 
205 		if (find_path(lml, name, clmp, flags, &fdesc, &_rej) == 0)
206 			rejection_inherit(rej, &_rej, &fdesc);
207 		else {
208 			DBG_CALL(Dbg_cap_hw_candidate(lml, name));
209 
210 			/*
211 			 * If this object has already been loaded, obtain the
212 			 * hardware capabilities for later sorting.  Otherwise
213 			 * we have a new candidate.
214 			 */
215 			if (fdesc.fd_lmp)
216 				fdesc.fd_fmap.fm_hwptr = HWCAP(fdesc.fd_lmp);
217 			else
218 				fdesc.fd_fmap = *fmap;
219 
220 			if (alist_append(&fdalp, &fdesc,
221 			    sizeof (Fdesc), 10) == 0) {
222 				remove_fdesc(&fdesc);
223 				error = 1;
224 				break;
225 			}
226 		}
227 
228 		/*
229 		 * Clear the global file mapping structure so that the mapping
230 		 * for this file won't be overriden.
231 		 */
232 		fmap->fm_mflags = MAP_PRIVATE;
233 		fmap->fm_maddr = 0;
234 		fmap->fm_msize = syspagsz;
235 		fmap->fm_hwptr = 0;
236 
237 	}
238 	(void) closedir(dir);
239 
240 	/*
241 	 * If no objects have been found, we're done.  Also, if an allocation
242 	 * error occurred while processing any object, remove any objects that
243 	 * had already been added to the list and return.
244 	 */
245 	if ((fdalp == 0) || error) {
246 		if (fdalp) {
247 			for (ALIST_TRAVERSE(fdalp, off, fdp))
248 				remove_fdesc(fdp);
249 			free(fdalp);
250 		}
251 		return (0);
252 	}
253 
254 	/*
255 	 * Having processed and retained all candidates from this directory,
256 	 * sort them, based on the precedence of their hardware capabilities.
257 	 */
258 	qsort(&(fdalp->al_data[0]), ((fdalp->al_next - (sizeof (Alist) -
259 	    sizeof (void *))) / fdalp->al_size), fdalp->al_size, compare);
260 
261 	*fdalpp = fdalp;
262 	return (1);
263 }
264 
265 static Pnode *
266 _hwcap_filtees(Pnode **pnpp, Aliste nlmco, Rt_map *flmp, const char *ref,
267     const char *dir, int mode, uint_t flags)
268 {
269 	Alist		*fdalp = 0;
270 	Aliste		off;
271 	Pnode		*fpnp = 0, *lpnp, *npnp = (*pnpp)->p_next;
272 	Fdesc		*fdp;
273 	Lm_list		*lml = LIST(flmp);
274 	int		unused = 0;
275 	Rej_desc	rej = { 0 };
276 
277 	if (hwcap_dir(&fdalp, lml, dir, flmp, flags, &rej) == 0) {
278 		remove_rej(&rej);
279 		return (0);
280 	}
281 
282 	/*
283 	 * Now complete the mapping of each of the ordered objects, adding
284 	 * each object to a new Pnode.
285 	 */
286 	for (ALIST_TRAVERSE(fdalp, off, fdp)) {
287 		Rt_map	*nlmp;
288 		Grp_hdl	*ghp = 0;
289 		Pnode	*pnp;
290 		int	audit = 0;
291 
292 		if (unused) {
293 			/*
294 			 * Flush out objects remaining.
295 			 */
296 			remove_fdesc(fdp);
297 			continue;
298 		}
299 
300 		/*
301 		 * Complete mapping the file, obtaining a handle, and continue
302 		 * to analyze the object, establishing dependencies and
303 		 * relocating.  Remove the file descriptor at this point, as it
304 		 * is no longer required.
305 		 */
306 		DBG_CALL(Dbg_file_filtee(lml, NAME(flmp), fdp->fd_nname, 0));
307 
308 		nlmp = load_path(lml, nlmco, fdp->fd_nname, flmp, mode,
309 		    (flags | FLG_RT_HANDLE), &ghp, fdp, &rej);
310 		remove_fdesc(fdp);
311 
312 		/*
313 		 * Create a new Pnode to represent this filtee, and substitute
314 		 * the calling Pnode (which was used to represent the hardware
315 		 * capability directory).
316 		 */
317 		if ((pnp = calloc(1, sizeof (Pnode))) == 0) {
318 			if (ghp)
319 				(void) dlclose_core(ghp, flmp);
320 			return (0);
321 		}
322 		if ((pnp->p_name = strdup(NAME(nlmp))) == 0) {
323 			if (ghp)
324 				(void) dlclose_core(ghp, flmp);
325 			free(pnp);
326 			return (0);
327 		}
328 		pnp->p_len = strlen(NAME(nlmp));
329 		pnp->p_info = (void *)ghp;
330 		pnp->p_next = npnp;
331 
332 		if (fpnp == 0) {
333 			Pnode	*opnp = (*pnpp);
334 
335 			/*
336 			 * If this is the first pnode, reuse the original after
337 			 * freeing any of its pathnames.
338 			 */
339 			if (opnp->p_name)
340 				free((void *)opnp->p_name);
341 			if (opnp->p_oname)
342 				free((void *)opnp->p_oname);
343 			*opnp = *pnp;
344 			free((void *)pnp);
345 			fpnp = lpnp = pnp = opnp;
346 		} else {
347 			lpnp->p_next = pnp;
348 			lpnp = pnp;
349 		}
350 
351 		/*
352 		 * Establish the filter handle to prevent any recursion.
353 		 */
354 		if (nlmp && ghp) {
355 			ghp->gh_flags |= GPH_FILTEE;
356 			pnp->p_info = (void *)ghp;
357 		}
358 
359 		/*
360 		 * Audit the filter/filtee established.  A return of 0
361 		 * indicates the auditor wishes to ignore this filtee.
362 		 */
363 		if (nlmp && (lml->lm_tflags | FLAGS1(flmp)) &
364 		    LML_TFLG_AUD_OBJFILTER) {
365 			if (audit_objfilter(flmp, ref, nlmp, 0) == 0) {
366 				audit = 1;
367 				nlmp = 0;
368 			}
369 		}
370 
371 		/*
372 		 * Finish processing the objects associated with this request.
373 		 */
374 		if (nlmp && ghp && ((analyze_lmc(lml, nlmco, nlmp) == 0) ||
375 		    (relocate_lmc(lml, nlmco, nlmp) == 0)))
376 			nlmp = 0;
377 
378 		/*
379 		 * Finally, if the filtee is part of a link-map control list
380 		 * that is equivalent, or less, than the filter control list,
381 		 * create an association between the filter and filtee.  This
382 		 * association provides sufficient information to tear down the
383 		 * filter and filtee if necessary.
384 		 */
385 		if (nlmp && ghp && (CNTL(nlmp) <= CNTL(flmp)) &&
386 		    (hdl_add(ghp, flmp, GPD_FILTER) == 0))
387 			nlmp = 0;
388 
389 		/*
390 		 * If this object is marked an end-filtee, we're done.
391 		 */
392 		if (nlmp && ghp && (FLAGS1(nlmp) & FL1_RT_ENDFILTE))
393 			unused = 1;
394 
395 		/*
396 		 * Generate a diagnostic if the filtee couldn't be loaded, null
397 		 * out the pnode entry, and continue the search.
398 		 */
399 		if (nlmp == 0) {
400 			pnp->p_info = 0;
401 			DBG_CALL(Dbg_file_filtee(lml, 0, pnp->p_name, audit));
402 			if (ghp)
403 				(void) dlclose_core(ghp, flmp);
404 
405 			pnp->p_len = 0;
406 		}
407 	}
408 
409 	free(fdalp);
410 	return (fpnp);
411 }
412 
413 Pnode *
414 hwcap_filtees(Pnode **pnpp, Aliste nlmco, Dyninfo *dip, Rt_map *flmp,
415     const char *ref, int mode, uint_t flags)
416 {
417 	Pnode		*pnp = *pnpp;
418 	const char	*dir = pnp->p_name;
419 	Lm_list		*flml = LIST(flmp);
420 
421 	DBG_CALL(Dbg_cap_hw_filter(flml, dir, flmp));
422 
423 	if ((pnp = _hwcap_filtees(pnpp, nlmco, flmp, ref, dir, mode,
424 	    flags)) != 0)
425 		return (pnp);
426 
427 	/*
428 	 * If no hardware capability filtees have been found, provide suitable
429 	 * diagnostics and mark the incoming Pnode as unused.
430 	 */
431 	if ((flml->lm_flags & LML_FLG_TRC_ENABLE) &&
432 	    (dip->di_flags & FLG_DI_AUXFLTR) && (rtld_flags & RT_FL_WARNFLTR))
433 		(void) printf(MSG_INTL(MSG_LDD_HWCAP_NFOUND), dir);
434 
435 	DBG_CALL(Dbg_cap_hw_filter(flml, dir, 0));
436 
437 	pnp = *pnpp;
438 	pnp->p_len = 0;
439 	return (pnp);
440 }
441 
442 /*
443  * Load an individual hardware capabilities object.
444  */
445 Rt_map *
446 load_hwcap(Lm_list *lml, Aliste lmco, const char *dir, Rt_map *clmp,
447     uint_t mode, uint_t flags, Grp_hdl **hdl, Rej_desc *rej)
448 {
449 	Alist		*fdalp = 0;
450 	Aliste		off;
451 	Fdesc		*fdp;
452 	int		found = 0;
453 	Rt_map		*lmp = 0;
454 
455 	/*
456 	 * Obtain the sorted list of hardware capabilites objects available.
457 	 */
458 	if (hwcap_dir(&fdalp, lml, dir, clmp, flags, rej) == 0)
459 		return (0);
460 
461 	/*
462 	 * From the list of hardware capability objects, use the first and
463 	 * discard the rest.
464 	 */
465 	for (ALIST_TRAVERSE(fdalp, off, fdp)) {
466 		if ((found == 0) && ((lmp = load_path(lml, lmco, fdp->fd_nname,
467 		    clmp, mode, flags, hdl, fdp, rej)) != 0))
468 			found++;
469 
470 		/*
471 		 * Remove the used file descriptor and any objects remaining.
472 		 */
473 		remove_fdesc(fdp);
474 	}
475 
476 	free(fdalp);
477 	return (lmp);
478 }
479