path_group.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  9. * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group".
  10. *
  11. * @group_name: The name of pathname group.
  12. *
  13. * Returns pointer to "struct tomoyo_path_group" on success, NULL otherwise.
  14. */
  15. struct tomoyo_group *tomoyo_get_path_group(const char *group_name)
  16. {
  17. struct tomoyo_group *entry = NULL;
  18. struct tomoyo_group *group = NULL;
  19. const struct tomoyo_path_info *saved_group_name;
  20. int error = -ENOMEM;
  21. if (!tomoyo_correct_word(group_name))
  22. return NULL;
  23. saved_group_name = tomoyo_get_name(group_name);
  24. if (!saved_group_name)
  25. return NULL;
  26. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  27. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  28. goto out;
  29. list_for_each_entry_rcu(group, &tomoyo_group_list[TOMOYO_PATH_GROUP],
  30. list) {
  31. if (saved_group_name != group->group_name)
  32. continue;
  33. atomic_inc(&group->users);
  34. error = 0;
  35. break;
  36. }
  37. if (error && tomoyo_memory_ok(entry)) {
  38. INIT_LIST_HEAD(&entry->member_list);
  39. entry->group_name = saved_group_name;
  40. saved_group_name = NULL;
  41. atomic_set(&entry->users, 1);
  42. list_add_tail_rcu(&entry->list,
  43. &tomoyo_group_list[TOMOYO_PATH_GROUP]);
  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, head)
  58. ->member_name ==
  59. container_of(b, struct tomoyo_path_group, 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_group *group;
  73. struct tomoyo_path_group 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_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,
  106. &tomoyo_group_list[TOMOYO_PATH_GROUP]) {
  107. struct tomoyo_group *group;
  108. group = list_entry(gpos, struct tomoyo_group, list);
  109. list_for_each_cookie(mpos, head->read_var2,
  110. &group->member_list) {
  111. struct tomoyo_path_group *member;
  112. member = list_entry(mpos,
  113. struct tomoyo_path_group,
  114. head.list);
  115. if (member->head.is_deleted)
  116. continue;
  117. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_PATH_GROUP
  118. "%s %s\n",
  119. group->group_name->name,
  120. member->member_name->name))
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. /**
  127. * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
  128. *
  129. * @pathname: The name of pathname.
  130. * @group: Pointer to "struct tomoyo_path_group".
  131. *
  132. * Returns true if @pathname matches pathnames in @group, false otherwise.
  133. *
  134. * Caller holds tomoyo_read_lock().
  135. */
  136. bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  137. const struct tomoyo_group *group)
  138. {
  139. struct tomoyo_path_group *member;
  140. bool matched = false;
  141. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  142. if (member->head.is_deleted)
  143. continue;
  144. if (!tomoyo_path_matches_pattern(pathname,
  145. member->member_name))
  146. continue;
  147. matched = true;
  148. break;
  149. }
  150. return matched;
  151. }