memory.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * security/tomoyo/memory.c
  3. *
  4. * Memory management functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include <linux/hash.h>
  9. #include <linux/slab.h>
  10. #include "common.h"
  11. /**
  12. * tomoyo_warn_oom - Print out of memory warning message.
  13. *
  14. * @function: Function's name.
  15. */
  16. void tomoyo_warn_oom(const char *function)
  17. {
  18. /* Reduce error messages. */
  19. static pid_t tomoyo_last_pid;
  20. const pid_t pid = current->pid;
  21. if (tomoyo_last_pid != pid) {
  22. printk(KERN_WARNING "ERROR: Out of memory at %s.\n",
  23. function);
  24. tomoyo_last_pid = pid;
  25. }
  26. if (!tomoyo_policy_loaded)
  27. panic("MAC Initialization failed.\n");
  28. }
  29. /* Memoy currently used by policy/audit log/query. */
  30. unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
  31. /* Memory quota for "policy"/"audit log"/"query". */
  32. unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
  33. /* Memory allocated for policy. */
  34. static atomic_t tomoyo_policy_memory_size;
  35. /* Quota for holding policy. */
  36. static unsigned int tomoyo_quota_for_policy;
  37. /**
  38. * tomoyo_memory_ok - Check memory quota.
  39. *
  40. * @ptr: Pointer to allocated memory.
  41. *
  42. * Returns true on success, false otherwise.
  43. *
  44. * Returns true if @ptr is not NULL and quota not exceeded, false otherwise.
  45. */
  46. bool tomoyo_memory_ok(void *ptr)
  47. {
  48. size_t s = ptr ? ksize(ptr) : 0;
  49. atomic_add(s, &tomoyo_policy_memory_size);
  50. if (ptr && (!tomoyo_quota_for_policy ||
  51. atomic_read(&tomoyo_policy_memory_size)
  52. <= tomoyo_quota_for_policy)) {
  53. memset(ptr, 0, s);
  54. return true;
  55. }
  56. atomic_sub(s, &tomoyo_policy_memory_size);
  57. tomoyo_warn_oom(__func__);
  58. return false;
  59. }
  60. /**
  61. * tomoyo_commit_ok - Check memory quota.
  62. *
  63. * @data: Data to copy from.
  64. * @size: Size in byte.
  65. *
  66. * Returns pointer to allocated memory on success, NULL otherwise.
  67. * @data is zero-cleared on success.
  68. */
  69. void *tomoyo_commit_ok(void *data, const unsigned int size)
  70. {
  71. void *ptr = kzalloc(size, GFP_NOFS);
  72. if (tomoyo_memory_ok(ptr)) {
  73. memmove(ptr, data, size);
  74. memset(data, 0, size);
  75. return ptr;
  76. }
  77. kfree(ptr);
  78. return NULL;
  79. }
  80. /**
  81. * tomoyo_memory_free - Free memory for elements.
  82. *
  83. * @ptr: Pointer to allocated memory.
  84. */
  85. void tomoyo_memory_free(void *ptr)
  86. {
  87. atomic_sub(ksize(ptr), &tomoyo_policy_memory_size);
  88. kfree(ptr);
  89. }
  90. /**
  91. * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
  92. *
  93. * @param: Pointer to "struct tomoyo_acl_param".
  94. * @idx: Index number.
  95. *
  96. * Returns pointer to "struct tomoyo_group" on success, NULL otherwise.
  97. */
  98. struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
  99. const u8 idx)
  100. {
  101. struct tomoyo_group e = { };
  102. struct tomoyo_group *group = NULL;
  103. struct list_head *list;
  104. const char *group_name = tomoyo_read_token(param);
  105. bool found = false;
  106. if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP)
  107. return NULL;
  108. e.group_name = tomoyo_get_name(group_name);
  109. if (!e.group_name)
  110. return NULL;
  111. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  112. goto out;
  113. list = &param->ns->group_list[idx];
  114. list_for_each_entry(group, list, head.list) {
  115. if (e.group_name != group->group_name)
  116. continue;
  117. atomic_inc(&group->head.users);
  118. found = true;
  119. break;
  120. }
  121. if (!found) {
  122. struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e));
  123. if (entry) {
  124. INIT_LIST_HEAD(&entry->member_list);
  125. atomic_set(&entry->head.users, 1);
  126. list_add_tail_rcu(&entry->head.list, list);
  127. group = entry;
  128. found = true;
  129. }
  130. }
  131. mutex_unlock(&tomoyo_policy_lock);
  132. out:
  133. tomoyo_put_name(e.group_name);
  134. return found ? group : NULL;
  135. }
  136. /*
  137. * tomoyo_name_list is used for holding string data used by TOMOYO.
  138. * Since same string data is likely used for multiple times (e.g.
  139. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  140. * "const struct tomoyo_path_info *".
  141. */
  142. struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  143. /**
  144. * tomoyo_get_name - Allocate permanent memory for string data.
  145. *
  146. * @name: The string to store into the permernent memory.
  147. *
  148. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  149. */
  150. const struct tomoyo_path_info *tomoyo_get_name(const char *name)
  151. {
  152. struct tomoyo_name *ptr;
  153. unsigned int hash;
  154. int len;
  155. int allocated_len;
  156. struct list_head *head;
  157. if (!name)
  158. return NULL;
  159. len = strlen(name) + 1;
  160. hash = full_name_hash((const unsigned char *) name, len - 1);
  161. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  162. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  163. return NULL;
  164. list_for_each_entry(ptr, head, head.list) {
  165. if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
  166. continue;
  167. atomic_inc(&ptr->head.users);
  168. goto out;
  169. }
  170. ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS);
  171. allocated_len = ptr ? ksize(ptr) : 0;
  172. if (!ptr || (tomoyo_quota_for_policy &&
  173. atomic_read(&tomoyo_policy_memory_size) + allocated_len
  174. > tomoyo_quota_for_policy)) {
  175. kfree(ptr);
  176. ptr = NULL;
  177. tomoyo_warn_oom(__func__);
  178. goto out;
  179. }
  180. atomic_add(allocated_len, &tomoyo_policy_memory_size);
  181. ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
  182. memmove((char *) ptr->entry.name, name, len);
  183. atomic_set(&ptr->head.users, 1);
  184. tomoyo_fill_path_info(&ptr->entry);
  185. list_add_tail(&ptr->head.list, head);
  186. out:
  187. mutex_unlock(&tomoyo_policy_lock);
  188. return ptr ? &ptr->entry : NULL;
  189. }
  190. /* Initial namespace.*/
  191. struct tomoyo_policy_namespace tomoyo_kernel_namespace;
  192. /**
  193. * tomoyo_mm_init - Initialize mm related code.
  194. */
  195. void __init tomoyo_mm_init(void)
  196. {
  197. int idx;
  198. for (idx = 0; idx < TOMOYO_MAX_HASH; idx++)
  199. INIT_LIST_HEAD(&tomoyo_name_list[idx]);
  200. tomoyo_kernel_namespace.name = "<kernel>";
  201. tomoyo_init_policy_namespace(&tomoyo_kernel_namespace);
  202. tomoyo_kernel_domain.ns = &tomoyo_kernel_namespace;
  203. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  204. tomoyo_kernel_domain.domainname = tomoyo_get_name("<kernel>");
  205. list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  206. #if 0
  207. /* Will be replaced with tomoyo_load_builtin_policy(). */
  208. {
  209. /* Load built-in policy. */
  210. tomoyo_write_transition_control("/sbin/hotplug", false,
  211. TOMOYO_TRANSITION_CONTROL_INITIALIZE);
  212. tomoyo_write_transition_control("/sbin/modprobe", false,
  213. TOMOYO_TRANSITION_CONTROL_INITIALIZE);
  214. }
  215. #endif
  216. }
  217. /* Memory allocated for query lists. */
  218. unsigned int tomoyo_query_memory_size;
  219. /* Quota for holding query lists. */
  220. unsigned int tomoyo_quota_for_query;
  221. /**
  222. * tomoyo_read_memory_counter - Check for memory usage in bytes.
  223. *
  224. * @head: Pointer to "struct tomoyo_io_buffer".
  225. *
  226. * Returns memory usage.
  227. */
  228. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
  229. {
  230. if (!head->r.eof) {
  231. const unsigned int policy
  232. = atomic_read(&tomoyo_policy_memory_size);
  233. const unsigned int query = tomoyo_query_memory_size;
  234. char buffer[64];
  235. memset(buffer, 0, sizeof(buffer));
  236. if (tomoyo_quota_for_policy)
  237. snprintf(buffer, sizeof(buffer) - 1,
  238. " (Quota: %10u)",
  239. tomoyo_quota_for_policy);
  240. else
  241. buffer[0] = '\0';
  242. tomoyo_io_printf(head, "Policy: %10u%s\n", policy,
  243. buffer);
  244. if (tomoyo_quota_for_query)
  245. snprintf(buffer, sizeof(buffer) - 1,
  246. " (Quota: %10u)",
  247. tomoyo_quota_for_query);
  248. else
  249. buffer[0] = '\0';
  250. tomoyo_io_printf(head, "Query lists: %10u%s\n", query,
  251. buffer);
  252. tomoyo_io_printf(head, "Total: %10u\n", policy + query);
  253. head->r.eof = true;
  254. }
  255. }
  256. /**
  257. * tomoyo_write_memory_quota - Set memory quota.
  258. *
  259. * @head: Pointer to "struct tomoyo_io_buffer".
  260. *
  261. * Returns 0.
  262. */
  263. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
  264. {
  265. char *data = head->write_buf;
  266. unsigned int size;
  267. if (sscanf(data, "Policy: %u", &size) == 1)
  268. tomoyo_quota_for_policy = size;
  269. else if (sscanf(data, "Query lists: %u", &size) == 1)
  270. tomoyo_quota_for_query = size;
  271. return 0;
  272. }