4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
26 * This file contains the envelope code for system call auditing.
29 #include <sys/param.h>
30 #include <sys/types.h>
34 #include <sys/vnode.h>
37 #include <sys/stropts.h>
38 #include <sys/systm.h>
39 #include <sys/pathname.h>
40 #include <sys/debug.h>
44 #include <c2/audit_kernel.h>
45 #include <c2/audit_kevents.h>
46 #include <c2/audit_record.h>
47 #include "audit_door_infc.h"
49 extern uint_t num_syscall
; /* size of audit_s2e table */
50 extern kmutex_t pidlock
; /* proc table lock */
53 * Obsolete and ignored - Historically, the 'set c2audit:audit_load=1' entry
54 * in /etc/system enabled auditing. The No Reboot Audit project does not
55 * use this entry. However, to prevent the system from printing warning
56 * messages, the audit_load entry is being left in /etc/system. It will be
57 * removed when there is a small chance that the entry is used on currently
62 kmutex_t module_lock
; /* audit_module_state lock */
65 * Das Boot. Initialize first process. Also generate an audit record indicating
66 * that the system has been booted.
73 t_audit_data_t
*tad
= U2A(u
);
76 * Solaris Auditing module is being loaded -> change the state. The lock
77 * is here to prevent memory leaks caused by multiple initializations.
79 mutex_enter(&module_lock
);
80 if (audit_active
!= C2AUDIT_UNLOADED
) {
81 mutex_exit(&module_lock
);
84 audit_active
= C2AUDIT_LOADED
;
85 mutex_exit(&module_lock
);
87 /* initialize memory allocators */
91 * setup environment for asynchronous auditing. We can't use
92 * audit_async_start() here since it assumes the audit system
93 * has been started via auditd(1m). auditd sets the variable,
94 * auk_auditstate, to indicate audit record generation should
95 * commence. Here we want to always generate an audit record.
98 /* process audit policy (AUDIT_AHLT) for asynchronous events */
99 audit_async_drop((caddr_t
*)(&rp
), 0);
103 ASSERT(tad
->tad_errjmp
== NULL
);
104 tad
->tad_errjmp
= (void *)&jb
;
105 tad
->tad_ctrl
|= TAD_ERRJMP
;
107 /* generate a system-booted audit record */
108 au_write((caddr_t
*)&rp
, au_to_text("booting kernel"));
109 audit_async_finish((caddr_t
*)&rp
, AUE_SYSTEMBOOT
, NULL
,
110 &(p0
.p_user
.u_start
));
115 * Enter system call. Do any necessary setup here. allocate resouces, etc.
118 #include <sys/syscall.h>
126 uint32_t audit_state
,
130 struct t_audit_data
*tad
;
136 /* Remember the audit state in the cache */
137 tad
->tad_audit
= audit_state
;
145 audit_update_context(curproc
, NULL
);
148 * if this is an indirect system call then don't do anything.
149 * audit_start will be called again from indir() in trap.c
156 if (scid
>= num_syscall
)
160 * we can no longer depend on a valid lwp_ap, so we need to
161 * copy the syscall args as future audit stuff may need them.
163 (void) save_syscall_args();
166 * We need to gather paths for certain system calls even if they are
167 * not audited so that we can audit the various f* calls and be
168 * sure to have a CWD and CAR. Thus we thus set tad_ctrl over the
169 * system call regardless if the call is audited or not.
170 * We allow the event specific initial processing routines (au_init)
171 * to adjust the tad_ctrl as necessary.
173 tad
->tad_ctrl
= audit_s2e
[scid
].au_ctrl
;
174 tad
->tad_scid
= scid
;
176 /* get basic event for system call */
177 tad
->tad_event
= audit_s2e
[scid
].au_event
;
178 if (audit_s2e
[scid
].au_init
!= (au_event_t
)AUE_NULL
) {
179 /* get specific event */
180 tad
->tad_event
= (*audit_s2e
[scid
].au_init
)(tad
->tad_event
);
185 /* now do preselection. Audit or not to Audit, that is the question */
186 if ((tad
->tad_flag
= auditme(kctx
, tad
,
187 kctx
->auk_ets
[tad
->tad_event
])) == 0) {
189 * we assume that audit_finish will always be called.
195 * if auditing not enabled, then don't generate an audit record
196 * and don't count it.
198 if (audit_state
& ~(AUC_AUDITING
| AUC_INIT_AUDIT
)) {
200 * we assume that audit_finish will always be called.
207 * audit daemon has informed us that there is no longer any
208 * space left to hold audit records. We decide here if records
209 * should be dropped (but counted).
211 if (audit_state
== AUC_NOSPACE
) {
212 if ((kctx
->auk_policy
& AUDIT_CNT
) ||
213 (kctx
->auk_policy
& AUDIT_SCNT
)) {
214 /* assume that audit_finish will always be called. */
217 /* just count # of dropped audit records */
218 AS_INC(as_dropped
, 1, kctx
);
226 if (audit_s2e
[scid
].au_start
!= NULL
) {
227 /* do start of system call processing */
228 (*audit_s2e
[scid
].au_start
)(tad
);
235 * system call has completed. Now determine if we genearate an audit record
246 struct t_audit_data
*tad
;
248 au_defer_info_t
*attr
;
249 au_kcontext_t
*kctx
= GET_KCTX_PZ
;
254 * Process all deferred events first.
256 attr
= tad
->tad_defer_head
;
257 while (attr
!= NULL
) {
258 au_defer_info_t
*tmp_attr
= attr
;
260 au_close_time(kctx
, (token_t
*)attr
->audi_ad
, attr
->audi_flag
,
261 attr
->audi_e_type
, attr
->audi_e_mod
, &(attr
->audi_atime
));
263 attr
= attr
->audi_next
;
264 kmem_free(tmp_attr
, sizeof (au_defer_info_t
));
266 tad
->tad_defer_head
= tad
->tad_defer_tail
= NULL
;
268 if (tad
->tad_flag
== 0 && !(tad
->tad_ctrl
& TAD_SAVPATH
)) {
270 * clear the ctrl flag so that we don't have spurious
271 * collection of audit information.
277 tad
->tad_audit
= AUC_UNSET
;
278 ASSERT(tad
->tad_aupath
== NULL
);
282 scid
= tad
->tad_scid
;
285 * Perform any extra processing and determine if we are
286 * really going to generate any audit record.
288 if (audit_s2e
[scid
].au_finish
!= NULL
) {
289 /* do any post system call processing */
290 (*audit_s2e
[scid
].au_finish
)(tad
, error
, rval
);
295 if (flag
= audit_success(kctx
, tad
, error
, NULL
)) {
296 unsigned int sy_flags
;
298 const auditinfo_addr_t
*ainfo
= crgetauinfo(cr
);
300 ASSERT(ainfo
!= NULL
);
302 /* Add subject information */
303 AUDIT_SETSUBJ(&(u_ad
), cr
, ainfo
, kctx
);
305 if (tad
->tad_evmod
& PAD_SPRIVUSE
) {
307 au_to_privset("", &tad
->tad_sprivs
,
311 if (tad
->tad_evmod
& PAD_FPRIVUSE
) {
313 au_to_privset("", &tad
->tad_fprivs
,
317 /* Add a return token */
318 #ifdef _SYSCALL32_IMPL
319 if (lwp_getdatamodel(ttolwp(curthread
)) ==
321 sy_flags
= sysent
[scid
].sy_flags
& SE_RVAL_MASK
;
324 sysent32
[scid
].sy_flags
& SE_RVAL_MASK
;
326 #else /* _SYSCALL64_IMPL */
327 sy_flags
= sysent
[scid
].sy_flags
& SE_RVAL_MASK
;
328 #endif /* _SYSCALL32_IMPL */
330 if (sy_flags
== SE_32RVAL1
) {
333 au_to_return32(error
, 0));
335 au_write(&(u_ad
), au_to_return32(error
,
339 if (sy_flags
== (SE_32RVAL2
|SE_32RVAL1
)) {
342 au_to_return32(error
, 0));
345 au_to_return32(error
,
347 #ifdef NOTYET /* for possible future support */
348 au_write(&(u_ad
), au_to_return32(error
,
353 if (sy_flags
== SE_64RVAL
) {
356 au_to_return64(error
, 0));
358 au_write(&(u_ad
), au_to_return64(error
,
363 AS_INC(as_generated
, 1, kctx
);
364 AS_INC(as_kernel
, 1, kctx
);
367 /* Close up everything */
368 au_close(kctx
, &(u_ad
), flag
, tad
->tad_event
, tad
->tad_evmod
,
372 ASSERT(u_ad
== NULL
);
374 /* free up any space remaining with the path's */
375 if (tad
->tad_aupath
!= NULL
) {
376 au_pathrele(tad
->tad_aupath
);
377 tad
->tad_aupath
= NULL
;
380 /* free up any space remaining with openat path's */
381 if (tad
->tad_atpath
) {
382 au_pathrele(tad
->tad_atpath
);
383 tad
->tad_atpath
= NULL
;
387 * clear the ctrl flag so that we don't have spurious collection of
394 tad
->tad_audit
= AUC_UNSET
;
398 audit_success(au_kcontext_t
*kctx
, struct t_audit_data
*tad
, int error
,
404 const auditinfo_addr_t
*ainfo
;
406 ess
= esf
= kctx
->auk_ets
[tad
->tad_event
];
409 tad
->tad_evmod
|= PAD_FAILURE
;
411 /* see if we really want to generate an audit record */
412 if (tad
->tad_ctrl
& TAD_NOAUDIT
)
416 * Used passed cred if available, otherwise use cred from kernel thread
420 ainfo
= crgetauinfo(cr
);
423 amask
= ainfo
->ai_mask
;
426 return ((ess
& amask
.as_success
) ? AU_OK
: 0);
428 return ((esf
& amask
.as_failure
) ? AU_OK
: 0);
432 * determine if we've preselected this event (system call).
435 auditme(au_kcontext_t
*kctx
, struct t_audit_data
*tad
, au_state_t estate
)
439 const auditinfo_addr_t
*ainfo
;
441 ainfo
= crgetauinfo(CRED());
444 amask
= ainfo
->ai_mask
;
446 /* preselected system call */
448 if (amask
.as_success
& estate
|| amask
.as_failure
& estate
) {
450 } else if ((tad
->tad_scid
== SYS_putmsg
) ||
451 (tad
->tad_scid
== SYS_getmsg
)) {
452 estate
= kctx
->auk_ets
[AUE_SOCKCONNECT
] |
453 kctx
->auk_ets
[AUE_SOCKACCEPT
] |
454 kctx
->auk_ets
[AUE_SOCKSEND
] |
455 kctx
->auk_ets
[AUE_SOCKRECEIVE
];
456 if (amask
.as_success
& estate
|| amask
.as_failure
& estate
)
458 } else if (tad
->tad_scid
== SYS_execve
&&
459 getpflags(PRIV_PFEXEC
, CRED()) != 0) {
460 estate
= kctx
->auk_ets
[AUE_PFEXEC
];
461 if (amask
.as_success
& estate
|| amask
.as_failure
& estate
)