1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/group.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
8 #include <linux/slab.h>
9 #include <linux/rculist.h>
14 * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry.
16 * @a: Pointer to "struct tomoyo_acl_head".
17 * @b: Pointer to "struct tomoyo_acl_head".
19 * Returns true if @a == @b, false otherwise.
21 static bool tomoyo_same_path_group(const struct tomoyo_acl_head
*a
,
22 const struct tomoyo_acl_head
*b
)
24 return container_of(a
, struct tomoyo_path_group
, head
)->member_name
==
25 container_of(b
, struct tomoyo_path_group
, head
)->member_name
;
29 * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry.
31 * @a: Pointer to "struct tomoyo_acl_head".
32 * @b: Pointer to "struct tomoyo_acl_head".
34 * Returns true if @a == @b, false otherwise.
36 static bool tomoyo_same_number_group(const struct tomoyo_acl_head
*a
,
37 const struct tomoyo_acl_head
*b
)
39 return !memcmp(&container_of(a
, struct tomoyo_number_group
, head
)
41 &container_of(b
, struct tomoyo_number_group
, head
)
43 sizeof(container_of(a
, struct tomoyo_number_group
, head
)
48 * tomoyo_same_address_group - Check for duplicated "struct tomoyo_address_group" entry.
50 * @a: Pointer to "struct tomoyo_acl_head".
51 * @b: Pointer to "struct tomoyo_acl_head".
53 * Returns true if @a == @b, false otherwise.
55 static bool tomoyo_same_address_group(const struct tomoyo_acl_head
*a
,
56 const struct tomoyo_acl_head
*b
)
58 const struct tomoyo_address_group
*p1
= container_of(a
, typeof(*p1
),
60 const struct tomoyo_address_group
*p2
= container_of(b
, typeof(*p2
),
63 return tomoyo_same_ipaddr_union(&p1
->address
, &p2
->address
);
67 * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
69 * @param: Pointer to "struct tomoyo_acl_param".
70 * @type: Type of this group.
72 * Returns 0 on success, negative value otherwise.
74 int tomoyo_write_group(struct tomoyo_acl_param
*param
, const u8 type
)
76 struct tomoyo_group
*group
= tomoyo_get_group(param
, type
);
81 param
->list
= &group
->member_list
;
82 if (type
== TOMOYO_PATH_GROUP
) {
83 struct tomoyo_path_group e
= { };
85 e
.member_name
= tomoyo_get_name(tomoyo_read_token(param
));
90 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
91 tomoyo_same_path_group
);
92 tomoyo_put_name(e
.member_name
);
93 } else if (type
== TOMOYO_NUMBER_GROUP
) {
94 struct tomoyo_number_group e
= { };
96 if (param
->data
[0] == '@' ||
97 !tomoyo_parse_number_union(param
, &e
.number
))
99 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
100 tomoyo_same_number_group
);
102 * tomoyo_put_number_union() is not needed because
103 * param->data[0] != '@'.
106 struct tomoyo_address_group e
= { };
108 if (param
->data
[0] == '@' ||
109 !tomoyo_parse_ipaddr_union(param
, &e
.address
))
111 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
112 tomoyo_same_address_group
);
115 tomoyo_put_group(group
);
120 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
122 * @pathname: The name of pathname.
123 * @group: Pointer to "struct tomoyo_path_group".
125 * Returns matched member's pathname if @pathname matches pathnames in @group,
128 * Caller holds tomoyo_read_lock().
130 const struct tomoyo_path_info
*
131 tomoyo_path_matches_group(const struct tomoyo_path_info
*pathname
,
132 const struct tomoyo_group
*group
)
134 struct tomoyo_path_group
*member
;
136 list_for_each_entry_rcu(member
, &group
->member_list
, head
.list
,
137 srcu_read_lock_held(&tomoyo_ss
)) {
138 if (member
->head
.is_deleted
)
140 if (!tomoyo_path_matches_pattern(pathname
, member
->member_name
))
142 return member
->member_name
;
148 * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
152 * @group: Pointer to "struct tomoyo_number_group".
154 * Returns true if @min and @max partially overlaps @group, false otherwise.
156 * Caller holds tomoyo_read_lock().
158 bool tomoyo_number_matches_group(const unsigned long min
,
159 const unsigned long max
,
160 const struct tomoyo_group
*group
)
162 struct tomoyo_number_group
*member
;
163 bool matched
= false;
165 list_for_each_entry_rcu(member
, &group
->member_list
, head
.list
,
166 srcu_read_lock_held(&tomoyo_ss
)) {
167 if (member
->head
.is_deleted
)
169 if (min
> member
->number
.values
[1] ||
170 max
< member
->number
.values
[0])
179 * tomoyo_address_matches_group - Check whether the given address matches members of the given address group.
181 * @is_ipv6: True if @address is an IPv6 address.
182 * @address: An IPv4 or IPv6 address.
183 * @group: Pointer to "struct tomoyo_address_group".
185 * Returns true if @address matches addresses in @group group, false otherwise.
187 * Caller holds tomoyo_read_lock().
189 bool tomoyo_address_matches_group(const bool is_ipv6
, const __be32
*address
,
190 const struct tomoyo_group
*group
)
192 struct tomoyo_address_group
*member
;
193 bool matched
= false;
194 const u8 size
= is_ipv6
? 16 : 4;
196 list_for_each_entry_rcu(member
, &group
->member_list
, head
.list
,
197 srcu_read_lock_held(&tomoyo_ss
)) {
198 if (member
->head
.is_deleted
)
200 if (member
->address
.is_ipv6
!= is_ipv6
)
202 if (memcmp(&member
->address
.ip
[0], address
, size
) > 0 ||
203 memcmp(address
, &member
->address
.ip
[1], size
) > 0)