number_group.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /**
  9. * tomoyo_get_group - Allocate memory for "struct tomoyo_number_group".
  10. *
  11. * @group_name: The name of number group.
  12. *
  13. * Returns pointer to "struct tomoyo_number_group" on success,
  14. * NULL otherwise.
  15. */
  16. struct tomoyo_group *tomoyo_get_number_group(const char *group_name)
  17. {
  18. struct tomoyo_group *entry = NULL;
  19. struct tomoyo_group *group = NULL;
  20. const struct tomoyo_path_info *saved_group_name;
  21. int error = -ENOMEM;
  22. if (!tomoyo_correct_word(group_name))
  23. return NULL;
  24. saved_group_name = tomoyo_get_name(group_name);
  25. if (!saved_group_name)
  26. return NULL;
  27. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  28. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  29. goto out;
  30. list_for_each_entry_rcu(group, &tomoyo_group_list[TOMOYO_NUMBER_GROUP],
  31. 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,
  44. &tomoyo_group_list[TOMOYO_NUMBER_GROUP]);
  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_number_matches_group - Check whether the given number matches members of the given number group.
  96. *
  97. * @min: Min number.
  98. * @max: Max number.
  99. * @group: Pointer to "struct tomoyo_number_group".
  100. *
  101. * Returns true if @min and @max partially overlaps @group, false otherwise.
  102. *
  103. * Caller holds tomoyo_read_lock().
  104. */
  105. bool tomoyo_number_matches_group(const unsigned long min,
  106. const unsigned long max,
  107. const struct tomoyo_group *group)
  108. {
  109. struct tomoyo_number_group *member;
  110. bool matched = false;
  111. list_for_each_entry_rcu(member, &group->member_list, head.list) {
  112. if (member->head.is_deleted)
  113. continue;
  114. if (min > member->number.values[1] ||
  115. max < member->number.values[0])
  116. continue;
  117. matched = true;
  118. break;
  119. }
  120. return matched;
  121. }