xref: /onnv-gate/usr/src/cmd/sgs/libcrle/common/audit.c (revision 8394:cfddc4c3786c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8394SAli.Bahrami@Sun.COM  * Common Development and Distribution License (the "License").
6*8394SAli.Bahrami@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8394SAli.Bahrami@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /* LINTLIBRARY */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include	<link.h>
290Sstevel@tonic-gate #include	<stdio.h>
300Sstevel@tonic-gate #include	<stdlib.h>
310Sstevel@tonic-gate #include	<unistd.h>
320Sstevel@tonic-gate #include	<strings.h>
330Sstevel@tonic-gate #include	<limits.h>
340Sstevel@tonic-gate #include	"rtld.h"
350Sstevel@tonic-gate #include	"_crle.h"
360Sstevel@tonic-gate #include	"msg.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * This file provides the LD_AUDIT interfaces for libcrle.so.1, which are
400Sstevel@tonic-gate  * called for one of two reasons:
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * CRLE_AUD_DEPENDS
430Sstevel@tonic-gate  *		under this mode, the dependencies of the application are
440Sstevel@tonic-gate  *		gathered (similar to ldd(1)) and written back to the calling
450Sstevel@tonic-gate  *		process.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * CRLE_AUD_DLDUMP
480Sstevel@tonic-gate  *		under this mode, the LD_CONFIG file is read to determine which
490Sstevel@tonic-gate  *		objects are to be dldump()'ed. The memory range occupied by
500Sstevel@tonic-gate  *		the dumped images is written back to the calling process.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * Both of these interfaces are invoked via the crle(1) calling process.  The
530Sstevel@tonic-gate  * following environment variables are used to communicate between the two:
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * CRLE_FD	the file descriptor on which to communicate to the calling
560Sstevel@tonic-gate  *		process (used for CRLE_AUD_DEPENDS and CRLE_AUD_DUMP).
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * CRLE_FLAGS 	this signals CRLE_AUD_DLDUMP mode, and indicates the required
590Sstevel@tonic-gate  *		flags for the dldump(3x) calls.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static int	auflag;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate int		pfd;
650Sstevel@tonic-gate int		dlflag = RTLD_CONFSET;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * Initial audit handshake, establish audit mode.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate uint_t
710Sstevel@tonic-gate /* ARGSUSED */
la_version(uint_t version)720Sstevel@tonic-gate la_version(uint_t version)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	char	*str;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	/*
770Sstevel@tonic-gate 	 * Establish the file desciptor to communicate with the calling process,
780Sstevel@tonic-gate 	 * If there are any errors terminate the process.
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	if ((str = getenv(MSG_ORIG(MSG_ENV_AUD_FD))) == NULL)
810Sstevel@tonic-gate 		exit(1);
820Sstevel@tonic-gate 	pfd = atoi(str);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	/*
850Sstevel@tonic-gate 	 * Determine which audit mode is required based on the existance of
860Sstevel@tonic-gate 	 * CRLE_FLAGS.
870Sstevel@tonic-gate 	 */
880Sstevel@tonic-gate 	if ((str = getenv(MSG_ORIG(MSG_ENV_AUD_FLAGS))) == NULL) {
890Sstevel@tonic-gate 		auflag = CRLE_AUD_DEPENDS;
900Sstevel@tonic-gate 	} else {
910Sstevel@tonic-gate 		auflag = CRLE_AUD_DLDUMP;
920Sstevel@tonic-gate 		dlflag |= atoi(str);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		/*
950Sstevel@tonic-gate 		 * Fill any memory holes before anything gets mapped.
960Sstevel@tonic-gate 		 */
970Sstevel@tonic-gate 		if (filladdr() != 0)
980Sstevel@tonic-gate 			exit(1);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/*
1020Sstevel@tonic-gate 	 * We need the audit interface containing la_objfilter().
1030Sstevel@tonic-gate 	 */
1040Sstevel@tonic-gate 	return (LAV_VERSION3);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * Audit interface called for each dependency.  If in CRLE_AUD_DEPENDS mode,
1090Sstevel@tonic-gate  * return each dependency of the primary link-map to the caller.
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate uint_t
1120Sstevel@tonic-gate /* ARGSUSED2 */
la_objopen(Link_map * lmp,Lmid_t lmid,uintptr_t * cookie)1130Sstevel@tonic-gate la_objopen(Link_map * lmp, Lmid_t lmid, uintptr_t *cookie)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP)
1160Sstevel@tonic-gate 		return (0);
1170Sstevel@tonic-gate 
118*8394SAli.Bahrami@Sun.COM 	if ((lmid == LM_ID_BASE) &&
119*8394SAli.Bahrami@Sun.COM 	    !(FLAGS(LINKMAP_TO_RTMAP(lmp)) & FLG_RT_ISMAIN)) {
1200Sstevel@tonic-gate 		char	buffer[PATH_MAX];
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		(void) snprintf(buffer, PATH_MAX, MSG_ORIG(MSG_AUD_DEPEND),
1230Sstevel@tonic-gate 		    lmp->l_name);
1240Sstevel@tonic-gate 		(void) write(pfd, buffer, strlen(buffer));
1250Sstevel@tonic-gate 		*cookie = (uintptr_t)lmp->l_name;
1260Sstevel@tonic-gate 	} else
1270Sstevel@tonic-gate 		*cookie = (uintptr_t)0;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	return (0);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * Audit interface called for any filter/filtee pairs.  If in CRLE_AUD_DEPENDS
1340Sstevel@tonic-gate  * mode, return the filter/filtee association to the caller.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate int
1370Sstevel@tonic-gate /* ARGSUSED2 */
la_objfilter(uintptr_t * fltrcook,const char * fltestr,uintptr_t * fltecook,uint_t flags)1380Sstevel@tonic-gate la_objfilter(uintptr_t *fltrcook, const char *fltestr, uintptr_t *fltecook,
1390Sstevel@tonic-gate     uint_t flags)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP)
1420Sstevel@tonic-gate 		return (0);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (*fltrcook && *fltestr && *fltecook) {
1450Sstevel@tonic-gate 		char	buffer[PATH_MAX];
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		(void) snprintf(buffer, PATH_MAX, MSG_ORIG(MSG_AUD_FILTER),
1480Sstevel@tonic-gate 		    (char *)(*fltrcook), fltestr, (char *)(*fltecook));
1490Sstevel@tonic-gate 		(void) write(pfd, buffer, strlen(buffer));
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 	return (1);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * Audit interface called before transfer of control to application.  If in
1560Sstevel@tonic-gate  * CRLE_AUD_DLDUMP mode read the configuration file and dldump() all necessary
1570Sstevel@tonic-gate  * objects.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate void
1600Sstevel@tonic-gate /* ARGSUSED */
la_preinit(uintptr_t * cookie)1610Sstevel@tonic-gate la_preinit(uintptr_t *cookie)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP) {
1640Sstevel@tonic-gate 		if (dumpconfig() != 0)
1650Sstevel@tonic-gate 			exit(1);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	exit(0);
1680Sstevel@tonic-gate }
169