audit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * security/tomoyo/audit.c
  3. *
  4. * Copyright (C) 2005-2011 NTT DATA CORPORATION
  5. */
  6. #include "common.h"
  7. #include <linux/slab.h>
  8. /**
  9. * tomoyo_print_bprm - Print "struct linux_binprm" for auditing.
  10. *
  11. * @bprm: Pointer to "struct linux_binprm".
  12. * @dump: Pointer to "struct tomoyo_page_dump".
  13. *
  14. * Returns the contents of @bprm on success, NULL otherwise.
  15. *
  16. * This function uses kzalloc(), so caller must kfree() if this function
  17. * didn't return NULL.
  18. */
  19. static char *tomoyo_print_bprm(struct linux_binprm *bprm,
  20. struct tomoyo_page_dump *dump)
  21. {
  22. static const int tomoyo_buffer_len = 4096 * 2;
  23. char *buffer = kzalloc(tomoyo_buffer_len, GFP_NOFS);
  24. char *cp;
  25. char *last_start;
  26. int len;
  27. unsigned long pos = bprm->p;
  28. int offset = pos % PAGE_SIZE;
  29. int argv_count = bprm->argc;
  30. int envp_count = bprm->envc;
  31. bool truncated = false;
  32. if (!buffer)
  33. return NULL;
  34. len = snprintf(buffer, tomoyo_buffer_len - 1, "argv[]={ ");
  35. cp = buffer + len;
  36. if (!argv_count) {
  37. memmove(cp, "} envp[]={ ", 11);
  38. cp += 11;
  39. }
  40. last_start = cp;
  41. while (argv_count || envp_count) {
  42. if (!tomoyo_dump_page(bprm, pos, dump))
  43. goto out;
  44. pos += PAGE_SIZE - offset;
  45. /* Read. */
  46. while (offset < PAGE_SIZE) {
  47. const char *kaddr = dump->data;
  48. const unsigned char c = kaddr[offset++];
  49. if (cp == last_start)
  50. *cp++ = '"';
  51. if (cp >= buffer + tomoyo_buffer_len - 32) {
  52. /* Reserve some room for "..." string. */
  53. truncated = true;
  54. } else if (c == '\\') {
  55. *cp++ = '\\';
  56. *cp++ = '\\';
  57. } else if (c > ' ' && c < 127) {
  58. *cp++ = c;
  59. } else if (!c) {
  60. *cp++ = '"';
  61. *cp++ = ' ';
  62. last_start = cp;
  63. } else {
  64. *cp++ = '\\';
  65. *cp++ = (c >> 6) + '0';
  66. *cp++ = ((c >> 3) & 7) + '0';
  67. *cp++ = (c & 7) + '0';
  68. }
  69. if (c)
  70. continue;
  71. if (argv_count) {
  72. if (--argv_count == 0) {
  73. if (truncated) {
  74. cp = last_start;
  75. memmove(cp, "... ", 4);
  76. cp += 4;
  77. }
  78. memmove(cp, "} envp[]={ ", 11);
  79. cp += 11;
  80. last_start = cp;
  81. truncated = false;
  82. }
  83. } else if (envp_count) {
  84. if (--envp_count == 0) {
  85. if (truncated) {
  86. cp = last_start;
  87. memmove(cp, "... ", 4);
  88. cp += 4;
  89. }
  90. }
  91. }
  92. if (!argv_count && !envp_count)
  93. break;
  94. }
  95. offset = 0;
  96. }
  97. *cp++ = '}';
  98. *cp = '\0';
  99. return buffer;
  100. out:
  101. snprintf(buffer, tomoyo_buffer_len - 1,
  102. "argv[]={ ... } envp[]= { ... }");
  103. return buffer;
  104. }
  105. /**
  106. * tomoyo_filetype - Get string representation of file type.
  107. *
  108. * @mode: Mode value for stat().
  109. *
  110. * Returns file type string.
  111. */
  112. static inline const char *tomoyo_filetype(const umode_t mode)
  113. {
  114. switch (mode & S_IFMT) {
  115. case S_IFREG:
  116. case 0:
  117. return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FILE];
  118. case S_IFDIR:
  119. return tomoyo_condition_keyword[TOMOYO_TYPE_IS_DIRECTORY];
  120. case S_IFLNK:
  121. return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SYMLINK];
  122. case S_IFIFO:
  123. return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FIFO];
  124. case S_IFSOCK:
  125. return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SOCKET];
  126. case S_IFBLK:
  127. return tomoyo_condition_keyword[TOMOYO_TYPE_IS_BLOCK_DEV];
  128. case S_IFCHR:
  129. return tomoyo_condition_keyword[TOMOYO_TYPE_IS_CHAR_DEV];
  130. }
  131. return "unknown"; /* This should not happen. */
  132. }
  133. /**
  134. * tomoyo_print_header - Get header line of audit log.
  135. *
  136. * @r: Pointer to "struct tomoyo_request_info".
  137. *
  138. * Returns string representation.
  139. *
  140. * This function uses kmalloc(), so caller must kfree() if this function
  141. * didn't return NULL.
  142. */
  143. static char *tomoyo_print_header(struct tomoyo_request_info *r)
  144. {
  145. struct tomoyo_time stamp;
  146. const pid_t gpid = task_pid_nr(current);
  147. struct tomoyo_obj_info *obj = r->obj;
  148. static const int tomoyo_buffer_len = 4096;
  149. char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
  150. int pos;
  151. u8 i;
  152. if (!buffer)
  153. return NULL;
  154. {
  155. struct timeval tv;
  156. do_gettimeofday(&tv);
  157. tomoyo_convert_time(tv.tv_sec, &stamp);
  158. }
  159. pos = snprintf(buffer, tomoyo_buffer_len - 1,
  160. "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s "
  161. "granted=%s (global-pid=%u) task={ pid=%u ppid=%u "
  162. "uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u "
  163. "fsuid=%u fsgid=%u }", stamp.year, stamp.month,
  164. stamp.day, stamp.hour, stamp.min, stamp.sec, r->profile,
  165. tomoyo_mode[r->mode], tomoyo_yesno(r->granted), gpid,
  166. tomoyo_sys_getpid(), tomoyo_sys_getppid(),
  167. from_kuid(&init_user_ns, current_uid()),
  168. from_kgid(&init_user_ns, current_gid()),
  169. from_kuid(&init_user_ns, current_euid()),
  170. from_kgid(&init_user_ns, current_egid()),
  171. from_kuid(&init_user_ns, current_suid()),
  172. from_kgid(&init_user_ns, current_sgid()),
  173. from_kuid(&init_user_ns, current_fsuid()),
  174. from_kgid(&init_user_ns, current_fsgid()));
  175. if (!obj)
  176. goto no_obj_info;
  177. if (!obj->validate_done) {
  178. tomoyo_get_attributes(obj);
  179. obj->validate_done = true;
  180. }
  181. for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
  182. struct tomoyo_mini_stat *stat;
  183. unsigned int dev;
  184. umode_t mode;
  185. if (!obj->stat_valid[i])
  186. continue;
  187. stat = &obj->stat[i];
  188. dev = stat->dev;
  189. mode = stat->mode;
  190. if (i & 1) {
  191. pos += snprintf(buffer + pos,
  192. tomoyo_buffer_len - 1 - pos,
  193. " path%u.parent={ uid=%u gid=%u "
  194. "ino=%lu perm=0%o }", (i >> 1) + 1,
  195. from_kuid(&init_user_ns, stat->uid),
  196. from_kgid(&init_user_ns, stat->gid),
  197. (unsigned long)stat->ino,
  198. stat->mode & S_IALLUGO);
  199. continue;
  200. }
  201. pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos,
  202. " path%u={ uid=%u gid=%u ino=%lu major=%u"
  203. " minor=%u perm=0%o type=%s", (i >> 1) + 1,
  204. from_kuid(&init_user_ns, stat->uid),
  205. from_kgid(&init_user_ns, stat->gid),
  206. (unsigned long)stat->ino,
  207. MAJOR(dev), MINOR(dev),
  208. mode & S_IALLUGO, tomoyo_filetype(mode));
  209. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  210. dev = stat->rdev;
  211. pos += snprintf(buffer + pos,
  212. tomoyo_buffer_len - 1 - pos,
  213. " dev_major=%u dev_minor=%u",
  214. MAJOR(dev), MINOR(dev));
  215. }
  216. pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos,
  217. " }");
  218. }
  219. no_obj_info:
  220. if (pos < tomoyo_buffer_len - 1)
  221. return buffer;
  222. kfree(buffer);
  223. return NULL;
  224. }
  225. /**
  226. * tomoyo_init_log - Allocate buffer for audit logs.
  227. *
  228. * @r: Pointer to "struct tomoyo_request_info".
  229. * @len: Buffer size needed for @fmt and @args.
  230. * @fmt: The printf()'s format string.
  231. * @args: va_list structure for @fmt.
  232. *
  233. * Returns pointer to allocated memory.
  234. *
  235. * This function uses kzalloc(), so caller must kfree() if this function
  236. * didn't return NULL.
  237. */
  238. char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
  239. va_list args)
  240. {
  241. char *buf = NULL;
  242. char *bprm_info = NULL;
  243. const char *header = NULL;
  244. char *realpath = NULL;
  245. const char *symlink = NULL;
  246. int pos;
  247. const char *domainname = r->domain->domainname->name;
  248. header = tomoyo_print_header(r);
  249. if (!header)
  250. return NULL;
  251. /* +10 is for '\n' etc. and '\0'. */
  252. len += strlen(domainname) + strlen(header) + 10;
  253. if (r->ee) {
  254. struct file *file = r->ee->bprm->file;
  255. realpath = tomoyo_realpath_from_path(&file->f_path);
  256. bprm_info = tomoyo_print_bprm(r->ee->bprm, &r->ee->dump);
  257. if (!realpath || !bprm_info)
  258. goto out;
  259. /* +80 is for " exec={ realpath=\"%s\" argc=%d envc=%d %s }" */
  260. len += strlen(realpath) + 80 + strlen(bprm_info);
  261. } else if (r->obj && r->obj->symlink_target) {
  262. symlink = r->obj->symlink_target->name;
  263. /* +18 is for " symlink.target=\"%s\"" */
  264. len += 18 + strlen(symlink);
  265. }
  266. len = tomoyo_round2(len);
  267. buf = kzalloc(len, GFP_NOFS);
  268. if (!buf)
  269. goto out;
  270. len--;
  271. pos = snprintf(buf, len, "%s", header);
  272. if (realpath) {
  273. struct linux_binprm *bprm = r->ee->bprm;
  274. pos += snprintf(buf + pos, len - pos,
  275. " exec={ realpath=\"%s\" argc=%d envc=%d %s }",
  276. realpath, bprm->argc, bprm->envc, bprm_info);
  277. } else if (symlink)
  278. pos += snprintf(buf + pos, len - pos, " symlink.target=\"%s\"",
  279. symlink);
  280. pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname);
  281. vsnprintf(buf + pos, len - pos, fmt, args);
  282. out:
  283. kfree(realpath);
  284. kfree(bprm_info);
  285. kfree(header);
  286. return buf;
  287. }
  288. /* Wait queue for /sys/kernel/security/tomoyo/audit. */
  289. static DECLARE_WAIT_QUEUE_HEAD(tomoyo_log_wait);
  290. /* Structure for audit log. */
  291. struct tomoyo_log {
  292. struct list_head list;
  293. char *log;
  294. int size;
  295. };
  296. /* The list for "struct tomoyo_log". */
  297. static LIST_HEAD(tomoyo_log);
  298. /* Lock for "struct list_head tomoyo_log". */
  299. static DEFINE_SPINLOCK(tomoyo_log_lock);
  300. /* Length of "stuct list_head tomoyo_log". */
  301. static unsigned int tomoyo_log_count;
  302. /**
  303. * tomoyo_get_audit - Get audit mode.
  304. *
  305. * @ns: Pointer to "struct tomoyo_policy_namespace".
  306. * @profile: Profile number.
  307. * @index: Index number of functionality.
  308. * @is_granted: True if granted log, false otherwise.
  309. *
  310. * Returns true if this request should be audited, false otherwise.
  311. */
  312. static bool tomoyo_get_audit(const struct tomoyo_policy_namespace *ns,
  313. const u8 profile, const u8 index,
  314. const struct tomoyo_acl_info *matched_acl,
  315. const bool is_granted)
  316. {
  317. u8 mode;
  318. const u8 category = tomoyo_index2category[index] +
  319. TOMOYO_MAX_MAC_INDEX;
  320. struct tomoyo_profile *p;
  321. if (!tomoyo_policy_loaded)
  322. return false;
  323. p = tomoyo_profile(ns, profile);
  324. if (tomoyo_log_count >= p->pref[TOMOYO_PREF_MAX_AUDIT_LOG])
  325. return false;
  326. if (is_granted && matched_acl && matched_acl->cond &&
  327. matched_acl->cond->grant_log != TOMOYO_GRANTLOG_AUTO)
  328. return matched_acl->cond->grant_log == TOMOYO_GRANTLOG_YES;
  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;
  334. if (is_granted)
  335. return mode & TOMOYO_CONFIG_WANT_GRANT_LOG;
  336. return mode & TOMOYO_CONFIG_WANT_REJECT_LOG;
  337. }
  338. /**
  339. * tomoyo_write_log2 - Write an audit log.
  340. *
  341. * @r: Pointer to "struct tomoyo_request_info".
  342. * @len: Buffer size needed for @fmt and @args.
  343. * @fmt: The printf()'s format string.
  344. * @args: va_list structure for @fmt.
  345. *
  346. * Returns nothing.
  347. */
  348. void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
  349. va_list args)
  350. {
  351. char *buf;
  352. struct tomoyo_log *entry;
  353. bool quota_exceeded = false;
  354. if (!tomoyo_get_audit(r->domain->ns, r->profile, r->type,
  355. r->matched_acl, r->granted))
  356. goto out;
  357. buf = tomoyo_init_log(r, len, fmt, args);
  358. if (!buf)
  359. goto out;
  360. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  361. if (!entry) {
  362. kfree(buf);
  363. goto out;
  364. }
  365. entry->log = buf;
  366. len = tomoyo_round2(strlen(buf) + 1);
  367. /*
  368. * The entry->size is used for memory quota checks.
  369. * Don't go beyond strlen(entry->log).
  370. */
  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;
  377. } else {
  378. tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] += entry->size;
  379. list_add_tail(&entry->list, &tomoyo_log);
  380. tomoyo_log_count++;
  381. }
  382. spin_unlock(&tomoyo_log_lock);
  383. if (quota_exceeded) {
  384. kfree(buf);
  385. kfree(entry);
  386. goto out;
  387. }
  388. wake_up(&tomoyo_log_wait);
  389. out:
  390. return;
  391. }
  392. /**
  393. * tomoyo_write_log - Write an audit log.
  394. *
  395. * @r: Pointer to "struct tomoyo_request_info".
  396. * @fmt: The printf()'s format string, followed by parameters.
  397. *
  398. * Returns nothing.
  399. */
  400. void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
  401. {
  402. va_list args;
  403. int len;
  404. va_start(args, fmt);
  405. len = vsnprintf((char *) &len, 1, fmt, args) + 1;
  406. va_end(args);
  407. va_start(args, fmt);
  408. tomoyo_write_log2(r, len, fmt, args);
  409. va_end(args);
  410. }
  411. /**
  412. * tomoyo_read_log - Read an audit log.
  413. *
  414. * @head: Pointer to "struct tomoyo_io_buffer".
  415. *
  416. * Returns nothing.
  417. */
  418. void tomoyo_read_log(struct tomoyo_io_buffer *head)
  419. {
  420. struct tomoyo_log *ptr = NULL;
  421. if (head->r.w_pos)
  422. return;
  423. kfree(head->read_buf);
  424. head->read_buf = NULL;
  425. spin_lock(&tomoyo_log_lock);
  426. if (!list_empty(&tomoyo_log)) {
  427. ptr = list_entry(tomoyo_log.next, typeof(*ptr), list);
  428. list_del(&ptr->list);
  429. tomoyo_log_count--;
  430. tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] -= ptr->size;
  431. }
  432. spin_unlock(&tomoyo_log_lock);
  433. if (ptr) {
  434. head->read_buf = ptr->log;
  435. head->r.w[head->r.w_pos++] = head->read_buf;
  436. kfree(ptr);
  437. }
  438. }
  439. /**
  440. * tomoyo_poll_log - Wait for an audit log.
  441. *
  442. * @file: Pointer to "struct file".
  443. * @wait: Pointer to "poll_table". Maybe NULL.
  444. *
  445. * Returns POLLIN | POLLRDNORM when ready to read an audit log.
  446. */
  447. unsigned int tomoyo_poll_log(struct file *file, poll_table *wait)
  448. {
  449. if (tomoyo_log_count)
  450. return POLLIN | POLLRDNORM;
  451. poll_wait(file, &tomoyo_log_wait, wait);
  452. if (tomoyo_log_count)
  453. return POLLIN | POLLRDNORM;
  454. return 0;
  455. }