2 * security/tomoyo/audit.c
4 * Pathname restriction functions.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
10 #include <linux/slab.h>
13 * tomoyo_print_bprm - Print "struct linux_binprm" for auditing.
15 * @bprm: Pointer to "struct linux_binprm".
16 * @dump: Pointer to "struct tomoyo_page_dump".
18 * Returns the contents of @bprm on success, NULL otherwise.
20 * This function uses kzalloc(), so caller must kfree() if this function
23 static char *tomoyo_print_bprm(struct linux_binprm *bprm,
24 struct tomoyo_page_dump *dump)
26 static const int tomoyo_buffer_len = 4096 * 2;
27 char *buffer = kzalloc(tomoyo_buffer_len, GFP_NOFS);
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;
38 len = snprintf(buffer, tomoyo_buffer_len - 1, "argv[]={ ");
41 memmove(cp, "} envp[]={ ", 11);
45 while (argv_count || envp_count) {
46 if (!tomoyo_dump_page(bprm, pos, dump))
48 pos += PAGE_SIZE - offset;
50 while (offset < PAGE_SIZE) {
51 const char *kaddr = dump->data;
52 const unsigned char c = kaddr[offset++];
55 if (cp >= buffer + tomoyo_buffer_len - 32) {
56 /* Reserve some room for "..." string. */
58 } else if (c == '\\') {
61 } else if (c > ' ' && c < 127) {
69 *cp++ = (c >> 6) + '0';
70 *cp++ = ((c >> 3) & 7) + '0';
71 *cp++ = (c & 7) + '0';
76 if (--argv_count == 0) {
79 memmove(cp, "... ", 4);
82 memmove(cp, "} envp[]={ ", 11);
87 } else if (envp_count) {
88 if (--envp_count == 0) {
91 memmove(cp, "... ", 4);
96 if (!argv_count && !envp_count)
105 snprintf(buffer, tomoyo_buffer_len - 1,
106 "argv[]={ ... } envp[]= { ... }");
111 * tomoyo_filetype - Get string representation of file type.
113 * @mode: Mode value for stat().
115 * Returns file type string.
117 static inline const char *tomoyo_filetype(const mode_t mode)
119 switch (mode & S_IFMT) {
122 return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FILE];
124 return tomoyo_condition_keyword[TOMOYO_TYPE_IS_DIRECTORY];
126 return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SYMLINK];
128 return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FIFO];
130 return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SOCKET];
132 return tomoyo_condition_keyword[TOMOYO_TYPE_IS_BLOCK_DEV];
134 return tomoyo_condition_keyword[TOMOYO_TYPE_IS_CHAR_DEV];
136 return "unknown"; /* This should not happen. */
140 * tomoyo_print_header - Get header line of audit log.
142 * @r: Pointer to "struct tomoyo_request_info".
144 * Returns string representation.
146 * This function uses kmalloc(), so caller must kfree() if this function
147 * didn't return NULL.
149 static char *tomoyo_print_header(struct tomoyo_request_info *r)
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);
162 do_gettimeofday(&tv);
163 tomoyo_convert_time(tv.tv_sec, &stamp);
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());
178 if (!obj->validate_done) {
179 tomoyo_get_attributes(obj);
180 obj->validate_done = true;
182 for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
183 struct tomoyo_mini_stat *stat;
186 if (!obj->stat_valid[i])
188 stat = &obj->stat[i];
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);
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)) {
208 pos += snprintf(buffer + pos,
209 tomoyo_buffer_len - 1 - pos,
210 " dev_major=%u dev_minor=%u",
211 MAJOR(dev), MINOR(dev));
213 pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos,
217 if (pos < tomoyo_buffer_len - 1)
224 * tomoyo_init_log - Allocate buffer for audit logs.
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.
231 * Returns pointer to allocated memory.
233 * This function uses kzalloc(), so caller must kfree() if this function
234 * didn't return NULL.
236 char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
240 char *bprm_info = NULL;
241 const char *header = NULL;
242 char *realpath = NULL;
243 const char *symlink = NULL;
245 const char *domainname = r->domain->domainname->name;
246 header = tomoyo_print_header(r);
249 /* +10 is for '\n' etc. and '\0'. */
250 len += strlen(domainname) + strlen(header) + 10;
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)
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);
264 len = tomoyo_round2(len);
265 buf = kzalloc(len, GFP_NOFS);
269 pos = snprintf(buf, len, "%s", header);
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);
276 pos += snprintf(buf + pos, len - pos, " symlink.target=\"%s\"",
278 pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname);
279 vsnprintf(buf + pos, len - pos, fmt, args);
287 /* Wait queue for /sys/kernel/security/tomoyo/audit. */
288 static DECLARE_WAIT_QUEUE_HEAD(tomoyo_log_wait);
290 /* Structure for audit log. */
292 struct list_head list;
297 /* The list for "struct tomoyo_log". */
298 static LIST_HEAD(tomoyo_log);
300 /* Lock for "struct list_head tomoyo_log". */
301 static DEFINE_SPINLOCK(tomoyo_log_lock);
303 /* Length of "stuct list_head tomoyo_log". */
304 static unsigned int tomoyo_log_count;
307 * tomoyo_get_audit - Get audit mode.
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.
314 * Returns true if this request should be audited, false otherwise.
316 static bool tomoyo_get_audit(const struct tomoyo_policy_namespace *ns,
317 const u8 profile, const u8 index,
318 const bool is_granted)
321 const u8 category = tomoyo_index2category[index] +
322 TOMOYO_MAX_MAC_INDEX;
323 struct tomoyo_profile *p;
324 if (!tomoyo_policy_loaded)
326 p = tomoyo_profile(ns, profile);
327 if (tomoyo_log_count >= p->pref[TOMOYO_PREF_MAX_AUDIT_LOG])
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;
335 return mode & TOMOYO_CONFIG_WANT_GRANT_LOG;
336 return mode & TOMOYO_CONFIG_WANT_REJECT_LOG;
340 * tomoyo_write_log2 - Write an audit log.
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.
349 void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
353 struct tomoyo_log *entry;
354 bool quota_exceeded = false;
355 if (!tomoyo_get_audit(r->domain->ns, r->profile, r->type, r->granted))
357 buf = tomoyo_init_log(r, len, fmt, args);
360 entry = kzalloc(sizeof(*entry), GFP_NOFS);
366 len = tomoyo_round2(strlen(buf) + 1);
368 * The entry->size is used for memory quota checks.
369 * Don't go beyond strlen(entry->log).
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;
378 tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] += entry->size;
379 list_add_tail(&entry->list, &tomoyo_log);
382 spin_unlock(&tomoyo_log_lock);
383 if (quota_exceeded) {
388 wake_up(&tomoyo_log_wait);
394 * tomoyo_write_log - Write an audit log.
396 * @r: Pointer to "struct tomoyo_request_info".
397 * @fmt: The printf()'s format string, followed by parameters.
401 void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
406 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
409 tomoyo_write_log2(r, len, fmt, args);
414 * tomoyo_read_log - Read an audit log.
416 * @head: Pointer to "struct tomoyo_io_buffer".
420 void tomoyo_read_log(struct tomoyo_io_buffer *head)
422 struct tomoyo_log *ptr = NULL;
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);
432 tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] -= ptr->size;
434 spin_unlock(&tomoyo_log_lock);
436 head->read_buf = ptr->log;
437 head->r.w[head->r.w_pos++] = head->read_buf;
443 * tomoyo_poll_log - Wait for an audit log.
445 * @file: Pointer to "struct file".
446 * @wait: Pointer to "poll_table".
448 * Returns POLLIN | POLLRDNORM when ready to read an audit log.
450 int tomoyo_poll_log(struct file *file, poll_table *wait)
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;