xref: /onnv-gate/usr/src/cmd/sgs/libcrle/common/audit.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /* LINTLIBRARY */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include	<link.h>
31*0Sstevel@tonic-gate #include	<stdio.h>
32*0Sstevel@tonic-gate #include	<stdlib.h>
33*0Sstevel@tonic-gate #include	<unistd.h>
34*0Sstevel@tonic-gate #include	<strings.h>
35*0Sstevel@tonic-gate #include	<limits.h>
36*0Sstevel@tonic-gate #include	"rtld.h"
37*0Sstevel@tonic-gate #include	"_crle.h"
38*0Sstevel@tonic-gate #include	"msg.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * This file provides the LD_AUDIT interfaces for libcrle.so.1, which are
42*0Sstevel@tonic-gate  * called for one of two reasons:
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * CRLE_AUD_DEPENDS
45*0Sstevel@tonic-gate  *		under this mode, the dependencies of the application are
46*0Sstevel@tonic-gate  *		gathered (similar to ldd(1)) and written back to the calling
47*0Sstevel@tonic-gate  *		process.
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * CRLE_AUD_DLDUMP
50*0Sstevel@tonic-gate  *		under this mode, the LD_CONFIG file is read to determine which
51*0Sstevel@tonic-gate  *		objects are to be dldump()'ed. The memory range occupied by
52*0Sstevel@tonic-gate  *		the dumped images is written back to the calling process.
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  * Both of these interfaces are invoked via the crle(1) calling process.  The
55*0Sstevel@tonic-gate  * following environment variables are used to communicate between the two:
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  * CRLE_FD	the file descriptor on which to communicate to the calling
58*0Sstevel@tonic-gate  *		process (used for CRLE_AUD_DEPENDS and CRLE_AUD_DUMP).
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  * CRLE_FLAGS 	this signals CRLE_AUD_DLDUMP mode, and indicates the required
61*0Sstevel@tonic-gate  *		flags for the dldump(3x) calls.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static int	auflag;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate int		pfd;
67*0Sstevel@tonic-gate int		dlflag = RTLD_CONFSET;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate /*
70*0Sstevel@tonic-gate  * Initial audit handshake, establish audit mode.
71*0Sstevel@tonic-gate  */
72*0Sstevel@tonic-gate uint_t
73*0Sstevel@tonic-gate /* ARGSUSED */
74*0Sstevel@tonic-gate la_version(uint_t version)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	char	*str;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	/*
79*0Sstevel@tonic-gate 	 * Establish the file desciptor to communicate with the calling process,
80*0Sstevel@tonic-gate 	 * If there are any errors terminate the process.
81*0Sstevel@tonic-gate 	 */
82*0Sstevel@tonic-gate 	if ((str = getenv(MSG_ORIG(MSG_ENV_AUD_FD))) == NULL)
83*0Sstevel@tonic-gate 		exit(1);
84*0Sstevel@tonic-gate 	pfd = atoi(str);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	/*
87*0Sstevel@tonic-gate 	 * Determine which audit mode is required based on the existance of
88*0Sstevel@tonic-gate 	 * CRLE_FLAGS.
89*0Sstevel@tonic-gate 	 */
90*0Sstevel@tonic-gate 	if ((str = getenv(MSG_ORIG(MSG_ENV_AUD_FLAGS))) == NULL) {
91*0Sstevel@tonic-gate 		auflag = CRLE_AUD_DEPENDS;
92*0Sstevel@tonic-gate 	} else {
93*0Sstevel@tonic-gate 		auflag = CRLE_AUD_DLDUMP;
94*0Sstevel@tonic-gate 		dlflag |= atoi(str);
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 		/*
97*0Sstevel@tonic-gate 		 * Fill any memory holes before anything gets mapped.
98*0Sstevel@tonic-gate 		 */
99*0Sstevel@tonic-gate 		if (filladdr() != 0)
100*0Sstevel@tonic-gate 			exit(1);
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/*
104*0Sstevel@tonic-gate 	 * We need the audit interface containing la_objfilter().
105*0Sstevel@tonic-gate 	 */
106*0Sstevel@tonic-gate 	return (LAV_VERSION3);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * Audit interface called for each dependency.  If in CRLE_AUD_DEPENDS mode,
111*0Sstevel@tonic-gate  * return each dependency of the primary link-map to the caller.
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate uint_t
114*0Sstevel@tonic-gate /* ARGSUSED2 */
115*0Sstevel@tonic-gate la_objopen(Link_map * lmp, Lmid_t lmid, uintptr_t *cookie)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP)
118*0Sstevel@tonic-gate 		return (0);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if ((lmid == LM_ID_BASE) && !(FLAGS((Rt_map *)lmp) & FLG_RT_ISMAIN)) {
121*0Sstevel@tonic-gate 		char	buffer[PATH_MAX];
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 		(void) snprintf(buffer, PATH_MAX, MSG_ORIG(MSG_AUD_DEPEND),
124*0Sstevel@tonic-gate 		    lmp->l_name);
125*0Sstevel@tonic-gate 		(void) write(pfd, buffer, strlen(buffer));
126*0Sstevel@tonic-gate 		*cookie = (uintptr_t)lmp->l_name;
127*0Sstevel@tonic-gate 	} else
128*0Sstevel@tonic-gate 		*cookie = (uintptr_t)0;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	return (0);
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /*
134*0Sstevel@tonic-gate  * Audit interface called for any filter/filtee pairs.  If in CRLE_AUD_DEPENDS
135*0Sstevel@tonic-gate  * mode, return the filter/filtee association to the caller.
136*0Sstevel@tonic-gate  */
137*0Sstevel@tonic-gate int
138*0Sstevel@tonic-gate /* ARGSUSED2 */
139*0Sstevel@tonic-gate la_objfilter(uintptr_t *fltrcook, const char *fltestr, uintptr_t *fltecook,
140*0Sstevel@tonic-gate     uint_t flags)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP)
143*0Sstevel@tonic-gate 		return (0);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	if (*fltrcook && *fltestr && *fltecook) {
146*0Sstevel@tonic-gate 		char	buffer[PATH_MAX];
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		(void) snprintf(buffer, PATH_MAX, MSG_ORIG(MSG_AUD_FILTER),
149*0Sstevel@tonic-gate 		    (char *)(*fltrcook), fltestr, (char *)(*fltecook));
150*0Sstevel@tonic-gate 		(void) write(pfd, buffer, strlen(buffer));
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 	return (1);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate  * Audit interface called before transfer of control to application.  If in
157*0Sstevel@tonic-gate  * CRLE_AUD_DLDUMP mode read the configuration file and dldump() all necessary
158*0Sstevel@tonic-gate  * objects.
159*0Sstevel@tonic-gate  */
160*0Sstevel@tonic-gate void
161*0Sstevel@tonic-gate /* ARGSUSED */
162*0Sstevel@tonic-gate la_preinit(uintptr_t *cookie)
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP) {
165*0Sstevel@tonic-gate 		if (dumpconfig() != 0)
166*0Sstevel@tonic-gate 			exit(1);
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 	exit(0);
169*0Sstevel@tonic-gate }
170