Skip to content
Snippets Groups Projects
audit.c 40.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		printk(" (until reboot)");
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    __setup("audit=", audit_enable);
    
    
    static void audit_buffer_free(struct audit_buffer *ab)
    {
    	unsigned long flags;
    
    
    	if (ab->skb)
    		kfree_skb(ab->skb);
    
    	spin_lock_irqsave(&audit_freelist_lock, flags);
    
    	if (audit_freelist_count > AUDIT_MAXFREE)
    
    	else {
    		audit_freelist_count++;
    
    		list_add(&ab->list, &audit_freelist);
    
    	spin_unlock_irqrestore(&audit_freelist_lock, flags);
    }
    
    
    static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
    
    						gfp_t gfp_mask, int type)
    
    {
    	unsigned long flags;
    	struct audit_buffer *ab = NULL;
    
    	struct nlmsghdr *nlh;
    
    
    	spin_lock_irqsave(&audit_freelist_lock, flags);
    	if (!list_empty(&audit_freelist)) {
    		ab = list_entry(audit_freelist.next,
    				struct audit_buffer, list);
    		list_del(&ab->list);
    		--audit_freelist_count;
    	}
    	spin_unlock_irqrestore(&audit_freelist_lock, flags);
    
    	if (!ab) {
    
    		ab = kmalloc(sizeof(*ab), gfp_mask);
    
    
    	ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask);
    	if (!ab->skb)
    
    	nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0);
    	if (!nlh)
    		goto out_kfree_skb;
    
    	kfree_skb(ab->skb);
    	ab->skb = NULL;
    
    err:
    	audit_buffer_free(ab);
    	return NULL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    /**
     * audit_serial - compute a serial number for the audit record
     *
     * Compute a serial number for the audit record.  Audit records are
    
     * written to user-space as soon as they are generated, so a complete
     * audit record may be written in several pieces.  The timestamp of the
     * record and this serial number are used by the user-space tools to
     * determine which pieces belong to the same audit record.  The
     * (timestamp,serial) tuple is unique for each syscall and is live from
     * syscall entry to syscall exit.
     *
     * NOTE: Another possibility is to store the formatted records off the
     * audit context (for those records that have a context), and emit them
     * all at syscall exit.  However, this could delay the reporting of
     * significant errors until syscall exit (or never, if the system
    
    unsigned int audit_serial(void)
    {
    
    	static DEFINE_SPINLOCK(serial_lock);
    
    	static unsigned int serial = 0;
    
    	unsigned long flags;
    	unsigned int ret;
    
    	spin_lock_irqsave(&serial_lock, flags);
    
    		ret = ++serial;
    	} while (unlikely(!ret));
    
    	spin_unlock_irqrestore(&serial_lock, flags);
    
    static inline void audit_get_stamp(struct audit_context *ctx,
    
    				   struct timespec *t, unsigned int *serial)
    {
    
    	if (!ctx || !auditsc_get_stamp(ctx, t, serial)) {
    
    		*t = CURRENT_TIME;
    		*serial = audit_serial();
    	}
    }
    
    
    /*
     * Wait for auditd to drain the queue a little
     */
    static void wait_for_auditd(unsigned long sleep_time)
    {
    	DECLARE_WAITQUEUE(wait, current);
    	set_current_state(TASK_INTERRUPTIBLE);
    	add_wait_queue(&audit_backlog_wait, &wait);
    
    	if (audit_backlog_limit &&
    	    skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
    		schedule_timeout(sleep_time);
    
    	__set_current_state(TASK_RUNNING);
    	remove_wait_queue(&audit_backlog_wait, &wait);
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /* Obtain an audit buffer.  This routine does locking to obtain the
     * audit buffer, but then no locking is required for calls to
     * audit_log_*format.  If the tsk is a task that is currently in a
     * syscall, then the syscall is marked as auditable and an audit record
     * will be written at syscall exit.  If there is no associated task, tsk
     * should be NULL. */
    
    /**
     * audit_log_start - obtain an audit buffer
     * @ctx: audit_context (may be NULL)
     * @gfp_mask: type of allocation
     * @type: audit message type
     *
     * Returns audit_buffer pointer on success or NULL on error.
     *
     * Obtain an audit buffer.  This routine does locking to obtain the
     * audit buffer, but then no locking is required for calls to
     * audit_log_*format.  If the task (ctx) is a task that is currently in a
     * syscall, then the syscall is marked as auditable and an audit record
     * will be written at syscall exit.  If there is no associated task, then
     * task context (ctx) should be NULL.
     */
    
    Al Viro's avatar
    Al Viro committed
    struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct audit_buffer	*ab	= NULL;
    	struct timespec		t;
    
    	unsigned int		uninitialized_var(serial);
    
    	unsigned long timeout_start = jiffies;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (audit_initialized != AUDIT_INITIALIZED)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return NULL;
    
    
    	if (unlikely(audit_filter_type(type)))
    		return NULL;
    
    
    	if (gfp_mask & __GFP_WAIT)
    		reserve = 0;
    	else
    
    		reserve = 5; /* Allow atomic callers to go up to five
    
    				entries over the normal backlog limit */
    
    	while (audit_backlog_limit
    	       && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
    
    		if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time) {
    			unsigned long sleep_time;
    
    			sleep_time = timeout_start + audit_backlog_wait_time -
    					jiffies;
    			if ((long)sleep_time > 0)
    				wait_for_auditd(sleep_time);
    
    		if (audit_rate_check() && printk_ratelimit())
    
    			printk(KERN_WARNING
    			       "audit: audit_backlog=%d > "
    			       "audit_backlog_limit=%d\n",
    			       skb_queue_len(&audit_skb_queue),
    			       audit_backlog_limit);
    		audit_log_lost("backlog limit exceeded");
    
    		audit_backlog_wait_time = audit_backlog_wait_overflow;
    		wake_up(&audit_backlog_wait);
    
    	ab = audit_buffer_alloc(ctx, gfp_mask, type);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!ab) {
    		audit_log_lost("out of memory in audit_log_start");
    		return NULL;
    	}
    
    
    	audit_get_stamp(ab->ctx, &t, &serial);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	audit_log_format(ab, "audit(%lu.%03lu:%u): ",
    			 t.tv_sec, t.tv_nsec/1000000, serial);
    	return ab;
    }
    
    
     * audit_expand - expand skb in the audit buffer
    
     * @ab: audit_buffer
    
     * @extra: space to add at tail of the skb
    
     *
     * Returns 0 (no space) on failed expansion, or available space if
     * successful.
     */
    
    static inline int audit_expand(struct audit_buffer *ab, int extra)
    
    	struct sk_buff *skb = ab->skb;
    
    	int oldtail = skb_tailroom(skb);
    	int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
    	int newtail = skb_tailroom(skb);
    
    
    	if (ret < 0) {
    		audit_log_lost("out of memory in audit_expand");
    
    
    	skb->truesize += newtail - oldtail;
    	return newtail;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    /*
     * Format an audit message into the audit buffer.  If there isn't enough
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * room in the audit buffer, more room will be allocated and vsnprint
     * will be called a second time.  Currently, we assume that a printk
    
     * can't format message larger than 1024 bytes, so we don't either.
     */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
    			      va_list args)
    {
    	int len, avail;
    
    	struct sk_buff *skb;
    
    	va_list args2;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (!ab)
    		return;
    
    
    	BUG_ON(!ab->skb);
    	skb = ab->skb;
    	avail = skb_tailroom(skb);
    	if (avail == 0) {
    
    		avail = audit_expand(ab, AUDIT_BUFSIZ);
    
    		if (!avail)
    			goto out;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	va_copy(args2, args);
    
    	len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (len >= avail) {
    		/* The printk buffer is 1024 bytes long, so if we get
    		 * here and AUDIT_BUFSIZ is at least 1024, then we can
    		 * log everything that printk could have logged. */
    
    		avail = audit_expand(ab,
    			max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
    
    			goto out_va_end;
    
    		len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	if (len > 0)
    		skb_put(skb, len);
    
    out_va_end:
    	va_end(args2);
    
    /**
     * audit_log_format - format a message into the audit buffer.
     * @ab: audit_buffer
     * @fmt: format string
     * @...: optional parameters matching @fmt string
     *
     * All the work is done in audit_log_vformat.
     */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
    {
    	va_list args;
    
    	if (!ab)
    		return;
    	va_start(args, fmt);
    	audit_log_vformat(ab, fmt, args);
    	va_end(args);
    }
    
    
    /**
     * audit_log_hex - convert a buffer to hex and append it to the audit skb
     * @ab: the audit_buffer
     * @buf: buffer to convert to hex
     * @len: length of @buf to be converted
     *
     * No return value; failure to expand is silently ignored.
     *
     * This function will take the passed buf and convert it into a string of
     * ascii hex digits. The new string is placed onto the skb.
     */
    
    void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
    
    	int i, avail, new_len;
    	unsigned char *ptr;
    	struct sk_buff *skb;
    	static const unsigned char *hex = "0123456789ABCDEF";
    
    
    	BUG_ON(!ab->skb);
    	skb = ab->skb;
    	avail = skb_tailroom(skb);
    	new_len = len<<1;
    	if (new_len >= avail) {
    		/* Round the buffer request up to the next multiple */
    		new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
    		avail = audit_expand(ab, new_len);
    		if (!avail)
    			return;
    	}
    
    	ptr = skb_tail_pointer(skb);
    
    	for (i=0; i<len; i++) {
    		*ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
    		*ptr++ = hex[buf[i] & 0x0F];	  /* Lower nibble */
    	}
    	*ptr = 0;
    	skb_put(skb, len << 1); /* new string is twice the old string */
    
    /*
     * Format a string of no more than slen characters into the audit buffer,
     * enclosed in quote marks.
     */
    
    void audit_log_n_string(struct audit_buffer *ab, const char *string,
    			size_t slen)
    
    {
    	int avail, new_len;
    	unsigned char *ptr;
    	struct sk_buff *skb;
    
    
    	BUG_ON(!ab->skb);
    	skb = ab->skb;
    	avail = skb_tailroom(skb);
    	new_len = slen + 3;	/* enclosing quotes + null terminator */
    	if (new_len > avail) {
    		avail = audit_expand(ab, new_len);
    		if (!avail)
    			return;
    	}
    
    	ptr = skb_tail_pointer(skb);
    
    	*ptr++ = '"';
    	memcpy(ptr, string, slen);
    	ptr += slen;
    	*ptr++ = '"';
    	*ptr = 0;
    	skb_put(skb, slen + 2);	/* don't include null terminator */
    }
    
    
    /**
     * audit_string_contains_control - does a string need to be logged in hex
    
     * @string: string to be checked
     * @len: max length of the string to check
    
     */
    int audit_string_contains_control(const char *string, size_t len)
    {
    	const unsigned char *p;
    
    	for (p = string; p < (const unsigned char *)string + len; p++) {
    
    		if (*p == '"' || *p < 0x21 || *p > 0x7e)
    
     * audit_log_n_untrustedstring - log a string that may contain random characters
    
     * @ab: audit_buffer
    
     * @len: length of string (not including trailing null)
    
     * @string: string to be logged
     *
     * This code will escape a string that is passed to it if the string
     * contains a control character, unprintable character, double quote mark,
    
     * or a space. Unescaped strings will start and end with a double quote mark.
    
     * Strings that are escaped are printed in hex (2 digits per char).
    
     *
     * The caller specifies the number of characters in the string to log, which may
     * or may not be the entire string.
    
    void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
    				 size_t len)
    
    	if (audit_string_contains_control(string, len))
    
    		audit_log_n_hex(ab, string, len);
    
    		audit_log_n_string(ab, string, len);
    
     * audit_log_untrustedstring - log a string that may contain random characters
    
     * @ab: audit_buffer
     * @string: string to be logged
     *
    
     * Same as audit_log_n_untrustedstring(), except that strlen is used to
    
     * determine string length.
     */
    
    void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
    
    	audit_log_n_untrustedstring(ab, string, strlen(string));
    
    /* This is a helper-function to print the escaped d_path */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
    
    		      const struct path *path)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	char *p, *pathname;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		audit_log_format(ab, "%s", prefix);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* We will allow 11 spaces for ' (deleted)' to be appended */
    
    	pathname = kmalloc(PATH_MAX+11, ab->gfp_mask);
    	if (!pathname) {
    
    		audit_log_string(ab, "<no_memory>");
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	p = d_path(path, pathname, PATH_MAX+11);
    
    	if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
    		/* FIXME: can we save some information here? */
    
    		audit_log_string(ab, "<too_long>");
    
    		audit_log_untrustedstring(ab, p);
    
    void audit_log_key(struct audit_buffer *ab, char *key)
    {
    	audit_log_format(ab, " key=");
    	if (key)
    		audit_log_untrustedstring(ab, key);
    	else
    		audit_log_format(ab, "(null)");
    }
    
    
    /**
     * audit_log_link_denied - report a link restriction denial
     * @operation: specific link opreation
     * @link: the path that triggered the restriction
     */
    void audit_log_link_denied(const char *operation, struct path *link)
    {
    	struct audit_buffer *ab;
    
    	ab = audit_log_start(current->audit_context, GFP_KERNEL,
    			     AUDIT_ANOM_LINK);
    
    	audit_log_format(ab, "op=%s action=denied", operation);
    	audit_log_format(ab, " pid=%d comm=", current->pid);
    	audit_log_untrustedstring(ab, current->comm);
    	audit_log_d_path(ab, " path=", link);
    	audit_log_format(ab, " dev=");
    	audit_log_untrustedstring(ab, link->dentry->d_inode->i_sb->s_id);
    	audit_log_format(ab, " ino=%lu", link->dentry->d_inode->i_ino);
    	audit_log_end(ab);
    }
    
    
    /**
     * audit_log_end - end one audit record
     * @ab: the audit_buffer
     *
     * The netlink_* functions cannot be called inside an irq context, so
     * the audit buffer is placed on a queue and a tasklet is scheduled to
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * remove them from the queue outside the irq context.  May be called in
    
    void audit_log_end(struct audit_buffer *ab)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	if (!ab)
    		return;
    	if (!audit_rate_check()) {
    		audit_log_lost("rate limit exceeded");
    	} else {
    
    		struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
    
    		nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
    
    
    		if (audit_pid) {
    			skb_queue_tail(&audit_skb_queue, ab->skb);
    			wake_up_interruptible(&kauditd_wait);
    
    			audit_printk_skb(ab->skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    /**
     * audit_log - Log an audit record
     * @ctx: audit context
     * @gfp_mask: type of allocation
     * @type: audit message type
     * @fmt: format string to use
     * @...: variable parameters matching the format string
     *
     * This is a convenience function that calls audit_log_start,
     * audit_log_vformat, and audit_log_end.  It may be called
     * in any context.
     */
    
    void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct audit_buffer *ab;
    	va_list args;
    
    
    	ab = audit_log_start(ctx, gfp_mask, type);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (ab) {
    		va_start(args, fmt);
    		audit_log_vformat(ab, fmt, args);
    		va_end(args);
    		audit_log_end(ab);
    	}
    }
    
    #ifdef CONFIG_SECURITY
    /**
     * audit_log_secctx - Converts and logs SELinux context
     * @ab: audit_buffer
     * @secid: security number
     *
     * This is a helper function that calls security_secid_to_secctx to convert
     * secid to secctx and then adds the (converted) SELinux context to the audit
     * log by calling audit_log_format, thus also preventing leak of internal secid
     * to userspace. If secid cannot be converted audit_panic is called.
     */
    void audit_log_secctx(struct audit_buffer *ab, u32 secid)
    {
    	u32 len;
    	char *secctx;
    
    	if (security_secid_to_secctx(secid, &secctx, &len)) {
    		audit_panic("Cannot convert secid to context");
    	} else {
    		audit_log_format(ab, " obj=%s", secctx);
    		security_release_secctx(secctx, len);
    	}
    }
    EXPORT_SYMBOL(audit_log_secctx);
    #endif
    
    
    EXPORT_SYMBOL(audit_log_start);
    EXPORT_SYMBOL(audit_log_end);
    EXPORT_SYMBOL(audit_log_format);
    EXPORT_SYMBOL(audit_log);