group.c 3.6 KB

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