xref: /titanic_41/usr/src/cmd/sgs/libcrle/common/audit.c (revision cb511613a15cb3949eadffd67b37d3c665b4ef22)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb511613SAli Bahrami  * Common Development and Distribution License (the "License").
6*cb511613SAli Bahrami  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*cb511613SAli Bahrami  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include	<link.h>
297c478bd9Sstevel@tonic-gate #include	<stdio.h>
307c478bd9Sstevel@tonic-gate #include	<stdlib.h>
317c478bd9Sstevel@tonic-gate #include	<unistd.h>
327c478bd9Sstevel@tonic-gate #include	<strings.h>
337c478bd9Sstevel@tonic-gate #include	<limits.h>
347c478bd9Sstevel@tonic-gate #include	"rtld.h"
357c478bd9Sstevel@tonic-gate #include	"_crle.h"
367c478bd9Sstevel@tonic-gate #include	"msg.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * This file provides the LD_AUDIT interfaces for libcrle.so.1, which are
407c478bd9Sstevel@tonic-gate  * called for one of two reasons:
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * CRLE_AUD_DEPENDS
437c478bd9Sstevel@tonic-gate  *		under this mode, the dependencies of the application are
447c478bd9Sstevel@tonic-gate  *		gathered (similar to ldd(1)) and written back to the calling
457c478bd9Sstevel@tonic-gate  *		process.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * CRLE_AUD_DLDUMP
487c478bd9Sstevel@tonic-gate  *		under this mode, the LD_CONFIG file is read to determine which
497c478bd9Sstevel@tonic-gate  *		objects are to be dldump()'ed. The memory range occupied by
507c478bd9Sstevel@tonic-gate  *		the dumped images is written back to the calling process.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Both of these interfaces are invoked via the crle(1) calling process.  The
537c478bd9Sstevel@tonic-gate  * following environment variables are used to communicate between the two:
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * CRLE_FD	the file descriptor on which to communicate to the calling
567c478bd9Sstevel@tonic-gate  *		process (used for CRLE_AUD_DEPENDS and CRLE_AUD_DUMP).
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * CRLE_FLAGS 	this signals CRLE_AUD_DLDUMP mode, and indicates the required
597c478bd9Sstevel@tonic-gate  *		flags for the dldump(3x) calls.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static int	auflag;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate int		pfd;
657c478bd9Sstevel@tonic-gate int		dlflag = RTLD_CONFSET;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Initial audit handshake, establish audit mode.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate uint_t
717c478bd9Sstevel@tonic-gate /* ARGSUSED */
la_version(uint_t version)727c478bd9Sstevel@tonic-gate la_version(uint_t version)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	char	*str;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * Establish the file desciptor to communicate with the calling process,
787c478bd9Sstevel@tonic-gate 	 * If there are any errors terminate the process.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 	if ((str = getenv(MSG_ORIG(MSG_ENV_AUD_FD))) == NULL)
817c478bd9Sstevel@tonic-gate 		exit(1);
827c478bd9Sstevel@tonic-gate 	pfd = atoi(str);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * Determine which audit mode is required based on the existance of
867c478bd9Sstevel@tonic-gate 	 * CRLE_FLAGS.
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	if ((str = getenv(MSG_ORIG(MSG_ENV_AUD_FLAGS))) == NULL) {
897c478bd9Sstevel@tonic-gate 		auflag = CRLE_AUD_DEPENDS;
907c478bd9Sstevel@tonic-gate 	} else {
917c478bd9Sstevel@tonic-gate 		auflag = CRLE_AUD_DLDUMP;
927c478bd9Sstevel@tonic-gate 		dlflag |= atoi(str);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 		/*
957c478bd9Sstevel@tonic-gate 		 * Fill any memory holes before anything gets mapped.
967c478bd9Sstevel@tonic-gate 		 */
977c478bd9Sstevel@tonic-gate 		if (filladdr() != 0)
987c478bd9Sstevel@tonic-gate 			exit(1);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * We need the audit interface containing la_objfilter().
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	return (LAV_VERSION3);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Audit interface called for each dependency.  If in CRLE_AUD_DEPENDS mode,
1097c478bd9Sstevel@tonic-gate  * return each dependency of the primary link-map to the caller.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate uint_t
1127c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
la_objopen(Link_map * lmp,Lmid_t lmid,uintptr_t * cookie)1137c478bd9Sstevel@tonic-gate la_objopen(Link_map * lmp, Lmid_t lmid, uintptr_t *cookie)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP)
1167c478bd9Sstevel@tonic-gate 		return (0);
1177c478bd9Sstevel@tonic-gate 
118*cb511613SAli Bahrami 	if ((lmid == LM_ID_BASE) &&
119*cb511613SAli Bahrami 	    !(FLAGS(LINKMAP_TO_RTMAP(lmp)) & FLG_RT_ISMAIN)) {
1207c478bd9Sstevel@tonic-gate 		char	buffer[PATH_MAX];
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer, PATH_MAX, MSG_ORIG(MSG_AUD_DEPEND),
1237c478bd9Sstevel@tonic-gate 		    lmp->l_name);
1247c478bd9Sstevel@tonic-gate 		(void) write(pfd, buffer, strlen(buffer));
1257c478bd9Sstevel@tonic-gate 		*cookie = (uintptr_t)lmp->l_name;
1267c478bd9Sstevel@tonic-gate 	} else
1277c478bd9Sstevel@tonic-gate 		*cookie = (uintptr_t)0;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	return (0);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Audit interface called for any filter/filtee pairs.  If in CRLE_AUD_DEPENDS
1347c478bd9Sstevel@tonic-gate  * mode, return the filter/filtee association to the caller.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate int
1377c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
la_objfilter(uintptr_t * fltrcook,const char * fltestr,uintptr_t * fltecook,uint_t flags)1387c478bd9Sstevel@tonic-gate la_objfilter(uintptr_t *fltrcook, const char *fltestr, uintptr_t *fltecook,
1397c478bd9Sstevel@tonic-gate     uint_t flags)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP)
1427c478bd9Sstevel@tonic-gate 		return (0);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (*fltrcook && *fltestr && *fltecook) {
1457c478bd9Sstevel@tonic-gate 		char	buffer[PATH_MAX];
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer, PATH_MAX, MSG_ORIG(MSG_AUD_FILTER),
1487c478bd9Sstevel@tonic-gate 		    (char *)(*fltrcook), fltestr, (char *)(*fltecook));
1497c478bd9Sstevel@tonic-gate 		(void) write(pfd, buffer, strlen(buffer));
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	return (1);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Audit interface called before transfer of control to application.  If in
1567c478bd9Sstevel@tonic-gate  * CRLE_AUD_DLDUMP mode read the configuration file and dldump() all necessary
1577c478bd9Sstevel@tonic-gate  * objects.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate void
1607c478bd9Sstevel@tonic-gate /* ARGSUSED */
la_preinit(uintptr_t * cookie)1617c478bd9Sstevel@tonic-gate la_preinit(uintptr_t *cookie)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	if (auflag == CRLE_AUD_DLDUMP) {
1647c478bd9Sstevel@tonic-gate 		if (dumpconfig() != 0)
1657c478bd9Sstevel@tonic-gate 			exit(1);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	exit(0);
1687c478bd9Sstevel@tonic-gate }
169