blob: 944ad77d8fbac2aa376cbd03aeb08001caf9fbfe [file] [log] [blame]
Tetsuo Handa7c2ea222010-06-17 16:55:58 +09001/*
2 * security/tomoyo/group.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handa7c2ea222010-06-17 16:55:58 +09005 */
6
7#include <linux/slab.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +01008#include <linux/rculist.h>
9
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090010#include "common.h"
11
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090012/**
13 * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry.
14 *
15 * @a: Pointer to "struct tomoyo_acl_head".
16 * @b: Pointer to "struct tomoyo_acl_head".
17 *
18 * Returns true if @a == @b, false otherwise.
19 */
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090020static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090021 const struct tomoyo_acl_head *b)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090022{
23 return container_of(a, struct tomoyo_path_group, head)->member_name ==
24 container_of(b, struct tomoyo_path_group, head)->member_name;
25}
26
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090027/**
28 * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry.
29 *
30 * @a: Pointer to "struct tomoyo_acl_head".
31 * @b: Pointer to "struct tomoyo_acl_head".
32 *
33 * Returns true if @a == @b, false otherwise.
34 */
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090035static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090036 const struct tomoyo_acl_head *b)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090037{
38 return !memcmp(&container_of(a, struct tomoyo_number_group, head)
39 ->number,
40 &container_of(b, struct tomoyo_number_group, head)
41 ->number,
42 sizeof(container_of(a, struct tomoyo_number_group, head)
43 ->number));
44}
45
46/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +090047 * tomoyo_same_address_group - Check for duplicated "struct tomoyo_address_group" entry.
48 *
49 * @a: Pointer to "struct tomoyo_acl_head".
50 * @b: Pointer to "struct tomoyo_acl_head".
51 *
52 * Returns true if @a == @b, false otherwise.
53 */
54static bool tomoyo_same_address_group(const struct tomoyo_acl_head *a,
55 const struct tomoyo_acl_head *b)
56{
57 const struct tomoyo_address_group *p1 = container_of(a, typeof(*p1),
58 head);
59 const struct tomoyo_address_group *p2 = container_of(b, typeof(*p2),
60 head);
61
62 return tomoyo_same_ipaddr_union(&p1->address, &p2->address);
63}
64
65/**
66 * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090067 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +090068 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090069 * @type: Type of this group.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090070 *
71 * Returns 0 on success, negative value otherwise.
72 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +090073int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090074{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090075 struct tomoyo_group *group = tomoyo_get_group(param, type);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090076 int error = -EINVAL;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090077 if (!group)
78 return -ENOMEM;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090079 param->list = &group->member_list;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090080 if (type == TOMOYO_PATH_GROUP) {
81 struct tomoyo_path_group e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +090082 e.member_name = tomoyo_get_name(tomoyo_read_token(param));
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090083 if (!e.member_name) {
84 error = -ENOMEM;
85 goto out;
86 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +090087 error = tomoyo_update_policy(&e.head, sizeof(e), param,
88 tomoyo_same_path_group);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090089 tomoyo_put_name(e.member_name);
90 } else if (type == TOMOYO_NUMBER_GROUP) {
91 struct tomoyo_number_group e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +090092 if (param->data[0] == '@' ||
93 !tomoyo_parse_number_union(param, &e.number))
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090094 goto out;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090095 error = tomoyo_update_policy(&e.head, sizeof(e), param,
96 tomoyo_same_number_group);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090097 /*
98 * tomoyo_put_number_union() is not needed because
Tetsuo Handaa238cf52011-06-26 23:17:10 +090099 * param->data[0] != '@'.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900100 */
Tetsuo Handa059d84d2011-09-10 15:23:54 +0900101 } else {
102 struct tomoyo_address_group e = { };
103
104 if (param->data[0] == '@' ||
105 !tomoyo_parse_ipaddr_union(param, &e.address))
106 goto out;
107 error = tomoyo_update_policy(&e.head, sizeof(e), param,
108 tomoyo_same_address_group);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900109 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900110out:
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900111 tomoyo_put_group(group);
112 return error;
113}
114
115/**
116 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
117 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900118 * @pathname: The name of pathname.
119 * @group: Pointer to "struct tomoyo_path_group".
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900120 *
Tetsuo Handa484ca792010-07-29 14:29:55 +0900121 * Returns matched member's pathname if @pathname matches pathnames in @group,
122 * NULL otherwise.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900123 *
124 * Caller holds tomoyo_read_lock().
125 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900126const struct tomoyo_path_info *
127tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
128 const struct tomoyo_group *group)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900129{
130 struct tomoyo_path_group *member;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900131 list_for_each_entry_rcu(member, &group->member_list, head.list) {
132 if (member->head.is_deleted)
133 continue;
134 if (!tomoyo_path_matches_pattern(pathname, member->member_name))
135 continue;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900136 return member->member_name;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900137 }
Tetsuo Handa484ca792010-07-29 14:29:55 +0900138 return NULL;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900139}
140
141/**
142 * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
143 *
144 * @min: Min number.
145 * @max: Max number.
146 * @group: Pointer to "struct tomoyo_number_group".
147 *
148 * Returns true if @min and @max partially overlaps @group, false otherwise.
149 *
150 * Caller holds tomoyo_read_lock().
151 */
152bool tomoyo_number_matches_group(const unsigned long min,
153 const unsigned long max,
154 const struct tomoyo_group *group)
155{
156 struct tomoyo_number_group *member;
157 bool matched = false;
158 list_for_each_entry_rcu(member, &group->member_list, head.list) {
159 if (member->head.is_deleted)
160 continue;
161 if (min > member->number.values[1] ||
162 max < member->number.values[0])
163 continue;
164 matched = true;
165 break;
166 }
167 return matched;
168}
Tetsuo Handa059d84d2011-09-10 15:23:54 +0900169
170/**
171 * tomoyo_address_matches_group - Check whether the given address matches members of the given address group.
172 *
173 * @is_ipv6: True if @address is an IPv6 address.
174 * @address: An IPv4 or IPv6 address.
175 * @group: Pointer to "struct tomoyo_address_group".
176 *
177 * Returns true if @address matches addresses in @group group, false otherwise.
178 *
179 * Caller holds tomoyo_read_lock().
180 */
181bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
182 const struct tomoyo_group *group)
183{
184 struct tomoyo_address_group *member;
185 bool matched = false;
186 const u8 size = is_ipv6 ? 16 : 4;
187
188 list_for_each_entry_rcu(member, &group->member_list, head.list) {
189 if (member->head.is_deleted)
190 continue;
191 if (member->address.is_ipv6 != is_ipv6)
192 continue;
193 if (memcmp(&member->address.ip[0], address, size) > 0 ||
194 memcmp(address, &member->address.ip[1], size) > 0)
195 continue;
196 matched = true;
197 break;
198 }
199 return matched;
200}