path_group.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * security/tomoyo/path_group.c
  3. *
  4. * Copyright (C) 2005-2009 NTT DATA CORPORATION
  5. */
  6. #include <linux/slab.h>
  7. #include "common.h"
  8. /* The list for "struct ccs_path_group". */
  9. LIST_HEAD(tomoyo_path_group_list);
  10. /**
  11. * tomoyo_get_path_group - Allocate memory for "struct tomoyo_path_group".
  12. *
  13. * @group_name: The name of pathname group.
  14. *
  15. * Returns pointer to "struct tomoyo_path_group" on success, NULL otherwise.
  16. */
  17. struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name)
  18. {
  19. struct tomoyo_path_group *entry = NULL;
  20. struct tomoyo_path_group *group = NULL;
  21. const struct tomoyo_path_info *saved_group_name;
  22. int error = -ENOMEM;
  23. if (!tomoyo_is_correct_path(group_name, 0, 0, 0) ||
  24. !group_name[0])
  25. return NULL;
  26. saved_group_name = tomoyo_get_name(group_name);
  27. if (!saved_group_name)
  28. return NULL;
  29. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  30. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  31. goto out;
  32. list_for_each_entry_rcu(group, &tomoyo_path_group_list, list) {
  33. if (saved_group_name != group->group_name)
  34. continue;
  35. atomic_inc(&group->users);
  36. error = 0;
  37. break;
  38. }
  39. if (error && tomoyo_memory_ok(entry)) {
  40. INIT_LIST_HEAD(&entry->member_list);
  41. entry->group_name = saved_group_name;
  42. saved_group_name = NULL;
  43. atomic_set(&entry->users, 1);
  44. list_add_tail_rcu(&entry->list, &tomoyo_path_group_list);
  45. group = entry;
  46. entry = NULL;
  47. error = 0;
  48. }
  49. mutex_unlock(&tomoyo_policy_lock);
  50. out:
  51. tomoyo_put_name(saved_group_name);
  52. kfree(entry);
  53. return !error ? group : NULL;
  54. }
  55. /**
  56. * tomoyo_write_path_group_policy - Write "struct tomoyo_path_group" list.
  57. *
  58. * @data: String to parse.
  59. * @is_delete: True if it is a delete request.
  60. *
  61. * Returns 0 on success, nagative value otherwise.
  62. */
  63. int tomoyo_write_path_group_policy(char *data, const bool is_delete)
  64. {
  65. struct tomoyo_path_group *group;
  66. struct tomoyo_path_group_member *member;
  67. struct tomoyo_path_group_member e = { };
  68. int error = is_delete ? -ENOENT : -ENOMEM;
  69. char *w[2];
  70. if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
  71. return -EINVAL;
  72. group = tomoyo_get_path_group(w[0]);
  73. if (!group)
  74. return -ENOMEM;
  75. e.member_name = tomoyo_get_name(w[1]);
  76. if (!e.member_name)
  77. goto out;
  78. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  79. goto out;
  80. list_for_each_entry_rcu(member, &group->member_list, list) {
  81. if (member->member_name != e.member_name)
  82. continue;
  83. member->is_deleted = is_delete;
  84. error = 0;
  85. break;
  86. }
  87. if (!is_delete && error) {
  88. struct tomoyo_path_group_member *entry =
  89. tomoyo_commit_ok(&e, sizeof(e));
  90. if (entry) {
  91. list_add_tail_rcu(&entry->list, &group->member_list);
  92. error = 0;
  93. }
  94. }
  95. mutex_unlock(&tomoyo_policy_lock);
  96. out:
  97. tomoyo_put_name(e.member_name);
  98. tomoyo_put_path_group(group);
  99. return error;
  100. }
  101. /**
  102. * tomoyo_read_path_group_policy - Read "struct tomoyo_path_group" list.
  103. *
  104. * @head: Pointer to "struct tomoyo_io_buffer".
  105. *
  106. * Returns true on success, false otherwise.
  107. *
  108. * Caller holds tomoyo_read_lock().
  109. */
  110. bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head)
  111. {
  112. struct list_head *gpos;
  113. struct list_head *mpos;
  114. list_for_each_cookie(gpos, head->read_var1, &tomoyo_path_group_list) {
  115. struct tomoyo_path_group *group;
  116. group = list_entry(gpos, struct tomoyo_path_group, list);
  117. list_for_each_cookie(mpos, head->read_var2,
  118. &group->member_list) {
  119. struct tomoyo_path_group_member *member;
  120. member = list_entry(mpos,
  121. struct tomoyo_path_group_member,
  122. list);
  123. if (member->is_deleted)
  124. continue;
  125. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_PATH_GROUP
  126. "%s %s\n",
  127. group->group_name->name,
  128. member->member_name->name))
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
  136. *
  137. * @pathname: The name of pathname.
  138. * @group: Pointer to "struct tomoyo_path_group".
  139. * @may_use_pattern: True if wild card is permitted.
  140. *
  141. * Returns true if @pathname matches pathnames in @group, false otherwise.
  142. *
  143. * Caller holds tomoyo_read_lock().
  144. */
  145. bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  146. const struct tomoyo_path_group *group,
  147. const bool may_use_pattern)
  148. {
  149. struct tomoyo_path_group_member *member;
  150. bool matched = false;
  151. list_for_each_entry_rcu(member, &group->member_list, list) {
  152. if (member->is_deleted)
  153. continue;
  154. if (!member->member_name->is_patterned) {
  155. if (tomoyo_pathcmp(pathname, member->member_name))
  156. continue;
  157. } else if (may_use_pattern) {
  158. if (!tomoyo_path_matches_pattern(pathname,
  159. member->member_name))
  160. continue;
  161. } else
  162. continue;
  163. matched = true;
  164. break;
  165. }
  166. return matched;
  167. }