Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 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 | /** |
Tetsuo Handa | 8761afd | 2011-07-08 13:22:41 +0900 | [diff] [blame^] | 13 | * tomoyo_filetype - Get string representation of file type. |
| 14 | * |
| 15 | * @mode: Mode value for stat(). |
| 16 | * |
| 17 | * Returns file type string. |
| 18 | */ |
| 19 | static inline const char *tomoyo_filetype(const mode_t mode) |
| 20 | { |
| 21 | switch (mode & S_IFMT) { |
| 22 | case S_IFREG: |
| 23 | case 0: |
| 24 | return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FILE]; |
| 25 | case S_IFDIR: |
| 26 | return tomoyo_condition_keyword[TOMOYO_TYPE_IS_DIRECTORY]; |
| 27 | case S_IFLNK: |
| 28 | return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SYMLINK]; |
| 29 | case S_IFIFO: |
| 30 | return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FIFO]; |
| 31 | case S_IFSOCK: |
| 32 | return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SOCKET]; |
| 33 | case S_IFBLK: |
| 34 | return tomoyo_condition_keyword[TOMOYO_TYPE_IS_BLOCK_DEV]; |
| 35 | case S_IFCHR: |
| 36 | return tomoyo_condition_keyword[TOMOYO_TYPE_IS_CHAR_DEV]; |
| 37 | } |
| 38 | return "unknown"; /* This should not happen. */ |
| 39 | } |
| 40 | |
| 41 | /** |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 42 | * tomoyo_print_header - Get header line of audit log. |
| 43 | * |
| 44 | * @r: Pointer to "struct tomoyo_request_info". |
| 45 | * |
| 46 | * Returns string representation. |
| 47 | * |
| 48 | * This function uses kmalloc(), so caller must kfree() if this function |
| 49 | * didn't return NULL. |
| 50 | */ |
| 51 | static char *tomoyo_print_header(struct tomoyo_request_info *r) |
| 52 | { |
| 53 | struct tomoyo_time stamp; |
| 54 | const pid_t gpid = task_pid_nr(current); |
Tetsuo Handa | 8761afd | 2011-07-08 13:22:41 +0900 | [diff] [blame^] | 55 | struct tomoyo_obj_info *obj = r->obj; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 56 | static const int tomoyo_buffer_len = 4096; |
| 57 | char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS); |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 58 | int pos; |
Tetsuo Handa | 8761afd | 2011-07-08 13:22:41 +0900 | [diff] [blame^] | 59 | u8 i; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 60 | if (!buffer) |
| 61 | return NULL; |
| 62 | { |
| 63 | struct timeval tv; |
| 64 | do_gettimeofday(&tv); |
| 65 | tomoyo_convert_time(tv.tv_sec, &stamp); |
| 66 | } |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 67 | pos = snprintf(buffer, tomoyo_buffer_len - 1, |
| 68 | "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s " |
| 69 | "granted=%s (global-pid=%u) task={ pid=%u ppid=%u " |
| 70 | "uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u " |
| 71 | "fsuid=%u fsgid=%u }", stamp.year, stamp.month, |
| 72 | stamp.day, stamp.hour, stamp.min, stamp.sec, r->profile, |
| 73 | tomoyo_mode[r->mode], tomoyo_yesno(r->granted), gpid, |
| 74 | tomoyo_sys_getpid(), tomoyo_sys_getppid(), |
| 75 | current_uid(), current_gid(), current_euid(), |
| 76 | current_egid(), current_suid(), current_sgid(), |
| 77 | current_fsuid(), current_fsgid()); |
Tetsuo Handa | 8761afd | 2011-07-08 13:22:41 +0900 | [diff] [blame^] | 78 | if (!obj) |
| 79 | goto no_obj_info; |
| 80 | if (!obj->validate_done) { |
| 81 | tomoyo_get_attributes(obj); |
| 82 | obj->validate_done = true; |
| 83 | } |
| 84 | for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) { |
| 85 | struct tomoyo_mini_stat *stat; |
| 86 | unsigned int dev; |
| 87 | mode_t mode; |
| 88 | if (!obj->stat_valid[i]) |
| 89 | continue; |
| 90 | stat = &obj->stat[i]; |
| 91 | dev = stat->dev; |
| 92 | mode = stat->mode; |
| 93 | if (i & 1) { |
| 94 | pos += snprintf(buffer + pos, |
| 95 | tomoyo_buffer_len - 1 - pos, |
| 96 | " path%u.parent={ uid=%u gid=%u " |
| 97 | "ino=%lu perm=0%o }", (i >> 1) + 1, |
| 98 | stat->uid, stat->gid, (unsigned long) |
| 99 | stat->ino, stat->mode & S_IALLUGO); |
| 100 | continue; |
| 101 | } |
| 102 | pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos, |
| 103 | " path%u={ uid=%u gid=%u ino=%lu major=%u" |
| 104 | " minor=%u perm=0%o type=%s", (i >> 1) + 1, |
| 105 | stat->uid, stat->gid, (unsigned long) |
| 106 | stat->ino, MAJOR(dev), MINOR(dev), |
| 107 | mode & S_IALLUGO, tomoyo_filetype(mode)); |
| 108 | if (S_ISCHR(mode) || S_ISBLK(mode)) { |
| 109 | dev = stat->rdev; |
| 110 | pos += snprintf(buffer + pos, |
| 111 | tomoyo_buffer_len - 1 - pos, |
| 112 | " dev_major=%u dev_minor=%u", |
| 113 | MAJOR(dev), MINOR(dev)); |
| 114 | } |
| 115 | pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos, |
| 116 | " }"); |
| 117 | } |
| 118 | no_obj_info: |
Tetsuo Handa | 2066a36 | 2011-07-08 13:21:37 +0900 | [diff] [blame] | 119 | if (pos < tomoyo_buffer_len - 1) |
| 120 | return buffer; |
| 121 | kfree(buffer); |
| 122 | return NULL; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * tomoyo_init_log - Allocate buffer for audit logs. |
| 127 | * |
| 128 | * @r: Pointer to "struct tomoyo_request_info". |
| 129 | * @len: Buffer size needed for @fmt and @args. |
| 130 | * @fmt: The printf()'s format string. |
| 131 | * @args: va_list structure for @fmt. |
| 132 | * |
| 133 | * Returns pointer to allocated memory. |
| 134 | * |
| 135 | * This function uses kzalloc(), so caller must kfree() if this function |
| 136 | * didn't return NULL. |
| 137 | */ |
| 138 | char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt, |
| 139 | va_list args) |
| 140 | { |
| 141 | char *buf = NULL; |
| 142 | const char *header = NULL; |
| 143 | int pos; |
Tetsuo Handa | ea50481 | 2011-06-30 17:32:30 +0900 | [diff] [blame] | 144 | const char *domainname = r->domain->domainname->name; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 145 | header = tomoyo_print_header(r); |
| 146 | if (!header) |
| 147 | return NULL; |
| 148 | /* +10 is for '\n' etc. and '\0'. */ |
| 149 | len += strlen(domainname) + strlen(header) + 10; |
| 150 | len = tomoyo_round2(len); |
| 151 | buf = kzalloc(len, GFP_NOFS); |
| 152 | if (!buf) |
| 153 | goto out; |
| 154 | len--; |
| 155 | pos = snprintf(buf, len, "%s", header); |
| 156 | pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname); |
| 157 | vsnprintf(buf + pos, len - pos, fmt, args); |
| 158 | out: |
| 159 | kfree(header); |
| 160 | return buf; |
| 161 | } |
| 162 | |
| 163 | /* Wait queue for /sys/kernel/security/tomoyo/audit. */ |
| 164 | static DECLARE_WAIT_QUEUE_HEAD(tomoyo_log_wait); |
| 165 | |
| 166 | /* Structure for audit log. */ |
| 167 | struct tomoyo_log { |
| 168 | struct list_head list; |
| 169 | char *log; |
| 170 | int size; |
| 171 | }; |
| 172 | |
| 173 | /* The list for "struct tomoyo_log". */ |
| 174 | static LIST_HEAD(tomoyo_log); |
| 175 | |
| 176 | /* Lock for "struct list_head tomoyo_log". */ |
| 177 | static DEFINE_SPINLOCK(tomoyo_log_lock); |
| 178 | |
| 179 | /* Length of "stuct list_head tomoyo_log". */ |
| 180 | static unsigned int tomoyo_log_count; |
| 181 | |
| 182 | /** |
| 183 | * tomoyo_get_audit - Get audit mode. |
| 184 | * |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 185 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 186 | * @profile: Profile number. |
| 187 | * @index: Index number of functionality. |
| 188 | * @is_granted: True if granted log, false otherwise. |
| 189 | * |
| 190 | * Returns true if this request should be audited, false otherwise. |
| 191 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 192 | static bool tomoyo_get_audit(const struct tomoyo_policy_namespace *ns, |
| 193 | const u8 profile, const u8 index, |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 194 | const bool is_granted) |
| 195 | { |
| 196 | u8 mode; |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 197 | const u8 category = tomoyo_index2category[index] + |
| 198 | TOMOYO_MAX_MAC_INDEX; |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 199 | struct tomoyo_profile *p; |
| 200 | if (!tomoyo_policy_loaded) |
| 201 | return false; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 202 | p = tomoyo_profile(ns, profile); |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 203 | if (tomoyo_log_count >= p->pref[TOMOYO_PREF_MAX_AUDIT_LOG]) |
| 204 | return false; |
| 205 | mode = p->config[index]; |
| 206 | if (mode == TOMOYO_CONFIG_USE_DEFAULT) |
| 207 | mode = p->config[category]; |
| 208 | if (mode == TOMOYO_CONFIG_USE_DEFAULT) |
| 209 | mode = p->default_config; |
| 210 | if (is_granted) |
| 211 | return mode & TOMOYO_CONFIG_WANT_GRANT_LOG; |
| 212 | return mode & TOMOYO_CONFIG_WANT_REJECT_LOG; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * tomoyo_write_log2 - Write an audit log. |
| 217 | * |
| 218 | * @r: Pointer to "struct tomoyo_request_info". |
| 219 | * @len: Buffer size needed for @fmt and @args. |
| 220 | * @fmt: The printf()'s format string. |
| 221 | * @args: va_list structure for @fmt. |
| 222 | * |
| 223 | * Returns nothing. |
| 224 | */ |
| 225 | void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt, |
| 226 | va_list args) |
| 227 | { |
| 228 | char *buf; |
| 229 | struct tomoyo_log *entry; |
| 230 | bool quota_exceeded = false; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 231 | if (!tomoyo_get_audit(r->domain->ns, r->profile, r->type, r->granted)) |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 232 | goto out; |
| 233 | buf = tomoyo_init_log(r, len, fmt, args); |
| 234 | if (!buf) |
| 235 | goto out; |
| 236 | entry = kzalloc(sizeof(*entry), GFP_NOFS); |
| 237 | if (!entry) { |
| 238 | kfree(buf); |
| 239 | goto out; |
| 240 | } |
| 241 | entry->log = buf; |
| 242 | len = tomoyo_round2(strlen(buf) + 1); |
| 243 | /* |
| 244 | * The entry->size is used for memory quota checks. |
| 245 | * Don't go beyond strlen(entry->log). |
| 246 | */ |
| 247 | entry->size = len + tomoyo_round2(sizeof(*entry)); |
| 248 | spin_lock(&tomoyo_log_lock); |
| 249 | if (tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT] && |
| 250 | tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] + entry->size >= |
| 251 | tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT]) { |
| 252 | quota_exceeded = true; |
| 253 | } else { |
| 254 | tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] += entry->size; |
| 255 | list_add_tail(&entry->list, &tomoyo_log); |
| 256 | tomoyo_log_count++; |
| 257 | } |
| 258 | spin_unlock(&tomoyo_log_lock); |
| 259 | if (quota_exceeded) { |
| 260 | kfree(buf); |
| 261 | kfree(entry); |
| 262 | goto out; |
| 263 | } |
| 264 | wake_up(&tomoyo_log_wait); |
| 265 | out: |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * tomoyo_write_log - Write an audit log. |
| 271 | * |
| 272 | * @r: Pointer to "struct tomoyo_request_info". |
| 273 | * @fmt: The printf()'s format string, followed by parameters. |
| 274 | * |
| 275 | * Returns nothing. |
| 276 | */ |
| 277 | void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...) |
| 278 | { |
| 279 | va_list args; |
| 280 | int len; |
| 281 | va_start(args, fmt); |
| 282 | len = vsnprintf((char *) &len, 1, fmt, args) + 1; |
| 283 | va_end(args); |
| 284 | va_start(args, fmt); |
| 285 | tomoyo_write_log2(r, len, fmt, args); |
| 286 | va_end(args); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * tomoyo_read_log - Read an audit log. |
| 291 | * |
| 292 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 293 | * |
| 294 | * Returns nothing. |
| 295 | */ |
| 296 | void tomoyo_read_log(struct tomoyo_io_buffer *head) |
| 297 | { |
| 298 | struct tomoyo_log *ptr = NULL; |
| 299 | if (head->r.w_pos) |
| 300 | return; |
| 301 | kfree(head->read_buf); |
| 302 | head->read_buf = NULL; |
| 303 | spin_lock(&tomoyo_log_lock); |
| 304 | if (!list_empty(&tomoyo_log)) { |
| 305 | ptr = list_entry(tomoyo_log.next, typeof(*ptr), list); |
| 306 | list_del(&ptr->list); |
| 307 | tomoyo_log_count--; |
| 308 | tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] -= ptr->size; |
| 309 | } |
| 310 | spin_unlock(&tomoyo_log_lock); |
| 311 | if (ptr) { |
| 312 | head->read_buf = ptr->log; |
| 313 | head->r.w[head->r.w_pos++] = head->read_buf; |
| 314 | kfree(ptr); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * tomoyo_poll_log - Wait for an audit log. |
| 320 | * |
| 321 | * @file: Pointer to "struct file". |
| 322 | * @wait: Pointer to "poll_table". |
| 323 | * |
| 324 | * Returns POLLIN | POLLRDNORM when ready to read an audit log. |
| 325 | */ |
| 326 | int tomoyo_poll_log(struct file *file, poll_table *wait) |
| 327 | { |
| 328 | if (tomoyo_log_count) |
| 329 | return POLLIN | POLLRDNORM; |
| 330 | poll_wait(file, &tomoyo_log_wait, wait); |
| 331 | if (tomoyo_log_count) |
| 332 | return POLLIN | POLLRDNORM; |
| 333 | return 0; |
| 334 | } |