]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/path_group.c
TOMOYO: Aggregate reader functions.
[net-next-2.6.git] / security / tomoyo / path_group.c
CommitLineData
7762fbff
TH
1/*
2 * security/tomoyo/path_group.c
3 *
4 * Copyright (C) 2005-2009 NTT DATA CORPORATION
5 */
6
7#include <linux/slab.h>
8#include "common.h"
7762fbff
TH
9
10/**
a98aa4de 11 * tomoyo_get_group - Allocate memory for "struct tomoyo_path_group".
7762fbff
TH
12 *
13 * @group_name: The name of pathname group.
14 *
15 * Returns pointer to "struct tomoyo_path_group" on success, NULL otherwise.
16 */
a98aa4de 17struct tomoyo_group *tomoyo_get_path_group(const char *group_name)
7762fbff 18{
a98aa4de
TH
19 struct tomoyo_group *entry = NULL;
20 struct tomoyo_group *group = NULL;
7762fbff
TH
21 const struct tomoyo_path_info *saved_group_name;
22 int error = -ENOMEM;
75093152 23 if (!tomoyo_correct_word(group_name))
7762fbff
TH
24 return NULL;
25 saved_group_name = tomoyo_get_name(group_name);
26 if (!saved_group_name)
27 return NULL;
28 entry = kzalloc(sizeof(*entry), GFP_NOFS);
29 if (mutex_lock_interruptible(&tomoyo_policy_lock))
30 goto out;
a230f9e7
TH
31 list_for_each_entry_rcu(group, &tomoyo_group_list[TOMOYO_PATH_GROUP],
32 list) {
7762fbff
TH
33 if (saved_group_name != group->group_name)
34 continue;
35 atomic_inc(&group->users);
36 error = 0;
37 break;
38 }
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);
a230f9e7
TH
44 list_add_tail_rcu(&entry->list,
45 &tomoyo_group_list[TOMOYO_PATH_GROUP]);
7762fbff
TH
46 group = entry;
47 entry = NULL;
48 error = 0;
49 }
50 mutex_unlock(&tomoyo_policy_lock);
51 out:
52 tomoyo_put_name(saved_group_name);
53 kfree(entry);
54 return !error ? group : NULL;
55}
56
36f5e1ff
TH
57static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
58 const struct tomoyo_acl_head *b)
59{
a98aa4de 60 return container_of(a, struct tomoyo_path_group, head)
36f5e1ff 61 ->member_name ==
a98aa4de 62 container_of(b, struct tomoyo_path_group, head)
36f5e1ff
TH
63 ->member_name;
64}
65
7762fbff
TH
66/**
67 * tomoyo_write_path_group_policy - Write "struct tomoyo_path_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 */
74int tomoyo_write_path_group_policy(char *data, const bool is_delete)
75{
a98aa4de
TH
76 struct tomoyo_group *group;
77 struct tomoyo_path_group e = { };
7762fbff
TH
78 int error = is_delete ? -ENOENT : -ENOMEM;
79 char *w[2];
80 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
81 return -EINVAL;
82 group = tomoyo_get_path_group(w[0]);
83 if (!group)
84 return -ENOMEM;
85 e.member_name = tomoyo_get_name(w[1]);
86 if (!e.member_name)
87 goto out;
36f5e1ff
TH
88 error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
89 &group->member_list,
90 tomoyo_same_path_group);
7762fbff
TH
91 out:
92 tomoyo_put_name(e.member_name);
a98aa4de 93 tomoyo_put_group(group);
7762fbff
TH
94 return error;
95}
96
7762fbff
TH
97/**
98 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
99 *
100 * @pathname: The name of pathname.
101 * @group: Pointer to "struct tomoyo_path_group".
7762fbff
TH
102 *
103 * Returns true if @pathname matches pathnames in @group, false otherwise.
104 *
105 * Caller holds tomoyo_read_lock().
106 */
107bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
a98aa4de 108 const struct tomoyo_group *group)
7762fbff 109{
a98aa4de 110 struct tomoyo_path_group *member;
7762fbff 111 bool matched = false;
82e0f001
TH
112 list_for_each_entry_rcu(member, &group->member_list, head.list) {
113 if (member->head.is_deleted)
7762fbff 114 continue;
3f629636
TH
115 if (!tomoyo_path_matches_pattern(pathname,
116 member->member_name))
7762fbff
TH
117 continue;
118 matched = true;
119 break;
120 }
121 return matched;
122}