number_group.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * security/tomoyo/number_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_number_group". */
  9. LIST_HEAD(tomoyo_number_group_list);
  10. /**
  11. * tomoyo_get_group - Allocate memory for "struct tomoyo_number_group".
  12. *
  13. * @group_name: The name of number group.
  14. *
  15. * Returns pointer to "struct tomoyo_number_group" on success,
  16. * NULL otherwise.
  17. */
  18. struct tomoyo_group *tomoyo_get_number_group(const char *group_name)
  19. {
  20. struct tomoyo_group *entry = NULL;
  21. struct tomoyo_group *group = NULL;
  22. const struct tomoyo_path_info *saved_group_name;
  23. int error = -ENOMEM;
  24. if (!tomoyo_correct_word(group_name))
  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_number_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_number_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. static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
  56. const struct tomoyo_acl_head *b)
  57. {
  58. return !memcmp(&container_of(a, struct tomoyo_number_group,
  59. head)->number,
  60. &container_of(b, struct tomoyo_number_group,
  61. head)->number,
  62. sizeof(container_of(a,
  63. struct tomoyo_number_group,
  64. head)->number));
  65. }
  66. /**
  67. * tomoyo_write_number_group_policy - Write "struct tomoyo_number_group" list.
  68. *
  69. * @data: String to parse.
  70. * @is_delete: True if it is a delete request.
  71. *
  72. * Returns 0 on success, nagative value otherwise.
  73. */
  74. int tomoyo_write_number_group_policy(char *data, const bool is_delete)
  75. {
  76. struct tomoyo_group *group;
  77. struct tomoyo_number_group e = { };
  78. int error;
  79. char *w[2];
  80. if (!tomoyo_tokenize(data, w, sizeof(w)))
  81. return -EINVAL;
  82. if (w[1][0] == '@' || !tomoyo_parse_number_union(w[1], &e.number) ||
  83. e.number.values[0] > e.number.values[1])
  84. return -EINVAL;
  85. group = tomoyo_get_number_group(w[0]);
  86. if (!group)
  87. return -ENOMEM;
  88. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  89. &group->member_list,
  90. tomoyo_same_number_group);
  91. tomoyo_put_group(group);
  92. return error;
  93. }
  94. /**
  95. * tomoyo_read_number_group_policy - Read "struct tomoyo_number_group" list.
  96. *
  97. * @head: Pointer to "struct tomoyo_io_buffer".
  98. *
  99. * Returns true on success, false otherwise.
  100. *
  101. * Caller holds tomoyo_read_lock().
  102. */
  103. bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head)
  104. {
  105. struct list_head *gpos;
  106. struct list_head *mpos;
  107. list_for_each_cookie(gpos, head->read_var1, &tomoyo_number_group_list) {
  108. struct tomoyo_group *group;
  109. const char *name;
  110. group = list_entry(gpos, struct tomoyo_group, list);
  111. name = group->group_name->name;
  112. list_for_each_cookie(mpos, head->read_var2,
  113. &group->member_list) {
  114. int pos;
  115. const struct tomoyo_number_group *member
  116. = list_entry(mpos,
  117. struct tomoyo_number_group,
  118. head.list);
  119. if (member->head.is_deleted)
  120. continue;
  121. pos = head->read_avail;
  122. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_NUMBER_GROUP
  123. "%s", name) ||
  124. !tomoyo_print_number_union(head, &member->number) ||
  125. !tomoyo_io_printf(head, "\n")) {
  126. head->read_avail = pos;
  127. return false;
  128. }
  129. }
  130. }
  131. return true;
  132. }
  133. /**
  134. * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
  135. *
  136. * @min: Min number.
  137. * @max: Max number.
  138. * @group: Pointer to "struct tomoyo_number_group".
  139. *
  140. * Returns true if @min and @max partially overlaps @group, false otherwise.
  141. *
  142. * Caller holds tomoyo_read_lock().
  143. */
  144. bool tomoyo_number_matches_group(const unsigned long min,
  145. const unsigned long max,
  146. const struct tomoyo_group *group)
  147. {
  148. struct tomoyo_number_group *member;
  149. bool matched = false;
  150. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  151. if (member->head.is_deleted)
  152. continue;
  153. if (min > member->number.values[1] ||
  154. max < member->number.values[0])
  155. continue;
  156. matched = true;
  157. break;
  158. }
  159. return matched;
  160. }