2 * security/tomoyo/number_group.c
4 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 #include <linux/slab.h>
11 * tomoyo_get_group - Allocate memory for "struct tomoyo_number_group".
13 * @group_name: The name of number group.
15 * Returns pointer to "struct tomoyo_number_group" on success,
18 struct tomoyo_group *tomoyo_get_number_group(const char *group_name)
20 struct tomoyo_group *entry = NULL;
21 struct tomoyo_group *group = NULL;
22 const struct tomoyo_path_info *saved_group_name;
24 if (!tomoyo_correct_word(group_name))
26 saved_group_name = tomoyo_get_name(group_name);
27 if (!saved_group_name)
29 entry = kzalloc(sizeof(*entry), GFP_NOFS);
30 if (mutex_lock_interruptible(&tomoyo_policy_lock))
32 list_for_each_entry_rcu(group, &tomoyo_group_list[TOMOYO_NUMBER_GROUP],
34 if (saved_group_name != group->group_name)
36 atomic_inc(&group->users);
40 if (error && tomoyo_memory_ok(entry)) {
41 INIT_LIST_HEAD(&entry->member_list);
42 entry->group_name = saved_group_name;
43 saved_group_name = NULL;
44 atomic_set(&entry->users, 1);
45 list_add_tail_rcu(&entry->list,
46 &tomoyo_group_list[TOMOYO_NUMBER_GROUP]);
51 mutex_unlock(&tomoyo_policy_lock);
53 tomoyo_put_name(saved_group_name);
55 return !error ? group : NULL;
58 static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
59 const struct tomoyo_acl_head *b)
61 return !memcmp(&container_of(a, struct tomoyo_number_group,
63 &container_of(b, struct tomoyo_number_group,
65 sizeof(container_of(a,
66 struct tomoyo_number_group,
71 * tomoyo_write_number_group_policy - Write "struct tomoyo_number_group" list.
73 * @data: String to parse.
74 * @is_delete: True if it is a delete request.
76 * Returns 0 on success, nagative value otherwise.
78 int tomoyo_write_number_group_policy(char *data, const bool is_delete)
80 struct tomoyo_group *group;
81 struct tomoyo_number_group e = { };
84 if (!tomoyo_tokenize(data, w, sizeof(w)))
86 if (w[1][0] == '@' || !tomoyo_parse_number_union(w[1], &e.number) ||
87 e.number.values[0] > e.number.values[1])
89 group = tomoyo_get_number_group(w[0]);
92 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
94 tomoyo_same_number_group);
95 tomoyo_put_group(group);
100 * tomoyo_read_number_group_policy - Read "struct tomoyo_number_group" list.
102 * @head: Pointer to "struct tomoyo_io_buffer".
104 * Returns true on success, false otherwise.
106 * Caller holds tomoyo_read_lock().
108 bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head)
110 struct list_head *gpos;
111 struct list_head *mpos;
112 list_for_each_cookie(gpos, head->read_var1,
113 &tomoyo_group_list[TOMOYO_NUMBER_GROUP]) {
114 struct tomoyo_group *group;
116 group = list_entry(gpos, struct tomoyo_group, list);
117 name = group->group_name->name;
118 list_for_each_cookie(mpos, head->read_var2,
119 &group->member_list) {
121 const struct tomoyo_number_group *member
123 struct tomoyo_number_group,
125 if (member->head.is_deleted)
127 pos = head->read_avail;
128 if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_NUMBER_GROUP
130 !tomoyo_print_number_union(head, &member->number) ||
131 !tomoyo_io_printf(head, "\n")) {
132 head->read_avail = pos;
141 * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
145 * @group: Pointer to "struct tomoyo_number_group".
147 * Returns true if @min and @max partially overlaps @group, false otherwise.
149 * Caller holds tomoyo_read_lock().
151 bool tomoyo_number_matches_group(const unsigned long min,
152 const unsigned long max,
153 const struct tomoyo_group *group)
155 struct tomoyo_number_group *member;
156 bool matched = false;
157 list_for_each_entry_rcu(member, &group->member_list, head.list) {
158 if (member->head.is_deleted)
160 if (min > member->number.values[1] ||
161 max < member->number.values[0])