From owner-p4-projects@FreeBSD.ORG Thu Feb 15 20:28:12 2007 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id DFB5F16A408; Thu, 15 Feb 2007 20:28:11 +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 BDADA16A400 for ; Thu, 15 Feb 2007 20:28:11 +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 A286D13C4B5 for ; Thu, 15 Feb 2007 20:28:11 +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 l1FKSBYW073747 for ; Thu, 15 Feb 2007 20:28:11 GMT (envelope-from millert@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.6/8.13.4/Submit) id l1FKSBgm073734 for perforce@freebsd.org; Thu, 15 Feb 2007 20:28:11 GMT (envelope-from millert@freebsd.org) Date: Thu, 15 Feb 2007 20:28:11 GMT Message-Id: <200702152028.l1FKSBgm073734@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 114584 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Feb 2007 20:28:12 -0000 http://perforce.freebsd.org/chv.cgi?CH=114584 Change 114584 by millert@millert_p4 on 2007/02/15 20:27:17 Make avc audit rouines use uma_zalloc() and keep a spare buffer around for efficiency. Also add some more types to linux-compat.h and remove the non-kernel pieces. Affected files ... .. //depot/projects/trustedbsd/sebsd/sys/security/sebsd/avc/avc_audit.c#2 edit .. //depot/projects/trustedbsd/sebsd/sys/security/sebsd/linux-compat.h#5 edit Differences ... ==== //depot/projects/trustedbsd/sebsd/sys/security/sebsd/avc/avc_audit.c#2 (text+ko) ==== @@ -33,11 +33,13 @@ #include #include #include +#include #include #include #include +#include /* * Emulate Linux audit API. @@ -45,7 +47,7 @@ * TBD: use a freelist so we don't have to mallc/free so much. */ -static struct mtx avc_log_lock; +struct mtx avc_log_lock; MTX_SYSINIT(avc_log_lock, &avc_log_lock, "SEBSD message lock", MTX_DEF); struct audit_buffer { @@ -53,15 +55,34 @@ char buf[1024]; }; +static uma_zone_t avc_audit_zone; /* audit buffer zone */ +static struct audit_buffer *spare_buf; /* spare buffer */ + +void +avc_audit_init(void) +{ + + avc_audit_zone = uma_zcreate("avc_audit", sizeof(struct audit_buffer), + NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); + spare_buf = uma_zalloc(avc_audit_zone, M_WAITOK); +} + struct audit_buffer * -audit_log_start(void) +_audit_log_start(int flag) { - struct audit_buffer *ab; + struct audit_buffer *ab = spare_buf; - ab = sebsd_malloc(sizeof(*ab), M_SEBSD, M_NOWAIT); + /* Use a free buffer if available, else alloc a new one. */ + if (ab != NULL && + atomic_cmpset_ptr((intptr_t *)&spare_buf, (intptr_t)ab, 0) == 0) + ab = NULL; if (ab == NULL) { - printf("%s: unable to allocate audit buffer\n", __func__); - return (NULL); + ab = uma_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); @@ -75,24 +96,13 @@ mtx_lock(&avc_log_lock); printf("\n%s\n", sbuf_data(&ab->sbuf)); mtx_unlock(&avc_log_lock); - sbuf_delete(&ab->sbuf); - sebsd_free(ab, M_SEBSD); + /* Always keep a free buffer around. */ + if (spare_buf != NULL || + atomic_cmpset_ptr((intptr_t *)&spare_buf, 0, (intptr_t)ab) == 0) + uma_zfree(avc_audit_zone, ab); } void -audit_log(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - mtx_lock(&avc_log_lock); - vprintf(fmt, ap); - printf("\n"); - mtx_unlock(&avc_log_lock); - va_end(ap); -} - -void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) { va_list ap; @@ -106,5 +116,5 @@ audit_log_untrustedstring(struct audit_buffer *ab, const char *s) { - sbuf_cat(&ab->sbuf, s); + sbuf_cat(&ab->sbuf, s); /* XXX - wants vis(3) support */ } ==== //depot/projects/trustedbsd/sebsd/sys/security/sebsd/linux-compat.h#5 (text+ko) ==== @@ -48,51 +48,31 @@ #include #include +#include typedef u_int64_t u64; typedef u_int64_t __le64; typedef u_int32_t u32; typedef u_int32_t __le32; +typedef u_int32_t __be32; typedef u_int16_t u16; typedef u_int16_t __le16; typedef u_int16_t __be16; typedef u_int8_t u8; +typedef int gfp_t; -#ifndef _KERNEL - -#if BYTE_ORDER == LITTLE_ENDIAN -#define cpu_to_le16(x) ((__uint16_t)(x)) -#define cpu_to_le32(x) ((__uint32_t)(x)) -#define cpu_to_le64(x) ((__uint64_t)(x)) -#define le16_to_cpu(x) ((__uint16_t)(x)) -#define le32_to_cpu(x) ((__uint32_t)(x)) -#define le64_to_cpu(x) ((__uint64_t)(x)) -#else /* BYTE_ORDER != LITTLE_ENDIAN */ -#define cpu_to_le16(x) bswap16((x)) -#define cpu_to_le32(x) bswap32((x)) -#define cpu_to_le64(x) bswap64((x)) -#define le16_to_cpu(x) bswap16((x)) -#define le32_to_cpu(x) bswap32((x)) -#define le64_to_cpu(x) bswap64((x)) -#endif /* BYTE_ORDER */ - -/* sebsd uses same ss source files for userspace */ - -#define kcalloc(nmemb, size, flags) calloc(nmemb, size) -#define kmalloc(size,flags) malloc(size) -#define kzalloc(size,flags) calloc(1, size) -#define kfree(v) free(v) -#define __get_free_page(flags) malloc (4096) /* XXX need page size */ -#define GFP_ATOMIC 1 -#define GFP_KERNEL 2 - -#else /* _KERNEL */ - +#define cpu_to_le16(a) htole16(a) +#define cpu_to_le32(a) htole32(a) +#define cpu_to_le64(a) htole64(a) #define le16_to_cpu(a) le16toh(a) #define le32_to_cpu(a) le32toh(a) #define le64_to_cpu(a) le64toh(a) +/* branch prediction macros, uses a GCC extension. */ +#define likely(exp) __builtin_expect(!!(exp), 1) +#define unlikely(exp) __builtin_expect(!!(exp), 0) + #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define NIPQUAD(addr) \ @@ -104,13 +84,13 @@ #define __init /* kmalloc */ -#define kcalloc(nmemb, size, flags) sebsd_malloc(nmemb * size, M_SEBSD, flags | M_ZERO) -#define kmalloc(size,flags) malloc(size, M_SEBSD, flags) -#define kzalloc(size,flags) malloc(size, M_SEBSD, flags | M_ZERO) -#define kfree(v) free(v, M_SEBSD) -#define __get_free_page(flags) malloc(4096, M_SEBSD, flags) /* XXX need page size */ #define GFP_ATOMIC M_NOWAIT #define GFP_KERNEL M_NOWAIT +#define kcalloc(nmemb, size, flags) malloc(nmemb * size, M_SEBSD, flags | M_ZERO) +#define kmalloc(size,flags) malloc(size, M_SEBSD, flags) +#define kzalloc(size,flags) malloc(size, M_SEBSD, flags | M_ZERO) +#define kfree(v) free(v, M_SEBSD) +#define __get_free_page(flags) malloc(4096, M_SEBSD, flags) /* XXX need page size */ /* also defined in sebsd.h */ #ifndef sebsd_malloc @@ -124,22 +104,46 @@ #define _M_SEBSD_DEF #endif -/* spinlock */ +static inline char * +kstrdup(const char *str, int mflag) +{ + char *newstr; + size_t len = strlen(str) + 1; + + newstr = malloc(len, M_SEBSD, mflag); + if (newstr != NULL) + memcpy(newstr, str, len); + return (newstr); +} + +/* FreeBSD has no spinlock, use mutex instead */ #define spinlock_t struct mtx #define spin_lock_irqsave(m,flags) mtx_lock(m) #define spin_unlock_irqrestore(m,flags) mtx_unlock(m) /* emulate linux audit support */ +extern struct mtx avc_log_lock; struct audit_buffer; -struct audit_buffer *audit_log_start(void); -void audit_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_start(ac, mf, af) _audit_log_start(mf) +#define audit_log(ac, mf, af, ...) do { \ + mtx_lock(&avc_log_lock); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + mtx_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() /* * Atomic integer operations, Linux style */ +typedef unsigned int atomic_t; #define atomic_inc(p) atomic_add_acq_32(p, 1) #define atomic_inc_return(p) atomic_fetchadd_32(p, 1) #define atomic_dec(p) atomic_subtract_acq_32(p, 1) @@ -150,8 +154,6 @@ /* FreeBSD has index() not strchr() in the kernel. */ #define strchr(s, c) index(s, c) -#endif /* _KERNEL */ - #define BUG() printf("BUG: %s:%d", __FILE__, __LINE__) #define BUG_ON(x) do { if (x) BUG(); } while(0)