2 * security/tomoyo/group.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/slab.h>
8 #include <linux/rculist.h>
13 * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry.
15 * @a: Pointer to "struct tomoyo_acl_head".
16 * @b: Pointer to "struct tomoyo_acl_head".
18 * Returns true if @a == @b, false otherwise.
20 static bool tomoyo_same_path_group(const struct tomoyo_acl_head
*a
,
21 const struct tomoyo_acl_head
*b
)
23 return container_of(a
, struct tomoyo_path_group
, head
)->member_name
==
24 container_of(b
, struct tomoyo_path_group
, head
)->member_name
;
28 * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry.
30 * @a: Pointer to "struct tomoyo_acl_head".
31 * @b: Pointer to "struct tomoyo_acl_head".
33 * Returns true if @a == @b, false otherwise.
35 static bool tomoyo_same_number_group(const struct tomoyo_acl_head
*a
,
36 const struct tomoyo_acl_head
*b
)
38 return !memcmp(&container_of(a
, struct tomoyo_number_group
, head
)
40 &container_of(b
, struct tomoyo_number_group
, head
)
42 sizeof(container_of(a
, struct tomoyo_number_group
, head
)
47 * tomoyo_same_address_group - Check for duplicated "struct tomoyo_address_group" entry.
49 * @a: Pointer to "struct tomoyo_acl_head".
50 * @b: Pointer to "struct tomoyo_acl_head".
52 * Returns true if @a == @b, false otherwise.
54 static bool tomoyo_same_address_group(const struct tomoyo_acl_head
*a
,
55 const struct tomoyo_acl_head
*b
)
57 const struct tomoyo_address_group
*p1
= container_of(a
, typeof(*p1
),
59 const struct tomoyo_address_group
*p2
= container_of(b
, typeof(*p2
),
62 return tomoyo_same_ipaddr_union(&p1
->address
, &p2
->address
);
66 * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
68 * @param: Pointer to "struct tomoyo_acl_param".
69 * @type: Type of this group.
71 * Returns 0 on success, negative value otherwise.
73 int tomoyo_write_group(struct tomoyo_acl_param
*param
, const u8 type
)
75 struct tomoyo_group
*group
= tomoyo_get_group(param
, type
);
79 param
->list
= &group
->member_list
;
80 if (type
== TOMOYO_PATH_GROUP
) {
81 struct tomoyo_path_group e
= { };
82 e
.member_name
= tomoyo_get_name(tomoyo_read_token(param
));
87 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
88 tomoyo_same_path_group
);
89 tomoyo_put_name(e
.member_name
);
90 } else if (type
== TOMOYO_NUMBER_GROUP
) {
91 struct tomoyo_number_group e
= { };
92 if (param
->data
[0] == '@' ||
93 !tomoyo_parse_number_union(param
, &e
.number
))
95 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
96 tomoyo_same_number_group
);
98 * tomoyo_put_number_union() is not needed because
99 * param->data[0] != '@'.
102 struct tomoyo_address_group e
= { };
104 if (param
->data
[0] == '@' ||
105 !tomoyo_parse_ipaddr_union(param
, &e
.address
))
107 error
= tomoyo_update_policy(&e
.head
, sizeof(e
), param
,
108 tomoyo_same_address_group
);
111 tomoyo_put_group(group
);
116 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
118 * @pathname: The name of pathname.
119 * @group: Pointer to "struct tomoyo_path_group".
121 * Returns matched member's pathname if @pathname matches pathnames in @group,
124 * Caller holds tomoyo_read_lock().
126 const struct tomoyo_path_info
*
127 tomoyo_path_matches_group(const struct tomoyo_path_info
*pathname
,
128 const struct tomoyo_group
*group
)
130 struct tomoyo_path_group
*member
;
131 list_for_each_entry_rcu(member
, &group
->member_list
, head
.list
) {
132 if (member
->head
.is_deleted
)
134 if (!tomoyo_path_matches_pattern(pathname
, member
->member_name
))
136 return member
->member_name
;
142 * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
146 * @group: Pointer to "struct tomoyo_number_group".
148 * Returns true if @min and @max partially overlaps @group, false otherwise.
150 * Caller holds tomoyo_read_lock().
152 bool tomoyo_number_matches_group(const unsigned long min
,
153 const unsigned long max
,
154 const struct tomoyo_group
*group
)
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
)
161 if (min
> member
->number
.values
[1] ||
162 max
< member
->number
.values
[0])
171 * tomoyo_address_matches_group - Check whether the given address matches members of the given address group.
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".
177 * Returns true if @address matches addresses in @group group, false otherwise.
179 * Caller holds tomoyo_read_lock().
181 bool tomoyo_address_matches_group(const bool is_ipv6
, const __be32
*address
,
182 const struct tomoyo_group
*group
)
184 struct tomoyo_address_group
*member
;
185 bool matched
= false;
186 const u8 size
= is_ipv6
? 16 : 4;
188 list_for_each_entry_rcu(member
, &group
->member_list
, head
.list
) {
189 if (member
->head
.is_deleted
)
191 if (member
->address
.is_ipv6
!= is_ipv6
)
193 if (memcmp(&member
->address
.ip
[0], address
, size
) > 0 ||
194 memcmp(address
, &member
->address
.ip
[1], size
) > 0)