Skip to content
Snippets Groups Projects
audit.c 50.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    		spin_lock(&tsk->sighand->siglock);
    		old.enabled = tsk->signal->audit_tty;
    		old.log_passwd = tsk->signal->audit_tty_log_passwd;
    
    		if (!err) {
    			tsk->signal->audit_tty = s.enabled;
    			tsk->signal->audit_tty_log_passwd = s.log_passwd;
    		}
    
    		spin_unlock(&tsk->sighand->siglock);
    
    		audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE);
    
    		audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d"
    				 " old-log_passwd=%d new-log_passwd=%d res=%d",
    				 old.enabled, s.enabled, old.log_passwd,
    				 s.log_passwd, !err);
    
    		audit_log_end(ab);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	default:
    		err = -EINVAL;
    		break;
    	}
    
    	return err < 0 ? err : 0;
    }
    
    
     * Get message from skb.  Each message is processed by audit_receive_msg.
     * Malformed skbs with wrong length are discarded silently.
    
    static void audit_receive_skb(struct sk_buff *skb)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nlmsghdr *nlh;
    	/*
    
    	 * len MUST be signed for nlmsg_next to be able to dec it below 0
    
    	 * if the nlmsg_len was not aligned
    	 */
    	int len;
    	int err;
    
    	nlh = nlmsg_hdr(skb);
    	len = skb->len;
    
    
    	while (nlmsg_ok(nlh, len)) {
    
    		err = audit_receive_msg(skb, nlh);
    		/* if err or if this message says it wants a response */
    		if (err || (nlh->nlmsg_flags & NLM_F_ACK))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			netlink_ack(skb, nlh, err);
    
    		nlh = nlmsg_next(nlh, &len);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    }
    
    /* Receive messages from netlink socket. */
    
    static void audit_receive(struct sk_buff  *skb)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	mutex_lock(&audit_cmd_mutex);
    
    	mutex_unlock(&audit_cmd_mutex);
    
    static int __net_init audit_net_init(struct net *net)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct netlink_kernel_cfg cfg = {
    		.input	= audit_receive,
    	};
    
    	struct audit_net *aunet = net_generic(net, audit_net_id);
    
    	pr_info("audit: initializing netlink socket in namespace\n");
    
    	aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg);
    
    	if (aunet->nlsk == NULL) {
    
    		audit_panic("cannot initialize netlink socket in namespace");
    
    		return -ENOMEM;
    	}
    	aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
    
    	return 0;
    }
    
    static void __net_exit audit_net_exit(struct net *net)
    {
    	struct audit_net *aunet = net_generic(net, audit_net_id);
    	struct sock *sock = aunet->nlsk;
    	if (sock == audit_sock) {
    		audit_pid = 0;
    		audit_sock = NULL;
    	}
    
    	rcu_assign_pointer(aunet->nlsk, NULL);
    	synchronize_net();
    	netlink_kernel_release(sock);
    }
    
    static struct pernet_operations __net_initdata audit_net_ops = {
    	.init = audit_net_init,
    	.exit = audit_net_exit,
    	.id = &audit_net_id,
    	.size = sizeof(struct audit_net),
    };
    
    /* Initialize audit support at boot time. */
    static int __init audit_init(void)
    {
    	int i;
    
    
    	if (audit_initialized == AUDIT_DISABLED)
    		return 0;
    
    
    	pr_info("audit: initializing netlink subsys (%s)\n",
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	       audit_default ? "enabled" : "disabled");
    
    	register_pernet_subsys(&audit_net_ops);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	skb_queue_head_init(&audit_skb_queue);
    
    	skb_queue_head_init(&audit_skb_hold_queue);
    
    	audit_initialized = AUDIT_INITIALIZED;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	audit_enabled = audit_default;
    
    	audit_ever_enabled |= !!audit_default;
    
    	audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
    
    
    	for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
    		INIT_LIST_HEAD(&audit_inode_hash[i]);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return 0;
    }
    __initcall(audit_init);
    
    /* Process kernel command-line parameter at boot time.  audit=0 or audit=1. */
    static int __init audit_enable(char *str)
    {
    	audit_default = !!simple_strtol(str, NULL, 0);
    
    	if (!audit_default)
    		audit_initialized = AUDIT_DISABLED;
    
    
    	pr_info("audit: %s\n", audit_default ?
    		"enabled (after initialization)" : "disabled (until reboot)");
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    __setup("audit=", audit_enable);
    
    
    /* Process kernel command-line parameter at boot time.
     * audit_backlog_limit=<n> */
    static int __init audit_backlog_limit_set(char *str)
    {
    	long int audit_backlog_limit_arg;
    	pr_info("audit_backlog_limit: ");
    	if (kstrtol(str, 0, &audit_backlog_limit_arg)) {
    		printk("using default of %d, unable to parse %s\n",
    		       audit_backlog_limit, str);
    		return 1;
    	}
    	if (audit_backlog_limit_arg >= 0)
    		audit_backlog_limit = (int)audit_backlog_limit_arg;
    	printk("%d\n", audit_backlog_limit);
    
    	return 1;
    }
    __setup("audit_backlog_limit=", audit_backlog_limit_set);
    
    
    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 long wait_for_auditd(long sleep_time)
    
    {
    	DECLARE_WAITQUEUE(wait, current);
    
    	set_current_state(TASK_UNINTERRUPTIBLE);
    
    	add_wait_queue_exclusive(&audit_backlog_wait, &wait);
    
    
    	if (audit_backlog_limit &&
    	    skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
    
    		sleep_time = schedule_timeout(sleep_time);
    
    
    	__set_current_state(TASK_RUNNING);
    	remove_wait_queue(&audit_backlog_wait, &wait);
    
    	return sleep_time;
    
    /**
     * 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);
    
    	int reserve = 5; /* Allow atomic callers to go up to five
    			    entries over the normal backlog limit */
    
    	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) {
    		if (audit_pid && audit_pid == current->pid)
    			gfp_mask &= ~__GFP_WAIT;
    		else
    			reserve = 0;
    	}
    
    
    	while (audit_backlog_limit
    	       && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
    
    		if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time) {
    
    			long sleep_time;
    
    			sleep_time = timeout_start + audit_backlog_wait_time - jiffies;
    			if (sleep_time > 0) {
    
    				sleep_time = wait_for_auditd(sleep_time);
    
    				if (sleep_time > 0)
    
    		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);
    
    	audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME;
    
    
    	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_session_info(struct audit_buffer *ab)
    {
    
    	unsigned int sessionid = audit_get_sessionid(current);
    
    	uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current));
    
    
    	audit_log_format(ab, " auid=%u ses=%u", auid, sessionid);
    
    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)");
    }
    
    
    void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
    {
    	int i;
    
    	audit_log_format(ab, " %s=", prefix);
    	CAP_FOR_EACH_U32(i) {
    		audit_log_format(ab, "%08x",
    				 cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
    	}
    }
    
    void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
    {
    	kernel_cap_t *perm = &name->fcap.permitted;
    	kernel_cap_t *inh = &name->fcap.inheritable;
    	int log = 0;
    
    	if (!cap_isclear(*perm)) {
    		audit_log_cap(ab, "cap_fp", perm);
    		log = 1;
    	}
    	if (!cap_isclear(*inh)) {
    		audit_log_cap(ab, "cap_fi", inh);
    		log = 1;
    	}
    
    	if (log)
    		audit_log_format(ab, " cap_fe=%d cap_fver=%x",
    				 name->fcap.fE, name->fcap_ver);
    }
    
    static inline int audit_copy_fcaps(struct audit_names *name,
    				   const struct dentry *dentry)
    {
    	struct cpu_vfs_cap_data caps;
    	int rc;
    
    	if (!dentry)
    		return 0;
    
    	rc = get_vfs_caps_from_disk(dentry, &caps);
    	if (rc)
    		return rc;
    
    	name->fcap.permitted = caps.permitted;
    	name->fcap.inheritable = caps.inheritable;
    	name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
    	name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
    				VFS_CAP_REVISION_SHIFT;
    
    	return 0;
    }
    
    /* Copy inode data into an audit_names. */
    void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
    		      const struct inode *inode)
    {
    	name->ino   = inode->i_ino;
    	name->dev   = inode->i_sb->s_dev;
    	name->mode  = inode->i_mode;
    	name->uid   = inode->i_uid;
    	name->gid   = inode->i_gid;
    	name->rdev  = inode->i_rdev;
    	security_inode_getsecid(inode, &name->osid);
    	audit_copy_fcaps(name, dentry);
    }
    
    /**
     * audit_log_name - produce AUDIT_PATH record from struct audit_names
     * @context: audit_context for the task
     * @n: audit_names structure with reportable details
     * @path: optional path to report instead of audit_names->name
     * @record_num: record number to report when handling a list of names
     * @call_panic: optional pointer to int that will be updated if secid fails
     */
    void audit_log_name(struct audit_context *context, struct audit_names *n,
    		    struct path *path, int record_num, int *call_panic)
    {
    	struct audit_buffer *ab;
    	ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
    	if (!ab)
    		return;
    
    	audit_log_format(ab, "item=%d", record_num);
    
    	if (path)
    		audit_log_d_path(ab, " name=", path);
    	else if (n->name) {
    		switch (n->name_len) {
    		case AUDIT_NAME_FULL:
    			/* log the full path */
    			audit_log_format(ab, " name=");
    			audit_log_untrustedstring(ab, n->name->name);
    			break;
    		case 0:
    			/* name was specified as a relative path and the
    			 * directory component is the cwd */
    			audit_log_d_path(ab, " name=", &context->pwd);
    			break;
    		default:
    			/* log the name's directory component */
    			audit_log_format(ab, " name=");
    			audit_log_n_untrustedstring(ab, n->name->name,
    						    n->name_len);
    		}
    	} else
    		audit_log_format(ab, " name=(null)");
    
    	if (n->ino != (unsigned long)-1) {
    		audit_log_format(ab, " inode=%lu"
    				 " dev=%02x:%02x mode=%#ho"
    				 " ouid=%u ogid=%u rdev=%02x:%02x",
    				 n->ino,
    				 MAJOR(n->dev),
    				 MINOR(n->dev),
    				 n->mode,
    				 from_kuid(&init_user_ns, n->uid),
    				 from_kgid(&init_user_ns, n->gid),
    				 MAJOR(n->rdev),
    				 MINOR(n->rdev));
    	}
    	if (n->osid != 0) {
    		char *ctx = NULL;
    		u32 len;
    		if (security_secid_to_secctx(
    			n->osid, &ctx, &len)) {
    			audit_log_format(ab, " osid=%u", n->osid);
    			if (call_panic)
    				*call_panic = 2;
    		} else {
    			audit_log_format(ab, " obj=%s", ctx);
    			security_release_secctx(ctx, len);
    		}
    	}
    
    
    	/* log the audit_names record type */
    	audit_log_format(ab, " nametype=");
    	switch(n->type) {
    	case AUDIT_TYPE_NORMAL:
    		audit_log_format(ab, "NORMAL");
    		break;
    	case AUDIT_TYPE_PARENT:
    		audit_log_format(ab, "PARENT");
    		break;
    	case AUDIT_TYPE_CHILD_DELETE:
    		audit_log_format(ab, "DELETE");
    		break;
    	case AUDIT_TYPE_CHILD_CREATE:
    		audit_log_format(ab, "CREATE");
    		break;
    	default:
    		audit_log_format(ab, "UNKNOWN");
    		break;
    	}
    
    
    	audit_log_fcaps(ab, n);
    	audit_log_end(ab);
    }
    
    int audit_log_task_context(struct audit_buffer *ab)
    {
    	char *ctx = NULL;
    	unsigned len;
    	int error;
    	u32 sid;
    
    	security_task_getsecid(current, &sid);
    	if (!sid)
    		return 0;
    
    	error = security_secid_to_secctx(sid, &ctx, &len);
    	if (error) {
    		if (error != -EINVAL)
    			goto error_path;
    		return 0;
    	}
    
    	audit_log_format(ab, " subj=%s", ctx);
    	security_release_secctx(ctx, len);
    	return 0;
    
    error_path:
    	audit_panic("error in audit_log_task_context");
    	return error;
    }
    EXPORT_SYMBOL(audit_log_task_context);
    
    void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
    {
    	const struct cred *cred;
    	char name[sizeof(tsk->comm)];
    	struct mm_struct *mm = tsk->mm;
    	char *tty;
    
    	if (!ab)
    		return;
    
    	/* tsk == current */
    	cred = current_cred();
    
    	spin_lock_irq(&tsk->sighand->siglock);
    	if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
    		tty = tsk->signal->tty->name;
    	else
    		tty = "(none)";
    	spin_unlock_irq(&tsk->sighand->siglock);
    
    	audit_log_format(ab,
    			 " ppid=%ld pid=%d auid=%u uid=%u gid=%u"
    			 " euid=%u suid=%u fsuid=%u"
    
    			 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
    
    			 sys_getppid(),
    			 tsk->pid,
    			 from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
    			 from_kuid(&init_user_ns, cred->uid),
    			 from_kgid(&init_user_ns, cred->gid),
    			 from_kuid(&init_user_ns, cred->euid),
    			 from_kuid(&init_user_ns, cred->suid),
    			 from_kuid(&init_user_ns, cred->fsuid),
    			 from_kgid(&init_user_ns, cred->egid),
    			 from_kgid(&init_user_ns, cred->sgid),
    			 from_kgid(&init_user_ns, cred->fsgid),
    
    			 tty, audit_get_sessionid(tsk));
    
    
    	get_task_comm(name, tsk);
    	audit_log_format(ab, " comm=");
    	audit_log_untrustedstring(ab, name);
    
    	if (mm) {
    		down_read(&mm->mmap_sem);
    		if (mm->exe_file)
    			audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
    		up_read(&mm->mmap_sem);
    
    	} else
    		audit_log_format(ab, " exe=(null)");
    
    	audit_log_task_context(ab);
    }
    EXPORT_SYMBOL(audit_log_task_info);
    
    
    /**
     * 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;
    
    	struct audit_names *name;
    
    	name = kzalloc(sizeof(*name), GFP_NOFS);
    	if (!name)
    		return;
    
    	/* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
    
    	ab = audit_log_start(current->audit_context, GFP_KERNEL,
    			     AUDIT_ANOM_LINK);
    
    		goto out;
    	audit_log_format(ab, "op=%s", operation);
    	audit_log_task_info(ab, current);
    	audit_log_format(ab, " res=0");
    
    	audit_log_end(ab);
    
    
    	/* Generate AUDIT_PATH record with object. */
    	name->type = AUDIT_TYPE_NORMAL;
    	audit_copy_inode(name, link->dentry, link->dentry->d_inode);
    	audit_log_name(current->audit_context, name, link, 0, NULL);
    out:
    	kfree(name);
    
    /**
     * 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_HDRLEN;
    
    		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);