audit.c revision 38f4bddda7216cf3550c325e8cabe56d08a2bce9
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
*
* Audit interfaces. Auditing can be enabled in two ways:
*
* - Using the LD_AUDIT environment variable
*
* - From individual objects containing a DT_DEPAUDIT entry
* (see ld(1) -P/-p options).
*
* The former establishes a global set of audit libraries which can inspect all
* objects from a given process. The latter establishes a local set of audit
* libraries which can inspect the immediate dependencies of the caller.
*
* Audit library capabilities are indicated by flags within the link-map list
* header (for global auditing), see LML_TFLG_AUD_* flags, or by the same flags
* within the individual link-map (for local auditing). Although both sets of
* flags can occur in different data items they are defined as one to simplify
* audit interface requirements. The basic test for all audit interfaces is:
*
* if ((lml->lm_tflags | AFLAGS(lmp)) & LML_TFLG_AUD_MASK)
*
* Note. Auditors themselves are identified with the LML_TFLG_NOAUDIT link-map
* list flag, and no LML_TFLG_AUD_MASK flags. These flags get propagated from
* a callers link-map list to any new link-map lists created. Thus, standard
* link-maps lists have the LML_TFLG_AUD_MASK flags propagated, and should a
* new link-map list be created by an auditor, that list gets tagged as
* LML_TFLG_NOAUDIT.
*
* The latter link-map list equivalence test insures that auditors themselves
* (invoked through DT_DEPAUDIT) are not audited.
*
* The history of version changes:
*
* LAV_VERSION1 (Solaris 2.6)
* Auditing implementation added.
*
* LAV_VERSION2 (Solaris 2.6)
* LA_SYMB_ALTVALUE support added.
*
* LAV_VERSION3 (Solaris 9 update 7)
* ld_objfilter() added.
*
* LAV_VERSION4 (Solaris 10 update 5)
* Correction of activity calls for local auditors, and introduction of
* -z globalaudit concept.
*
* LAV_VERSION5 (Solaris 11)
* Under this version, preinit and activity events are enabled from local
* auditors. The la_preinit and la_activity interfaces require a cookie
* that represents the head of the link-map list being audited. If a
* local preinit or activity interface is detected, the local auditors
* la_objopen() routine is called with a cookie that represents the object
* that heads the link-map list of the object being audited.
*
* A local auditor is loaded through adding a new dependency that requests
* auditing, and therefore an la_activity(ADD) event is already in effect.
* Regardless, the local auditors la_activity() routine is called with the
* cookie that represents the object that heads the link-map list of the
* object being audited.
*
* A local auditor can be loaded prior to the preinit event. In this case,
* the local auditors la_preinit() routine is called with the cookie that
* represents the object that heads the link-map list of the object being
* audited. After the preinit event, any la_preinit() routine within a
* local auditor will not be called.
*
* These events are intended to follow the expected sequence of events
* received by global auditors, ie:
*
* - la_objopen(main)
* - la_activity(ADD)
* - la_objopen(dependency)
* - la_activity(CONSISTENT)
* - la_preinit(main)
*/
#include <stdio.h>
#include <stdio.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <string.h>
#include <debug.h>
#include "_rtld.h"
#include "_audit.h"
#include "_elf.h"
#include "msg.h"
/* simplify boot_elf.s access. */
/*
* Obtain a head link-map cookie. Local auditors can provide la_preinit() and
* la_activity() routines, and these routines require a cookie that represents
* the object that heads the link-map of the object being audited. A list of
* these cookies is maintained on the link-map list. This list allows multiple
* local objects to specify the same auditor, and to obtain the same cookie
* for the link-map that heads the link-map list.
*
* The initial cookie is created by _audit_create_head_client() which is called
* from _audit_add_head(). This cookies address is then passed to the local
* auditors ld_objopen() and la_activity() routines. Subsequent preinit and
* activity events use _audit_get_head_client() to dynamically retrieve the
* cookies address.
*/
static Audit_client *
{
return (acp);
}
return (NULL);
}
static Audit_client *
{
return (NULL);
return (acp);
}
/*
* Determine the appropriate client. Each client structure identifies the
* link-map of the auditor it is associated with. From the client structure,
* the address of the associated cookie, that represents the object being
* audited, is retrieved so that the address can be passed to any audit call.
*
* Note, objects that are being locally audited, can provide la_preinit() and
* la_activity() routines. These routines must be passed cookies that represent
* the object that heads the link-map list of the object being audited. These
* cookies are not maintained on this objects Audit_client structure, but are
* obtained from the associated link-map lists lm_cookies alist.
*/
static Audit_client *
{
int ndx;
return (NULL);
}
return (NULL);
}
/*
* la_filter() caller. Traverse through all audit libraries and call any
* la_filter() entry points found. A zero return from an auditor indicates
* that the filtee should be ignored.
*/
static int
{
int ret;
continue;
continue;
continue;
(void) enter(thr_flg_reenter);
if (ret == 0) {
return (0);
}
}
return (1);
}
int
{
int respond = 1;
if (rt_critical())
return (respond);
return (respond);
}
/*
* la_objsearch() caller. Traverse through all audit libraries and call any
* la_objsearch() entry points found.
*
* Effectively any audit library can change the name we're working with, so we
* continue to propagate the new name to each audit library. Any 0 return
* terminates the search.
*/
static char *
{
continue;
continue;
(void) enter(thr_flg_reenter);
/*
* Diagnose any return name that differs from the original name
* passed to the auditor.
*/
break;
}
return (oname);
}
char *
{
if (rt_critical())
return (nname);
return (nname);
}
/*
* la_activity() caller. Traverse through all audit libraries and call any
* la_activity() entry points found.
*/
static void
{
if (alp->al_activity == 0)
continue;
/*
* Determine what cookie is required. Any auditing that
* originates from the object that heads the link-map list has
* its own cookie. Local auditors must obtain the cookie that
* represents the object that heads the link-map list.
*/
if (client)
else
continue;
/*
* at a time. This ensures the library doesn't see numerous
* events from lazy loading a series of libraries. Keep track
* of this caller having called an auditor, so that the
* appropriate "consistent" event can be supplied on leaving
* ld.so.1.
*/
continue;
} else {
continue;
}
(void) enter(thr_flg_reenter);
}
}
void
{
if (rt_critical())
return;
FALSE);
}
}
}
/*
* Determine whether an auditor is in use by the head link-map object.
*/
static int
{
return (1);
}
return (0);
}
/*
* la_objopen() caller for the head link-map. Global auditors, or an auditor
* started from the object that heads a link-map list (typically the dynamic
* executable), are passed to la_objopen(). However, local auditors can
* provide activity and preinit events, and for these events, a cookie
* representing the head link-map list object is expected. This routine obtains
* these cookies from the link-map list lm_cookies element. This element
* ensures all clients of the same auditor use the same cookie.
*
* Although a local auditor will get an la_objopen() call for the object that
* heads the link-map list of the object being audited, the auditor is not
* permitted to request binding information for this head object. The head
* object has already been in existence, and bindings may have been resolved
* with it. This local auditor is coming into existence too late, and thus we
* don't allow any bindings to be caught at all.
*/
static int
{
int save = 0;
/*
* Ensure this local auditor isn't already in existence as an
* auditor for the head of the link-map list. If it is, then
* this auditor will have already receive preinit and activity
* events.
*/
continue;
/*
* Create a cookie that represents the object that heads the
* link-map list. If the cookie already exists, then this
* auditor has already been established for another objects
* local auditing. In this case, do not issue a la_objopen()
* or la_activity() event, as these will have already occurred.
*/
continue;
if ((acp =
return (0);
save++;
/*
* Call the la_objopen() if available.
*/
if (alp->al_objopen) {
cookie);
(void) enter(thr_flg_reenter);
if (flags) {
}
}
/*
* Call the la_activity() if available.
*/
if (alp->al_activity) {
(void) enter(thr_flg_reenter);
}
}
/*
* If new head link-map cookies have been generated, then maintain
*/
if (save) {
AL_CNT_AUDITORS) == NULL))
return (0);
AL_CNT_AUDITORS) == NULL))
return (0);
}
return (1);
}
/*
* la_objopen() caller. Create an audit information structure for the indicated
* link-map, regardless of an la_objopen() entry point. This structure is used
* to supply information to various audit interfaces (see LML_MSK_AUDINFO).
* Traverse through all audit libraries and call any la_objopen() entry points
* found.
*/
static int
int *ndx)
{
/*
* Associate a cookie with the audit library, and assign the
* initial cookie as the present link-map.
*/
continue;
(void) enter(thr_flg_reenter);
/*
* Diagnose any flags returned by the auditor.
*/
if (flags) {
}
if (flags & LA_FLG_BINDTO)
if (flags & LA_FLG_BINDFROM) {
/*
* pltexit() entry point exist in one of our auditing
* libraries.
*/
continue;
/*
* Create one dynplt for every 'PLT' that exists in the
* object.
*/
dyn_plt_ent_size)) == NULL)
return (0);
}
}
return (1);
}
int
{
if (rt_critical())
return (respond);
/*
* Determine the number of auditors that can receive information
* regarding this object. This provides the number of client
* structures required for this object.
*/
if (auditors)
/*
* Allocate an audit information structure. Each audited object
* maintains a AUDINFO() structure. As this structure can only be
* created once all auditors are loaded, a client count can now be
* computed.
*
* The allocation of the audit information structure includes an array
* of audit clients, 1 per audit library that has been loaded.
*
* ---------------
* | ai_cnt |
* Audit_info | ai_clients |-------
* | ai_dynplts | |
* |---------------| |
* Audit_client | 1 |<------
* |---------------|
* | 2 |
* .........
*/
return (0);
sizeof (Audit_info));
if (auditors)
return (respond);
}
/*
* la_objclose() caller. Traverse through all audit libraries and call any
* la_objclose() entry points found.
*/
void
{
continue;
continue;
(void) enter(thr_flg_reenter);
}
}
/*
* Determine any la_objclose() requirements. An object that is about to be
* deleted needs to trigger an la_objclose() event to any associated auditors.
* In the case of local auditing, a deleted object may have a number of callers,
* and each of these callers may have their own auditing requirements. To
* ensure only one la_objclose() event is sent to each auditor, collect the
* auditors from any callers and make sure there's no duplication.
*/
inline static void
{
return;
}
}
}
void
{
if (rt_critical())
return;
/*
* If this link-map list contains local auditors, determine if this
* object, or any of this objects CALLERS have instantiated auditors
* that need to know of la_objclose() events.
*/
}
/*
* If this close originated from dlclose(), determine whether the caller
* requires a la_objclose() event.
*/
if (clmp)
if (alp) {
}
}
/*
* la_pltenter() caller. Traverse through all audit libraries and call any
* la_pltenter() entry points found. NOTE: this routine is called via the
* glue code established in elf_plt_trace_write(), the symbol descriptor is
* created as part of the glue and for 32bit environments the st_name is a
* pointer to the real symbol name (ie. it's already been adjusted with the
* objects base offset). For 64bit environments the st_name remains the
* original symbol offset and in this case it is used to compute the real name
* pointer and pass as a separate argument to the auditor.
*/
static void
{
#if defined(_ELF64)
#else
#endif
if (alp->al_pltenter == 0)
continue;
continue;
continue;
continue;
/* BEGIN CSTYLED */
#if defined(_ELF64)
#else
flags);
#endif
/* END CSTYLED */
(void) enter(thr_flg_reenter);
}
}
}
{
if (rt_critical())
/*
* We're effectively entering ld.so.1 from user (glue) code.
*/
(void) enter(0);
}
/*
* la_pltexit() caller. Traverse through all audit libraries and call any
* la_pltexit() entry points found. See notes above (_audit_pltenter) for
* discussion on st_name.
*/
static Addr
{
#if defined(_ELF64)
#else
#endif
if (alp->al_pltexit == 0)
continue;
continue;
continue;
continue;
/* BEGIN CSTYLED */
#if defined(_ELF64)
#else
retval);
#endif
/* END CSTYLED */
(void) enter(thr_flg_reenter);
}
return (retval);
}
{
if (rt_critical())
return (_retval);
/*
* We're effectively entering ld.so.1 from user (glue) code.
*/
(void) enter(0);
return (_retval);
}
/*
* la_symbind() caller. Traverse through all audit libraries and call any
* la_symbind() entry points found.
*/
static Addr
{
#if defined(_ELF64)
#else
#endif
if (alp->al_symbind == 0)
continue;
continue;
continue;
continue;
/*
* The la_symbind interface is only called when the destination
* object has been identified as BINDTO and either the
* destination object is being locally audited or the calling
* object has been identified as BINDFROM. Use a local version
* of the flags, so that any user update can be collected.
*/
(*called)++;
/* BEGIN CSTYLED */
#if defined(_ELF64)
#else
&lflags);
#endif
/* END CSTYLED */
(void) enter(thr_flg_reenter);
/*
* If the auditor indicated that they did not want to process
* pltenter, or pltexit audits for this symbol, retain this
* information. Also retain whether an alternative symbol value
* has been supplied.
*/
*flags |= LA_SYMB_ALTVALUE;
}
}
}
{
int called = 0;
/*
* Construct a new symbol from that supplied but with the real address.
* In the 64-bit world the st_name field is only 32-bits which isn't
* big enough to hold a character pointer. We pass this pointer as a
* separate parameter for 64-bit audit libraries.
*/
if (rt_critical())
#if !defined(_ELF64)
#endif
}
/*
* If no la_symbind() was called for this interface, fabricate that no
* la_pltenter, or la_pltexit is required. This helps reduce the glue
* code created for further auditing.
*/
if (called == 0)
}
/*
* la_preinit() caller. Traverse through all audit libraries and call any
* la_preinit() entry points found.
*/
static void
{
if (alp->al_preinit == 0)
continue;
/*
* Determine what cookie is required. Any auditing that
* originates from the object that heads the link-map list has
* its own cookie. Local auditors must obtain the cookie that
* represents the object that heads the link-map list.
*/
if (client)
else
continue;
(void) enter(thr_flg_reenter);
}
}
void
{
if (rt_critical())
return;
}
}
/*
* Clean up (free) an audit descriptor. First, gather a list of all handles,
* and then close each one down. This is done rather than using the handles
* directly from the auditors, as the audit list can be torn down as a result
* of the dlclose. In other words, what you're pointing at can be removed
* while you're still pointing at it.
*/
void
{
return;
/*
* Indicate that the caller is no longer being audited.
*/
if (ghalp) {
}
}
}
/*
* Objects that establish local auditors may have been added to preinit or
* activity lists. Remove the object from this list if it is present.
*/
static void
{
return;
}
}
}
/*
* Clean up (free) an audit information structure.
*/
void
{
return;
if (aip->ai_dynplts)
if (aud_preinit)
if (aud_activity)
}
/*
* Create a data structure of symbol lookup names and associated flags to help
* simplify audit_symget() use.
*/
typedef struct {
} Aud_info;
{ MSG_SYM_LAVERSION, 0, 0 }, /* MSG_ORIG(MSG_SYM_LAVERSION) */
{ MSG_SYM_LAPREINIT, /* MSG_ORIG(MSG_SYM_LAPREINIT) */
LML_TFLG_AUD_PREINIT, 0 },
{ MSG_SYM_LAOBJSEARCH, /* MSG_ORIG(MSG_SYM_LAOBJSEARCH) */
LML_TFLG_AUD_OBJSEARCH, 0 },
{ MSG_SYM_LAOBJOPEN, /* MSG_ORIG(MSG_SYM_LAOBJOPEN) */
LML_TFLG_AUD_OBJOPEN, 0 },
{ MSG_SYM_LAOBJFILTER, /* MSG_ORIG(MSG_SYM_LAOBJFILTER */
LML_TFLG_AUD_OBJFILTER, 0 },
{ MSG_SYM_LAOBJCLOSE, /* MSG_ORIG(MSG_SYM_LAOBJCLOSE) */
LML_TFLG_AUD_OBJCLOSE, 0 },
{ MSG_SYM_LAACTIVITY, /* MSG_ORIG(MSG_SYM_LAACTIVITY) */
LML_TFLG_AUD_ACTIVITY, 0 },
#if defined(_ELF64)
{ MSG_SYM_LASYMBIND_64, /* MSG_ORIG(MSG_SYM_LASYMBIND_64) */
#else
{ MSG_SYM_LASYMBIND, /* MSG_ORIG(MSG_SYM_LASYMBIND) */
#endif
LML_TFLG_AUD_SYMBIND, 0 },
#if defined(__sparcv9)
{ MSG_SYM_LAV9PLTENTER, /* MSG_ORIG(MSG_SYM_LAV9PLTENTER) */
{ MSG_SYM_LAV8PLTENTER, /* MSG_ORIG(MSG_SYM_LAV8PLTENTER) */
{ MSG_SYM_LAAMD64PLTENTER, /* MSG_ORIG(MSG_SYM_LAAMD64PLTENTER) */
{ MSG_SYM_LAX86PLTENTER, /* MSG_ORIG(MSG_SYM_LAX86PLTENTER) */
#else
#endif
#if defined(_ELF64)
{ MSG_SYM_LAPLTEXIT_64, /* MSG_ORIG(MSG_SYM_LAPLTEXIT_64) */
#else
{ MSG_SYM_LAPLTEXIT, /* MSG_ORIG(MSG_SYM_LAPLTEXIT) */
#endif
};
#define AI_LAVERSION 0
#define AI_LAPREINIT 1
#define AI_LAOBJSEARCH 2
#define AI_LAOBJOPEN 3
#define AI_LAOBJFILTER 4
#define AI_LAOBJCLOSE 5
#define AI_LAACTIVITY 6
#define AI_LASYMBIND 7
#define AI_LAPLTENTER 8
#define AI_LAPLTEXIT 9
static Addr
{
/*
* Initialize the symbol lookup, and symbol result, data structures.
*/
0, 0, 0, 0, (LKUP_FIRST | LKUP_DLSYM));
if (alflag)
if (auflag)
audit_flags |= auflag;
/*
* Note, unlike most other diagnostics, where we wish to
* identify the lmid of the caller, here we use the lmid of
* the auditor itself to show the association of the auditor
* and the interfaces it provides.
*/
return (addr);
}
return (0);
}
/*
* Centralize cleanup routines.
*/
static int
{
if (ghp)
if (alp)
return (0);
}
/*
* Given a list of one or more audit libraries, open each one and establish a
* a descriptor representing the entry points it provides.
*/
int
{
/*
* Determine the type of auditing for diagnostics.
*/
if (DBG_ENABLED) {
int type;
if (orig & PD_FLG_EXTLOAD)
else
}
/*
* Mark that we have at least one auditing link map
*/
/*
* The audit definitions may be a list (which will already have been
* dupped) so split it into individual tokens.
*/
/*
* Open the audit library on its own link-map.
*/
continue;
}
/*
* If this auditor has already been loaded, reuse it.
*/
AL_CNT_AUDITORS) == NULL)
/*
* If this existing auditor provides preinit or
* activity routines, track their existence. The
* instantiation of a local auditor requires a cookie
* be created that represents the object that heads
* the link-map list of the object being audited.
*/
if (alp->al_preinit)
preinit++;
if (alp->al_activity)
activity++;
continue;
}
/*
* Prior to the Unified Process Model (UPM) environment, an
* rtld lock had to be held upon leave(). However, even within
* a UPM environment, an old auditor, that has a lazy dependency
* on libc, is still a possibility. As libc isn't loaded, we
* don't know the process model, and will determine this later.
* Refer to external.c:get_lcinterface().
*/
if ((rtld_flags2 & RT_FL2_UNIFPROC) == 0)
/*
* Allocate an audit list descriptor for this object and
* search for all known entry points.
*/
/*
* All audit libraries must handshake through la_version().
* Determine that the symbol exists, finish initializing the
* object, and then call the function.
*/
AI_LAVERSION, in_nfavl)) == 0) {
continue;
}
if (tobj)
(void) enter(thr_flg_reenter);
continue;
}
AL_CNT_AUDITORS) == NULL)
/*
* Collect any remaining entry points.
*/
preinit++;
activity++;
/*
* Collect the individual object flags, and assign this audit
* list descriptor to its associated link-map list.
*/
}
/*
* If the caller isn't the head of its own link-map list, then any
* preinit or activity entry points need to be tracked separately.
* These "events" are not associated with a particular link-map, and
* thus a traversal of any existing preinit and activity clients is
* required.
*
* If either of these events are required, establish a cookie for the
* object at the head of the link-map list, and make an initial ADD
* activity for these local auditors.
*/
return (0);
/*
* Free the original audit string, as this descriptor may be used again
* to add additional auditing.
*/
return (error);
}