| 85c8721 | 2005-04-29 16:23:29 +0100 | [diff] [blame] | 1 | /* audit.c -- Auditing support | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  * Gateway between the kernel (e.g., selinux) and the user-space audit daemon. | 
 | 3 |  * System-call specific features have moved to auditsc.c | 
 | 4 |  * | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 5 |  * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 |  * All Rights Reserved. | 
 | 7 |  * | 
 | 8 |  * This program is free software; you can redistribute it and/or modify | 
 | 9 |  * it under the terms of the GNU General Public License as published by | 
 | 10 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 11 |  * (at your option) any later version. | 
 | 12 |  * | 
 | 13 |  * This program is distributed in the hope that it will be useful, | 
 | 14 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 16 |  * GNU General Public License for more details. | 
 | 17 |  * | 
 | 18 |  * You should have received a copy of the GNU General Public License | 
 | 19 |  * along with this program; if not, write to the Free Software | 
 | 20 |  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
 | 21 |  * | 
 | 22 |  * Written by Rickard E. (Rik) Faith <faith@redhat.com> | 
 | 23 |  * | 
 | 24 |  * Goals: 1) Integrate fully with SELinux. | 
 | 25 |  *	  2) Minimal run-time overhead: | 
 | 26 |  *	     a) Minimal when syscall auditing is disabled (audit_enable=0). | 
 | 27 |  *	     b) Small when syscall auditing is enabled and no audit record | 
 | 28 |  *		is generated (defer as much work as possible to record | 
 | 29 |  *		generation time): | 
 | 30 |  *		i) context is allocated, | 
 | 31 |  *		ii) names from getname are stored without a copy, and | 
 | 32 |  *		iii) inode information stored from path_lookup. | 
 | 33 |  *	  3) Ability to disable syscall auditing at boot time (audit=0). | 
 | 34 |  *	  4) Usable by other parts of the kernel (if audit_log* is called, | 
 | 35 |  *	     then a syscall record will be generated automatically for the | 
 | 36 |  *	     current syscall). | 
 | 37 |  *	  5) Netlink interface to user-space. | 
 | 38 |  *	  6) Support low-overhead kernel-based filtering to minimize the | 
 | 39 |  *	     information that must be passed to user-space. | 
 | 40 |  * | 
 | 85c8721 | 2005-04-29 16:23:29 +0100 | [diff] [blame] | 41 |  * Example user-space utilities: http://people.redhat.com/sgrubb/audit/ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 |  */ | 
 | 43 |  | 
 | 44 | #include <linux/init.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <asm/types.h> | 
| Alan Cox | 715b49e | 2006-01-18 17:44:07 -0800 | [diff] [blame] | 46 | #include <asm/atomic.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/mm.h> | 
 | 48 | #include <linux/module.h> | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 49 | #include <linux/err.h> | 
 | 50 | #include <linux/kthread.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
 | 52 | #include <linux/audit.h> | 
 | 53 |  | 
 | 54 | #include <net/sock.h> | 
| Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 55 | #include <net/netlink.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/skbuff.h> | 
 | 57 | #include <linux/netlink.h> | 
| Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 58 | #include <linux/selinux.h> | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 59 | #include <linux/inotify.h> | 
| Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 60 | #include <linux/freezer.h> | 
| Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 61 |  | 
 | 62 | #include "audit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 |  | 
 | 64 | /* No auditing will take place until audit_initialized != 0. | 
 | 65 |  * (Initialization happens after skb_init is called.) */ | 
 | 66 | static int	audit_initialized; | 
 | 67 |  | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 68 | /* 0 - no auditing | 
 | 69 |  * 1 - auditing enabled | 
 | 70 |  * 2 - auditing enabled and configuration is locked/unchangeable. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | int		audit_enabled; | 
 | 72 |  | 
 | 73 | /* Default state when kernel boots without any parameters. */ | 
 | 74 | static int	audit_default; | 
 | 75 |  | 
 | 76 | /* If auditing cannot proceed, audit_failure selects what happens. */ | 
 | 77 | static int	audit_failure = AUDIT_FAIL_PRINTK; | 
 | 78 |  | 
 | 79 | /* If audit records are to be written to the netlink socket, audit_pid | 
 | 80 |  * contains the (non-zero) pid. */ | 
| Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 81 | int		audit_pid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 83 | /* If audit_rate_limit is non-zero, limit the rate of sending audit records | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 |  * to that number per second.  This prevents DoS attacks, but results in | 
 | 85 |  * audit records being dropped. */ | 
 | 86 | static int	audit_rate_limit; | 
 | 87 |  | 
 | 88 | /* Number of outstanding audit_buffers allowed. */ | 
 | 89 | static int	audit_backlog_limit = 64; | 
| David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 90 | static int	audit_backlog_wait_time = 60 * HZ; | 
 | 91 | static int	audit_backlog_wait_overflow = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 |  | 
| Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 93 | /* The identity of the user shutting down the audit system. */ | 
 | 94 | uid_t		audit_sig_uid = -1; | 
 | 95 | pid_t		audit_sig_pid = -1; | 
| Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 96 | u32		audit_sig_sid = 0; | 
| Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 97 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* Records can be lost in several ways: | 
 | 99 |    0) [suppressed in audit_alloc] | 
 | 100 |    1) out of memory in audit_log_start [kmalloc of struct audit_buffer] | 
 | 101 |    2) out of memory in audit_log_move [alloc_skb] | 
 | 102 |    3) suppressed due to audit_rate_limit | 
 | 103 |    4) suppressed due to audit_backlog_limit | 
 | 104 | */ | 
 | 105 | static atomic_t    audit_lost = ATOMIC_INIT(0); | 
 | 106 |  | 
 | 107 | /* The netlink socket. */ | 
 | 108 | static struct sock *audit_sock; | 
 | 109 |  | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 110 | /* Inotify handle. */ | 
 | 111 | struct inotify_handle *audit_ih; | 
 | 112 |  | 
 | 113 | /* Hash for inode-based rules */ | 
 | 114 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; | 
 | 115 |  | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 116 | /* The audit_freelist is a list of pre-allocated audit buffers (if more | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 |  * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of | 
 | 118 |  * being placed on the freelist). */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | static DEFINE_SPINLOCK(audit_freelist_lock); | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 120 | static int	   audit_freelist_count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | static LIST_HEAD(audit_freelist); | 
 | 122 |  | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 123 | static struct sk_buff_head audit_skb_queue; | 
 | 124 | static struct task_struct *kauditd_task; | 
 | 125 | static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait); | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 126 | static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 |  | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 128 | /* Serialize requests from userspace. */ | 
 | 129 | static DEFINE_MUTEX(audit_cmd_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 |  | 
 | 131 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting | 
 | 132 |  * audit records.  Since printk uses a 1024 byte buffer, this buffer | 
 | 133 |  * should be at least that large. */ | 
 | 134 | #define AUDIT_BUFSIZ 1024 | 
 | 135 |  | 
 | 136 | /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the | 
 | 137 |  * audit_freelist.  Doing so eliminates many kmalloc/kfree calls. */ | 
 | 138 | #define AUDIT_MAXFREE  (2*NR_CPUS) | 
 | 139 |  | 
 | 140 | /* The audit_buffer is used when formatting an audit record.  The caller | 
 | 141 |  * locks briefly to get the record off the freelist or to allocate the | 
 | 142 |  * buffer, and locks briefly to send the buffer to the netlink layer or | 
 | 143 |  * to place it on a transmit queue.  Multiple audit_buffers can be in | 
 | 144 |  * use simultaneously. */ | 
 | 145 | struct audit_buffer { | 
 | 146 | 	struct list_head     list; | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 147 | 	struct sk_buff       *skb;	/* formatted skb ready to send */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 	struct audit_context *ctx;	/* NULL or associated context */ | 
| Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 149 | 	gfp_t		     gfp_mask; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | }; | 
 | 151 |  | 
| Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 152 | static void audit_set_pid(struct audit_buffer *ab, pid_t pid) | 
 | 153 | { | 
 | 154 | 	struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data; | 
 | 155 | 	nlh->nlmsg_pid = pid; | 
 | 156 | } | 
 | 157 |  | 
| Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 158 | void audit_panic(const char *message) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { | 
 | 160 | 	switch (audit_failure) | 
 | 161 | 	{ | 
 | 162 | 	case AUDIT_FAIL_SILENT: | 
 | 163 | 		break; | 
 | 164 | 	case AUDIT_FAIL_PRINTK: | 
 | 165 | 		printk(KERN_ERR "audit: %s\n", message); | 
 | 166 | 		break; | 
 | 167 | 	case AUDIT_FAIL_PANIC: | 
 | 168 | 		panic("audit: %s\n", message); | 
 | 169 | 		break; | 
 | 170 | 	} | 
 | 171 | } | 
 | 172 |  | 
 | 173 | static inline int audit_rate_check(void) | 
 | 174 | { | 
 | 175 | 	static unsigned long	last_check = 0; | 
 | 176 | 	static int		messages   = 0; | 
 | 177 | 	static DEFINE_SPINLOCK(lock); | 
 | 178 | 	unsigned long		flags; | 
 | 179 | 	unsigned long		now; | 
 | 180 | 	unsigned long		elapsed; | 
 | 181 | 	int			retval	   = 0; | 
 | 182 |  | 
 | 183 | 	if (!audit_rate_limit) return 1; | 
 | 184 |  | 
 | 185 | 	spin_lock_irqsave(&lock, flags); | 
 | 186 | 	if (++messages < audit_rate_limit) { | 
 | 187 | 		retval = 1; | 
 | 188 | 	} else { | 
 | 189 | 		now     = jiffies; | 
 | 190 | 		elapsed = now - last_check; | 
 | 191 | 		if (elapsed > HZ) { | 
 | 192 | 			last_check = now; | 
 | 193 | 			messages   = 0; | 
 | 194 | 			retval     = 1; | 
 | 195 | 		} | 
 | 196 | 	} | 
 | 197 | 	spin_unlock_irqrestore(&lock, flags); | 
 | 198 |  | 
 | 199 | 	return retval; | 
 | 200 | } | 
 | 201 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 202 | /** | 
 | 203 |  * audit_log_lost - conditionally log lost audit message event | 
 | 204 |  * @message: the message stating reason for lost audit message | 
 | 205 |  * | 
 | 206 |  * Emit at least 1 message per second, even if audit_rate_check is | 
 | 207 |  * throttling. | 
 | 208 |  * Always increment the lost messages counter. | 
 | 209 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | void audit_log_lost(const char *message) | 
 | 211 | { | 
 | 212 | 	static unsigned long	last_msg = 0; | 
 | 213 | 	static DEFINE_SPINLOCK(lock); | 
 | 214 | 	unsigned long		flags; | 
 | 215 | 	unsigned long		now; | 
 | 216 | 	int			print; | 
 | 217 |  | 
 | 218 | 	atomic_inc(&audit_lost); | 
 | 219 |  | 
 | 220 | 	print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); | 
 | 221 |  | 
 | 222 | 	if (!print) { | 
 | 223 | 		spin_lock_irqsave(&lock, flags); | 
 | 224 | 		now = jiffies; | 
 | 225 | 		if (now - last_msg > HZ) { | 
 | 226 | 			print = 1; | 
 | 227 | 			last_msg = now; | 
 | 228 | 		} | 
 | 229 | 		spin_unlock_irqrestore(&lock, flags); | 
 | 230 | 	} | 
 | 231 |  | 
 | 232 | 	if (print) { | 
 | 233 | 		printk(KERN_WARNING | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 234 | 		       "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | 		       atomic_read(&audit_lost), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | 		       audit_rate_limit, | 
 | 237 | 		       audit_backlog_limit); | 
 | 238 | 		audit_panic(message); | 
 | 239 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } | 
 | 241 |  | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 242 | static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 244 | 	int res, rc = 0, old = audit_rate_limit; | 
 | 245 |  | 
 | 246 | 	/* check if we are locked */ | 
 | 247 | 	if (audit_enabled == 2) | 
 | 248 | 		res = 0; | 
 | 249 | 	else | 
 | 250 | 		res = 1; | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 251 |  | 
 | 252 | 	if (sid) { | 
 | 253 | 		char *ctx = NULL; | 
 | 254 | 		u32 len; | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 255 | 		if ((rc = selinux_sid_to_string(sid, &ctx, &len)) == 0) { | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 256 | 			audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 257 | 				"audit_rate_limit=%d old=%d by auid=%u" | 
 | 258 | 				" subj=%s res=%d", | 
 | 259 | 				limit, old, loginuid, ctx, res); | 
 | 260 | 			kfree(ctx); | 
 | 261 | 		} else | 
 | 262 | 			res = 0; /* Something weird, deny request */ | 
 | 263 | 	} | 
 | 264 | 	audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
 | 265 | 		"audit_rate_limit=%d old=%d by auid=%u res=%d", | 
 | 266 | 		limit, old, loginuid, res); | 
 | 267 |  | 
 | 268 | 	/* If we are allowed, make the change */ | 
 | 269 | 	if (res == 1) | 
 | 270 | 		audit_rate_limit = limit; | 
 | 271 | 	/* Not allowed, update reason */ | 
 | 272 | 	else if (rc == 0) | 
 | 273 | 		rc = -EPERM; | 
 | 274 | 	return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } | 
 | 276 |  | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 277 | static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 279 | 	int res, rc = 0, old = audit_backlog_limit; | 
 | 280 |  | 
 | 281 | 	/* check if we are locked */ | 
 | 282 | 	if (audit_enabled == 2) | 
 | 283 | 		res = 0; | 
 | 284 | 	else | 
 | 285 | 		res = 1; | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 286 |  | 
 | 287 | 	if (sid) { | 
 | 288 | 		char *ctx = NULL; | 
 | 289 | 		u32 len; | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 290 | 		if ((rc = selinux_sid_to_string(sid, &ctx, &len)) == 0) { | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 291 | 			audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 292 | 				"audit_backlog_limit=%d old=%d by auid=%u" | 
 | 293 | 				" subj=%s res=%d", | 
 | 294 | 				limit, old, loginuid, ctx, res); | 
 | 295 | 			kfree(ctx); | 
 | 296 | 		} else | 
 | 297 | 			res = 0; /* Something weird, deny request */ | 
 | 298 | 	} | 
 | 299 | 	audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
 | 300 | 		"audit_backlog_limit=%d old=%d by auid=%u res=%d", | 
 | 301 | 		limit, old, loginuid, res); | 
 | 302 |  | 
 | 303 | 	/* If we are allowed, make the change */ | 
 | 304 | 	if (res == 1) | 
 | 305 | 		audit_backlog_limit = limit; | 
 | 306 | 	/* Not allowed, update reason */ | 
 | 307 | 	else if (rc == 0) | 
 | 308 | 		rc = -EPERM; | 
 | 309 | 	return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } | 
 | 311 |  | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 312 | static int audit_set_enabled(int state, uid_t loginuid, u32 sid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 314 | 	int res, rc = 0, old = audit_enabled; | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 315 |  | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 316 | 	if (state < 0 || state > 2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | 		return -EINVAL; | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 318 |  | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 319 | 	/* check if we are locked */ | 
 | 320 | 	if (audit_enabled == 2) | 
 | 321 | 		res = 0; | 
 | 322 | 	else | 
 | 323 | 		res = 1; | 
 | 324 |  | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 325 | 	if (sid) { | 
 | 326 | 		char *ctx = NULL; | 
 | 327 | 		u32 len; | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 328 | 		if ((rc = selinux_sid_to_string(sid, &ctx, &len)) == 0) { | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 329 | 			audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 330 | 				"audit_enabled=%d old=%d by auid=%u" | 
 | 331 | 				" subj=%s res=%d", | 
 | 332 | 				state, old, loginuid, ctx, res); | 
 | 333 | 			kfree(ctx); | 
 | 334 | 		} else | 
 | 335 | 			res = 0; /* Something weird, deny request */ | 
 | 336 | 	} | 
 | 337 | 	audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
 | 338 | 		"audit_enabled=%d old=%d by auid=%u res=%d", | 
 | 339 | 		state, old, loginuid, res); | 
 | 340 |  | 
 | 341 | 	/* If we are allowed, make the change */ | 
 | 342 | 	if (res == 1) | 
 | 343 | 		audit_enabled = state; | 
 | 344 | 	/* Not allowed, update reason */ | 
 | 345 | 	else if (rc == 0) | 
 | 346 | 		rc = -EPERM; | 
 | 347 | 	return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } | 
 | 349 |  | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 350 | static int audit_set_failure(int state, uid_t loginuid, u32 sid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 352 | 	int res, rc = 0, old = audit_failure; | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 353 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | 	if (state != AUDIT_FAIL_SILENT | 
 | 355 | 	    && state != AUDIT_FAIL_PRINTK | 
 | 356 | 	    && state != AUDIT_FAIL_PANIC) | 
 | 357 | 		return -EINVAL; | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 358 |  | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 359 | 	/* check if we are locked */ | 
 | 360 | 	if (audit_enabled == 2) | 
 | 361 | 		res = 0; | 
 | 362 | 	else | 
 | 363 | 		res = 1; | 
 | 364 |  | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 365 | 	if (sid) { | 
 | 366 | 		char *ctx = NULL; | 
 | 367 | 		u32 len; | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 368 | 		if ((rc = selinux_sid_to_string(sid, &ctx, &len)) == 0) { | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 369 | 			audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 370 | 				"audit_failure=%d old=%d by auid=%u" | 
 | 371 | 				" subj=%s res=%d", | 
 | 372 | 				state, old, loginuid, ctx, res); | 
 | 373 | 			kfree(ctx); | 
 | 374 | 		} else | 
 | 375 | 			res = 0; /* Something weird, deny request */ | 
 | 376 | 	} | 
 | 377 | 	audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
 | 378 | 		"audit_failure=%d old=%d by auid=%u res=%d", | 
 | 379 | 		state, old, loginuid, res); | 
 | 380 |  | 
 | 381 | 	/* If we are allowed, make the change */ | 
 | 382 | 	if (res == 1) | 
 | 383 | 		audit_failure = state; | 
 | 384 | 	/* Not allowed, update reason */ | 
 | 385 | 	else if (rc == 0) | 
 | 386 | 		rc = -EPERM; | 
 | 387 | 	return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } | 
 | 389 |  | 
| Adrian Bunk | 97a41e2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 390 | static int kauditd_thread(void *dummy) | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 391 | { | 
 | 392 | 	struct sk_buff *skb; | 
 | 393 |  | 
| Andrew Morton | 4899b8b | 2006-10-06 00:43:48 -0700 | [diff] [blame] | 394 | 	while (!kthread_should_stop()) { | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 395 | 		skb = skb_dequeue(&audit_skb_queue); | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 396 | 		wake_up(&audit_backlog_wait); | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 397 | 		if (skb) { | 
 | 398 | 			if (audit_pid) { | 
 | 399 | 				int err = netlink_unicast(audit_sock, skb, audit_pid, 0); | 
 | 400 | 				if (err < 0) { | 
 | 401 | 					BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */ | 
 | 402 | 					printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid); | 
 | 403 | 					audit_pid = 0; | 
 | 404 | 				} | 
 | 405 | 			} else { | 
| David Woodhouse | e1b09eb | 2005-06-24 17:24:11 +0100 | [diff] [blame] | 406 | 				printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0)); | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 407 | 				kfree_skb(skb); | 
 | 408 | 			} | 
 | 409 | 		} else { | 
 | 410 | 			DECLARE_WAITQUEUE(wait, current); | 
 | 411 | 			set_current_state(TASK_INTERRUPTIBLE); | 
 | 412 | 			add_wait_queue(&kauditd_wait, &wait); | 
 | 413 |  | 
| Pierre Ossman | 7a4ae74 | 2005-12-12 00:37:22 -0800 | [diff] [blame] | 414 | 			if (!skb_queue_len(&audit_skb_queue)) { | 
 | 415 | 				try_to_freeze(); | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 416 | 				schedule(); | 
| Pierre Ossman | 7a4ae74 | 2005-12-12 00:37:22 -0800 | [diff] [blame] | 417 | 			} | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 418 |  | 
 | 419 | 			__set_current_state(TASK_RUNNING); | 
 | 420 | 			remove_wait_queue(&kauditd_wait, &wait); | 
 | 421 | 		} | 
 | 422 | 	} | 
| Andrew Morton | 4899b8b | 2006-10-06 00:43:48 -0700 | [diff] [blame] | 423 | 	return 0; | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 424 | } | 
 | 425 |  | 
| Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 426 | int audit_send_list(void *_dest) | 
 | 427 | { | 
 | 428 | 	struct audit_netlink_list *dest = _dest; | 
 | 429 | 	int pid = dest->pid; | 
 | 430 | 	struct sk_buff *skb; | 
 | 431 |  | 
 | 432 | 	/* wait for parent to finish and send an ACK */ | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 433 | 	mutex_lock(&audit_cmd_mutex); | 
 | 434 | 	mutex_unlock(&audit_cmd_mutex); | 
| Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 435 |  | 
 | 436 | 	while ((skb = __skb_dequeue(&dest->q)) != NULL) | 
 | 437 | 		netlink_unicast(audit_sock, skb, pid, 0); | 
 | 438 |  | 
 | 439 | 	kfree(dest); | 
 | 440 |  | 
 | 441 | 	return 0; | 
 | 442 | } | 
 | 443 |  | 
 | 444 | struct sk_buff *audit_make_reply(int pid, int seq, int type, int done, | 
 | 445 | 				 int multi, void *payload, int size) | 
 | 446 | { | 
 | 447 | 	struct sk_buff	*skb; | 
 | 448 | 	struct nlmsghdr	*nlh; | 
 | 449 | 	int		len = NLMSG_SPACE(size); | 
 | 450 | 	void		*data; | 
 | 451 | 	int		flags = multi ? NLM_F_MULTI : 0; | 
 | 452 | 	int		t     = done  ? NLMSG_DONE  : type; | 
 | 453 |  | 
 | 454 | 	skb = alloc_skb(len, GFP_KERNEL); | 
 | 455 | 	if (!skb) | 
 | 456 | 		return NULL; | 
 | 457 |  | 
 | 458 | 	nlh		 = NLMSG_PUT(skb, pid, seq, t, size); | 
 | 459 | 	nlh->nlmsg_flags = flags; | 
 | 460 | 	data		 = NLMSG_DATA(nlh); | 
 | 461 | 	memcpy(data, payload, size); | 
 | 462 | 	return skb; | 
 | 463 |  | 
 | 464 | nlmsg_failure:			/* Used by NLMSG_PUT */ | 
 | 465 | 	if (skb) | 
 | 466 | 		kfree_skb(skb); | 
 | 467 | 	return NULL; | 
 | 468 | } | 
 | 469 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 470 | /** | 
 | 471 |  * audit_send_reply - send an audit reply message via netlink | 
 | 472 |  * @pid: process id to send reply to | 
 | 473 |  * @seq: sequence number | 
 | 474 |  * @type: audit message type | 
 | 475 |  * @done: done (last) flag | 
 | 476 |  * @multi: multi-part message flag | 
 | 477 |  * @payload: payload data | 
 | 478 |  * @size: payload size | 
 | 479 |  * | 
 | 480 |  * Allocates an skb, builds the netlink message, and sends it to the pid. | 
 | 481 |  * No failure notifications. | 
 | 482 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | void audit_send_reply(int pid, int seq, int type, int done, int multi, | 
 | 484 | 		      void *payload, int size) | 
 | 485 | { | 
 | 486 | 	struct sk_buff	*skb; | 
| Al Viro | 9044e6b | 2006-05-22 01:09:24 -0400 | [diff] [blame] | 487 | 	skb = audit_make_reply(pid, seq, type, done, multi, payload, size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | 	if (!skb) | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 489 | 		return; | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 490 | 	/* Ignore failure. It'll only happen if the sender goes away, | 
 | 491 | 	   because our timeout is set to infinite. */ | 
 | 492 | 	netlink_unicast(audit_sock, skb, pid, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | 	return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } | 
 | 495 |  | 
 | 496 | /* | 
 | 497 |  * Check for appropriate CAP_AUDIT_ capabilities on incoming audit | 
 | 498 |  * control messages. | 
 | 499 |  */ | 
| Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 500 | static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { | 
 | 502 | 	int err = 0; | 
 | 503 |  | 
 | 504 | 	switch (msg_type) { | 
 | 505 | 	case AUDIT_GET: | 
 | 506 | 	case AUDIT_LIST: | 
| Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 507 | 	case AUDIT_LIST_RULES: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | 	case AUDIT_SET: | 
 | 509 | 	case AUDIT_ADD: | 
| Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 510 | 	case AUDIT_ADD_RULE: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | 	case AUDIT_DEL: | 
| Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 512 | 	case AUDIT_DEL_RULE: | 
| Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 513 | 	case AUDIT_SIGNAL_INFO: | 
| Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 514 | 		if (security_netlink_recv(skb, CAP_AUDIT_CONTROL)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | 			err = -EPERM; | 
 | 516 | 		break; | 
| Steve Grubb | 0547410 | 2005-05-21 00:18:37 +0100 | [diff] [blame] | 517 | 	case AUDIT_USER: | 
| David Woodhouse | 209aba0 | 2005-05-18 10:21:07 +0100 | [diff] [blame] | 518 | 	case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG: | 
| Steve Grubb | 90d526c | 2005-11-03 15:48:08 +0000 | [diff] [blame] | 519 | 	case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2: | 
| Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 520 | 		if (security_netlink_recv(skb, CAP_AUDIT_WRITE)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | 			err = -EPERM; | 
 | 522 | 		break; | 
 | 523 | 	default:  /* bad msg */ | 
 | 524 | 		err = -EINVAL; | 
 | 525 | 	} | 
 | 526 |  | 
 | 527 | 	return err; | 
 | 528 | } | 
 | 529 |  | 
 | 530 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | 
 | 531 | { | 
| Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 532 | 	u32			uid, pid, seq, sid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | 	void			*data; | 
 | 534 | 	struct audit_status	*status_get, status_set; | 
 | 535 | 	int			err; | 
| Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 536 | 	struct audit_buffer	*ab; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | 	u16			msg_type = nlh->nlmsg_type; | 
| Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 538 | 	uid_t			loginuid; /* loginuid of sender */ | 
| Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 539 | 	struct audit_sig_info   *sig_data; | 
 | 540 | 	char			*ctx; | 
 | 541 | 	u32			len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 |  | 
| Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 543 | 	err = audit_netlink_ok(skb, msg_type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | 	if (err) | 
 | 545 | 		return err; | 
 | 546 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 547 | 	/* As soon as there's any sign of userspace auditd, | 
 | 548 | 	 * start kauditd to talk to it */ | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 549 | 	if (!kauditd_task) | 
 | 550 | 		kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd"); | 
 | 551 | 	if (IS_ERR(kauditd_task)) { | 
 | 552 | 		err = PTR_ERR(kauditd_task); | 
 | 553 | 		kauditd_task = NULL; | 
 | 554 | 		return err; | 
 | 555 | 	} | 
 | 556 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | 	pid  = NETLINK_CREDS(skb)->pid; | 
 | 558 | 	uid  = NETLINK_CREDS(skb)->uid; | 
| Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 559 | 	loginuid = NETLINK_CB(skb).loginuid; | 
| Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 560 | 	sid  = NETLINK_CB(skb).sid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | 	seq  = nlh->nlmsg_seq; | 
 | 562 | 	data = NLMSG_DATA(nlh); | 
 | 563 |  | 
 | 564 | 	switch (msg_type) { | 
 | 565 | 	case AUDIT_GET: | 
 | 566 | 		status_set.enabled	 = audit_enabled; | 
 | 567 | 		status_set.failure	 = audit_failure; | 
 | 568 | 		status_set.pid		 = audit_pid; | 
 | 569 | 		status_set.rate_limit	 = audit_rate_limit; | 
 | 570 | 		status_set.backlog_limit = audit_backlog_limit; | 
 | 571 | 		status_set.lost		 = atomic_read(&audit_lost); | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 572 | 		status_set.backlog	 = skb_queue_len(&audit_skb_queue); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | 		audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0, | 
 | 574 | 				 &status_set, sizeof(status_set)); | 
 | 575 | 		break; | 
 | 576 | 	case AUDIT_SET: | 
 | 577 | 		if (nlh->nlmsg_len < sizeof(struct audit_status)) | 
 | 578 | 			return -EINVAL; | 
 | 579 | 		status_get   = (struct audit_status *)data; | 
 | 580 | 		if (status_get->mask & AUDIT_STATUS_ENABLED) { | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 581 | 			err = audit_set_enabled(status_get->enabled, | 
 | 582 | 							loginuid, sid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | 			if (err < 0) return err; | 
 | 584 | 		} | 
 | 585 | 		if (status_get->mask & AUDIT_STATUS_FAILURE) { | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 586 | 			err = audit_set_failure(status_get->failure, | 
 | 587 | 							 loginuid, sid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | 			if (err < 0) return err; | 
 | 589 | 		} | 
 | 590 | 		if (status_get->mask & AUDIT_STATUS_PID) { | 
 | 591 | 			int old   = audit_pid; | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 592 | 			if (sid) { | 
| Stephen Smalley | 1a70cd4 | 2006-09-25 23:31:57 -0700 | [diff] [blame] | 593 | 				if ((err = selinux_sid_to_string( | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 594 | 						sid, &ctx, &len))) | 
| Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 595 | 					return err; | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 596 | 				else | 
 | 597 | 					audit_log(NULL, GFP_KERNEL, | 
 | 598 | 						AUDIT_CONFIG_CHANGE, | 
 | 599 | 						"audit_pid=%d old=%d by auid=%u subj=%s", | 
 | 600 | 						status_get->pid, old, | 
 | 601 | 						loginuid, ctx); | 
 | 602 | 				kfree(ctx); | 
 | 603 | 			} else | 
 | 604 | 				audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, | 
 | 605 | 					"audit_pid=%d old=%d by auid=%u", | 
 | 606 | 					  status_get->pid, old, loginuid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | 			audit_pid = status_get->pid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | 		} | 
 | 609 | 		if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) | 
| Serge E. Hallyn | 5d136a0 | 2006-04-27 16:45:14 -0500 | [diff] [blame] | 610 | 			err = audit_set_rate_limit(status_get->rate_limit, | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 611 | 							 loginuid, sid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | 		if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT) | 
| Serge E. Hallyn | 5d136a0 | 2006-04-27 16:45:14 -0500 | [diff] [blame] | 613 | 			err = audit_set_backlog_limit(status_get->backlog_limit, | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 614 | 							loginuid, sid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | 		break; | 
| Steve Grubb | 0547410 | 2005-05-21 00:18:37 +0100 | [diff] [blame] | 616 | 	case AUDIT_USER: | 
| David Woodhouse | 209aba0 | 2005-05-18 10:21:07 +0100 | [diff] [blame] | 617 | 	case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG: | 
| Steve Grubb | 90d526c | 2005-11-03 15:48:08 +0000 | [diff] [blame] | 618 | 	case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2: | 
| David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 619 | 		if (!audit_enabled && msg_type != AUDIT_USER_AVC) | 
 | 620 | 			return 0; | 
| David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 621 |  | 
| David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 622 | 		err = audit_filter_user(&NETLINK_CB(skb), msg_type); | 
| David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 623 | 		if (err == 1) { | 
 | 624 | 			err = 0; | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 625 | 			ab = audit_log_start(NULL, GFP_KERNEL, msg_type); | 
| David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 626 | 			if (ab) { | 
 | 627 | 				audit_log_format(ab, | 
| Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 628 | 						 "user pid=%d uid=%u auid=%u", | 
 | 629 | 						 pid, uid, loginuid); | 
 | 630 | 				if (sid) { | 
| Stephen Smalley | 1a70cd4 | 2006-09-25 23:31:57 -0700 | [diff] [blame] | 631 | 					if (selinux_sid_to_string( | 
| Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 632 | 							sid, &ctx, &len)) { | 
 | 633 | 						audit_log_format(ab,  | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 634 | 							" ssid=%u", sid); | 
| Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 635 | 						/* Maybe call audit_panic? */ | 
 | 636 | 					} else | 
 | 637 | 						audit_log_format(ab,  | 
 | 638 | 							" subj=%s", ctx); | 
 | 639 | 					kfree(ctx); | 
 | 640 | 				} | 
 | 641 | 				audit_log_format(ab, " msg='%.1024s'", | 
 | 642 | 					 (char *)data); | 
| David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 643 | 				audit_set_pid(ab, pid); | 
 | 644 | 				audit_log_end(ab); | 
 | 645 | 			} | 
| David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 646 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | 		break; | 
 | 648 | 	case AUDIT_ADD: | 
 | 649 | 	case AUDIT_DEL: | 
| Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 650 | 		if (nlmsg_len(nlh) < sizeof(struct audit_rule)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | 			return -EINVAL; | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 652 | 		if (audit_enabled == 2) { | 
 | 653 | 			ab = audit_log_start(NULL, GFP_KERNEL, | 
 | 654 | 					AUDIT_CONFIG_CHANGE); | 
 | 655 | 			if (ab) { | 
 | 656 | 				audit_log_format(ab, | 
 | 657 | 						 "pid=%d uid=%u auid=%u", | 
 | 658 | 						 pid, uid, loginuid); | 
 | 659 | 				if (sid) { | 
 | 660 | 					if (selinux_sid_to_string( | 
 | 661 | 							sid, &ctx, &len)) { | 
 | 662 | 						audit_log_format(ab, | 
 | 663 | 							" ssid=%u", sid); | 
 | 664 | 						/* Maybe call audit_panic? */ | 
 | 665 | 					} else | 
 | 666 | 						audit_log_format(ab, | 
 | 667 | 							" subj=%s", ctx); | 
 | 668 | 					kfree(ctx); | 
 | 669 | 				} | 
 | 670 | 				audit_log_format(ab, " audit_enabled=%d res=0", | 
 | 671 | 					audit_enabled); | 
 | 672 | 				audit_log_end(ab); | 
 | 673 | 			} | 
 | 674 | 			return -EPERM; | 
 | 675 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | 		/* fallthrough */ | 
 | 677 | 	case AUDIT_LIST: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | 		err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, | 
| Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 679 | 					   uid, seq, data, nlmsg_len(nlh), | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 680 | 					   loginuid, sid); | 
| Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 681 | 		break; | 
 | 682 | 	case AUDIT_ADD_RULE: | 
 | 683 | 	case AUDIT_DEL_RULE: | 
 | 684 | 		if (nlmsg_len(nlh) < sizeof(struct audit_rule_data)) | 
 | 685 | 			return -EINVAL; | 
| Steve Grubb | 6a01b07 | 2007-01-19 14:39:55 -0500 | [diff] [blame] | 686 | 		if (audit_enabled == 2) { | 
 | 687 | 			ab = audit_log_start(NULL, GFP_KERNEL, | 
 | 688 | 					AUDIT_CONFIG_CHANGE); | 
 | 689 | 			if (ab) { | 
 | 690 | 				audit_log_format(ab, | 
 | 691 | 						 "pid=%d uid=%u auid=%u", | 
 | 692 | 						 pid, uid, loginuid); | 
 | 693 | 				if (sid) { | 
 | 694 | 					if (selinux_sid_to_string( | 
 | 695 | 							sid, &ctx, &len)) { | 
 | 696 | 						audit_log_format(ab, | 
 | 697 | 							" ssid=%u", sid); | 
 | 698 | 						/* Maybe call audit_panic? */ | 
 | 699 | 					} else | 
 | 700 | 						audit_log_format(ab, | 
 | 701 | 							" subj=%s", ctx); | 
 | 702 | 					kfree(ctx); | 
 | 703 | 				} | 
 | 704 | 				audit_log_format(ab, " audit_enabled=%d res=0", | 
 | 705 | 					audit_enabled); | 
 | 706 | 				audit_log_end(ab); | 
 | 707 | 			} | 
 | 708 | 			return -EPERM; | 
 | 709 | 		} | 
| Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 710 | 		/* fallthrough */ | 
 | 711 | 	case AUDIT_LIST_RULES: | 
 | 712 | 		err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, | 
 | 713 | 					   uid, seq, data, nlmsg_len(nlh), | 
| Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 714 | 					   loginuid, sid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | 		break; | 
| Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 716 | 	case AUDIT_SIGNAL_INFO: | 
| Stephen Smalley | 1a70cd4 | 2006-09-25 23:31:57 -0700 | [diff] [blame] | 717 | 		err = selinux_sid_to_string(audit_sig_sid, &ctx, &len); | 
| Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 718 | 		if (err) | 
 | 719 | 			return err; | 
 | 720 | 		sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL); | 
 | 721 | 		if (!sig_data) { | 
 | 722 | 			kfree(ctx); | 
 | 723 | 			return -ENOMEM; | 
 | 724 | 		} | 
 | 725 | 		sig_data->uid = audit_sig_uid; | 
 | 726 | 		sig_data->pid = audit_sig_pid; | 
 | 727 | 		memcpy(sig_data->ctx, ctx, len); | 
 | 728 | 		kfree(ctx); | 
| Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 729 | 		audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,  | 
| Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 730 | 				0, 0, sig_data, sizeof(*sig_data) + len); | 
 | 731 | 		kfree(sig_data); | 
| Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 732 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | 	default: | 
 | 734 | 		err = -EINVAL; | 
 | 735 | 		break; | 
 | 736 | 	} | 
 | 737 |  | 
 | 738 | 	return err < 0 ? err : 0; | 
 | 739 | } | 
 | 740 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 741 | /* | 
 | 742 |  * Get message from skb (based on rtnetlink_rcv_skb).  Each message is | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 |  * processed by audit_receive_msg.  Malformed skbs with wrong length are | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 744 |  * discarded silently. | 
 | 745 |  */ | 
| Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 746 | static void audit_receive_skb(struct sk_buff *skb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { | 
 | 748 | 	int		err; | 
 | 749 | 	struct nlmsghdr	*nlh; | 
 | 750 | 	u32		rlen; | 
 | 751 |  | 
 | 752 | 	while (skb->len >= NLMSG_SPACE(0)) { | 
 | 753 | 		nlh = (struct nlmsghdr *)skb->data; | 
 | 754 | 		if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) | 
| Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 755 | 			return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | 		rlen = NLMSG_ALIGN(nlh->nlmsg_len); | 
 | 757 | 		if (rlen > skb->len) | 
 | 758 | 			rlen = skb->len; | 
 | 759 | 		if ((err = audit_receive_msg(skb, nlh))) { | 
 | 760 | 			netlink_ack(skb, nlh, err); | 
 | 761 | 		} else if (nlh->nlmsg_flags & NLM_F_ACK) | 
 | 762 | 			netlink_ack(skb, nlh, 0); | 
 | 763 | 		skb_pull(skb, rlen); | 
 | 764 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } | 
 | 766 |  | 
 | 767 | /* Receive messages from netlink socket. */ | 
 | 768 | static void audit_receive(struct sock *sk, int length) | 
 | 769 | { | 
 | 770 | 	struct sk_buff  *skb; | 
| Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 771 | 	unsigned int qlen; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 |  | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 773 | 	mutex_lock(&audit_cmd_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 |  | 
| Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 775 | 	for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) { | 
 | 776 | 		skb = skb_dequeue(&sk->sk_receive_queue); | 
 | 777 | 		audit_receive_skb(skb); | 
 | 778 | 		kfree_skb(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | 	} | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 780 | 	mutex_unlock(&audit_cmd_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } | 
 | 782 |  | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 783 | #ifdef CONFIG_AUDITSYSCALL | 
 | 784 | static const struct inotify_operations audit_inotify_ops = { | 
 | 785 | 	.handle_event	= audit_handle_ievent, | 
 | 786 | 	.destroy_watch	= audit_free_parent, | 
 | 787 | }; | 
 | 788 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 |  | 
 | 790 | /* Initialize audit support at boot time. */ | 
 | 791 | static int __init audit_init(void) | 
 | 792 | { | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 793 | 	int i; | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 794 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | 	printk(KERN_INFO "audit: initializing netlink socket (%s)\n", | 
 | 796 | 	       audit_default ? "enabled" : "disabled"); | 
| Patrick McHardy | 0662860 | 2005-08-15 12:33:26 -0700 | [diff] [blame] | 797 | 	audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive, | 
| Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 798 | 					   THIS_MODULE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | 	if (!audit_sock) | 
 | 800 | 		audit_panic("cannot initialize netlink socket"); | 
| Amy Griffis | 71e1c78 | 2006-03-06 22:40:05 -0500 | [diff] [blame] | 801 | 	else | 
 | 802 | 		audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 |  | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 804 | 	skb_queue_head_init(&audit_skb_queue); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | 	audit_initialized = 1; | 
 | 806 | 	audit_enabled = audit_default; | 
| Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 807 |  | 
 | 808 | 	/* Register the callback with selinux.  This callback will be invoked | 
 | 809 | 	 * when a new policy is loaded. */ | 
 | 810 | 	selinux_audit_set_callback(&selinux_audit_rule_update); | 
 | 811 |  | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 812 | 	audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized"); | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 813 |  | 
 | 814 | #ifdef CONFIG_AUDITSYSCALL | 
 | 815 | 	audit_ih = inotify_init(&audit_inotify_ops); | 
 | 816 | 	if (IS_ERR(audit_ih)) | 
 | 817 | 		audit_panic("cannot initialize inotify handle"); | 
| Amy Griffis | 6988434 | 2006-07-13 13:17:12 -0400 | [diff] [blame] | 818 | #endif | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 819 |  | 
 | 820 | 	for (i = 0; i < AUDIT_INODE_BUCKETS; i++) | 
 | 821 | 		INIT_LIST_HEAD(&audit_inode_hash[i]); | 
| Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 822 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | 	return 0; | 
 | 824 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | __initcall(audit_init); | 
 | 826 |  | 
 | 827 | /* Process kernel command-line parameter at boot time.  audit=0 or audit=1. */ | 
 | 828 | static int __init audit_enable(char *str) | 
 | 829 | { | 
 | 830 | 	audit_default = !!simple_strtol(str, NULL, 0); | 
 | 831 | 	printk(KERN_INFO "audit: %s%s\n", | 
 | 832 | 	       audit_default ? "enabled" : "disabled", | 
 | 833 | 	       audit_initialized ? "" : " (after initialization)"); | 
 | 834 | 	if (audit_initialized) | 
 | 835 | 		audit_enabled = audit_default; | 
| OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 836 | 	return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } | 
 | 838 |  | 
 | 839 | __setup("audit=", audit_enable); | 
 | 840 |  | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 841 | static void audit_buffer_free(struct audit_buffer *ab) | 
 | 842 | { | 
 | 843 | 	unsigned long flags; | 
 | 844 |  | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 845 | 	if (!ab) | 
 | 846 | 		return; | 
 | 847 |  | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 848 | 	if (ab->skb) | 
 | 849 | 		kfree_skb(ab->skb); | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 850 |  | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 851 | 	spin_lock_irqsave(&audit_freelist_lock, flags); | 
| Serge E. Hallyn | 5d136a0 | 2006-04-27 16:45:14 -0500 | [diff] [blame] | 852 | 	if (audit_freelist_count > AUDIT_MAXFREE) | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 853 | 		kfree(ab); | 
| Serge E. Hallyn | 5d136a0 | 2006-04-27 16:45:14 -0500 | [diff] [blame] | 854 | 	else { | 
 | 855 | 		audit_freelist_count++; | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 856 | 		list_add(&ab->list, &audit_freelist); | 
| Serge E. Hallyn | 5d136a0 | 2006-04-27 16:45:14 -0500 | [diff] [blame] | 857 | 	} | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 858 | 	spin_unlock_irqrestore(&audit_freelist_lock, flags); | 
 | 859 | } | 
 | 860 |  | 
| Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 861 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, | 
| Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 862 | 						gfp_t gfp_mask, int type) | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 863 | { | 
 | 864 | 	unsigned long flags; | 
 | 865 | 	struct audit_buffer *ab = NULL; | 
| Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 866 | 	struct nlmsghdr *nlh; | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 867 |  | 
 | 868 | 	spin_lock_irqsave(&audit_freelist_lock, flags); | 
 | 869 | 	if (!list_empty(&audit_freelist)) { | 
 | 870 | 		ab = list_entry(audit_freelist.next, | 
 | 871 | 				struct audit_buffer, list); | 
 | 872 | 		list_del(&ab->list); | 
 | 873 | 		--audit_freelist_count; | 
 | 874 | 	} | 
 | 875 | 	spin_unlock_irqrestore(&audit_freelist_lock, flags); | 
 | 876 |  | 
 | 877 | 	if (!ab) { | 
| David Woodhouse | 4332bdd | 2005-05-06 15:59:57 +0100 | [diff] [blame] | 878 | 		ab = kmalloc(sizeof(*ab), gfp_mask); | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 879 | 		if (!ab) | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 880 | 			goto err; | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 881 | 	} | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 882 |  | 
| David Woodhouse | 4332bdd | 2005-05-06 15:59:57 +0100 | [diff] [blame] | 883 | 	ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask); | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 884 | 	if (!ab->skb) | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 885 | 		goto err; | 
 | 886 |  | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 887 | 	ab->ctx = ctx; | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 888 | 	ab->gfp_mask = gfp_mask; | 
| Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 889 | 	nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0)); | 
 | 890 | 	nlh->nlmsg_type = type; | 
 | 891 | 	nlh->nlmsg_flags = 0; | 
 | 892 | 	nlh->nlmsg_pid = 0; | 
 | 893 | 	nlh->nlmsg_seq = 0; | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 894 | 	return ab; | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 895 | err: | 
 | 896 | 	audit_buffer_free(ab); | 
 | 897 | 	return NULL; | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 898 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 900 | /** | 
 | 901 |  * audit_serial - compute a serial number for the audit record | 
 | 902 |  * | 
 | 903 |  * Compute a serial number for the audit record.  Audit records are | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 904 |  * written to user-space as soon as they are generated, so a complete | 
 | 905 |  * audit record may be written in several pieces.  The timestamp of the | 
 | 906 |  * record and this serial number are used by the user-space tools to | 
 | 907 |  * determine which pieces belong to the same audit record.  The | 
 | 908 |  * (timestamp,serial) tuple is unique for each syscall and is live from | 
 | 909 |  * syscall entry to syscall exit. | 
 | 910 |  * | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 911 |  * NOTE: Another possibility is to store the formatted records off the | 
 | 912 |  * audit context (for those records that have a context), and emit them | 
 | 913 |  * all at syscall exit.  However, this could delay the reporting of | 
 | 914 |  * significant errors until syscall exit (or never, if the system | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 915 |  * halts). | 
 | 916 |  */ | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 917 | unsigned int audit_serial(void) | 
 | 918 | { | 
| Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 919 | 	static DEFINE_SPINLOCK(serial_lock); | 
| David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 920 | 	static unsigned int serial = 0; | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 921 |  | 
| David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 922 | 	unsigned long flags; | 
 | 923 | 	unsigned int ret; | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 924 |  | 
| David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 925 | 	spin_lock_irqsave(&serial_lock, flags); | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 926 | 	do { | 
| David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 927 | 		ret = ++serial; | 
 | 928 | 	} while (unlikely(!ret)); | 
| David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 929 | 	spin_unlock_irqrestore(&serial_lock, flags); | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 930 |  | 
| David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 931 | 	return ret; | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 932 | } | 
 | 933 |  | 
 | 934 | static inline void audit_get_stamp(struct audit_context *ctx,  | 
 | 935 | 				   struct timespec *t, unsigned int *serial) | 
 | 936 | { | 
 | 937 | 	if (ctx) | 
 | 938 | 		auditsc_get_stamp(ctx, t, serial); | 
 | 939 | 	else { | 
 | 940 | 		*t = CURRENT_TIME; | 
 | 941 | 		*serial = audit_serial(); | 
 | 942 | 	} | 
 | 943 | } | 
 | 944 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /* Obtain an audit buffer.  This routine does locking to obtain the | 
 | 946 |  * audit buffer, but then no locking is required for calls to | 
 | 947 |  * audit_log_*format.  If the tsk is a task that is currently in a | 
 | 948 |  * syscall, then the syscall is marked as auditable and an audit record | 
 | 949 |  * will be written at syscall exit.  If there is no associated task, tsk | 
 | 950 |  * should be NULL. */ | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 951 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 952 | /** | 
 | 953 |  * audit_log_start - obtain an audit buffer | 
 | 954 |  * @ctx: audit_context (may be NULL) | 
 | 955 |  * @gfp_mask: type of allocation | 
 | 956 |  * @type: audit message type | 
 | 957 |  * | 
 | 958 |  * Returns audit_buffer pointer on success or NULL on error. | 
 | 959 |  * | 
 | 960 |  * Obtain an audit buffer.  This routine does locking to obtain the | 
 | 961 |  * audit buffer, but then no locking is required for calls to | 
 | 962 |  * audit_log_*format.  If the task (ctx) is a task that is currently in a | 
 | 963 |  * syscall, then the syscall is marked as auditable and an audit record | 
 | 964 |  * will be written at syscall exit.  If there is no associated task, then | 
 | 965 |  * task context (ctx) should be NULL. | 
 | 966 |  */ | 
| Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 967 | struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 968 | 				     int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | { | 
 | 970 | 	struct audit_buffer	*ab	= NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | 	struct timespec		t; | 
| Steve Grubb | d812ddb | 2005-04-29 16:09:52 +0100 | [diff] [blame] | 972 | 	unsigned int		serial; | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 973 | 	int reserve; | 
| David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 974 | 	unsigned long timeout_start = jiffies; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 |  | 
 | 976 | 	if (!audit_initialized) | 
 | 977 | 		return NULL; | 
 | 978 |  | 
| Dustin Kirkland | c8edc80 | 2005-11-03 16:12:36 +0000 | [diff] [blame] | 979 | 	if (unlikely(audit_filter_type(type))) | 
 | 980 | 		return NULL; | 
 | 981 |  | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 982 | 	if (gfp_mask & __GFP_WAIT) | 
 | 983 | 		reserve = 0; | 
 | 984 | 	else | 
 | 985 | 		reserve = 5; /* Allow atomic callers to go up to five  | 
 | 986 | 				entries over the normal backlog limit */ | 
 | 987 |  | 
 | 988 | 	while (audit_backlog_limit | 
 | 989 | 	       && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) { | 
| David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 990 | 		if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time | 
 | 991 | 		    && time_before(jiffies, timeout_start + audit_backlog_wait_time)) { | 
 | 992 |  | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 993 | 			/* Wait for auditd to drain the queue a little */ | 
 | 994 | 			DECLARE_WAITQUEUE(wait, current); | 
 | 995 | 			set_current_state(TASK_INTERRUPTIBLE); | 
 | 996 | 			add_wait_queue(&audit_backlog_wait, &wait); | 
 | 997 |  | 
 | 998 | 			if (audit_backlog_limit && | 
 | 999 | 			    skb_queue_len(&audit_skb_queue) > audit_backlog_limit) | 
| David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 1000 | 				schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies); | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1001 |  | 
 | 1002 | 			__set_current_state(TASK_RUNNING); | 
 | 1003 | 			remove_wait_queue(&audit_backlog_wait, &wait); | 
| David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 1004 | 			continue; | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1005 | 		} | 
| David Woodhouse | fb19b4c | 2005-05-19 14:55:56 +0100 | [diff] [blame] | 1006 | 		if (audit_rate_check()) | 
 | 1007 | 			printk(KERN_WARNING | 
 | 1008 | 			       "audit: audit_backlog=%d > " | 
 | 1009 | 			       "audit_backlog_limit=%d\n", | 
 | 1010 | 			       skb_queue_len(&audit_skb_queue), | 
 | 1011 | 			       audit_backlog_limit); | 
 | 1012 | 		audit_log_lost("backlog limit exceeded"); | 
| David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 1013 | 		audit_backlog_wait_time = audit_backlog_wait_overflow; | 
 | 1014 | 		wake_up(&audit_backlog_wait); | 
| David Woodhouse | fb19b4c | 2005-05-19 14:55:56 +0100 | [diff] [blame] | 1015 | 		return NULL; | 
 | 1016 | 	} | 
 | 1017 |  | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1018 | 	ab = audit_buffer_alloc(ctx, gfp_mask, type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | 	if (!ab) { | 
 | 1020 | 		audit_log_lost("out of memory in audit_log_start"); | 
 | 1021 | 		return NULL; | 
 | 1022 | 	} | 
 | 1023 |  | 
| David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1024 | 	audit_get_stamp(ab->ctx, &t, &serial); | 
| Chris Wright | 197c69c | 2005-05-11 10:54:05 +0100 | [diff] [blame] | 1025 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | 	audit_log_format(ab, "audit(%lu.%03lu:%u): ", | 
 | 1027 | 			 t.tv_sec, t.tv_nsec/1000000, serial); | 
 | 1028 | 	return ab; | 
 | 1029 | } | 
 | 1030 |  | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1031 | /** | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1032 |  * audit_expand - expand skb in the audit buffer | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1033 |  * @ab: audit_buffer | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1034 |  * @extra: space to add at tail of the skb | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1035 |  * | 
 | 1036 |  * Returns 0 (no space) on failed expansion, or available space if | 
 | 1037 |  * successful. | 
 | 1038 |  */ | 
| David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 1039 | static inline int audit_expand(struct audit_buffer *ab, int extra) | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1040 | { | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1041 | 	struct sk_buff *skb = ab->skb; | 
| David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 1042 | 	int ret = pskb_expand_head(skb, skb_headroom(skb), extra, | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1043 | 				   ab->gfp_mask); | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1044 | 	if (ret < 0) { | 
 | 1045 | 		audit_log_lost("out of memory in audit_expand"); | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1046 | 		return 0; | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1047 | 	} | 
 | 1048 | 	return skb_tailroom(skb); | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1049 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1051 | /* | 
 | 1052 |  * Format an audit message into the audit buffer.  If there isn't enough | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 |  * room in the audit buffer, more room will be allocated and vsnprint | 
 | 1054 |  * will be called a second time.  Currently, we assume that a printk | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1055 |  * can't format message larger than 1024 bytes, so we don't either. | 
 | 1056 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, | 
 | 1058 | 			      va_list args) | 
 | 1059 | { | 
 | 1060 | 	int len, avail; | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1061 | 	struct sk_buff *skb; | 
| David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame] | 1062 | 	va_list args2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 |  | 
 | 1064 | 	if (!ab) | 
 | 1065 | 		return; | 
 | 1066 |  | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1067 | 	BUG_ON(!ab->skb); | 
 | 1068 | 	skb = ab->skb; | 
 | 1069 | 	avail = skb_tailroom(skb); | 
 | 1070 | 	if (avail == 0) { | 
| David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 1071 | 		avail = audit_expand(ab, AUDIT_BUFSIZ); | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1072 | 		if (!avail) | 
 | 1073 | 			goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | 	} | 
| David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame] | 1075 | 	va_copy(args2, args); | 
| Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 1076 | 	len = vsnprintf(skb->tail, avail, fmt, args); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | 	if (len >= avail) { | 
 | 1078 | 		/* The printk buffer is 1024 bytes long, so if we get | 
 | 1079 | 		 * here and AUDIT_BUFSIZ is at least 1024, then we can | 
 | 1080 | 		 * log everything that printk could have logged. */ | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1081 | 		avail = audit_expand(ab, | 
 | 1082 | 			max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1083 | 		if (!avail) | 
 | 1084 | 			goto out; | 
| David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame] | 1085 | 		len = vsnprintf(skb->tail, avail, fmt, args2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | 	} | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1087 | 	if (len > 0) | 
 | 1088 | 		skb_put(skb, len); | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1089 | out: | 
 | 1090 | 	return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } | 
 | 1092 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1093 | /** | 
 | 1094 |  * audit_log_format - format a message into the audit buffer. | 
 | 1095 |  * @ab: audit_buffer | 
 | 1096 |  * @fmt: format string | 
 | 1097 |  * @...: optional parameters matching @fmt string | 
 | 1098 |  * | 
 | 1099 |  * All the work is done in audit_log_vformat. | 
 | 1100 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) | 
 | 1102 | { | 
 | 1103 | 	va_list args; | 
 | 1104 |  | 
 | 1105 | 	if (!ab) | 
 | 1106 | 		return; | 
 | 1107 | 	va_start(args, fmt); | 
 | 1108 | 	audit_log_vformat(ab, fmt, args); | 
 | 1109 | 	va_end(args); | 
 | 1110 | } | 
 | 1111 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1112 | /** | 
 | 1113 |  * audit_log_hex - convert a buffer to hex and append it to the audit skb | 
 | 1114 |  * @ab: the audit_buffer | 
 | 1115 |  * @buf: buffer to convert to hex | 
 | 1116 |  * @len: length of @buf to be converted | 
 | 1117 |  * | 
 | 1118 |  * No return value; failure to expand is silently ignored. | 
 | 1119 |  * | 
 | 1120 |  * This function will take the passed buf and convert it into a string of | 
 | 1121 |  * ascii hex digits. The new string is placed onto the skb. | 
 | 1122 |  */ | 
 | 1123 | void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1124 | 		size_t len) | 
 | 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1125 | { | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1126 | 	int i, avail, new_len; | 
 | 1127 | 	unsigned char *ptr; | 
 | 1128 | 	struct sk_buff *skb; | 
 | 1129 | 	static const unsigned char *hex = "0123456789ABCDEF"; | 
 | 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1130 |  | 
| Amy Griffis | 8ef2d30 | 2006-09-07 17:03:02 -0400 | [diff] [blame] | 1131 | 	if (!ab) | 
 | 1132 | 		return; | 
 | 1133 |  | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1134 | 	BUG_ON(!ab->skb); | 
 | 1135 | 	skb = ab->skb; | 
 | 1136 | 	avail = skb_tailroom(skb); | 
 | 1137 | 	new_len = len<<1; | 
 | 1138 | 	if (new_len >= avail) { | 
 | 1139 | 		/* Round the buffer request up to the next multiple */ | 
 | 1140 | 		new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1); | 
 | 1141 | 		avail = audit_expand(ab, new_len); | 
 | 1142 | 		if (!avail) | 
 | 1143 | 			return; | 
 | 1144 | 	} | 
 | 1145 |  | 
 | 1146 | 	ptr = skb->tail; | 
 | 1147 | 	for (i=0; i<len; i++) { | 
 | 1148 | 		*ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */ | 
 | 1149 | 		*ptr++ = hex[buf[i] & 0x0F];	  /* Lower nibble */ | 
 | 1150 | 	} | 
 | 1151 | 	*ptr = 0; | 
 | 1152 | 	skb_put(skb, len << 1); /* new string is twice the old string */ | 
 | 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1153 | } | 
 | 1154 |  | 
| Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1155 | /* | 
 | 1156 |  * Format a string of no more than slen characters into the audit buffer, | 
 | 1157 |  * enclosed in quote marks. | 
 | 1158 |  */ | 
 | 1159 | static void audit_log_n_string(struct audit_buffer *ab, size_t slen, | 
 | 1160 | 			       const char *string) | 
 | 1161 | { | 
 | 1162 | 	int avail, new_len; | 
 | 1163 | 	unsigned char *ptr; | 
 | 1164 | 	struct sk_buff *skb; | 
 | 1165 |  | 
| Amy Griffis | 8ef2d30 | 2006-09-07 17:03:02 -0400 | [diff] [blame] | 1166 | 	if (!ab) | 
 | 1167 | 		return; | 
 | 1168 |  | 
| Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1169 | 	BUG_ON(!ab->skb); | 
 | 1170 | 	skb = ab->skb; | 
 | 1171 | 	avail = skb_tailroom(skb); | 
 | 1172 | 	new_len = slen + 3;	/* enclosing quotes + null terminator */ | 
 | 1173 | 	if (new_len > avail) { | 
 | 1174 | 		avail = audit_expand(ab, new_len); | 
 | 1175 | 		if (!avail) | 
 | 1176 | 			return; | 
 | 1177 | 	} | 
 | 1178 | 	ptr = skb->tail; | 
 | 1179 | 	*ptr++ = '"'; | 
 | 1180 | 	memcpy(ptr, string, slen); | 
 | 1181 | 	ptr += slen; | 
 | 1182 | 	*ptr++ = '"'; | 
 | 1183 | 	*ptr = 0; | 
 | 1184 | 	skb_put(skb, slen + 2);	/* don't include null terminator */ | 
 | 1185 | } | 
 | 1186 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1187 | /** | 
| Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1188 |  * audit_log_n_unstrustedstring - log a string that may contain random characters | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1189 |  * @ab: audit_buffer | 
| Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1190 |  * @len: lenth of string (not including trailing null) | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1191 |  * @string: string to be logged | 
 | 1192 |  * | 
 | 1193 |  * This code will escape a string that is passed to it if the string | 
 | 1194 |  * contains a control character, unprintable character, double quote mark, | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1195 |  * or a space. Unescaped strings will start and end with a double quote mark. | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1196 |  * Strings that are escaped are printed in hex (2 digits per char). | 
| Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1197 |  * | 
 | 1198 |  * The caller specifies the number of characters in the string to log, which may | 
 | 1199 |  * or may not be the entire string. | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1200 |  */ | 
| Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1201 | const char *audit_log_n_untrustedstring(struct audit_buffer *ab, size_t len, | 
 | 1202 | 					const char *string) | 
 | 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1203 | { | 
| Andrew Morton | 81b7854 | 2005-04-29 15:59:11 +0100 | [diff] [blame] | 1204 | 	const unsigned char *p = string; | 
 | 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1205 |  | 
 | 1206 | 	while (*p) { | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1207 | 		if (*p == '"' || *p < 0x21 || *p > 0x7f) { | 
| Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1208 | 			audit_log_hex(ab, string, len); | 
 | 1209 | 			return string + len + 1; | 
 | 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1210 | 		} | 
 | 1211 | 		p++; | 
 | 1212 | 	} | 
| Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1213 | 	audit_log_n_string(ab, len, string); | 
| Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1214 | 	return p + 1; | 
 | 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1215 | } | 
 | 1216 |  | 
| Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1217 | /** | 
 | 1218 |  * audit_log_unstrustedstring - log a string that may contain random characters | 
 | 1219 |  * @ab: audit_buffer | 
 | 1220 |  * @string: string to be logged | 
 | 1221 |  * | 
 | 1222 |  * Same as audit_log_n_unstrustedstring(), except that strlen is used to | 
 | 1223 |  * determine string length. | 
 | 1224 |  */ | 
 | 1225 | const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *string) | 
 | 1226 | { | 
 | 1227 | 	return audit_log_n_untrustedstring(ab, strlen(string), string); | 
 | 1228 | } | 
 | 1229 |  | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1230 | /* This is a helper-function to print the escaped d_path */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, | 
 | 1232 | 		      struct dentry *dentry, struct vfsmount *vfsmnt) | 
 | 1233 | { | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1234 | 	char *p, *path; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 |  | 
| Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1236 | 	if (prefix) | 
 | 1237 | 		audit_log_format(ab, " %s", prefix); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 |  | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1239 | 	/* We will allow 11 spaces for ' (deleted)' to be appended */ | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1240 | 	path = kmalloc(PATH_MAX+11, ab->gfp_mask); | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1241 | 	if (!path) { | 
 | 1242 | 		audit_log_format(ab, "<no memory>"); | 
 | 1243 | 		return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | 	} | 
| Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1245 | 	p = d_path(dentry, vfsmnt, path, PATH_MAX+11); | 
 | 1246 | 	if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */ | 
 | 1247 | 		/* FIXME: can we save some information here? */ | 
 | 1248 | 		audit_log_format(ab, "<too long>"); | 
 | 1249 | 	} else  | 
 | 1250 | 		audit_log_untrustedstring(ab, p); | 
 | 1251 | 	kfree(path); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | } | 
 | 1253 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1254 | /** | 
 | 1255 |  * audit_log_end - end one audit record | 
 | 1256 |  * @ab: the audit_buffer | 
 | 1257 |  * | 
 | 1258 |  * The netlink_* functions cannot be called inside an irq context, so | 
 | 1259 |  * the audit buffer is placed on a queue and a tasklet is scheduled to | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 |  * remove them from the queue outside the irq context.  May be called in | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1261 |  * any context. | 
 | 1262 |  */ | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 1263 | void audit_log_end(struct audit_buffer *ab) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | 	if (!ab) | 
 | 1266 | 		return; | 
 | 1267 | 	if (!audit_rate_check()) { | 
 | 1268 | 		audit_log_lost("rate limit exceeded"); | 
 | 1269 | 	} else { | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 1270 | 		if (audit_pid) { | 
 | 1271 | 			struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data; | 
 | 1272 | 			nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0); | 
 | 1273 | 			skb_queue_tail(&audit_skb_queue, ab->skb); | 
 | 1274 | 			ab->skb = NULL; | 
 | 1275 | 			wake_up_interruptible(&kauditd_wait); | 
 | 1276 | 		} else { | 
| David Woodhouse | e1b09eb | 2005-06-24 17:24:11 +0100 | [diff] [blame] | 1277 | 			printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0)); | 
| David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 1278 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | 	} | 
| Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1280 | 	audit_buffer_free(ab); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | } | 
 | 1282 |  | 
| Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1283 | /** | 
 | 1284 |  * audit_log - Log an audit record | 
 | 1285 |  * @ctx: audit context | 
 | 1286 |  * @gfp_mask: type of allocation | 
 | 1287 |  * @type: audit message type | 
 | 1288 |  * @fmt: format string to use | 
 | 1289 |  * @...: variable parameters matching the format string | 
 | 1290 |  * | 
 | 1291 |  * This is a convenience function that calls audit_log_start, | 
 | 1292 |  * audit_log_vformat, and audit_log_end.  It may be called | 
 | 1293 |  * in any context. | 
 | 1294 |  */ | 
| Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 1295 | void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,  | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1296 | 	       const char *fmt, ...) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | { | 
 | 1298 | 	struct audit_buffer *ab; | 
 | 1299 | 	va_list args; | 
 | 1300 |  | 
| David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1301 | 	ab = audit_log_start(ctx, gfp_mask, type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | 	if (ab) { | 
 | 1303 | 		va_start(args, fmt); | 
 | 1304 | 		audit_log_vformat(ab, fmt, args); | 
 | 1305 | 		va_end(args); | 
 | 1306 | 		audit_log_end(ab); | 
 | 1307 | 	} | 
 | 1308 | } | 
| lorenzo@gnu.org | bf45da9 | 2006-03-09 00:33:47 +0100 | [diff] [blame] | 1309 |  | 
 | 1310 | EXPORT_SYMBOL(audit_log_start); | 
 | 1311 | EXPORT_SYMBOL(audit_log_end); | 
 | 1312 | EXPORT_SYMBOL(audit_log_format); | 
 | 1313 | EXPORT_SYMBOL(audit_log); |