memory.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * security/tomoyo/memory.c
  3. *
  4. * Copyright (C) 2005-2011 NTT DATA CORPORATION
  5. */
  6. #include <linux/hash.h>
  7. #include <linux/slab.h>
  8. #include "common.h"
  9. /**
  10. * tomoyo_warn_oom - Print out of memory warning message.
  11. *
  12. * @function: Function's name.
  13. */
  14. void tomoyo_warn_oom(const char *function)
  15. {
  16. /* Reduce error messages. */
  17. static pid_t tomoyo_last_pid;
  18. const pid_t pid = current->pid;
  19. if (tomoyo_last_pid != pid) {
  20. printk(KERN_WARNING "ERROR: Out of memory at %s.\n",
  21. function);
  22. tomoyo_last_pid = pid;
  23. }
  24. if (!tomoyo_policy_loaded)
  25. panic("MAC Initialization failed.\n");
  26. }
  27. /* Lock for protecting tomoyo_memory_used. */
  28. static DEFINE_SPINLOCK(tomoyo_policy_memory_lock);
  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. /**
  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. if (ptr) {
  45. const size_t s = ksize(ptr);
  46. bool result;
  47. spin_lock(&tomoyo_policy_memory_lock);
  48. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] += s;
  49. result = !tomoyo_memory_quota[TOMOYO_MEMORY_POLICY] ||
  50. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] <=
  51. tomoyo_memory_quota[TOMOYO_MEMORY_POLICY];
  52. if (!result)
  53. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= s;
  54. spin_unlock(&tomoyo_policy_memory_lock);
  55. if (result)
  56. return true;
  57. }
  58. tomoyo_warn_oom(__func__);
  59. return false;
  60. }
  61. /**
  62. * tomoyo_commit_ok - Check memory quota.
  63. *
  64. * @data: Data to copy from.
  65. * @size: Size in byte.
  66. *
  67. * Returns pointer to allocated memory on success, NULL otherwise.
  68. * @data is zero-cleared on success.
  69. */
  70. void *tomoyo_commit_ok(void *data, const unsigned int size)
  71. {
  72. void *ptr = kzalloc(size, GFP_NOFS);
  73. if (tomoyo_memory_ok(ptr)) {
  74. memmove(ptr, data, size);
  75. memset(data, 0, size);
  76. return ptr;
  77. }
  78. kfree(ptr);
  79. return NULL;
  80. }
  81. /**
  82. * tomoyo_memory_free - Free memory for elements.
  83. *
  84. * @ptr: Pointer to allocated memory.
  85. */
  86. void tomoyo_memory_free(void *ptr)
  87. {
  88. size_t s = ksize(ptr);
  89. spin_lock(&tomoyo_policy_memory_lock);
  90. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= s;
  91. spin_unlock(&tomoyo_policy_memory_lock);
  92. kfree(ptr);
  93. }
  94. /**
  95. * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
  96. *
  97. * @param: Pointer to "struct tomoyo_acl_param".
  98. * @idx: Index number.
  99. *
  100. * Returns pointer to "struct tomoyo_group" on success, NULL otherwise.
  101. */
  102. struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
  103. const u8 idx)
  104. {
  105. struct tomoyo_group e = { };
  106. struct tomoyo_group *group = NULL;
  107. struct list_head *list;
  108. const char *group_name = tomoyo_read_token(param);
  109. bool found = false;
  110. if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP)
  111. return NULL;
  112. e.group_name = tomoyo_get_name(group_name);
  113. if (!e.group_name)
  114. return NULL;
  115. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  116. goto out;
  117. list = &param->ns->group_list[idx];
  118. list_for_each_entry(group, list, head.list) {
  119. if (e.group_name != group->group_name)
  120. continue;
  121. atomic_inc(&group->head.users);
  122. found = true;
  123. break;
  124. }
  125. if (!found) {
  126. struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e));
  127. if (entry) {
  128. INIT_LIST_HEAD(&entry->member_list);
  129. atomic_set(&entry->head.users, 1);
  130. list_add_tail_rcu(&entry->head.list, list);
  131. group = entry;
  132. found = true;
  133. }
  134. }
  135. mutex_unlock(&tomoyo_policy_lock);
  136. out:
  137. tomoyo_put_name(e.group_name);
  138. return found ? group : NULL;
  139. }
  140. /*
  141. * tomoyo_name_list is used for holding string data used by TOMOYO.
  142. * Since same string data is likely used for multiple times (e.g.
  143. * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
  144. * "const struct tomoyo_path_info *".
  145. */
  146. struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  147. /**
  148. * tomoyo_get_name - Allocate permanent memory for string data.
  149. *
  150. * @name: The string to store into the permernent memory.
  151. *
  152. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  153. */
  154. const struct tomoyo_path_info *tomoyo_get_name(const char *name)
  155. {
  156. struct tomoyo_name *ptr;
  157. unsigned int hash;
  158. int len;
  159. struct list_head *head;
  160. if (!name)
  161. return NULL;
  162. len = strlen(name) + 1;
  163. hash = full_name_hash((const unsigned char *) name, len - 1);
  164. head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
  165. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  166. return NULL;
  167. list_for_each_entry(ptr, head, head.list) {
  168. if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name))
  169. continue;
  170. atomic_inc(&ptr->head.users);
  171. goto out;
  172. }
  173. ptr = kzalloc(sizeof(*ptr) + len, GFP_NOFS);
  174. if (tomoyo_memory_ok(ptr)) {
  175. ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
  176. memmove((char *) ptr->entry.name, name, len);
  177. atomic_set(&ptr->head.users, 1);
  178. tomoyo_fill_path_info(&ptr->entry);
  179. list_add_tail(&ptr->head.list, head);
  180. } else {
  181. kfree(ptr);
  182. ptr = NULL;
  183. }
  184. out:
  185. mutex_unlock(&tomoyo_policy_lock);
  186. return ptr ? &ptr->entry : NULL;
  187. }
  188. /* Initial namespace.*/
  189. struct tomoyo_policy_namespace tomoyo_kernel_namespace;
  190. /**
  191. * tomoyo_mm_init - Initialize mm related code.
  192. */
  193. void __init tomoyo_mm_init(void)
  194. {
  195. int idx;
  196. for (idx = 0; idx < TOMOYO_MAX_HASH; idx++)
  197. INIT_LIST_HEAD(&tomoyo_name_list[idx]);
  198. tomoyo_kernel_namespace.name = "<kernel>";
  199. tomoyo_init_policy_namespace(&tomoyo_kernel_namespace);
  200. tomoyo_kernel_domain.ns = &tomoyo_kernel_namespace;
  201. INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list);
  202. tomoyo_kernel_domain.domainname = tomoyo_get_name("<kernel>");
  203. list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list);
  204. }