memory.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /* Lock for protecting tomoyo_memory_used. */
  30. static DEFINE_SPINLOCK(tomoyo_policy_memory_lock);
  31. /* Memoy currently used by policy/audit log/query. */
  32. unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
  33. /* Memory quota for "policy"/"audit log"/"query". */
  34. unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
  35. /**
  36. * tomoyo_memory_ok - Check memory quota.
  37. *
  38. * @ptr: Pointer to allocated memory.
  39. *
  40. * Returns true on success, false otherwise.
  41. *
  42. * Returns true if @ptr is not NULL and quota not exceeded, false otherwise.
  43. */
  44. bool tomoyo_memory_ok(void *ptr)
  45. {
  46. if (ptr) {
  47. const size_t s = ksize(ptr);
  48. bool result;
  49. spin_lock(&tomoyo_policy_memory_lock);
  50. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] += s;
  51. result = !tomoyo_memory_quota[TOMOYO_MEMORY_POLICY] ||
  52. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] <=
  53. tomoyo_memory_quota[TOMOYO_MEMORY_POLICY];
  54. if (!result)
  55. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= s;
  56. spin_unlock(&tomoyo_policy_memory_lock);
  57. if (result)
  58. return true;
  59. }
  60. tomoyo_warn_oom(__func__);
  61. return false;
  62. }
  63. /**
  64. * tomoyo_commit_ok - Check memory quota.
  65. *
  66. * @data: Data to copy from.
  67. * @size: Size in byte.
  68. *
  69. * Returns pointer to allocated memory on success, NULL otherwise.
  70. * @data is zero-cleared on success.
  71. */
  72. void *tomoyo_commit_ok(void *data, const unsigned int size)
  73. {
  74. void *ptr = kzalloc(size, GFP_NOFS);
  75. if (tomoyo_memory_ok(ptr)) {
  76. memmove(ptr, data, size);
  77. memset(data, 0, size);
  78. return ptr;
  79. }
  80. kfree(ptr);
  81. return NULL;
  82. }
  83. /**
  84. * tomoyo_memory_free - Free memory for elements.
  85. *
  86. * @ptr: Pointer to allocated memory.
  87. */
  88. void tomoyo_memory_free(void *ptr)
  89. {
  90. size_t s = ksize(ptr);
  91. spin_lock(&tomoyo_policy_memory_lock);
  92. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= s;
  93. spin_unlock(&tomoyo_policy_memory_lock);
  94. kfree(ptr);
  95. }
  96. /**
  97. * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
  98. *
  99. * @param: Pointer to "struct tomoyo_acl_param".
  100. * @idx: Index number.
  101. *
  102. * Returns pointer to "struct tomoyo_group" on success, NULL otherwise.
  103. */
  104. struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
  105. const u8 idx)
  106. {
  107. struct tomoyo_group e = { };
  108. struct tomoyo_group *group = NULL;
  109. struct list_head *list;
  110. const char *group_name = tomoyo_read_token(param);
  111. bool found = false;
  112. if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP)
  113. return NULL;
  114. e.group_name = tomoyo_get_name(group_name);
  115. if (!e.group_name)
  116. return NULL;
  117. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  118. goto out;
  119. list = &param->ns->group_list[idx];
  120. list_for_each_entry(group, list, head.list) {
  121. if (e.group_name != group->group_name)
  122. continue;
  123. atomic_inc(&group->head.users);
  124. found = true;
  125. break;
  126. }
  127. if (!found) {
  128. struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e));
  129. if (entry) {
  130. INIT_LIST_HEAD(&entry->member_list);
  131. atomic_set(&entry->head.users, 1);
  132. list_add_tail_rcu(&entry->head.list, list);
  133. group = entry;
  134. found = true;
  135. }
  136. }
  137. mutex_unlock(&tomoyo_policy_lock);
  138. out:
  139. tomoyo_put_name(e.group_name);
  140. return found ? group : NULL;
  141. }
  142. /*
  143. * tomoyo_name_list is used for holding string data used by TOMOYO.
  144. * Since same string data is likely used for multiple times (e.g.
  145. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  146. * "const struct tomoyo_path_info *".
  147. */
  148. struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  149. /**
  150. * tomoyo_get_name - Allocate permanent memory for string data.
  151. *
  152. * @name: The string to store into the permernent memory.
  153. *
  154. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  155. */
  156. const struct tomoyo_path_info *tomoyo_get_name(const char *name)
  157. {
  158. struct tomoyo_name *ptr;
  159. unsigned int hash;
  160. int len;
  161. struct list_head *head;
  162. if (!name)
  163. return NULL;
  164. len = strlen(name) + 1;
  165. hash = full_name_hash((const unsigned char *) name, len - 1);
  166. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  167. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  168. return NULL;
  169. list_for_each_entry(ptr, head, head.list) {
  170. if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
  171. continue;
  172. atomic_inc(&ptr->head.users);
  173. goto out;
  174. }
  175. ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS);
  176. if (tomoyo_memory_ok(ptr)) {
  177. ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
  178. memmove((char *) ptr->entry.name, name, len);
  179. atomic_set(&ptr->head.users, 1);
  180. tomoyo_fill_path_info(&ptr->entry);
  181. list_add_tail(&ptr->head.list, head);
  182. } else {
  183. kfree(ptr);
  184. ptr = NULL;
  185. }
  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. }