blob: 21b0cc3a7e1a34ddad1d224826c0edac47ba357d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tetsuo Handa7c2ea222010-06-17 16:55:58 +09002/*
3 * security/tomoyo/group.c
4 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handa7c2ea222010-06-17 16:55:58 +09006 */
7
8#include <linux/slab.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +01009#include <linux/rculist.h>
10
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090011#include "common.h"
12
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090013/**
14 * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry.
15 *
16 * @a: Pointer to "struct tomoyo_acl_head".
17 * @b: Pointer to "struct tomoyo_acl_head".
18 *
19 * Returns true if @a == @b, false otherwise.
20 */
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090021static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090022 const struct tomoyo_acl_head *b)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090023{
24 return container_of(a, struct tomoyo_path_group, head)->member_name ==
25 container_of(b, struct tomoyo_path_group, head)->member_name;
26}
27
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090028/**
29 * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry.
30 *
31 * @a: Pointer to "struct tomoyo_acl_head".
32 * @b: Pointer to "struct tomoyo_acl_head".
33 *
34 * Returns true if @a == @b, false otherwise.
35 */
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090036static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090037 const struct tomoyo_acl_head *b)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090038{
39 return !memcmp(&container_of(a, struct tomoyo_number_group, head)
40 ->number,
41 &container_of(b, struct tomoyo_number_group, head)
42 ->number,
43 sizeof(container_of(a, struct tomoyo_number_group, head)
44 ->number));
45}
46
47/**
Tetsuo Handa059d84d2011-09-10 15:23:54 +090048 * tomoyo_same_address_group - Check for duplicated "struct tomoyo_address_group" entry.
49 *
50 * @a: Pointer to "struct tomoyo_acl_head".
51 * @b: Pointer to "struct tomoyo_acl_head".
52 *
53 * Returns true if @a == @b, false otherwise.
54 */
55static bool tomoyo_same_address_group(const struct tomoyo_acl_head *a,
56 const struct tomoyo_acl_head *b)
57{
58 const struct tomoyo_address_group *p1 = container_of(a, typeof(*p1),
59 head);
60 const struct tomoyo_address_group *p2 = container_of(b, typeof(*p2),
61 head);
62
63 return tomoyo_same_ipaddr_union(&p1->address, &p2->address);
64}
65
66/**
67 * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090068 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +090069 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090070 * @type: Type of this group.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090071 *
72 * Returns 0 on success, negative value otherwise.
73 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +090074int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090075{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090076 struct tomoyo_group *group = tomoyo_get_group(param, type);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090077 int error = -EINVAL;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090078 if (!group)
79 return -ENOMEM;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090080 param->list = &group->member_list;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090081 if (type == TOMOYO_PATH_GROUP) {
82 struct tomoyo_path_group e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +090083 e.member_name = tomoyo_get_name(tomoyo_read_token(param));
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090084 if (!e.member_name) {
85 error = -ENOMEM;
86 goto out;
87 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +090088 error = tomoyo_update_policy(&e.head, sizeof(e), param,
89 tomoyo_same_path_group);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090090 tomoyo_put_name(e.member_name);
91 } else if (type == TOMOYO_NUMBER_GROUP) {
92 struct tomoyo_number_group e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +090093 if (param->data[0] == '@' ||
94 !tomoyo_parse_number_union(param, &e.number))
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090095 goto out;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090096 error = tomoyo_update_policy(&e.head, sizeof(e), param,
97 tomoyo_same_number_group);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090098 /*
99 * tomoyo_put_number_union() is not needed because
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900100 * param->data[0] != '@'.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900101 */
Tetsuo Handa059d84d2011-09-10 15:23:54 +0900102 } else {
103 struct tomoyo_address_group e = { };
104
105 if (param->data[0] == '@' ||
106 !tomoyo_parse_ipaddr_union(param, &e.address))
107 goto out;
108 error = tomoyo_update_policy(&e.head, sizeof(e), param,
109 tomoyo_same_address_group);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900110 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900111out:
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900112 tomoyo_put_group(group);
113 return error;
114}
115
116/**
117 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
118 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900119 * @pathname: The name of pathname.
120 * @group: Pointer to "struct tomoyo_path_group".
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900121 *
Tetsuo Handa484ca792010-07-29 14:29:55 +0900122 * Returns matched member's pathname if @pathname matches pathnames in @group,
123 * NULL otherwise.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900124 *
125 * Caller holds tomoyo_read_lock().
126 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900127const struct tomoyo_path_info *
128tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
129 const struct tomoyo_group *group)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900130{
131 struct tomoyo_path_group *member;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900132 list_for_each_entry_rcu(member, &group->member_list, head.list) {
133 if (member->head.is_deleted)
134 continue;
135 if (!tomoyo_path_matches_pattern(pathname, member->member_name))
136 continue;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900137 return member->member_name;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900138 }
Tetsuo Handa484ca792010-07-29 14:29:55 +0900139 return NULL;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900140}
141
142/**
143 * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
144 *
145 * @min: Min number.
146 * @max: Max number.
147 * @group: Pointer to "struct tomoyo_number_group".
148 *
149 * Returns true if @min and @max partially overlaps @group, false otherwise.
150 *
151 * Caller holds tomoyo_read_lock().
152 */
153bool tomoyo_number_matches_group(const unsigned long min,
154 const unsigned long max,
155 const struct tomoyo_group *group)
156{
157 struct tomoyo_number_group *member;
158 bool matched = false;
159 list_for_each_entry_rcu(member, &group->member_list, head.list) {
160 if (member->head.is_deleted)
161 continue;
162 if (min > member->number.values[1] ||
163 max < member->number.values[0])
164 continue;
165 matched = true;
166 break;
167 }
168 return matched;
169}
Tetsuo Handa059d84d2011-09-10 15:23:54 +0900170
171/**
172 * tomoyo_address_matches_group - Check whether the given address matches members of the given address group.
173 *
174 * @is_ipv6: True if @address is an IPv6 address.
175 * @address: An IPv4 or IPv6 address.
176 * @group: Pointer to "struct tomoyo_address_group".
177 *
178 * Returns true if @address matches addresses in @group group, false otherwise.
179 *
180 * Caller holds tomoyo_read_lock().
181 */
182bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
183 const struct tomoyo_group *group)
184{
185 struct tomoyo_address_group *member;
186 bool matched = false;
187 const u8 size = is_ipv6 ? 16 : 4;
188
189 list_for_each_entry_rcu(member, &group->member_list, head.list) {
190 if (member->head.is_deleted)
191 continue;
192 if (member->address.is_ipv6 != is_ipv6)
193 continue;
194 if (memcmp(&member->address.ip[0], address, size) > 0 ||
195 memcmp(address, &member->address.ip[1], size) > 0)
196 continue;
197 matched = true;
198 break;
199 }
200 return matched;
201}