audit.c revision 5b636857fee642694e287e3a181b523b16098c93
1/*
2 * security/tomoyo/audit.c
3 *
4 * Pathname restriction functions.
5 *
6 * Copyright (C) 2005-2010  NTT DATA CORPORATION
7 */
8
9#include "common.h"
10#include <linux/slab.h>
11
12/**
13 * tomoyo_print_bprm - Print "struct linux_binprm" for auditing.
14 *
15 * @bprm: Pointer to "struct linux_binprm".
16 * @dump: Pointer to "struct tomoyo_page_dump".
17 *
18 * Returns the contents of @bprm on success, NULL otherwise.
19 *
20 * This function uses kzalloc(), so caller must kfree() if this function
21 * didn't return NULL.
22 */
23static char *tomoyo_print_bprm(struct linux_binprm *bprm,
24			       struct tomoyo_page_dump *dump)
25{
26	static const int tomoyo_buffer_len = 4096 * 2;
27	char *buffer = kzalloc(tomoyo_buffer_len, GFP_NOFS);
28	char *cp;
29	char *last_start;
30	int len;
31	unsigned long pos = bprm->p;
32	int offset = pos % PAGE_SIZE;
33	int argv_count = bprm->argc;
34	int envp_count = bprm->envc;
35	bool truncated = false;
36	if (!buffer)
37		return NULL;
38	len = snprintf(buffer, tomoyo_buffer_len - 1, "argv[]={ ");
39	cp = buffer + len;
40	if (!argv_count) {
41		memmove(cp, "} envp[]={ ", 11);
42		cp += 11;
43	}
44	last_start = cp;
45	while (argv_count || envp_count) {
46		if (!tomoyo_dump_page(bprm, pos, dump))
47			goto out;
48		pos += PAGE_SIZE - offset;
49		/* Read. */
50		while (offset < PAGE_SIZE) {
51			const char *kaddr = dump->data;
52			const unsigned char c = kaddr[offset++];
53			if (cp == last_start)
54				*cp++ = '"';
55			if (cp >= buffer + tomoyo_buffer_len - 32) {
56				/* Reserve some room for "..." string. */
57				truncated = true;
58			} else if (c == '\\') {
59				*cp++ = '\\';
60				*cp++ = '\\';
61			} else if (c > ' ' && c < 127) {
62				*cp++ = c;
63			} else if (!c) {
64				*cp++ = '"';
65				*cp++ = ' ';
66				last_start = cp;
67			} else {
68				*cp++ = '\\';
69				*cp++ = (c >> 6) + '0';
70				*cp++ = ((c >> 3) & 7) + '0';
71				*cp++ = (c & 7) + '0';
72			}
73			if (c)
74				continue;
75			if (argv_count) {
76				if (--argv_count == 0) {
77					if (truncated) {
78						cp = last_start;
79						memmove(cp, "... ", 4);
80						cp += 4;
81					}
82					memmove(cp, "} envp[]={ ", 11);
83					cp += 11;
84					last_start = cp;
85					truncated = false;
86				}
87			} else if (envp_count) {
88				if (--envp_count == 0) {
89					if (truncated) {
90						cp = last_start;
91						memmove(cp, "... ", 4);
92						cp += 4;
93					}
94				}
95			}
96			if (!argv_count && !envp_count)
97				break;
98		}
99		offset = 0;
100	}
101	*cp++ = '}';
102	*cp = '\0';
103	return buffer;
104out:
105	snprintf(buffer, tomoyo_buffer_len - 1,
106		 "argv[]={ ... } envp[]= { ... }");
107	return buffer;
108}
109
110/**
111 * tomoyo_filetype - Get string representation of file type.
112 *
113 * @mode: Mode value for stat().
114 *
115 * Returns file type string.
116 */
117static inline const char *tomoyo_filetype(const mode_t mode)
118{
119	switch (mode & S_IFMT) {
120	case S_IFREG:
121	case 0:
122		return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FILE];
123	case S_IFDIR:
124		return tomoyo_condition_keyword[TOMOYO_TYPE_IS_DIRECTORY];
125	case S_IFLNK:
126		return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SYMLINK];
127	case S_IFIFO:
128		return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FIFO];
129	case S_IFSOCK:
130		return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SOCKET];
131	case S_IFBLK:
132		return tomoyo_condition_keyword[TOMOYO_TYPE_IS_BLOCK_DEV];
133	case S_IFCHR:
134		return tomoyo_condition_keyword[TOMOYO_TYPE_IS_CHAR_DEV];
135	}
136	return "unknown"; /* This should not happen. */
137}
138
139/**
140 * tomoyo_print_header - Get header line of audit log.
141 *
142 * @r: Pointer to "struct tomoyo_request_info".
143 *
144 * Returns string representation.
145 *
146 * This function uses kmalloc(), so caller must kfree() if this function
147 * didn't return NULL.
148 */
149static char *tomoyo_print_header(struct tomoyo_request_info *r)
150{
151	struct tomoyo_time stamp;
152	const pid_t gpid = task_pid_nr(current);
153	struct tomoyo_obj_info *obj = r->obj;
154	static const int tomoyo_buffer_len = 4096;
155	char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
156	int pos;
157	u8 i;
158	if (!buffer)
159		return NULL;
160	{
161		struct timeval tv;
162		do_gettimeofday(&tv);
163		tomoyo_convert_time(tv.tv_sec, &stamp);
164	}
165	pos = snprintf(buffer, tomoyo_buffer_len - 1,
166		       "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s "
167		       "granted=%s (global-pid=%u) task={ pid=%u ppid=%u "
168		       "uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u "
169		       "fsuid=%u fsgid=%u }", stamp.year, stamp.month,
170		       stamp.day, stamp.hour, stamp.min, stamp.sec, r->profile,
171		       tomoyo_mode[r->mode], tomoyo_yesno(r->granted), gpid,
172		       tomoyo_sys_getpid(), tomoyo_sys_getppid(),
173		       current_uid(), current_gid(), current_euid(),
174		       current_egid(), current_suid(), current_sgid(),
175		       current_fsuid(), current_fsgid());
176	if (!obj)
177		goto no_obj_info;
178	if (!obj->validate_done) {
179		tomoyo_get_attributes(obj);
180		obj->validate_done = true;
181	}
182	for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
183		struct tomoyo_mini_stat *stat;
184		unsigned int dev;
185		mode_t mode;
186		if (!obj->stat_valid[i])
187			continue;
188		stat = &obj->stat[i];
189		dev = stat->dev;
190		mode = stat->mode;
191		if (i & 1) {
192			pos += snprintf(buffer + pos,
193					tomoyo_buffer_len - 1 - pos,
194					" path%u.parent={ uid=%u gid=%u "
195					"ino=%lu perm=0%o }", (i >> 1) + 1,
196					stat->uid, stat->gid, (unsigned long)
197					stat->ino, stat->mode & S_IALLUGO);
198			continue;
199		}
200		pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos,
201				" path%u={ uid=%u gid=%u ino=%lu major=%u"
202				" minor=%u perm=0%o type=%s", (i >> 1) + 1,
203				stat->uid, stat->gid, (unsigned long)
204				stat->ino, MAJOR(dev), MINOR(dev),
205				mode & S_IALLUGO, tomoyo_filetype(mode));
206		if (S_ISCHR(mode) || S_ISBLK(mode)) {
207			dev = stat->rdev;
208			pos += snprintf(buffer + pos,
209					tomoyo_buffer_len - 1 - pos,
210					" dev_major=%u dev_minor=%u",
211					MAJOR(dev), MINOR(dev));
212		}
213		pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos,
214				" }");
215	}
216no_obj_info:
217	if (pos < tomoyo_buffer_len - 1)
218		return buffer;
219	kfree(buffer);
220	return NULL;
221}
222
223/**
224 * tomoyo_init_log - Allocate buffer for audit logs.
225 *
226 * @r:    Pointer to "struct tomoyo_request_info".
227 * @len:  Buffer size needed for @fmt and @args.
228 * @fmt:  The printf()'s format string.
229 * @args: va_list structure for @fmt.
230 *
231 * Returns pointer to allocated memory.
232 *
233 * This function uses kzalloc(), so caller must kfree() if this function
234 * didn't return NULL.
235 */
236char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
237		      va_list args)
238{
239	char *buf = NULL;
240	char *bprm_info = NULL;
241	const char *header = NULL;
242	char *realpath = NULL;
243	const char *symlink = NULL;
244	int pos;
245	const char *domainname = r->domain->domainname->name;
246	header = tomoyo_print_header(r);
247	if (!header)
248		return NULL;
249	/* +10 is for '\n' etc. and '\0'. */
250	len += strlen(domainname) + strlen(header) + 10;
251	if (r->ee) {
252		struct file *file = r->ee->bprm->file;
253		realpath = tomoyo_realpath_from_path(&file->f_path);
254		bprm_info = tomoyo_print_bprm(r->ee->bprm, &r->ee->dump);
255		if (!realpath || !bprm_info)
256			goto out;
257		/* +80 is for " exec={ realpath=\"%s\" argc=%d envc=%d %s }" */
258		len += strlen(realpath) + 80 + strlen(bprm_info);
259	} else if (r->obj && r->obj->symlink_target) {
260		symlink = r->obj->symlink_target->name;
261		/* +18 is for " symlink.target=\"%s\"" */
262		len += 18 + strlen(symlink);
263	}
264	len = tomoyo_round2(len);
265	buf = kzalloc(len, GFP_NOFS);
266	if (!buf)
267		goto out;
268	len--;
269	pos = snprintf(buf, len, "%s", header);
270	if (realpath) {
271		struct linux_binprm *bprm = r->ee->bprm;
272		pos += snprintf(buf + pos, len - pos,
273				" exec={ realpath=\"%s\" argc=%d envc=%d %s }",
274				realpath, bprm->argc, bprm->envc, bprm_info);
275	} else if (symlink)
276		pos += snprintf(buf + pos, len - pos, " symlink.target=\"%s\"",
277				symlink);
278	pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname);
279	vsnprintf(buf + pos, len - pos, fmt, args);
280out:
281	kfree(realpath);
282	kfree(bprm_info);
283	kfree(header);
284	return buf;
285}
286
287/* Wait queue for /sys/kernel/security/tomoyo/audit. */
288static DECLARE_WAIT_QUEUE_HEAD(tomoyo_log_wait);
289
290/* Structure for audit log. */
291struct tomoyo_log {
292	struct list_head list;
293	char *log;
294	int size;
295};
296
297/* The list for "struct tomoyo_log". */
298static LIST_HEAD(tomoyo_log);
299
300/* Lock for "struct list_head tomoyo_log". */
301static DEFINE_SPINLOCK(tomoyo_log_lock);
302
303/* Length of "stuct list_head tomoyo_log". */
304static unsigned int tomoyo_log_count;
305
306/**
307 * tomoyo_get_audit - Get audit mode.
308 *
309 * @ns:          Pointer to "struct tomoyo_policy_namespace".
310 * @profile:     Profile number.
311 * @index:       Index number of functionality.
312 * @is_granted:  True if granted log, false otherwise.
313 *
314 * Returns true if this request should be audited, false otherwise.
315 */
316static bool tomoyo_get_audit(const struct tomoyo_policy_namespace *ns,
317			     const u8 profile, const u8 index,
318			     const bool is_granted)
319{
320	u8 mode;
321	const u8 category = tomoyo_index2category[index] +
322		TOMOYO_MAX_MAC_INDEX;
323	struct tomoyo_profile *p;
324	if (!tomoyo_policy_loaded)
325		return false;
326	p = tomoyo_profile(ns, profile);
327	if (tomoyo_log_count >= p->pref[TOMOYO_PREF_MAX_AUDIT_LOG])
328		return false;
329	mode = p->config[index];
330	if (mode == TOMOYO_CONFIG_USE_DEFAULT)
331		mode = p->config[category];
332	if (mode == TOMOYO_CONFIG_USE_DEFAULT)
333		mode = p->default_config;
334	if (is_granted)
335		return mode & TOMOYO_CONFIG_WANT_GRANT_LOG;
336	return mode & TOMOYO_CONFIG_WANT_REJECT_LOG;
337}
338
339/**
340 * tomoyo_write_log2 - Write an audit log.
341 *
342 * @r:    Pointer to "struct tomoyo_request_info".
343 * @len:  Buffer size needed for @fmt and @args.
344 * @fmt:  The printf()'s format string.
345 * @args: va_list structure for @fmt.
346 *
347 * Returns nothing.
348 */
349void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
350		       va_list args)
351{
352	char *buf;
353	struct tomoyo_log *entry;
354	bool quota_exceeded = false;
355	if (!tomoyo_get_audit(r->domain->ns, r->profile, r->type, r->granted))
356		goto out;
357	buf = tomoyo_init_log(r, len, fmt, args);
358	if (!buf)
359		goto out;
360	entry = kzalloc(sizeof(*entry), GFP_NOFS);
361	if (!entry) {
362		kfree(buf);
363		goto out;
364	}
365	entry->log = buf;
366	len = tomoyo_round2(strlen(buf) + 1);
367	/*
368	 * The entry->size is used for memory quota checks.
369	 * Don't go beyond strlen(entry->log).
370	 */
371	entry->size = len + tomoyo_round2(sizeof(*entry));
372	spin_lock(&tomoyo_log_lock);
373	if (tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT] &&
374	    tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] + entry->size >=
375	    tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT]) {
376		quota_exceeded = true;
377	} else {
378		tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] += entry->size;
379		list_add_tail(&entry->list, &tomoyo_log);
380		tomoyo_log_count++;
381	}
382	spin_unlock(&tomoyo_log_lock);
383	if (quota_exceeded) {
384		kfree(buf);
385		kfree(entry);
386		goto out;
387	}
388	wake_up(&tomoyo_log_wait);
389out:
390	return;
391}
392
393/**
394 * tomoyo_write_log - Write an audit log.
395 *
396 * @r:   Pointer to "struct tomoyo_request_info".
397 * @fmt: The printf()'s format string, followed by parameters.
398 *
399 * Returns nothing.
400 */
401void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
402{
403	va_list args;
404	int len;
405	va_start(args, fmt);
406	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
407	va_end(args);
408	va_start(args, fmt);
409	tomoyo_write_log2(r, len, fmt, args);
410	va_end(args);
411}
412
413/**
414 * tomoyo_read_log - Read an audit log.
415 *
416 * @head: Pointer to "struct tomoyo_io_buffer".
417 *
418 * Returns nothing.
419 */
420void tomoyo_read_log(struct tomoyo_io_buffer *head)
421{
422	struct tomoyo_log *ptr = NULL;
423	if (head->r.w_pos)
424		return;
425	kfree(head->read_buf);
426	head->read_buf = NULL;
427	spin_lock(&tomoyo_log_lock);
428	if (!list_empty(&tomoyo_log)) {
429		ptr = list_entry(tomoyo_log.next, typeof(*ptr), list);
430		list_del(&ptr->list);
431		tomoyo_log_count--;
432		tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] -= ptr->size;
433	}
434	spin_unlock(&tomoyo_log_lock);
435	if (ptr) {
436		head->read_buf = ptr->log;
437		head->r.w[head->r.w_pos++] = head->read_buf;
438		kfree(ptr);
439	}
440}
441
442/**
443 * tomoyo_poll_log - Wait for an audit log.
444 *
445 * @file: Pointer to "struct file".
446 * @wait: Pointer to "poll_table".
447 *
448 * Returns POLLIN | POLLRDNORM when ready to read an audit log.
449 */
450int tomoyo_poll_log(struct file *file, poll_table *wait)
451{
452	if (tomoyo_log_count)
453		return POLLIN | POLLRDNORM;
454	poll_wait(file, &tomoyo_log_wait, wait);
455	if (tomoyo_log_count)
456		return POLLIN | POLLRDNORM;
457	return 0;
458}
459