audit_bsm_klib.c revision 184660
1155192Srwatson/*
2184482Srwatson * Copyright (c) 1999-2008 Apple Inc.
3155192Srwatson * Copyright (c) 2005 Robert N. M. Watson
4155192Srwatson * All rights reserved.
5155192Srwatson *
6155192Srwatson * Redistribution and use in source and binary forms, with or without
7155192Srwatson * modification, are permitted provided that the following conditions
8155192Srwatson * are met:
9155192Srwatson * 1.  Redistributions of source code must retain the above copyright
10155192Srwatson *     notice, this list of conditions and the following disclaimer.
11155192Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
12155192Srwatson *     notice, this list of conditions and the following disclaimer in the
13155192Srwatson *     documentation and/or other materials provided with the distribution.
14180701Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15155192Srwatson *     its contributors may be used to endorse or promote products derived
16155192Srwatson *     from this software without specific prior written permission.
17155192Srwatson *
18155192Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
19155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21155192Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
22155192Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26155192Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27155192Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28155192Srwatson * POSSIBILITY OF SUCH DAMAGE.
29155192Srwatson */
30155192Srwatson
31178186Srwatson#include <sys/cdefs.h>
32178186Srwatson__FBSDID("$FreeBSD: head/sys/security/audit/audit_bsm_klib.c 184660 2008-11-04 22:30:24Z jhb $");
33178186Srwatson
34155192Srwatson#include <sys/param.h>
35155192Srwatson#include <sys/fcntl.h>
36155192Srwatson#include <sys/filedesc.h>
37155192Srwatson#include <sys/libkern.h>
38155192Srwatson#include <sys/malloc.h>
39155192Srwatson#include <sys/mount.h>
40155192Srwatson#include <sys/proc.h>
41184482Srwatson#include <sys/rwlock.h>
42155192Srwatson#include <sys/sem.h>
43181060Scsjp#include <sys/sbuf.h>
44155192Srwatson#include <sys/syscall.h>
45155192Srwatson#include <sys/sysctl.h>
46155192Srwatson#include <sys/sysent.h>
47155192Srwatson#include <sys/vnode.h>
48155192Srwatson
49155192Srwatson#include <bsm/audit.h>
50155192Srwatson#include <bsm/audit_kevents.h>
51155192Srwatson#include <security/audit/audit.h>
52155192Srwatson#include <security/audit/audit_private.h>
53155192Srwatson
54155192Srwatson/*
55155192Srwatson * Hash table functions for the audit event number to event class mask
56155192Srwatson * mapping.
57155192Srwatson */
58180735Srwatson#define	EVCLASSMAP_HASH_TABLE_SIZE	251
59155192Srwatsonstruct evclass_elem {
60155192Srwatson	au_event_t event;
61155192Srwatson	au_class_t class;
62155192Srwatson	LIST_ENTRY(evclass_elem) entry;
63155192Srwatson};
64155192Srwatsonstruct evclass_list {
65155192Srwatson	LIST_HEAD(, evclass_elem) head;
66155192Srwatson};
67155192Srwatson
68155192Srwatsonstatic MALLOC_DEFINE(M_AUDITEVCLASS, "audit_evclass", "Audit event class");
69184482Srwatsonstatic struct rwlock		evclass_lock;
70156889Srwatsonstatic struct evclass_list	evclass_hash[EVCLASSMAP_HASH_TABLE_SIZE];
71155192Srwatson
72184482Srwatson#define	EVCLASS_LOCK_INIT()	rw_init(&evclass_lock, "evclass_lock")
73184482Srwatson#define	EVCLASS_RLOCK()		rw_rlock(&evclass_lock)
74184482Srwatson#define	EVCLASS_RUNLOCK()	rw_runlock(&evclass_lock)
75184482Srwatson#define	EVCLASS_WLOCK()		rw_wlock(&evclass_lock)
76184482Srwatson#define	EVCLASS_WUNLOCK()	rw_wunlock(&evclass_lock)
77184482Srwatson
78155192Srwatson/*
79155192Srwatson * Look up the class for an audit event in the class mapping table.
80155192Srwatson */
81155192Srwatsonau_class_t
82155192Srwatsonau_event_class(au_event_t event)
83155192Srwatson{
84155192Srwatson	struct evclass_list *evcl;
85155192Srwatson	struct evclass_elem *evc;
86155192Srwatson	au_class_t class;
87155192Srwatson
88184482Srwatson	EVCLASS_RLOCK();
89155192Srwatson	evcl = &evclass_hash[event % EVCLASSMAP_HASH_TABLE_SIZE];
90173142Srwatson	class = 0;
91155192Srwatson	LIST_FOREACH(evc, &evcl->head, entry) {
92155192Srwatson		if (evc->event == event) {
93155192Srwatson			class = evc->class;
94155192Srwatson			goto out;
95155192Srwatson		}
96155192Srwatson	}
97155192Srwatsonout:
98184482Srwatson	EVCLASS_RUNLOCK();
99155192Srwatson	return (class);
100155192Srwatson}
101155192Srwatson
102156889Srwatson/*
103155192Srwatson * Insert a event to class mapping. If the event already exists in the
104155192Srwatson * mapping, then replace the mapping with the new one.
105156889Srwatson *
106155192Srwatson * XXX There is currently no constraints placed on the number of mappings.
107156889Srwatson * May want to either limit to a number, or in terms of memory usage.
108155192Srwatson */
109155192Srwatsonvoid
110156889Srwatsonau_evclassmap_insert(au_event_t event, au_class_t class)
111155192Srwatson{
112155192Srwatson	struct evclass_list *evcl;
113155192Srwatson	struct evclass_elem *evc, *evc_new;
114155192Srwatson
115155192Srwatson	/*
116155192Srwatson	 * Pessimistically, always allocate storage before acquiring mutex.
117155192Srwatson	 * Free if there is already a mapping for this event.
118155192Srwatson	 */
119155192Srwatson	evc_new = malloc(sizeof(*evc), M_AUDITEVCLASS, M_WAITOK);
120155192Srwatson
121184482Srwatson	EVCLASS_WLOCK();
122155192Srwatson	evcl = &evclass_hash[event % EVCLASSMAP_HASH_TABLE_SIZE];
123155192Srwatson	LIST_FOREACH(evc, &evcl->head, entry) {
124155192Srwatson		if (evc->event == event) {
125155192Srwatson			evc->class = class;
126184482Srwatson			EVCLASS_WUNLOCK();
127155192Srwatson			free(evc_new, M_AUDITEVCLASS);
128155192Srwatson			return;
129155192Srwatson		}
130155192Srwatson	}
131155192Srwatson	evc = evc_new;
132155192Srwatson	evc->event = event;
133155192Srwatson	evc->class = class;
134155192Srwatson	LIST_INSERT_HEAD(&evcl->head, evc, entry);
135184482Srwatson	EVCLASS_WUNLOCK();
136155192Srwatson}
137155192Srwatson
138155192Srwatsonvoid
139156889Srwatsonau_evclassmap_init(void)
140155192Srwatson{
141155192Srwatson	int i;
142155192Srwatson
143184482Srwatson	EVCLASS_LOCK_INIT();
144155192Srwatson	for (i = 0; i < EVCLASSMAP_HASH_TABLE_SIZE; i++)
145155192Srwatson		LIST_INIT(&evclass_hash[i].head);
146155192Srwatson
147155192Srwatson	/*
148155192Srwatson	 * Set up the initial event to class mapping for system calls.
149155192Srwatson	 *
150155192Srwatson	 * XXXRW: Really, this should walk all possible audit events, not all
151155192Srwatson	 * native ABI system calls, as there may be audit events reachable
152155192Srwatson	 * only through non-native system calls.  It also seems a shame to
153155192Srwatson	 * frob the mutex this early.
154156889Srwatson	 */
155155192Srwatson	for (i = 0; i < SYS_MAXSYSCALL; i++) {
156155192Srwatson		if (sysent[i].sy_auevent != AUE_NULL)
157173142Srwatson			au_evclassmap_insert(sysent[i].sy_auevent, 0);
158155192Srwatson	}
159155192Srwatson}
160155192Srwatson
161155192Srwatson/*
162155192Srwatson * Check whether an event is aditable by comparing the mask of classes this
163155192Srwatson * event is part of against the given mask.
164155192Srwatson */
165155192Srwatsonint
166159269Srwatsonau_preselect(au_event_t event, au_class_t class, au_mask_t *mask_p, int sorf)
167155192Srwatson{
168155192Srwatson	au_class_t effmask = 0;
169155192Srwatson
170155192Srwatson	if (mask_p == NULL)
171155192Srwatson		return (-1);
172155192Srwatson
173156889Srwatson	/*
174155192Srwatson	 * Perform the actual check of the masks against the event.
175155192Srwatson	 */
176155192Srwatson	if (sorf & AU_PRS_SUCCESS)
177159269Srwatson		effmask |= (mask_p->am_success & class);
178156889Srwatson
179155192Srwatson	if (sorf & AU_PRS_FAILURE)
180159269Srwatson		effmask |= (mask_p->am_failure & class);
181156889Srwatson
182155192Srwatson	if (effmask)
183155192Srwatson		return (1);
184156889Srwatson	else
185155192Srwatson		return (0);
186155192Srwatson}
187155192Srwatson
188155192Srwatson/*
189156889Srwatson * Convert sysctl names and present arguments to events.
190155192Srwatson */
191155192Srwatsonau_event_t
192176690Srwatsonaudit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg)
193155192Srwatson{
194155192Srwatson
195155192Srwatson	/* can't parse it - so return the worst case */
196156889Srwatson	if ((valid_arg & (ARG_CTLNAME | ARG_LEN)) != (ARG_CTLNAME | ARG_LEN))
197155192Srwatson		return (AUE_SYSCTL);
198155192Srwatson
199155192Srwatson	switch (name[0]) {
200155192Srwatson	/* non-admin "lookups" treat them special */
201155192Srwatson	case KERN_OSTYPE:
202155192Srwatson	case KERN_OSRELEASE:
203155192Srwatson	case KERN_OSREV:
204155192Srwatson	case KERN_VERSION:
205155192Srwatson	case KERN_ARGMAX:
206155192Srwatson	case KERN_CLOCKRATE:
207155192Srwatson	case KERN_BOOTTIME:
208155192Srwatson	case KERN_POSIX1:
209155192Srwatson	case KERN_NGROUPS:
210155192Srwatson	case KERN_JOB_CONTROL:
211155192Srwatson	case KERN_SAVED_IDS:
212155192Srwatson	case KERN_OSRELDATE:
213155192Srwatson	case KERN_DUMMY:
214155192Srwatson		return (AUE_SYSCTL_NONADMIN);
215155192Srwatson
216155192Srwatson	/* only treat the changeable controls as admin */
217155192Srwatson	case KERN_MAXVNODES:
218155192Srwatson	case KERN_MAXPROC:
219155192Srwatson	case KERN_MAXFILES:
220155192Srwatson	case KERN_MAXPROCPERUID:
221155192Srwatson	case KERN_MAXFILESPERPROC:
222155192Srwatson	case KERN_HOSTID:
223155192Srwatson	case KERN_SECURELVL:
224155192Srwatson	case KERN_HOSTNAME:
225155192Srwatson	case KERN_VNODE:
226155192Srwatson	case KERN_PROC:
227155192Srwatson	case KERN_FILE:
228155192Srwatson	case KERN_PROF:
229155192Srwatson	case KERN_NISDOMAINNAME:
230155192Srwatson	case KERN_UPDATEINTERVAL:
231155192Srwatson	case KERN_NTP_PLL:
232155192Srwatson	case KERN_BOOTFILE:
233155192Srwatson	case KERN_DUMPDEV:
234155192Srwatson	case KERN_IPC:
235155192Srwatson	case KERN_PS_STRINGS:
236155192Srwatson	case KERN_USRSTACK:
237155192Srwatson	case KERN_LOGSIGEXIT:
238155192Srwatson	case KERN_IOV_MAX:
239155192Srwatson	case KERN_MAXID:
240155192Srwatson		return ((valid_arg & ARG_VALUE) ?
241181053Srwatson		    AUE_SYSCTL : AUE_SYSCTL_NONADMIN);
242155192Srwatson
243155192Srwatson	default:
244155192Srwatson		return (AUE_SYSCTL);
245155192Srwatson	}
246155192Srwatson	/* NOTREACHED */
247155192Srwatson}
248155192Srwatson
249155192Srwatson/*
250156889Srwatson * Convert an open flags specifier into a specific type of open event for
251155192Srwatson * auditing purposes.
252155192Srwatson */
253155192Srwatsonau_event_t
254176690Srwatsonaudit_flags_and_error_to_openevent(int oflags, int error)
255156889Srwatson{
256155192Srwatson	au_event_t aevent;
257155192Srwatson
258156889Srwatson	/*
259156889Srwatson	 * Need to check only those flags we care about.
260156889Srwatson	 */
261155192Srwatson	oflags = oflags & (O_RDONLY | O_CREAT | O_TRUNC | O_RDWR | O_WRONLY);
262155192Srwatson
263155192Srwatson	/*
264155192Srwatson	 * These checks determine what flags are on with the condition that
265155192Srwatson	 * ONLY that combination is on, and no other flags are on.
266155192Srwatson	 */
267155192Srwatson	switch (oflags) {
268155192Srwatson	case O_RDONLY:
269155192Srwatson		aevent = AUE_OPEN_R;
270155192Srwatson		break;
271155192Srwatson
272155192Srwatson	case (O_RDONLY | O_CREAT):
273155192Srwatson		aevent = AUE_OPEN_RC;
274155192Srwatson		break;
275155192Srwatson
276155192Srwatson	case (O_RDONLY | O_CREAT | O_TRUNC):
277155192Srwatson		aevent = AUE_OPEN_RTC;
278155192Srwatson		break;
279155192Srwatson
280155192Srwatson	case (O_RDONLY | O_TRUNC):
281155192Srwatson		aevent = AUE_OPEN_RT;
282155192Srwatson		break;
283155192Srwatson
284155192Srwatson	case O_RDWR:
285155192Srwatson		aevent = AUE_OPEN_RW;
286155192Srwatson		break;
287155192Srwatson
288155192Srwatson	case (O_RDWR | O_CREAT):
289155192Srwatson		aevent = AUE_OPEN_RWC;
290155192Srwatson		break;
291155192Srwatson
292155192Srwatson	case (O_RDWR | O_CREAT | O_TRUNC):
293155192Srwatson		aevent = AUE_OPEN_RWTC;
294155192Srwatson		break;
295155192Srwatson
296155192Srwatson	case (O_RDWR | O_TRUNC):
297155192Srwatson		aevent = AUE_OPEN_RWT;
298155192Srwatson		break;
299155192Srwatson
300155192Srwatson	case O_WRONLY:
301155192Srwatson		aevent = AUE_OPEN_W;
302155192Srwatson		break;
303155192Srwatson
304155192Srwatson	case (O_WRONLY | O_CREAT):
305155192Srwatson		aevent = AUE_OPEN_WC;
306155192Srwatson		break;
307155192Srwatson
308155192Srwatson	case (O_WRONLY | O_CREAT | O_TRUNC):
309155192Srwatson		aevent = AUE_OPEN_WTC;
310155192Srwatson		break;
311155192Srwatson
312155192Srwatson	case (O_WRONLY | O_TRUNC):
313155192Srwatson		aevent = AUE_OPEN_WT;
314155192Srwatson		break;
315155192Srwatson
316155192Srwatson	default:
317155192Srwatson		aevent = AUE_OPEN;
318155192Srwatson		break;
319155192Srwatson	}
320155192Srwatson
321155192Srwatson#if 0
322156889Srwatson	/*
323170196Srwatson	 * Convert chatty errors to better matching events.  Failures to
324170196Srwatson	 * find a file are really just attribute events -- so recast them as
325170196Srwatson	 * such.
326155192Srwatson	 *
327155192Srwatson	 * XXXAUDIT: Solaris defines that AUE_OPEN will never be returned, it
328155192Srwatson	 * is just a placeholder.  However, in Darwin we return that in
329155192Srwatson	 * preference to other events.  For now, comment this out as we don't
330155192Srwatson	 * have a BSM conversion routine for AUE_OPEN.
331155192Srwatson	 */
332155192Srwatson	switch (aevent) {
333155192Srwatson	case AUE_OPEN_R:
334155192Srwatson	case AUE_OPEN_RT:
335155192Srwatson	case AUE_OPEN_RW:
336155192Srwatson	case AUE_OPEN_RWT:
337155192Srwatson	case AUE_OPEN_W:
338155192Srwatson	case AUE_OPEN_WT:
339155192Srwatson		if (error == ENOENT)
340155192Srwatson			aevent = AUE_OPEN;
341155192Srwatson	}
342155192Srwatson#endif
343155192Srwatson	return (aevent);
344155192Srwatson}
345155192Srwatson
346155192Srwatson/*
347155192Srwatson * Convert a MSGCTL command to a specific event.
348155192Srwatson */
349155192Srwatsonint
350176565Srwatsonaudit_msgctl_to_event(int cmd)
351155192Srwatson{
352155192Srwatson
353155192Srwatson	switch (cmd) {
354155192Srwatson	case IPC_RMID:
355155192Srwatson		return (AUE_MSGCTL_RMID);
356155192Srwatson
357155192Srwatson	case IPC_SET:
358155192Srwatson		return (AUE_MSGCTL_SET);
359155192Srwatson
360155192Srwatson	case IPC_STAT:
361155192Srwatson		return (AUE_MSGCTL_STAT);
362155192Srwatson
363155192Srwatson	default:
364170196Srwatson		/* We will audit a bad command. */
365155192Srwatson		return (AUE_MSGCTL);
366155192Srwatson	}
367155192Srwatson}
368155192Srwatson
369155192Srwatson/*
370155192Srwatson * Convert a SEMCTL command to a specific event.
371155192Srwatson */
372155192Srwatsonint
373176565Srwatsonaudit_semctl_to_event(int cmd)
374155192Srwatson{
375155192Srwatson
376155192Srwatson	switch (cmd) {
377155192Srwatson	case GETALL:
378155192Srwatson		return (AUE_SEMCTL_GETALL);
379155192Srwatson
380155192Srwatson	case GETNCNT:
381155192Srwatson		return (AUE_SEMCTL_GETNCNT);
382155192Srwatson
383155192Srwatson	case GETPID:
384155192Srwatson		return (AUE_SEMCTL_GETPID);
385155192Srwatson
386155192Srwatson	case GETVAL:
387155192Srwatson		return (AUE_SEMCTL_GETVAL);
388155192Srwatson
389155192Srwatson	case GETZCNT:
390155192Srwatson		return (AUE_SEMCTL_GETZCNT);
391155192Srwatson
392155192Srwatson	case IPC_RMID:
393155192Srwatson		return (AUE_SEMCTL_RMID);
394155192Srwatson
395155192Srwatson	case IPC_SET:
396155192Srwatson		return (AUE_SEMCTL_SET);
397155192Srwatson
398155192Srwatson	case SETALL:
399155192Srwatson		return (AUE_SEMCTL_SETALL);
400155192Srwatson
401155192Srwatson	case SETVAL:
402155192Srwatson		return (AUE_SEMCTL_SETVAL);
403155192Srwatson
404155192Srwatson	case IPC_STAT:
405155192Srwatson		return (AUE_SEMCTL_STAT);
406155192Srwatson
407155192Srwatson	default:
408181053Srwatson		/* We will audit a bad command. */
409155192Srwatson		return (AUE_SEMCTL);
410155192Srwatson	}
411155192Srwatson}
412155192Srwatson
413155192Srwatson/*
414155192Srwatson * Convert a command for the auditon() system call to a audit event.
415155192Srwatson */
416155192Srwatsonint
417155192Srwatsonauditon_command_event(int cmd)
418155192Srwatson{
419155192Srwatson
420155192Srwatson	switch(cmd) {
421155192Srwatson	case A_GETPOLICY:
422155192Srwatson		return (AUE_AUDITON_GPOLICY);
423155192Srwatson
424155192Srwatson	case A_SETPOLICY:
425155192Srwatson		return (AUE_AUDITON_SPOLICY);
426155192Srwatson
427155192Srwatson	case A_GETKMASK:
428155192Srwatson		return (AUE_AUDITON_GETKMASK);
429155192Srwatson
430155192Srwatson	case A_SETKMASK:
431155192Srwatson		return (AUE_AUDITON_SETKMASK);
432155192Srwatson
433155192Srwatson	case A_GETQCTRL:
434155192Srwatson		return (AUE_AUDITON_GQCTRL);
435155192Srwatson
436155192Srwatson	case A_SETQCTRL:
437155192Srwatson		return (AUE_AUDITON_SQCTRL);
438155192Srwatson
439155192Srwatson	case A_GETCWD:
440155192Srwatson		return (AUE_AUDITON_GETCWD);
441155192Srwatson
442155192Srwatson	case A_GETCAR:
443155192Srwatson		return (AUE_AUDITON_GETCAR);
444155192Srwatson
445155192Srwatson	case A_GETSTAT:
446155192Srwatson		return (AUE_AUDITON_GETSTAT);
447155192Srwatson
448155192Srwatson	case A_SETSTAT:
449155192Srwatson		return (AUE_AUDITON_SETSTAT);
450155192Srwatson
451155192Srwatson	case A_SETUMASK:
452155192Srwatson		return (AUE_AUDITON_SETUMASK);
453155192Srwatson
454155192Srwatson	case A_SETSMASK:
455155192Srwatson		return (AUE_AUDITON_SETSMASK);
456155192Srwatson
457155192Srwatson	case A_GETCOND:
458155192Srwatson		return (AUE_AUDITON_GETCOND);
459155192Srwatson
460155192Srwatson	case A_SETCOND:
461155192Srwatson		return (AUE_AUDITON_SETCOND);
462155192Srwatson
463155192Srwatson	case A_GETCLASS:
464155192Srwatson		return (AUE_AUDITON_GETCLASS);
465155192Srwatson
466155192Srwatson	case A_SETCLASS:
467155192Srwatson		return (AUE_AUDITON_SETCLASS);
468155192Srwatson
469155192Srwatson	case A_GETPINFO:
470155192Srwatson	case A_SETPMASK:
471155192Srwatson	case A_SETFSIZE:
472155192Srwatson	case A_GETFSIZE:
473155192Srwatson	case A_GETPINFO_ADDR:
474155192Srwatson	case A_GETKAUDIT:
475155192Srwatson	case A_SETKAUDIT:
476155192Srwatson	default:
477155192Srwatson		return (AUE_AUDITON);	/* No special record */
478155192Srwatson	}
479155192Srwatson}
480155192Srwatson
481156889Srwatson/*
482156889Srwatson * Create a canonical path from given path by prefixing either the root
483156889Srwatson * directory, or the current working directory.  If the process working
484170196Srwatson * directory is NULL, we could use 'rootvnode' to obtain the root directory,
485156889Srwatson * but this results in a volfs name written to the audit log. So we will
486156889Srwatson * leave the filename starting with '/' in the audit log in this case.
487155192Srwatson */
488155192Srwatsonvoid
489176565Srwatsonaudit_canon_path(struct thread *td, char *path, char *cpath)
490155192Srwatson{
491181060Scsjp	struct vnode *cvnp, *rvnp;
492181060Scsjp	char *rbuf, *fbuf, *copy;
493155192Srwatson	struct filedesc *fdp;
494181060Scsjp	struct sbuf sbf;
495184660Sjhb	int error, cwir;
496155192Srwatson
497181060Scsjp	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s: at %s:%d",
498181060Scsjp	    __func__,  __FILE__, __LINE__);
499165621Srwatson
500181060Scsjp	copy = path;
501181060Scsjp	rvnp = cvnp = NULL;
502155192Srwatson	fdp = td->td_proc->p_fd;
503168355Srwatson	FILEDESC_SLOCK(fdp);
504181060Scsjp	/*
505181060Scsjp	 * Make sure that we handle the chroot(2) case.  If there is an
506181060Scsjp	 * alternate root directory, prepend it to the audited pathname.
507181060Scsjp	 */
508181060Scsjp	if (fdp->fd_rdir != NULL && fdp->fd_rdir != rootvnode) {
509181060Scsjp		rvnp = fdp->fd_rdir;
510181060Scsjp		vhold(rvnp);
511155192Srwatson	}
512181060Scsjp	/*
513181060Scsjp	 * If the supplied path is relative, make sure we capture the current
514181060Scsjp	 * working directory so we can prepend it to the supplied relative
515181060Scsjp	 * path.
516181060Scsjp	 */
517181060Scsjp	if (*path != '/') {
518181060Scsjp		cvnp = fdp->fd_cdir;
519181060Scsjp		vhold(cvnp);
520181060Scsjp	}
521181060Scsjp	cwir = (fdp->fd_rdir == fdp->fd_cdir);
522168355Srwatson	FILEDESC_SUNLOCK(fdp);
523181060Scsjp	/*
524181060Scsjp	 * NB: We require that the supplied array be at least MAXPATHLEN bytes
525181060Scsjp	 * long.  If this is not the case, then we can run into serious trouble.
526181060Scsjp	 */
527181060Scsjp	(void) sbuf_new(&sbf, cpath, MAXPATHLEN, SBUF_FIXEDLEN);
528181060Scsjp	/*
529181060Scsjp	 * Strip leading forward slashes.
530181060Scsjp	 */
531181060Scsjp	while (*copy == '/')
532181060Scsjp		copy++;
533181060Scsjp	/*
534181060Scsjp	 * Make sure we handle chroot(2) and prepend the global path to these
535181060Scsjp	 * environments.
536181060Scsjp	 *
537181060Scsjp	 * NB: vn_fullpath(9) on FreeBSD is less reliable than vn_getpath(9)
538181060Scsjp	 * on Darwin.  As a result, this may need some additional attention
539181060Scsjp	 * in the future.
540181060Scsjp	 */
541181060Scsjp	if (rvnp != NULL) {
542184660Sjhb		error = vn_fullpath_global(td, rvnp, &rbuf, &fbuf);
543181060Scsjp		vdrop(rvnp);
544181060Scsjp		if (error) {
545155192Srwatson			cpath[0] = '\0';
546181060Scsjp			if (cvnp != NULL)
547181060Scsjp				vdrop(cvnp);
548181060Scsjp			return;
549181060Scsjp		}
550181060Scsjp		(void) sbuf_cat(&sbf, rbuf);
551181060Scsjp		free(fbuf, M_TEMP);
552181060Scsjp	}
553181060Scsjp	if (cvnp != NULL) {
554184660Sjhb		error = vn_fullpath(td, cvnp, &rbuf, &fbuf);
555181060Scsjp		vdrop(cvnp);
556181060Scsjp		if (error) {
557181060Scsjp			cpath[0] = '\0';
558181060Scsjp			return;
559181060Scsjp		}
560181060Scsjp		(void) sbuf_cat(&sbf, rbuf);
561181060Scsjp		free(fbuf, M_TEMP);
562181060Scsjp	}
563181060Scsjp	if (cwir == 0 || (cwir != 0 && cvnp == NULL))
564182090Scsjp		(void) sbuf_putc(&sbf, '/');
565181060Scsjp	/*
566181060Scsjp	 * Now that we have processed any alternate root and relative path
567181060Scsjp	 * names, add the supplied pathname.
568181060Scsjp	 */
569181060Scsjp        (void) sbuf_cat(&sbf, copy);
570181060Scsjp	/*
571181060Scsjp	 * One or more of the previous sbuf operations could have resulted in
572181060Scsjp	 * the supplied buffer being overflowed.  Check to see if this is the
573181060Scsjp	 * case.
574181060Scsjp	 */
575181060Scsjp	if (sbuf_overflowed(&sbf) != 0) {
576181060Scsjp		cpath[0] = '\0';
577181060Scsjp		return;
578181060Scsjp	}
579181060Scsjp	sbuf_finish(&sbf);
580155192Srwatson}
581