2 * security/tomoyo/path_group.c
4 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 #include <linux/slab.h>
9 /* The list for "struct tomoyo_path_group". */
10 LIST_HEAD(tomoyo_path_group_list);
13 * tomoyo_get_path_group - Allocate memory for "struct tomoyo_path_group".
15 * @group_name: The name of pathname group.
17 * Returns pointer to "struct tomoyo_path_group" on success, NULL otherwise.
19 struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name)
21 struct tomoyo_path_group *entry = NULL;
22 struct tomoyo_path_group *group = NULL;
23 const struct tomoyo_path_info *saved_group_name;
25 if (!tomoyo_is_correct_word(group_name))
27 saved_group_name = tomoyo_get_name(group_name);
28 if (!saved_group_name)
30 entry = kzalloc(sizeof(*entry), GFP_NOFS);
31 if (mutex_lock_interruptible(&tomoyo_policy_lock))
33 list_for_each_entry_rcu(group, &tomoyo_path_group_list, list) {
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, &tomoyo_path_group_list);
50 mutex_unlock(&tomoyo_policy_lock);
52 tomoyo_put_name(saved_group_name);
54 return !error ? group : NULL;
58 * tomoyo_write_path_group_policy - Write "struct tomoyo_path_group" list.
60 * @data: String to parse.
61 * @is_delete: True if it is a delete request.
63 * Returns 0 on success, nagative value otherwise.
65 int tomoyo_write_path_group_policy(char *data, const bool is_delete)
67 struct tomoyo_path_group *group;
68 struct tomoyo_path_group_member *member;
69 struct tomoyo_path_group_member e = { };
70 int error = is_delete ? -ENOENT : -ENOMEM;
72 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
74 group = tomoyo_get_path_group(w[0]);
77 e.member_name = tomoyo_get_name(w[1]);
80 if (mutex_lock_interruptible(&tomoyo_policy_lock))
82 list_for_each_entry_rcu(member, &group->member_list, list) {
83 if (member->member_name != e.member_name)
85 member->is_deleted = is_delete;
89 if (!is_delete && error) {
90 struct tomoyo_path_group_member *entry =
91 tomoyo_commit_ok(&e, sizeof(e));
93 list_add_tail_rcu(&entry->list, &group->member_list);
97 mutex_unlock(&tomoyo_policy_lock);
99 tomoyo_put_name(e.member_name);
100 tomoyo_put_path_group(group);
105 * tomoyo_read_path_group_policy - Read "struct tomoyo_path_group" list.
107 * @head: Pointer to "struct tomoyo_io_buffer".
109 * Returns true on success, false otherwise.
111 * Caller holds tomoyo_read_lock().
113 bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head)
115 struct list_head *gpos;
116 struct list_head *mpos;
117 list_for_each_cookie(gpos, head->read_var1, &tomoyo_path_group_list) {
118 struct tomoyo_path_group *group;
119 group = list_entry(gpos, struct tomoyo_path_group, list);
120 list_for_each_cookie(mpos, head->read_var2,
121 &group->member_list) {
122 struct tomoyo_path_group_member *member;
123 member = list_entry(mpos,
124 struct tomoyo_path_group_member,
126 if (member->is_deleted)
128 if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_PATH_GROUP
130 group->group_name->name,
131 member->member_name->name))
139 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
141 * @pathname: The name of pathname.
142 * @group: Pointer to "struct tomoyo_path_group".
144 * Returns true if @pathname matches pathnames in @group, false otherwise.
146 * Caller holds tomoyo_read_lock().
148 bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
149 const struct tomoyo_path_group *group)
151 struct tomoyo_path_group_member *member;
152 bool matched = false;
153 list_for_each_entry_rcu(member, &group->member_list, list) {
154 if (member->is_deleted)
156 if (!tomoyo_path_matches_pattern(pathname,
157 member->member_name))