audit.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * security/tomoyo/audit.c
  3. *
  4. * Pathname restriction functions.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include "common.h"
  9. #include <linux/slab.h>
  10. /**
  11. * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
  12. *
  13. * @time: Seconds since 1970/01/01 00:00:00.
  14. * @stamp: Pointer to "struct tomoyo_time".
  15. *
  16. * Returns nothing.
  17. *
  18. * This function does not handle Y2038 problem.
  19. */
  20. static void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp)
  21. {
  22. static const u16 tomoyo_eom[2][12] = {
  23. { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  24. { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  25. };
  26. u16 y;
  27. u8 m;
  28. bool r;
  29. stamp->sec = time % 60;
  30. time /= 60;
  31. stamp->min = time % 60;
  32. time /= 60;
  33. stamp->hour = time % 24;
  34. time /= 24;
  35. for (y = 1970; ; y++) {
  36. const unsigned short days = (y & 3) ? 365 : 366;
  37. if (time < days)
  38. break;
  39. time -= days;
  40. }
  41. r = (y & 3) == 0;
  42. for (m = 0; m < 11 && time >= tomoyo_eom[r][m]; m++)
  43. ;
  44. if (m)
  45. time -= tomoyo_eom[r][m - 1];
  46. stamp->year = y;
  47. stamp->month = ++m;
  48. stamp->day = ++time;
  49. }
  50. /**
  51. * tomoyo_print_header - Get header line of audit log.
  52. *
  53. * @r: Pointer to "struct tomoyo_request_info".
  54. *
  55. * Returns string representation.
  56. *
  57. * This function uses kmalloc(), so caller must kfree() if this function
  58. * didn't return NULL.
  59. */
  60. static char *tomoyo_print_header(struct tomoyo_request_info *r)
  61. {
  62. struct tomoyo_time stamp;
  63. const pid_t gpid = task_pid_nr(current);
  64. static const int tomoyo_buffer_len = 4096;
  65. char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
  66. pid_t ppid;
  67. if (!buffer)
  68. return NULL;
  69. {
  70. struct timeval tv;
  71. do_gettimeofday(&tv);
  72. tomoyo_convert_time(tv.tv_sec, &stamp);
  73. }
  74. rcu_read_lock();
  75. ppid = task_tgid_vnr(current->real_parent);
  76. rcu_read_unlock();
  77. snprintf(buffer, tomoyo_buffer_len - 1,
  78. "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s "
  79. "granted=%s (global-pid=%u) task={ pid=%u ppid=%u "
  80. "uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u "
  81. "fsuid=%u fsgid=%u }",
  82. stamp.year, stamp.month, stamp.day, stamp.hour,
  83. stamp.min, stamp.sec, r->profile, tomoyo_mode[r->mode],
  84. tomoyo_yesno(r->granted), gpid, task_tgid_vnr(current), ppid,
  85. current_uid(), current_gid(), current_euid(), current_egid(),
  86. current_suid(), current_sgid(), current_fsuid(),
  87. current_fsgid());
  88. return buffer;
  89. }
  90. /**
  91. * tomoyo_init_log - Allocate buffer for audit logs.
  92. *
  93. * @r: Pointer to "struct tomoyo_request_info".
  94. * @len: Buffer size needed for @fmt and @args.
  95. * @fmt: The printf()'s format string.
  96. * @args: va_list structure for @fmt.
  97. *
  98. * Returns pointer to allocated memory.
  99. *
  100. * This function uses kzalloc(), so caller must kfree() if this function
  101. * didn't return NULL.
  102. */
  103. char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
  104. va_list args)
  105. {
  106. char *buf = NULL;
  107. const char *header = NULL;
  108. int pos;
  109. const char *domainname = tomoyo_domain()->domainname->name;
  110. header = tomoyo_print_header(r);
  111. if (!header)
  112. return NULL;
  113. /* +10 is for '\n' etc. and '\0'. */
  114. len += strlen(domainname) + strlen(header) + 10;
  115. len = tomoyo_round2(len);
  116. buf = kzalloc(len, GFP_NOFS);
  117. if (!buf)
  118. goto out;
  119. len--;
  120. pos = snprintf(buf, len, "%s", header);
  121. pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname);
  122. vsnprintf(buf + pos, len - pos, fmt, args);
  123. out:
  124. kfree(header);
  125. return buf;
  126. }
  127. /* Wait queue for /sys/kernel/security/tomoyo/audit. */
  128. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_log_wait);
  129. /* Structure for audit log. */
  130. struct tomoyo_log {
  131. struct list_head list;
  132. char *log;
  133. int size;
  134. };
  135. /* The list for "struct tomoyo_log". */
  136. static LIST_HEAD(tomoyo_log);
  137. /* Lock for "struct list_head tomoyo_log". */
  138. static DEFINE_SPINLOCK(tomoyo_log_lock);
  139. /* Length of "stuct list_head tomoyo_log". */
  140. static unsigned int tomoyo_log_count;
  141. /**
  142. * tomoyo_get_audit - Get audit mode.
  143. *
  144. * @ns: Pointer to "struct tomoyo_policy_namespace".
  145. * @profile: Profile number.
  146. * @index: Index number of functionality.
  147. * @is_granted: True if granted log, false otherwise.
  148. *
  149. * Returns true if this request should be audited, false otherwise.
  150. */
  151. static bool tomoyo_get_audit(const struct tomoyo_policy_namespace *ns,
  152. const u8 profile, const u8 index,
  153. const bool is_granted)
  154. {
  155. u8 mode;
  156. const u8 category = TOMOYO_MAC_CATEGORY_FILE + TOMOYO_MAX_MAC_INDEX;
  157. struct tomoyo_profile *p;
  158. if (!tomoyo_policy_loaded)
  159. return false;
  160. p = tomoyo_profile(ns, profile);
  161. if (tomoyo_log_count >= p->pref[TOMOYO_PREF_MAX_AUDIT_LOG])
  162. return false;
  163. mode = p->config[index];
  164. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  165. mode = p->config[category];
  166. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  167. mode = p->default_config;
  168. if (is_granted)
  169. return mode & TOMOYO_CONFIG_WANT_GRANT_LOG;
  170. return mode & TOMOYO_CONFIG_WANT_REJECT_LOG;
  171. }
  172. /**
  173. * tomoyo_write_log2 - Write an audit log.
  174. *
  175. * @r: Pointer to "struct tomoyo_request_info".
  176. * @len: Buffer size needed for @fmt and @args.
  177. * @fmt: The printf()'s format string.
  178. * @args: va_list structure for @fmt.
  179. *
  180. * Returns nothing.
  181. */
  182. void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
  183. va_list args)
  184. {
  185. char *buf;
  186. struct tomoyo_log *entry;
  187. bool quota_exceeded = false;
  188. if (!tomoyo_get_audit(r->domain->ns, r->profile, r->type, r->granted))
  189. goto out;
  190. buf = tomoyo_init_log(r, len, fmt, args);
  191. if (!buf)
  192. goto out;
  193. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  194. if (!entry) {
  195. kfree(buf);
  196. goto out;
  197. }
  198. entry->log = buf;
  199. len = tomoyo_round2(strlen(buf) + 1);
  200. /*
  201. * The entry->size is used for memory quota checks.
  202. * Don't go beyond strlen(entry->log).
  203. */
  204. entry->size = len + tomoyo_round2(sizeof(*entry));
  205. spin_lock(&tomoyo_log_lock);
  206. if (tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT] &&
  207. tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] + entry->size >=
  208. tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT]) {
  209. quota_exceeded = true;
  210. } else {
  211. tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] += entry->size;
  212. list_add_tail(&entry->list, &tomoyo_log);
  213. tomoyo_log_count++;
  214. }
  215. spin_unlock(&tomoyo_log_lock);
  216. if (quota_exceeded) {
  217. kfree(buf);
  218. kfree(entry);
  219. goto out;
  220. }
  221. wake_up(&tomoyo_log_wait);
  222. out:
  223. return;
  224. }
  225. /**
  226. * tomoyo_write_log - Write an audit log.
  227. *
  228. * @r: Pointer to "struct tomoyo_request_info".
  229. * @fmt: The printf()'s format string, followed by parameters.
  230. *
  231. * Returns nothing.
  232. */
  233. void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
  234. {
  235. va_list args;
  236. int len;
  237. va_start(args, fmt);
  238. len = vsnprintf((char *) &len, 1, fmt, args) + 1;
  239. va_end(args);
  240. va_start(args, fmt);
  241. tomoyo_write_log2(r, len, fmt, args);
  242. va_end(args);
  243. }
  244. /**
  245. * tomoyo_read_log - Read an audit log.
  246. *
  247. * @head: Pointer to "struct tomoyo_io_buffer".
  248. *
  249. * Returns nothing.
  250. */
  251. void tomoyo_read_log(struct tomoyo_io_buffer *head)
  252. {
  253. struct tomoyo_log *ptr = NULL;
  254. if (head->r.w_pos)
  255. return;
  256. kfree(head->read_buf);
  257. head->read_buf = NULL;
  258. spin_lock(&tomoyo_log_lock);
  259. if (!list_empty(&tomoyo_log)) {
  260. ptr = list_entry(tomoyo_log.next, typeof(*ptr), list);
  261. list_del(&ptr->list);
  262. tomoyo_log_count--;
  263. tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] -= ptr->size;
  264. }
  265. spin_unlock(&tomoyo_log_lock);
  266. if (ptr) {
  267. head->read_buf = ptr->log;
  268. head->r.w[head->r.w_pos++] = head->read_buf;
  269. kfree(ptr);
  270. }
  271. }
  272. /**
  273. * tomoyo_poll_log - Wait for an audit log.
  274. *
  275. * @file: Pointer to "struct file".
  276. * @wait: Pointer to "poll_table".
  277. *
  278. * Returns POLLIN | POLLRDNORM when ready to read an audit log.
  279. */
  280. int tomoyo_poll_log(struct file *file, poll_table *wait)
  281. {
  282. if (tomoyo_log_count)
  283. return POLLIN | POLLRDNORM;
  284. poll_wait(file, &tomoyo_log_wait, wait);
  285. if (tomoyo_log_count)
  286. return POLLIN | POLLRDNORM;
  287. return 0;
  288. }