group.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * security/tomoyo/group.c
  3. *
  4. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  5. */
  6. #include <linux/slab.h>
  7. #include "common.h"
  8. static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
  9. const struct tomoyo_acl_head *b)
  10. {
  11. return container_of(a, struct tomoyo_path_group, head)->member_name ==
  12. container_of(b, struct tomoyo_path_group, head)->member_name;
  13. }
  14. static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
  15. const struct tomoyo_acl_head *b)
  16. {
  17. return !memcmp(&container_of(a, struct tomoyo_number_group, head)
  18. ->number,
  19. &container_of(b, struct tomoyo_number_group, head)
  20. ->number,
  21. sizeof(container_of(a, struct tomoyo_number_group, head)
  22. ->number));
  23. }
  24. /**
  25. * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
  26. *
  27. * @param: Pointer to "struct tomoyo_acl_param".
  28. * @type: Type of this group.
  29. *
  30. * Returns 0 on success, negative value otherwise.
  31. */
  32. int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
  33. {
  34. struct tomoyo_group *group = tomoyo_get_group(param, type);
  35. int error = -EINVAL;
  36. if (!group)
  37. return -ENOMEM;
  38. param->list = &group->member_list;
  39. if (type == TOMOYO_PATH_GROUP) {
  40. struct tomoyo_path_group e = { };
  41. e.member_name = tomoyo_get_name(tomoyo_read_token(param));
  42. if (!e.member_name) {
  43. error = -ENOMEM;
  44. goto out;
  45. }
  46. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  47. tomoyo_same_path_group);
  48. tomoyo_put_name(e.member_name);
  49. } else if (type == TOMOYO_NUMBER_GROUP) {
  50. struct tomoyo_number_group e = { };
  51. if (param->data[0] == '@' ||
  52. !tomoyo_parse_number_union(param, &e.number))
  53. goto out;
  54. error = tomoyo_update_policy(&e.head, sizeof(e), param,
  55. tomoyo_same_number_group);
  56. /*
  57. * tomoyo_put_number_union() is not needed because
  58. * param->data[0] != '@'.
  59. */
  60. }
  61. out:
  62. tomoyo_put_group(group);
  63. return error;
  64. }
  65. /**
  66. * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
  67. *
  68. * @pathname: The name of pathname.
  69. * @group: Pointer to "struct tomoyo_path_group".
  70. *
  71. * Returns matched member's pathname if @pathname matches pathnames in @group,
  72. * NULL otherwise.
  73. *
  74. * Caller holds tomoyo_read_lock().
  75. */
  76. const struct tomoyo_path_info *
  77. tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  78. const struct tomoyo_group *group)
  79. {
  80. struct tomoyo_path_group *member;
  81. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  82. if (member->head.is_deleted)
  83. continue;
  84. if (!tomoyo_path_matches_pattern(pathname, member->member_name))
  85. continue;
  86. return member->member_name;
  87. }
  88. return NULL;
  89. }
  90. /**
  91. * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
  92. *
  93. * @min: Min number.
  94. * @max: Max number.
  95. * @group: Pointer to "struct tomoyo_number_group".
  96. *
  97. * Returns true if @min and @max partially overlaps @group, false otherwise.
  98. *
  99. * Caller holds tomoyo_read_lock().
  100. */
  101. bool tomoyo_number_matches_group(const unsigned long min,
  102. const unsigned long max,
  103. const struct tomoyo_group *group)
  104. {
  105. struct tomoyo_number_group *member;
  106. bool matched = false;
  107. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  108. if (member->head.is_deleted)
  109. continue;
  110. if (min > member->number.values[1] ||
  111. max < member->number.values[0])
  112. continue;
  113. matched = true;
  114. break;
  115. }
  116. return matched;
  117. }