From owner-trustedbsd-cvs@FreeBSD.ORG Mon Jan 22 16:25:02 2007 Return-Path: X-Original-To: trustedbsd-cvs@freebsd.org Delivered-To: trustedbsd-cvs@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 1615B16A40F for ; Mon, 22 Jan 2007 16:25:02 +0000 (UTC) (envelope-from owner-perforce@freebsd.org) Received: from cyrus.watson.org (cyrus.watson.org [209.31.154.42]) by mx1.freebsd.org (Postfix) with ESMTP id 2CCB813C44B for ; Mon, 22 Jan 2007 16:25:01 +0000 (UTC) (envelope-from owner-perforce@freebsd.org) Received: from mx2.freebsd.org (mx2.freebsd.org [69.147.83.53]) by cyrus.watson.org (Postfix) with ESMTP id BA67A4883E for ; Mon, 22 Jan 2007 11:24:58 -0500 (EST) Received: from hub.freebsd.org (hub.freebsd.org [69.147.83.54]) by mx2.freebsd.org (Postfix) with ESMTP id 17E5DCFDD0; Mon, 22 Jan 2007 16:24:41 +0000 (GMT) (envelope-from owner-perforce@freebsd.org) Received: by hub.freebsd.org (Postfix, from userid 32767) id 8128E16A4F8; Mon, 22 Jan 2007 16:24:21 +0000 (UTC) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 4722B16A400 for ; Mon, 22 Jan 2007 16:24:21 +0000 (UTC) (envelope-from millert@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [69.147.83.41]) by mx1.freebsd.org (Postfix) with ESMTP id 2937F13C45A for ; Mon, 22 Jan 2007 16:24:21 +0000 (UTC) (envelope-from millert@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.6/8.13.6) with ESMTP id l0MGOLrd031673 for ; Mon, 22 Jan 2007 16:24:21 GMT (envelope-from millert@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.6/8.13.4/Submit) id l0MGOKvb031669 for perforce@freebsd.org; Mon, 22 Jan 2007 16:24:20 GMT (envelope-from millert@freebsd.org) Date: Mon, 22 Jan 2007 16:24:20 GMT Message-Id: <200701221624.l0MGOKvb031669@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to millert@freebsd.org using -f From: Todd Miller To: Perforce Change Reviews Cc: Subject: PERFORCE change 113365 for review X-BeenThere: trustedbsd-cvs@FreeBSD.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: TrustedBSD CVS and Perforce commit message list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 22 Jan 2007 16:25:02 -0000 http://perforce.freebsd.org/chv.cgi?CH=113365 Change 113365 by millert@millert_macbook on 2007/01/22 16:24:15 We can't hold the spinlock to protect the avc log printf() for as long as we do since vn_getpath() may sleep. Go back to allocating a new log struct for each avc audit message and only hold the spinlock for the duration of the printf(), which is safe. As an optimization, keep a spare log struct to avoid allocating when we don't need to (and since we are not allowed to sleep). Affected files ... .. //depot/projects/trustedbsd/sedarwin8/policies/sedarwin/sedarwin/avc/avc.c#19 edit .. //depot/projects/trustedbsd/sedarwin8/policies/sedarwin/sedarwin/avc/avc.h#9 edit .. //depot/projects/trustedbsd/sedarwin8/policies/sedarwin/sedarwin/avc/avc_audit.c#6 edit .. //depot/projects/trustedbsd/sedarwin8/policies/sedarwin/sedarwin/linux-compat.h#7 edit Differences ... ==== //depot/projects/trustedbsd/sedarwin8/policies/sedarwin/sedarwin/avc/avc.c#19 (text+ko) ==== @@ -270,10 +270,11 @@ lck_attr_setdefault(avc_lck_attr); /* no lock debugging */ /* allocate avc locks */ - avc_log_lock = lck_spin_alloc_init(avc_lck_grp, avc_lck_attr); avc_lock = lck_rw_alloc_init(avc_lck_grp, avc_lck_attr); notif_lock = lck_spin_alloc_init(avc_lck_grp, avc_lck_attr); + avc_audit_init(avc_lck_grp, avc_lck_attr); + for (i = 0; i < AVC_CACHE_SLOTS; i++) { LIST_INIT(&avc_cache.slots[i]); } @@ -603,7 +604,7 @@ return; } - ab = audit_log_start(); + ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_AVC); if (!ab) return; /* audit_panic has been called */ audit_log_format(ab, "avc: %s ", denied ? "denied" : "granted"); ==== //depot/projects/trustedbsd/sedarwin8/policies/sedarwin/sedarwin/avc/avc.h#9 (text+ko) ==== @@ -140,5 +140,7 @@ DECLARE_PER_CPU(struct avc_cache_stats, avc_cache_stats); #endif +void avc_audit_init(lck_grp_t *avc_lck_grp, lck_attr_t *avc_lck_attr); + #endif /* _SELINUX_AVC_H_ */ ==== //depot/projects/trustedbsd/sedarwin8/policies/sedarwin/sedarwin/avc/avc_audit.c#6 (text+ko) ==== @@ -38,8 +38,10 @@ #include #include +#include #include #include +#include /* * Emulate Linux audit API. @@ -52,16 +54,40 @@ struct sbuf sbuf; char buf[1024]; }; -static struct audit_buffer audit_buf; + +static zone_t avc_audit_zone; /* audit buffer zone */ +static struct audit_buffer *spare_buf; /* spare buffer */ + +void +avc_audit_init(lck_grp_t *avc_lck_grp, lck_attr_t *avc_lck_attr) +{ + + avc_log_lock = lck_spin_alloc_init(avc_lck_grp, avc_lck_attr); + avc_audit_zone = mac_zinit(sizeof(struct audit_buffer), + 512 * sizeof(struct audit_buffer), 0, "avc_audit"); + spare_buf = mac_zalloc(avc_audit_zone, M_WAITOK); +} struct audit_buffer * -audit_log_start(void) +_audit_log_start(int flag) { + struct audit_buffer *ab = spare_buf; - lck_spin_lock(avc_log_lock); - sbuf_new(&audit_buf.sbuf, audit_buf.buf, sizeof(audit_buf.buf), - SBUF_FIXEDLEN); - return (&audit_buf); + /* Use a free buffer if available, else alloc a new one. */ + if (ab != NULL) { + if (OSCompareAndSwap((UInt32)ab, 0, (UInt32 *)&spare_buf) == 0) + ab = NULL; + } + if (ab == NULL) { + ab = mac_zalloc(avc_audit_zone, flag); + if (ab == NULL) { + printf("%s: unable to allocate audit buffer\n", + __func__); + return (NULL); + } + } + sbuf_new(&ab->sbuf, ab->buf, sizeof(ab->buf), SBUF_FIXEDLEN); + return (ab); } void @@ -69,9 +95,13 @@ { sbuf_finish(&ab->sbuf); + lck_spin_lock(avc_log_lock); printf("\n%s\n", sbuf_data(&ab->sbuf)); - sbuf_delete(&ab->sbuf); /* XXX - not really needed */ lck_spin_unlock(avc_log_lock); + /* Always keep a free buffer around. */ + if (spare_buf != NULL || + OSCompareAndSwap(0, (UInt32)ab, (UInt32 *)&spare_buf) == 0) + mac_zfree(avc_audit_zone, ab); } void @@ -85,19 +115,6 @@ } void -sebsd_log(const char *fmt, ...) -{ - struct audit_buffer *ab; - va_list ap; - - ab = audit_log_start(); - va_start(ap, fmt); - sbuf_vprintf(&ab->sbuf, fmt, ap); - va_end(ap); - audit_log_end(ab); -} - -void audit_log_untrustedstring(struct audit_buffer *ab, const char *s) { ==== //depot/projects/trustedbsd/sedarwin8/policies/sedarwin/sedarwin/linux-compat.h#7 (text+ko) ==== @@ -137,12 +137,18 @@ /* emulate linux audit support */ extern lck_spin_t *avc_log_lock; struct audit_buffer; -struct audit_buffer *audit_log_start(void); -void sebsd_log(const char *, ...); +struct audit_buffer *_audit_log_start(int); void audit_log_end(struct audit_buffer *); void audit_log_format(struct audit_buffer *, const char *, ...); void audit_log_untrustedstring(struct audit_buffer *, const char *); -#define audit_log(ac, mf, af, ...) sebsd_log(__VA_ARGS__) +#define audit_log_start(ac, mf, af) _audit_log_start(mf) +#define audit_log(ac, mf, af, ...) do { \ + lck_spin_lock(avc_log_lock); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + lck_spin_unlock(avc_log_lock); \ +} while (0) +#define sebsd_log(fmt, ...) printf(fmt "\n", __VA_ARGS__) /* we don't enable the selinux netlbl support */ #define selinux_netlbl_cache_invalidate()