xref: /freebsd/cddl/usr.sbin/dwatch/libexec/priv (revision 434283fda99e89af20c3fe95fde427624ae96d04)
1# -*- tab-width: 4 -*- ;; Emacs
2# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
3############################################################ IDENT(1)
4#
5# $Title: dwatch(8) module for priv_check(9) privilege requests $
6# $Copyright: 2026 Devin Teske. All rights reserved. $
7#
8############################################################ DESCRIPTION
9#
10# Print privilege requests denied by priv_check(9), decoded to their
11# symbolic priv(9) names by the priv_string table in priv.d of
12# libdtrace(1). Answers "why is this process getting EPERM?" -- kernel
13# privilege denials (jails, unprivileged users, MAC policies) name the
14# exact privilege refused, something no amount of truss(1) will reveal.
15# Use priv-ok to instead watch privileges being granted.
16#
17# NB: Requires priv.d, a drop-in file for older releases like this module.
18#
19############################################################ PROBE
20
21case "$PROFILE" in
22priv)
23	: ${PROBE:=priv:kernel:priv_check:priv-err} ;;
24*)
25	: ${PROBE:=priv:kernel:priv_check:${PROFILE#priv-}}
26esac
27
28############################################################ ACTIONS
29
30exec 9<<EOF
31this int priv;
32
33$PROBE /* probe ID $ID */
34{${TRACE:+
35	printf("<$ID>");}
36	this->priv = (int)arg0;
37}
38EOF
39ACTIONS=$( cat <&9 )
40ID=$(( $ID + 1 ))
41
42############################################################ EVENT DETAILS
43
44if [ ! "$CUSTOM_DETAILS" ]; then
45exec 9<<EOF
46	/*
47	 * Print privilege request details
48	 */
49	printf("%s %s (%d)",
50		probename == "priv-err" ? "denied" : "granted",
51		priv_string[this->priv],
52		this->priv);
53EOF
54EVENT_DETAILS=$( cat <&9 )
55fi
56
57################################################################################
58# END
59################################################################################
60