memory.c 7.3 KB

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