2 * security/tomoyo/path_group.c
4 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 #include <linux/slab.h>
11 * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group".
13 * @group_name: The name of pathname group.
15 * Returns pointer to "struct tomoyo_path_group" on success, NULL otherwise.
17 struct tomoyo_group *tomoyo_get_path_group(const char *group_name)
19 struct tomoyo_group *entry = NULL;
20 struct tomoyo_group *group = NULL;
21 const struct tomoyo_path_info *saved_group_name;
23 if (!tomoyo_correct_word(group_name))
25 saved_group_name = tomoyo_get_name(group_name);
26 if (!saved_group_name)
28 entry = kzalloc(sizeof(*entry), GFP_NOFS);
29 if (mutex_lock_interruptible(&tomoyo_policy_lock))
31 list_for_each_entry_rcu(group, &tomoyo_group_list[TOMOYO_PATH_GROUP],
33 if (saved_group_name != group->group_name)
35 atomic_inc(&group->users);
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,
45 &tomoyo_group_list[TOMOYO_PATH_GROUP]);
50 mutex_unlock(&tomoyo_policy_lock);
52 tomoyo_put_name(saved_group_name);
54 return !error ? group : NULL;
57 static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
58 const struct tomoyo_acl_head *b)
60 return container_of(a, struct tomoyo_path_group, head)
62 container_of(b, struct tomoyo_path_group, head)
67 * tomoyo_write_path_group_policy - Write "struct tomoyo_path_group" list.
69 * @data: String to parse.
70 * @is_delete: True if it is a delete request.
72 * Returns 0 on success, nagative value otherwise.
74 int tomoyo_write_path_group_policy(char *data, const bool is_delete)
76 struct tomoyo_group *group;
77 struct tomoyo_path_group e = { };
78 int error = is_delete ? -ENOENT : -ENOMEM;
80 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
82 group = tomoyo_get_path_group(w[0]);
85 e.member_name = tomoyo_get_name(w[1]);
88 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
90 tomoyo_same_path_group);
92 tomoyo_put_name(e.member_name);
93 tomoyo_put_group(group);
98 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
100 * @pathname: The name of pathname.
101 * @group: Pointer to "struct tomoyo_path_group".
103 * Returns true if @pathname matches pathnames in @group, false otherwise.
105 * Caller holds tomoyo_read_lock().
107 bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
108 const struct tomoyo_group *group)
110 struct tomoyo_path_group *member;
111 bool matched = false;
112 list_for_each_entry_rcu(member, &group->member_list, head.list) {
113 if (member->head.is_deleted)
115 if (!tomoyo_path_matches_pattern(pathname,
116 member->member_name))