edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * CDDL HEADER START
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal *
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * This file and its contents are supplied under the terms of the
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Common Development and Distribution License ("CDDL"), version 1.0.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * You may only use this file in accordance with the terms of version
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * 1.0 of the CDDL.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal *
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * A full copy of the text of the CDDL should have accompanied this
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * source. A copy of the CDDL is also available via the Internet at
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * http://www.illumos.org/license/CDDL.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal *
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * CDDL HEADER END
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Copyright (c) 2012 by Delphix. All rights reserved.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * This file implements an audit library that can be used to force the loading
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * of helper providers. The default disposition for a helper provider -- USDT
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * and ustack helpers -- is to load itself from it's containing object's .init
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * section. In cases where startup time is deemed critical, USDT authors can
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * use the -xlazyload option to dtrace(1M) to disable automatic loading (it's
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * difficult to make the case for the utility of this feature for anything
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * other than libc which, indeed, was the sole motivation). If a binary has
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * been compiled with automatic loading disabled, this audit library may be
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * used to force automatic loading:
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal *
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * LD_AUDIT_32=/usr/lib/dtrace/libdaudit.so
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * LD_AUDIT_64=/usr/lib/dtrace/64/libdaudit.so
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <link.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <stdio.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <libproc.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <strings.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <dlink.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhaltypedef struct obj_list {
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal struct obj_list *ol_next;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal char *ol_name;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal uintptr_t ol_addr;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal Lmid_t ol_lmid;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal} obj_list_t;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalstatic obj_list_t *list;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#pragma init(dtrace_daudit_init)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalstatic void
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhaldtrace_daudit_init(void)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal{
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dtrace_link_init();
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal}
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*ARGSUSED*/
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhaluint_t
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalla_version(uint_t version)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal{
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal return (LAV_CURRENT);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal}
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Record objects into our linked list as they're loaded.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*ARGSUSED*/
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhaluint_t
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalla_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal{
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal obj_list_t *node;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal /*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * If we can't allocate the next node in our list, we'll try to emit a
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * message, but it's possible that might fail as well.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal if ((node = malloc(sizeof (obj_list_t))) == NULL) {
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dprintf(0, "libdaudit: failed to allocate");
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal return (0);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal }
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal node->ol_next = list;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal node->ol_name = strdup(lmp->l_name);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal node->ol_addr = lmp->l_addr;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal node->ol_lmid = lmid;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal list = node;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal return (0);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal}
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Once the link maps have reached a consistent state, process the list of
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * objects that were loaded. We need to use libproc to search for the
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * ___SUNW_dof symbol rather than dlsym(3C) since the symbol is not in the
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * dynamic (run-time) symbol table (though it is, of course, in the symtab).
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Once we find it, we ioctl(2) it to the kernel just as we would have from
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * the .init section if automatic loading were enabled.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*ARGSUSED*/
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalvoid
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalla_activity(uintptr_t *cookie, uint_t flags)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal{
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal struct ps_prochandle *P;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal int err, ret;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal GElf_Sym sym;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal if (flags != LA_ACT_CONSISTENT)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal return;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal while (list != NULL) {
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal obj_list_t *node = list;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal char *name = node->ol_name;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal list = node->ol_next;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal P = Pgrab(getpid(), PGRAB_RDONLY, &err);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal ret = Plookup_by_name(P, name, "___SUNW_dof", &sym);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal Prelease(P, 0);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal if (ret == 0) {
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dtrace_link_dof((void *)(uintptr_t)sym.st_value,
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal node->ol_lmid, node->ol_name, node->ol_addr);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal }
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal free(node->ol_name);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal free(node);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal }
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal}