group.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
  42. *
  43. * @param: Pointer to "struct tomoyo_acl_param".
  44. * @type: Type of this group.
  45. *
  46. * Returns 0 on success, negative value otherwise.
  47. */
  48. int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
  49. {
  50. struct tomoyo_group *group = tomoyo_get_group(param, type);
  51. int error = -EINVAL;
  52. if (!group)
  53. return -ENOMEM;
  54. param->list = &group->member_list;
  55. if (type == TOMOYO_PATH_GROUP) {
  56. struct tomoyo_path_group e = { };
  57. e.member_name = tomoyo_get_name(tomoyo_read_token(param));
  58. if (!e.member_name) {
  59. error = -ENOMEM;
  60. goto out;
  61. }
  62. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  63. tomoyo_same_path_group);
  64. tomoyo_put_name(e.member_name);
  65. } else if (type == TOMOYO_NUMBER_GROUP) {
  66. struct tomoyo_number_group e = { };
  67. if (param->data[0] == '@' ||
  68. !tomoyo_parse_number_union(param, &e.number))
  69. goto out;
  70. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  71. tomoyo_same_number_group);
  72. /*
  73. * tomoyo_put_number_union() is not needed because
  74. * param->data[0] != '@'.
  75. */
  76. }
  77. out:
  78. tomoyo_put_group(group);
  79. return error;
  80. }
  81. /**
  82. * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
  83. *
  84. * @pathname: The name of pathname.
  85. * @group: Pointer to "struct tomoyo_path_group".
  86. *
  87. * Returns matched member's pathname if @pathname matches pathnames in @group,
  88. * NULL otherwise.
  89. *
  90. * Caller holds tomoyo_read_lock().
  91. */
  92. const struct tomoyo_path_info *
  93. tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  94. const struct tomoyo_group *group)
  95. {
  96. struct tomoyo_path_group *member;
  97. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  98. if (member->head.is_deleted)
  99. continue;
  100. if (!tomoyo_path_matches_pattern(pathname, member->member_name))
  101. continue;
  102. return member->member_name;
  103. }
  104. return NULL;
  105. }
  106. /**
  107. * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
  108. *
  109. * @min: Min number.
  110. * @max: Max number.
  111. * @group: Pointer to "struct tomoyo_number_group".
  112. *
  113. * Returns true if @min and @max partially overlaps @group, false otherwise.
  114. *
  115. * Caller holds tomoyo_read_lock().
  116. */
  117. bool tomoyo_number_matches_group(const unsigned long min,
  118. const unsigned long max,
  119. const struct tomoyo_group *group)
  120. {
  121. struct tomoyo_number_group *member;
  122. bool matched = false;
  123. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  124. if (member->head.is_deleted)
  125. continue;
  126. if (min > member->number.values[1] ||
  127. max < member->number.values[0])
  128. continue;
  129. matched = true;
  130. break;
  131. }
  132. return matched;
  133. }