2 * security/tomoyo/number_group.c
4 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 #include <linux/slab.h>
10 /* The list for "struct tomoyo_number_group". */
11 LIST_HEAD(tomoyo_number_group_list);
14 * tomoyo_get_number_group - Allocate memory for "struct tomoyo_number_group".
16 * @group_name: The name of number group.
18 * Returns pointer to "struct tomoyo_number_group" on success,
21 struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name)
23 struct tomoyo_number_group *entry = NULL;
24 struct tomoyo_number_group *group = NULL;
25 const struct tomoyo_path_info *saved_group_name;
27 if (!tomoyo_is_correct_path(group_name, 0, 0, 0) ||
30 saved_group_name = tomoyo_get_name(group_name);
31 if (!saved_group_name)
33 entry = kzalloc(sizeof(*entry), GFP_NOFS);
34 if (mutex_lock_interruptible(&tomoyo_policy_lock))
36 list_for_each_entry_rcu(group, &tomoyo_number_group_list, list) {
37 if (saved_group_name != group->group_name)
39 atomic_inc(&group->users);
43 if (error && tomoyo_memory_ok(entry)) {
44 INIT_LIST_HEAD(&entry->member_list);
45 entry->group_name = saved_group_name;
46 saved_group_name = NULL;
47 atomic_set(&entry->users, 1);
48 list_add_tail_rcu(&entry->list, &tomoyo_number_group_list);
53 mutex_unlock(&tomoyo_policy_lock);
55 tomoyo_put_name(saved_group_name);
57 return !error ? group : NULL;
61 * tomoyo_write_number_group_policy - Write "struct tomoyo_number_group" list.
63 * @data: String to parse.
64 * @is_delete: True if it is a delete request.
66 * Returns 0 on success, nagative value otherwise.
68 int tomoyo_write_number_group_policy(char *data, const bool is_delete)
70 struct tomoyo_number_group *group;
71 struct tomoyo_number_group_member e = { };
72 struct tomoyo_number_group_member *member;
73 int error = is_delete ? -ENOENT : -ENOMEM;
75 if (!tomoyo_tokenize(data, w, sizeof(w)))
77 if (!tomoyo_parse_number_union(w[1], &e.number))
79 if (e.number.is_group || e.number.values[0] > e.number.values[1]) {
80 tomoyo_put_number_union(&e.number);
83 group = tomoyo_get_number_group(w[0]);
86 if (mutex_lock_interruptible(&tomoyo_policy_lock))
88 list_for_each_entry_rcu(member, &group->member_list, list) {
89 if (memcmp(&member->number, &e.number, sizeof(e.number)))
91 member->is_deleted = is_delete;
95 if (!is_delete && error) {
96 struct tomoyo_number_group_member *entry =
97 tomoyo_commit_ok(&e, sizeof(e));
99 list_add_tail_rcu(&entry->list, &group->member_list);
103 mutex_unlock(&tomoyo_policy_lock);
105 tomoyo_put_number_group(group);
110 * tomoyo_read_number_group_policy - Read "struct tomoyo_number_group" list.
112 * @head: Pointer to "struct tomoyo_io_buffer".
114 * Returns true on success, false otherwise.
116 * Caller holds tomoyo_read_lock().
118 bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head)
120 struct list_head *gpos;
121 struct list_head *mpos;
122 list_for_each_cookie(gpos, head->read_var1, &tomoyo_number_group_list) {
123 struct tomoyo_number_group *group;
125 group = list_entry(gpos, struct tomoyo_number_group, list);
126 name = group->group_name->name;
127 list_for_each_cookie(mpos, head->read_var2,
128 &group->member_list) {
130 const struct tomoyo_number_group_member *member
132 struct tomoyo_number_group_member,
134 if (member->is_deleted)
136 pos = head->read_avail;
137 if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_NUMBER_GROUP
139 !tomoyo_print_number_union(head, &member->number) ||
140 !tomoyo_io_printf(head, "\n")) {
141 head->read_avail = pos;
150 * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
154 * @group: Pointer to "struct tomoyo_number_group".
156 * Returns true if @min and @max partially overlaps @group, false otherwise.
158 * Caller holds tomoyo_read_lock().
160 bool tomoyo_number_matches_group(const unsigned long min,
161 const unsigned long max,
162 const struct tomoyo_number_group *group)
164 struct tomoyo_number_group_member *member;
165 bool matched = false;
166 list_for_each_entry_rcu(member, &group->member_list, list) {
167 if (member->is_deleted)
169 if (min > member->number.values[1] ||
170 max < member->number.values[0])