memory.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_name_list is used for holding string data used by TOMOYO.
  87. * Since same string data is likely used for multiple times (e.g.
  88. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  89. * "const struct tomoyo_path_info *".
  90. */
  91. struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  92. /**
  93. * tomoyo_get_name - Allocate permanent memory for string data.
  94. *
  95. * @name: The string to store into the permernent memory.
  96. *
  97. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  98. */
  99. const struct tomoyo_path_info *tomoyo_get_name(const char *name)
  100. {
  101. struct tomoyo_name_entry *ptr;
  102. unsigned int hash;
  103. int len;
  104. int allocated_len;
  105. struct list_head *head;
  106. if (!name)
  107. return NULL;
  108. len = strlen(name) + 1;
  109. hash = full_name_hash((const unsigned char *) name, len - 1);
  110. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  111. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  112. return NULL;
  113. list_for_each_entry(ptr, head, list) {
  114. if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
  115. continue;
  116. atomic_inc(&ptr->users);
  117. goto out;
  118. }
  119. ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS);
  120. allocated_len = ptr ? ksize(ptr) : 0;
  121. if (!ptr || (tomoyo_quota_for_policy &&
  122. atomic_read(&tomoyo_policy_memory_size) + allocated_len
  123. > tomoyo_quota_for_policy)) {
  124. kfree(ptr);
  125. ptr = NULL;
  126. tomoyo_warn_oom(__func__);
  127. goto out;
  128. }
  129. atomic_add(allocated_len, &tomoyo_policy_memory_size);
  130. ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
  131. memmove((char *) ptr->entry.name, name, len);
  132. atomic_set(&ptr->users, 1);
  133. tomoyo_fill_path_info(&ptr->entry);
  134. list_add_tail(&ptr->list, head);
  135. out:
  136. mutex_unlock(&tomoyo_policy_lock);
  137. return ptr ? &ptr->entry : NULL;
  138. }
  139. /**
  140. * tomoyo_mm_init - Initialize mm related code.
  141. */
  142. void __init tomoyo_mm_init(void)
  143. {
  144. int idx;
  145. for (idx = 0; idx < TOMOYO_MAX_HASH; idx++)
  146. INIT_LIST_HEAD(&tomoyo_name_list[idx]);
  147. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  148. tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME);
  149. list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  150. idx = tomoyo_read_lock();
  151. if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain)
  152. panic("Can't register tomoyo_kernel_domain");
  153. {
  154. /* Load built-in policy. */
  155. tomoyo_write_domain_initializer_policy("/sbin/hotplug",
  156. false, false);
  157. tomoyo_write_domain_initializer_policy("/sbin/modprobe",
  158. false, false);
  159. }
  160. tomoyo_read_unlock(idx);
  161. }
  162. /* Memory allocated for query lists. */
  163. unsigned int tomoyo_query_memory_size;
  164. /* Quota for holding query lists. */
  165. unsigned int tomoyo_quota_for_query;
  166. /**
  167. * tomoyo_read_memory_counter - Check for memory usage in bytes.
  168. *
  169. * @head: Pointer to "struct tomoyo_io_buffer".
  170. *
  171. * Returns memory usage.
  172. */
  173. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head)
  174. {
  175. if (!head->read_eof) {
  176. const unsigned int policy
  177. = atomic_read(&tomoyo_policy_memory_size);
  178. const unsigned int query = tomoyo_query_memory_size;
  179. char buffer[64];
  180. memset(buffer, 0, sizeof(buffer));
  181. if (tomoyo_quota_for_policy)
  182. snprintf(buffer, sizeof(buffer) - 1,
  183. " (Quota: %10u)",
  184. tomoyo_quota_for_policy);
  185. else
  186. buffer[0] = '\0';
  187. tomoyo_io_printf(head, "Policy: %10u%s\n", policy,
  188. buffer);
  189. if (tomoyo_quota_for_query)
  190. snprintf(buffer, sizeof(buffer) - 1,
  191. " (Quota: %10u)",
  192. tomoyo_quota_for_query);
  193. else
  194. buffer[0] = '\0';
  195. tomoyo_io_printf(head, "Query lists: %10u%s\n", query,
  196. buffer);
  197. tomoyo_io_printf(head, "Total: %10u\n", policy + query);
  198. head->read_eof = true;
  199. }
  200. }
  201. /**
  202. * tomoyo_write_memory_quota - Set memory quota.
  203. *
  204. * @head: Pointer to "struct tomoyo_io_buffer".
  205. *
  206. * Returns 0.
  207. */
  208. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head)
  209. {
  210. char *data = head->write_buf;
  211. unsigned int size;
  212. if (sscanf(data, "Policy: %u", &size) == 1)
  213. tomoyo_quota_for_policy = size;
  214. else if (sscanf(data, "Query lists: %u", &size) == 1)
  215. tomoyo_quota_for_query = size;
  216. return 0;
  217. }