audit.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. * @profile: Profile number.
  145. * @index: Index number of functionality.
  146. * @is_granted: True if granted log, false otherwise.
  147. *
  148. * Returns true if this request should be audited, false otherwise.
  149. */
  150. static bool tomoyo_get_audit(const u8 profile, const u8 index,
  151. const bool is_granted)
  152. {
  153. u8 mode;
  154. const u8 category = TOMOYO_MAC_CATEGORY_FILE + TOMOYO_MAX_MAC_INDEX;
  155. struct tomoyo_profile *p;
  156. if (!tomoyo_policy_loaded)
  157. return false;
  158. p = tomoyo_profile(profile);
  159. if (tomoyo_log_count >= p->pref[TOMOYO_PREF_MAX_AUDIT_LOG])
  160. return false;
  161. mode = p->config[index];
  162. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  163. mode = p->config[category];
  164. if (mode == TOMOYO_CONFIG_USE_DEFAULT)
  165. mode = p->default_config;
  166. if (is_granted)
  167. return mode & TOMOYO_CONFIG_WANT_GRANT_LOG;
  168. return mode & TOMOYO_CONFIG_WANT_REJECT_LOG;
  169. }
  170. /**
  171. * tomoyo_write_log2 - Write an audit log.
  172. *
  173. * @r: Pointer to "struct tomoyo_request_info".
  174. * @len: Buffer size needed for @fmt and @args.
  175. * @fmt: The printf()'s format string.
  176. * @args: va_list structure for @fmt.
  177. *
  178. * Returns nothing.
  179. */
  180. void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
  181. va_list args)
  182. {
  183. char *buf;
  184. struct tomoyo_log *entry;
  185. bool quota_exceeded = false;
  186. if (!tomoyo_get_audit(r->profile, r->type, r->granted))
  187. goto out;
  188. buf = tomoyo_init_log(r, len, fmt, args);
  189. if (!buf)
  190. goto out;
  191. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  192. if (!entry) {
  193. kfree(buf);
  194. goto out;
  195. }
  196. entry->log = buf;
  197. len = tomoyo_round2(strlen(buf) + 1);
  198. /*
  199. * The entry->size is used for memory quota checks.
  200. * Don't go beyond strlen(entry->log).
  201. */
  202. entry->size = len + tomoyo_round2(sizeof(*entry));
  203. spin_lock(&tomoyo_log_lock);
  204. if (tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT] &&
  205. tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] + entry->size >=
  206. tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT]) {
  207. quota_exceeded = true;
  208. } else {
  209. tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] += entry->size;
  210. list_add_tail(&entry->list, &tomoyo_log);
  211. tomoyo_log_count++;
  212. }
  213. spin_unlock(&tomoyo_log_lock);
  214. if (quota_exceeded) {
  215. kfree(buf);
  216. kfree(entry);
  217. goto out;
  218. }
  219. wake_up(&tomoyo_log_wait);
  220. out:
  221. return;
  222. }
  223. /**
  224. * tomoyo_write_log - Write an audit log.
  225. *
  226. * @r: Pointer to "struct tomoyo_request_info".
  227. * @fmt: The printf()'s format string, followed by parameters.
  228. *
  229. * Returns nothing.
  230. */
  231. void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
  232. {
  233. va_list args;
  234. int len;
  235. va_start(args, fmt);
  236. len = vsnprintf((char *) &len, 1, fmt, args) + 1;
  237. va_end(args);
  238. va_start(args, fmt);
  239. tomoyo_write_log2(r, len, fmt, args);
  240. va_end(args);
  241. }
  242. /**
  243. * tomoyo_read_log - Read an audit log.
  244. *
  245. * @head: Pointer to "struct tomoyo_io_buffer".
  246. *
  247. * Returns nothing.
  248. */
  249. void tomoyo_read_log(struct tomoyo_io_buffer *head)
  250. {
  251. struct tomoyo_log *ptr = NULL;
  252. if (head->r.w_pos)
  253. return;
  254. kfree(head->read_buf);
  255. head->read_buf = NULL;
  256. spin_lock(&tomoyo_log_lock);
  257. if (!list_empty(&tomoyo_log)) {
  258. ptr = list_entry(tomoyo_log.next, typeof(*ptr), list);
  259. list_del(&ptr->list);
  260. tomoyo_log_count--;
  261. tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] -= ptr->size;
  262. }
  263. spin_unlock(&tomoyo_log_lock);
  264. if (ptr) {
  265. head->read_buf = ptr->log;
  266. head->r.w[head->r.w_pos++] = head->read_buf;
  267. kfree(ptr);
  268. }
  269. }
  270. /**
  271. * tomoyo_poll_log - Wait for an audit log.
  272. *
  273. * @file: Pointer to "struct file".
  274. * @wait: Pointer to "poll_table".
  275. *
  276. * Returns POLLIN | POLLRDNORM when ready to read an audit log.
  277. */
  278. int tomoyo_poll_log(struct file *file, poll_table *wait)
  279. {
  280. if (tomoyo_log_count)
  281. return POLLIN | POLLRDNORM;
  282. poll_wait(file, &tomoyo_log_wait, wait);
  283. if (tomoyo_log_count)
  284. return POLLIN | POLLRDNORM;
  285. return 0;
  286. }