group.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * security/tomoyo/group.c
  4. *
  5. * Copyright (C) 2005-2011 NTT DATA CORPORATION
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/rculist.h>
  9. #include "common.h"
  10. /**
  11. * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry.
  12. *
  13. * @a: Pointer to "struct tomoyo_acl_head".
  14. * @b: Pointer to "struct tomoyo_acl_head".
  15. *
  16. * Returns true if @a == @b, false otherwise.
  17. */
  18. static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
  19. const struct tomoyo_acl_head *b)
  20. {
  21. return container_of(a, struct tomoyo_path_group, head)->member_name ==
  22. container_of(b, struct tomoyo_path_group, head)->member_name;
  23. }
  24. /**
  25. * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry.
  26. *
  27. * @a: Pointer to "struct tomoyo_acl_head".
  28. * @b: Pointer to "struct tomoyo_acl_head".
  29. *
  30. * Returns true if @a == @b, false otherwise.
  31. */
  32. static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
  33. const struct tomoyo_acl_head *b)
  34. {
  35. return !memcmp(&container_of(a, struct tomoyo_number_group, head)
  36. ->number,
  37. &container_of(b, struct tomoyo_number_group, head)
  38. ->number,
  39. sizeof(container_of(a, struct tomoyo_number_group, head)
  40. ->number));
  41. }
  42. /**
  43. * tomoyo_same_address_group - Check for duplicated "struct tomoyo_address_group" entry.
  44. *
  45. * @a: Pointer to "struct tomoyo_acl_head".
  46. * @b: Pointer to "struct tomoyo_acl_head".
  47. *
  48. * Returns true if @a == @b, false otherwise.
  49. */
  50. static bool tomoyo_same_address_group(const struct tomoyo_acl_head *a,
  51. const struct tomoyo_acl_head *b)
  52. {
  53. const struct tomoyo_address_group *p1 = container_of(a, typeof(*p1),
  54. head);
  55. const struct tomoyo_address_group *p2 = container_of(b, typeof(*p2),
  56. head);
  57. return tomoyo_same_ipaddr_union(&p1->address, &p2->address);
  58. }
  59. /**
  60. * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
  61. *
  62. * @param: Pointer to "struct tomoyo_acl_param".
  63. * @type: Type of this group.
  64. *
  65. * Returns 0 on success, negative value otherwise.
  66. */
  67. int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
  68. {
  69. struct tomoyo_group *group = tomoyo_get_group(param, type);
  70. int error = -EINVAL;
  71. if (!group)
  72. return -ENOMEM;
  73. param->list = &group->member_list;
  74. if (type == TOMOYO_PATH_GROUP) {
  75. struct tomoyo_path_group e = { };
  76. e.member_name = tomoyo_get_name(tomoyo_read_token(param));
  77. if (!e.member_name) {
  78. error = -ENOMEM;
  79. goto out;
  80. }
  81. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  82. tomoyo_same_path_group);
  83. tomoyo_put_name(e.member_name);
  84. } else if (type == TOMOYO_NUMBER_GROUP) {
  85. struct tomoyo_number_group e = { };
  86. if (param->data[0] == '@' ||
  87. !tomoyo_parse_number_union(param, &e.number))
  88. goto out;
  89. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  90. tomoyo_same_number_group);
  91. /*
  92. * tomoyo_put_number_union() is not needed because
  93. * param->data[0] != '@'.
  94. */
  95. } else {
  96. struct tomoyo_address_group e = { };
  97. if (param->data[0] == '@' ||
  98. !tomoyo_parse_ipaddr_union(param, &e.address))
  99. goto out;
  100. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  101. tomoyo_same_address_group);
  102. }
  103. out:
  104. tomoyo_put_group(group);
  105. return error;
  106. }
  107. /**
  108. * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
  109. *
  110. * @pathname: The name of pathname.
  111. * @group: Pointer to "struct tomoyo_path_group".
  112. *
  113. * Returns matched member's pathname if @pathname matches pathnames in @group,
  114. * NULL otherwise.
  115. *
  116. * Caller holds tomoyo_read_lock().
  117. */
  118. const struct tomoyo_path_info *
  119. tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  120. const struct tomoyo_group *group)
  121. {
  122. struct tomoyo_path_group *member;
  123. list_for_each_entry_rcu(member, &group->member_list, head.list,
  124. srcu_read_lock_held(&tomoyo_ss)) {
  125. if (member->head.is_deleted)
  126. continue;
  127. if (!tomoyo_path_matches_pattern(pathname, member->member_name))
  128. continue;
  129. return member->member_name;
  130. }
  131. return NULL;
  132. }
  133. /**
  134. * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
  135. *
  136. * @min: Min number.
  137. * @max: Max number.
  138. * @group: Pointer to "struct tomoyo_number_group".
  139. *
  140. * Returns true if @min and @max partially overlaps @group, false otherwise.
  141. *
  142. * Caller holds tomoyo_read_lock().
  143. */
  144. bool tomoyo_number_matches_group(const unsigned long min,
  145. const unsigned long max,
  146. const struct tomoyo_group *group)
  147. {
  148. struct tomoyo_number_group *member;
  149. bool matched = false;
  150. list_for_each_entry_rcu(member, &group->member_list, head.list,
  151. srcu_read_lock_held(&tomoyo_ss)) {
  152. if (member->head.is_deleted)
  153. continue;
  154. if (min > member->number.values[1] ||
  155. max < member->number.values[0])
  156. continue;
  157. matched = true;
  158. break;
  159. }
  160. return matched;
  161. }
  162. /**
  163. * tomoyo_address_matches_group - Check whether the given address matches members of the given address group.
  164. *
  165. * @is_ipv6: True if @address is an IPv6 address.
  166. * @address: An IPv4 or IPv6 address.
  167. * @group: Pointer to "struct tomoyo_address_group".
  168. *
  169. * Returns true if @address matches addresses in @group group, false otherwise.
  170. *
  171. * Caller holds tomoyo_read_lock().
  172. */
  173. bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
  174. const struct tomoyo_group *group)
  175. {
  176. struct tomoyo_address_group *member;
  177. bool matched = false;
  178. const u8 size = is_ipv6 ? 16 : 4;
  179. list_for_each_entry_rcu(member, &group->member_list, head.list,
  180. srcu_read_lock_held(&tomoyo_ss)) {
  181. if (member->head.is_deleted)
  182. continue;
  183. if (member->address.is_ipv6 != is_ipv6)
  184. continue;
  185. if (memcmp(&member->address.ip[0], address, size) > 0 ||
  186. memcmp(address, &member->address.ip[1], size) > 0)
  187. continue;
  188. matched = true;
  189. break;
  190. }
  191. return matched;
  192. }