memory.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /* Memory allocated for policy. */
  30. static atomic_t tomoyo_policy_memory_size;
  31. /* Quota for holding policy. */
  32. static unsigned int tomoyo_quota_for_policy;
  33. /**
  34. * tomoyo_memory_ok - Check memory quota.
  35. *
  36. * @ptr: Pointer to allocated memory.
  37. *
  38. * Returns true on success, false otherwise.
  39. *
  40. * Returns true if @ptr is not NULL and quota not exceeded, false otherwise.
  41. */
  42. bool tomoyo_memory_ok(void *ptr)
  43. {
  44. size_t s = ptr ? ksize(ptr) : 0;
  45. atomic_add(s, &tomoyo_policy_memory_size);
  46. if (ptr && (!tomoyo_quota_for_policy ||
  47. atomic_read(&tomoyo_policy_memory_size)
  48. <= tomoyo_quota_for_policy)) {
  49. memset(ptr, 0, s);
  50. return true;
  51. }
  52. atomic_sub(s, &tomoyo_policy_memory_size);
  53. tomoyo_warn_oom(__func__);
  54. return false;
  55. }
  56. /**
  57. * tomoyo_commit_ok - Check memory quota.
  58. *
  59. * @data: Data to copy from.
  60. * @size: Size in byte.
  61. *
  62. * Returns pointer to allocated memory on success, NULL otherwise.
  63. * @data is zero-cleared on success.
  64. */
  65. void *tomoyo_commit_ok(void *data, const unsigned int size)
  66. {
  67. void *ptr = kzalloc(size, GFP_NOFS);
  68. if (tomoyo_memory_ok(ptr)) {
  69. memmove(ptr, data, size);
  70. memset(data, 0, size);
  71. return ptr;
  72. }
  73. kfree(ptr);
  74. return NULL;
  75. }
  76. /**
  77. * tomoyo_memory_free - Free memory for elements.
  78. *
  79. * @ptr: Pointer to allocated memory.
  80. */
  81. void tomoyo_memory_free(void *ptr)
  82. {
  83. atomic_sub(ksize(ptr), &tomoyo_policy_memory_size);
  84. kfree(ptr);
  85. }
  86. /**
  87. * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
  88. *
  89. * @group_name: The name of address group.
  90. * @idx: Index number.
  91. *
  92. * Returns pointer to "struct tomoyo_group" on success, NULL otherwise.
  93. */
  94. struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 idx)
  95. {
  96. struct tomoyo_group e = { };
  97. struct tomoyo_group *group = NULL;
  98. bool found = false;
  99. if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP)
  100. return NULL;
  101. e.group_name = tomoyo_get_name(group_name);
  102. if (!e.group_name)
  103. return NULL;
  104. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  105. goto out;
  106. list_for_each_entry(group, &tomoyo_group_list[idx], list) {
  107. if (e.group_name != group->group_name)
  108. continue;
  109. atomic_inc(&group->users);
  110. found = true;
  111. break;
  112. }
  113. if (!found) {
  114. struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e));
  115. if (entry) {
  116. INIT_LIST_HEAD(&entry->member_list);
  117. atomic_set(&entry->users, 1);
  118. list_add_tail_rcu(&entry->list,
  119. &tomoyo_group_list[idx]);
  120. group = entry;
  121. found = true;
  122. }
  123. }
  124. mutex_unlock(&tomoyo_policy_lock);
  125. out:
  126. tomoyo_put_name(e.group_name);
  127. return found ? group : NULL;
  128. }
  129. /*
  130. * tomoyo_name_list is used for holding string data used by TOMOYO.
  131. * Since same string data is likely used for multiple times (e.g.
  132. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  133. * "const struct tomoyo_path_info *".
  134. */
  135. struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  136. /**
  137. * tomoyo_get_name - Allocate permanent memory for string data.
  138. *
  139. * @name: The string to store into the permernent memory.
  140. *
  141. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  142. */
  143. const struct tomoyo_path_info *tomoyo_get_name(const char *name)
  144. {
  145. struct tomoyo_name *ptr;
  146. unsigned int hash;
  147. int len;
  148. int allocated_len;
  149. struct list_head *head;
  150. if (!name)
  151. return NULL;
  152. len = strlen(name) + 1;
  153. hash = full_name_hash((const unsigned char *) name, len - 1);
  154. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  155. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  156. return NULL;
  157. list_for_each_entry(ptr, head, list) {
  158. if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
  159. continue;
  160. atomic_inc(&ptr->users);
  161. goto out;
  162. }
  163. ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS);
  164. allocated_len = ptr ? ksize(ptr) : 0;
  165. if (!ptr || (tomoyo_quota_for_policy &&
  166. atomic_read(&tomoyo_policy_memory_size) + allocated_len
  167. > tomoyo_quota_for_policy)) {
  168. kfree(ptr);
  169. ptr = NULL;
  170. tomoyo_warn_oom(__func__);
  171. goto out;
  172. }
  173. atomic_add(allocated_len, &tomoyo_policy_memory_size);
  174. ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
  175. memmove((char *) ptr->entry.name, name, len);
  176. atomic_set(&ptr->users, 1);
  177. tomoyo_fill_path_info(&ptr->entry);
  178. list_add_tail(&ptr->list, head);
  179. out:
  180. mutex_unlock(&tomoyo_policy_lock);
  181. return ptr ? &ptr->entry : NULL;
  182. }
  183. /**
  184. * tomoyo_mm_init - Initialize mm related code.
  185. */
  186. void __init tomoyo_mm_init(void)
  187. {
  188. int idx;
  189. for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
  190. INIT_LIST_HEAD(&tomoyo_policy_list[idx]);
  191. for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
  192. INIT_LIST_HEAD(&tomoyo_group_list[idx]);
  193. for (idx = 0; idx < TOMOYO_MAX_HASH; idx++)
  194. INIT_LIST_HEAD(&tomoyo_name_list[idx]);
  195. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  196. tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
  197. list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  198. idx = tomoyo_read_lock();
  199. if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
  200. panic("Can't register tomoyo_kernel_domain");
  201. {
  202. /* Load built-in policy. */
  203. tomoyo_write_transition_control("/sbin/hotplug", false,
  204. TOMOYO_TRANSITION_CONTROL_INITIALIZE);
  205. tomoyo_write_transition_control("/sbin/modprobe", false,
  206. TOMOYO_TRANSITION_CONTROL_INITIALIZE);
  207. }
  208. tomoyo_read_unlock(idx);
  209. }
  210. /* Memory allocated for query lists. */
  211. unsigned int tomoyo_query_memory_size;
  212. /* Quota for holding query lists. */
  213. unsigned int tomoyo_quota_for_query;
  214. /**
  215. * tomoyo_read_memory_counter - Check for memory usage in bytes.
  216. *
  217. * @head: Pointer to "struct tomoyo_io_buffer".
  218. *
  219. * Returns memory usage.
  220. */
  221. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
  222. {
  223. if (!head->r.eof) {
  224. const unsigned int policy
  225. = atomic_read(&tomoyo_policy_memory_size);
  226. const unsigned int query = tomoyo_query_memory_size;
  227. char buffer[64];
  228. memset(buffer, 0, sizeof(buffer));
  229. if (tomoyo_quota_for_policy)
  230. snprintf(buffer, sizeof(buffer) - 1,
  231. " (Quota: %10u)",
  232. tomoyo_quota_for_policy);
  233. else
  234. buffer[0] = '\0';
  235. tomoyo_io_printf(head, "Policy: %10u%s\n", policy,
  236. buffer);
  237. if (tomoyo_quota_for_query)
  238. snprintf(buffer, sizeof(buffer) - 1,
  239. " (Quota: %10u)",
  240. tomoyo_quota_for_query);
  241. else
  242. buffer[0] = '\0';
  243. tomoyo_io_printf(head, "Query lists: %10u%s\n", query,
  244. buffer);
  245. tomoyo_io_printf(head, "Total: %10u\n", policy + query);
  246. head->r.eof = true;
  247. }
  248. }
  249. /**
  250. * tomoyo_write_memory_quota - Set memory quota.
  251. *
  252. * @head: Pointer to "struct tomoyo_io_buffer".
  253. *
  254. * Returns 0.
  255. */
  256. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
  257. {
  258. char *data = head->write_buf;
  259. unsigned int size;
  260. if (sscanf(data, "Policy: %u", &size) == 1)
  261. tomoyo_quota_for_policy = size;
  262. else if (sscanf(data, "Query lists: %u", &size) == 1)
  263. tomoyo_quota_for_query = size;
  264. return 0;
  265. }