path_group.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 tomoyo_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_correct_word(group_name))
  24. return NULL;
  25. saved_group_name = tomoyo_get_name(group_name);
  26. if (!saved_group_name)
  27. return NULL;
  28. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  29. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  30. goto out;
  31. list_for_each_entry_rcu(group, &tomoyo_path_group_list, list) {
  32. if (saved_group_name != group->group_name)
  33. continue;
  34. atomic_inc(&group->users);
  35. error = 0;
  36. break;
  37. }
  38. if (error && tomoyo_memory_ok(entry)) {
  39. INIT_LIST_HEAD(&entry->member_list);
  40. entry->group_name = saved_group_name;
  41. saved_group_name = NULL;
  42. atomic_set(&entry->users, 1);
  43. list_add_tail_rcu(&entry->list, &tomoyo_path_group_list);
  44. group = entry;
  45. entry = NULL;
  46. error = 0;
  47. }
  48. mutex_unlock(&tomoyo_policy_lock);
  49. out:
  50. tomoyo_put_name(saved_group_name);
  51. kfree(entry);
  52. return !error ? group : NULL;
  53. }
  54. static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
  55. const struct tomoyo_acl_head *b)
  56. {
  57. return container_of(a, struct tomoyo_path_group_member, head)
  58. ->member_name ==
  59. container_of(b, struct tomoyo_path_group_member, head)
  60. ->member_name;
  61. }
  62. /**
  63. * tomoyo_write_path_group_policy - Write "struct tomoyo_path_group" list.
  64. *
  65. * @data: String to parse.
  66. * @is_delete: True if it is a delete request.
  67. *
  68. * Returns 0 on success, nagative value otherwise.
  69. */
  70. int tomoyo_write_path_group_policy(char *data, const bool is_delete)
  71. {
  72. struct tomoyo_path_group *group;
  73. struct tomoyo_path_group_member e = { };
  74. int error = is_delete ? -ENOENT : -ENOMEM;
  75. char *w[2];
  76. if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
  77. return -EINVAL;
  78. group = tomoyo_get_path_group(w[0]);
  79. if (!group)
  80. return -ENOMEM;
  81. e.member_name = tomoyo_get_name(w[1]);
  82. if (!e.member_name)
  83. goto out;
  84. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  85. &group->member_list,
  86. tomoyo_same_path_group);
  87. out:
  88. tomoyo_put_name(e.member_name);
  89. tomoyo_put_path_group(group);
  90. return error;
  91. }
  92. /**
  93. * tomoyo_read_path_group_policy - Read "struct tomoyo_path_group" list.
  94. *
  95. * @head: Pointer to "struct tomoyo_io_buffer".
  96. *
  97. * Returns true on success, false otherwise.
  98. *
  99. * Caller holds tomoyo_read_lock().
  100. */
  101. bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head)
  102. {
  103. struct list_head *gpos;
  104. struct list_head *mpos;
  105. list_for_each_cookie(gpos, head->read_var1, &tomoyo_path_group_list) {
  106. struct tomoyo_path_group *group;
  107. group = list_entry(gpos, struct tomoyo_path_group, list);
  108. list_for_each_cookie(mpos, head->read_var2,
  109. &group->member_list) {
  110. struct tomoyo_path_group_member *member;
  111. member = list_entry(mpos,
  112. struct tomoyo_path_group_member,
  113. head.list);
  114. if (member->head.is_deleted)
  115. continue;
  116. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_PATH_GROUP
  117. "%s %s\n",
  118. group->group_name->name,
  119. member->member_name->name))
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. /**
  126. * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
  127. *
  128. * @pathname: The name of pathname.
  129. * @group: Pointer to "struct tomoyo_path_group".
  130. *
  131. * Returns true if @pathname matches pathnames in @group, false otherwise.
  132. *
  133. * Caller holds tomoyo_read_lock().
  134. */
  135. bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  136. const struct tomoyo_path_group *group)
  137. {
  138. struct tomoyo_path_group_member *member;
  139. bool matched = false;
  140. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  141. if (member->head.is_deleted)
  142. continue;
  143. if (!tomoyo_path_matches_pattern(pathname,
  144. member->member_name))
  145. continue;
  146. matched = true;
  147. break;
  148. }
  149. return matched;
  150. }