group.c 5.6 KB

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