group.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. if (member->head.is_deleted)
  125. continue;
  126. if (!tomoyo_path_matches_pattern(pathname, member->member_name))
  127. continue;
  128. return member->member_name;
  129. }
  130. return NULL;
  131. }
  132. /**
  133. * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
  134. *
  135. * @min: Min number.
  136. * @max: Max number.
  137. * @group: Pointer to "struct tomoyo_number_group".
  138. *
  139. * Returns true if @min and @max partially overlaps @group, false otherwise.
  140. *
  141. * Caller holds tomoyo_read_lock().
  142. */
  143. bool tomoyo_number_matches_group(const unsigned long min,
  144. const unsigned long max,
  145. const struct tomoyo_group *group)
  146. {
  147. struct tomoyo_number_group *member;
  148. bool matched = false;
  149. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  150. if (member->head.is_deleted)
  151. continue;
  152. if (min > member->number.values[1] ||
  153. max < member->number.values[0])
  154. continue;
  155. matched = true;
  156. break;
  157. }
  158. return matched;
  159. }
  160. /**
  161. * tomoyo_address_matches_group - Check whether the given address matches members of the given address group.
  162. *
  163. * @is_ipv6: True if @address is an IPv6 address.
  164. * @address: An IPv4 or IPv6 address.
  165. * @group: Pointer to "struct tomoyo_address_group".
  166. *
  167. * Returns true if @address matches addresses in @group group, false otherwise.
  168. *
  169. * Caller holds tomoyo_read_lock().
  170. */
  171. bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
  172. const struct tomoyo_group *group)
  173. {
  174. struct tomoyo_address_group *member;
  175. bool matched = false;
  176. const u8 size = is_ipv6 ? 16 : 4;
  177. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  178. if (member->head.is_deleted)
  179. continue;
  180. if (member->address.is_ipv6 != is_ipv6)
  181. continue;
  182. if (memcmp(&member->address.ip[0], address, size) > 0 ||
  183. memcmp(address, &member->address.ip[1], size) > 0)
  184. continue;
  185. matched = true;
  186. break;
  187. }
  188. return matched;
  189. }