audit.c 6.7 KB

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