xref: /linux/security/tomoyo/group.c (revision 546121b65f47384e11ec1fa2e55449fc9f4846b2)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27c2ea22eSTetsuo Handa /*
37c2ea22eSTetsuo Handa  * security/tomoyo/group.c
47c2ea22eSTetsuo Handa  *
50f2a55d5STetsuo Handa  * Copyright (C) 2005-2011  NTT DATA CORPORATION
67c2ea22eSTetsuo Handa  */
77c2ea22eSTetsuo Handa 
87c2ea22eSTetsuo Handa #include <linux/slab.h>
9b2d09103SIngo Molnar #include <linux/rculist.h>
10b2d09103SIngo Molnar 
117c2ea22eSTetsuo Handa #include "common.h"
127c2ea22eSTetsuo Handa 
130f2a55d5STetsuo Handa /**
140f2a55d5STetsuo Handa  * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry.
150f2a55d5STetsuo Handa  *
160f2a55d5STetsuo Handa  * @a: Pointer to "struct tomoyo_acl_head".
170f2a55d5STetsuo Handa  * @b: Pointer to "struct tomoyo_acl_head".
180f2a55d5STetsuo Handa  *
190f2a55d5STetsuo Handa  * Returns true if @a == @b, false otherwise.
200f2a55d5STetsuo Handa  */
tomoyo_same_path_group(const struct tomoyo_acl_head * a,const struct tomoyo_acl_head * b)217c2ea22eSTetsuo Handa static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
227c2ea22eSTetsuo Handa 				   const struct tomoyo_acl_head *b)
237c2ea22eSTetsuo Handa {
247c2ea22eSTetsuo Handa 	return container_of(a, struct tomoyo_path_group, head)->member_name ==
257c2ea22eSTetsuo Handa 		container_of(b, struct tomoyo_path_group, head)->member_name;
267c2ea22eSTetsuo Handa }
277c2ea22eSTetsuo Handa 
280f2a55d5STetsuo Handa /**
290f2a55d5STetsuo Handa  * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry.
300f2a55d5STetsuo Handa  *
310f2a55d5STetsuo Handa  * @a: Pointer to "struct tomoyo_acl_head".
320f2a55d5STetsuo Handa  * @b: Pointer to "struct tomoyo_acl_head".
330f2a55d5STetsuo Handa  *
340f2a55d5STetsuo Handa  * Returns true if @a == @b, false otherwise.
350f2a55d5STetsuo Handa  */
tomoyo_same_number_group(const struct tomoyo_acl_head * a,const struct tomoyo_acl_head * b)367c2ea22eSTetsuo Handa static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
377c2ea22eSTetsuo Handa 				     const struct tomoyo_acl_head *b)
387c2ea22eSTetsuo Handa {
397c2ea22eSTetsuo Handa 	return !memcmp(&container_of(a, struct tomoyo_number_group, head)
407c2ea22eSTetsuo Handa 		       ->number,
417c2ea22eSTetsuo Handa 		       &container_of(b, struct tomoyo_number_group, head)
427c2ea22eSTetsuo Handa 		       ->number,
437c2ea22eSTetsuo Handa 		       sizeof(container_of(a, struct tomoyo_number_group, head)
447c2ea22eSTetsuo Handa 			      ->number));
457c2ea22eSTetsuo Handa }
467c2ea22eSTetsuo Handa 
477c2ea22eSTetsuo Handa /**
48059d84dbSTetsuo Handa  * tomoyo_same_address_group - Check for duplicated "struct tomoyo_address_group" entry.
49059d84dbSTetsuo Handa  *
50059d84dbSTetsuo Handa  * @a: Pointer to "struct tomoyo_acl_head".
51059d84dbSTetsuo Handa  * @b: Pointer to "struct tomoyo_acl_head".
52059d84dbSTetsuo Handa  *
53059d84dbSTetsuo Handa  * Returns true if @a == @b, false otherwise.
54059d84dbSTetsuo Handa  */
tomoyo_same_address_group(const struct tomoyo_acl_head * a,const struct tomoyo_acl_head * b)55059d84dbSTetsuo Handa static bool tomoyo_same_address_group(const struct tomoyo_acl_head *a,
56059d84dbSTetsuo Handa 				      const struct tomoyo_acl_head *b)
57059d84dbSTetsuo Handa {
58059d84dbSTetsuo Handa 	const struct tomoyo_address_group *p1 = container_of(a, typeof(*p1),
59059d84dbSTetsuo Handa 							     head);
60059d84dbSTetsuo Handa 	const struct tomoyo_address_group *p2 = container_of(b, typeof(*p2),
61059d84dbSTetsuo Handa 							     head);
62059d84dbSTetsuo Handa 
63059d84dbSTetsuo Handa 	return tomoyo_same_ipaddr_union(&p1->address, &p2->address);
64059d84dbSTetsuo Handa }
65059d84dbSTetsuo Handa 
66059d84dbSTetsuo Handa /**
67059d84dbSTetsuo Handa  * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
687c2ea22eSTetsuo Handa  *
69a238cf5bSTetsuo Handa  * @param: Pointer to "struct tomoyo_acl_param".
707c2ea22eSTetsuo Handa  * @type:  Type of this group.
717c2ea22eSTetsuo Handa  *
727c2ea22eSTetsuo Handa  * Returns 0 on success, negative value otherwise.
737c2ea22eSTetsuo Handa  */
tomoyo_write_group(struct tomoyo_acl_param * param,const u8 type)74a238cf5bSTetsuo Handa int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
757c2ea22eSTetsuo Handa {
76a238cf5bSTetsuo Handa 	struct tomoyo_group *group = tomoyo_get_group(param, type);
777c2ea22eSTetsuo Handa 	int error = -EINVAL;
78cdcf6723STetsuo Handa 
797c2ea22eSTetsuo Handa 	if (!group)
807c2ea22eSTetsuo Handa 		return -ENOMEM;
81a238cf5bSTetsuo Handa 	param->list = &group->member_list;
827c2ea22eSTetsuo Handa 	if (type == TOMOYO_PATH_GROUP) {
837c2ea22eSTetsuo Handa 		struct tomoyo_path_group e = { };
84cdcf6723STetsuo Handa 
85a238cf5bSTetsuo Handa 		e.member_name = tomoyo_get_name(tomoyo_read_token(param));
867c2ea22eSTetsuo Handa 		if (!e.member_name) {
877c2ea22eSTetsuo Handa 			error = -ENOMEM;
887c2ea22eSTetsuo Handa 			goto out;
897c2ea22eSTetsuo Handa 		}
90a238cf5bSTetsuo Handa 		error = tomoyo_update_policy(&e.head, sizeof(e), param,
91a238cf5bSTetsuo Handa 					  tomoyo_same_path_group);
927c2ea22eSTetsuo Handa 		tomoyo_put_name(e.member_name);
937c2ea22eSTetsuo Handa 	} else if (type == TOMOYO_NUMBER_GROUP) {
947c2ea22eSTetsuo Handa 		struct tomoyo_number_group e = { };
95cdcf6723STetsuo Handa 
96a238cf5bSTetsuo Handa 		if (param->data[0] == '@' ||
97a238cf5bSTetsuo Handa 		    !tomoyo_parse_number_union(param, &e.number))
987c2ea22eSTetsuo Handa 			goto out;
99a238cf5bSTetsuo Handa 		error = tomoyo_update_policy(&e.head, sizeof(e), param,
100a238cf5bSTetsuo Handa 					  tomoyo_same_number_group);
1017c2ea22eSTetsuo Handa 		/*
1027c2ea22eSTetsuo Handa 		 * tomoyo_put_number_union() is not needed because
103a238cf5bSTetsuo Handa 		 * param->data[0] != '@'.
1047c2ea22eSTetsuo Handa 		 */
105059d84dbSTetsuo Handa 	} else {
106059d84dbSTetsuo Handa 		struct tomoyo_address_group e = { };
107059d84dbSTetsuo Handa 
108059d84dbSTetsuo Handa 		if (param->data[0] == '@' ||
109059d84dbSTetsuo Handa 		    !tomoyo_parse_ipaddr_union(param, &e.address))
110059d84dbSTetsuo Handa 			goto out;
111059d84dbSTetsuo Handa 		error = tomoyo_update_policy(&e.head, sizeof(e), param,
112059d84dbSTetsuo Handa 					     tomoyo_same_address_group);
1137c2ea22eSTetsuo Handa 	}
1147c2ea22eSTetsuo Handa out:
1157c2ea22eSTetsuo Handa 	tomoyo_put_group(group);
1167c2ea22eSTetsuo Handa 	return error;
1177c2ea22eSTetsuo Handa }
1187c2ea22eSTetsuo Handa 
1197c2ea22eSTetsuo Handa /**
1207c2ea22eSTetsuo Handa  * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
1217c2ea22eSTetsuo Handa  *
1227c2ea22eSTetsuo Handa  * @pathname: The name of pathname.
1237c2ea22eSTetsuo Handa  * @group:    Pointer to "struct tomoyo_path_group".
1247c2ea22eSTetsuo Handa  *
125484ca79cSTetsuo Handa  * Returns matched member's pathname if @pathname matches pathnames in @group,
126484ca79cSTetsuo Handa  * NULL otherwise.
1277c2ea22eSTetsuo Handa  *
1287c2ea22eSTetsuo Handa  * Caller holds tomoyo_read_lock().
1297c2ea22eSTetsuo Handa  */
130484ca79cSTetsuo Handa const struct tomoyo_path_info *
tomoyo_path_matches_group(const struct tomoyo_path_info * pathname,const struct tomoyo_group * group)131484ca79cSTetsuo Handa tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
1327c2ea22eSTetsuo Handa 			  const struct tomoyo_group *group)
1337c2ea22eSTetsuo Handa {
1347c2ea22eSTetsuo Handa 	struct tomoyo_path_group *member;
135cdcf6723STetsuo Handa 
136*6bd5ce60STetsuo Handa 	list_for_each_entry_rcu(member, &group->member_list, head.list,
137*6bd5ce60STetsuo Handa 				srcu_read_lock_held(&tomoyo_ss)) {
1387c2ea22eSTetsuo Handa 		if (member->head.is_deleted)
1397c2ea22eSTetsuo Handa 			continue;
1407c2ea22eSTetsuo Handa 		if (!tomoyo_path_matches_pattern(pathname, member->member_name))
1417c2ea22eSTetsuo Handa 			continue;
142484ca79cSTetsuo Handa 		return member->member_name;
1437c2ea22eSTetsuo Handa 	}
144484ca79cSTetsuo Handa 	return NULL;
1457c2ea22eSTetsuo Handa }
1467c2ea22eSTetsuo Handa 
1477c2ea22eSTetsuo Handa /**
1487c2ea22eSTetsuo Handa  * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
1497c2ea22eSTetsuo Handa  *
1507c2ea22eSTetsuo Handa  * @min:   Min number.
1517c2ea22eSTetsuo Handa  * @max:   Max number.
1527c2ea22eSTetsuo Handa  * @group: Pointer to "struct tomoyo_number_group".
1537c2ea22eSTetsuo Handa  *
1547c2ea22eSTetsuo Handa  * Returns true if @min and @max partially overlaps @group, false otherwise.
1557c2ea22eSTetsuo Handa  *
1567c2ea22eSTetsuo Handa  * Caller holds tomoyo_read_lock().
1577c2ea22eSTetsuo Handa  */
tomoyo_number_matches_group(const unsigned long min,const unsigned long max,const struct tomoyo_group * group)1587c2ea22eSTetsuo Handa bool tomoyo_number_matches_group(const unsigned long min,
1597c2ea22eSTetsuo Handa 				 const unsigned long max,
1607c2ea22eSTetsuo Handa 				 const struct tomoyo_group *group)
1617c2ea22eSTetsuo Handa {
1627c2ea22eSTetsuo Handa 	struct tomoyo_number_group *member;
1637c2ea22eSTetsuo Handa 	bool matched = false;
164cdcf6723STetsuo Handa 
165*6bd5ce60STetsuo Handa 	list_for_each_entry_rcu(member, &group->member_list, head.list,
166*6bd5ce60STetsuo Handa 				srcu_read_lock_held(&tomoyo_ss)) {
1677c2ea22eSTetsuo Handa 		if (member->head.is_deleted)
1687c2ea22eSTetsuo Handa 			continue;
1697c2ea22eSTetsuo Handa 		if (min > member->number.values[1] ||
1707c2ea22eSTetsuo Handa 		    max < member->number.values[0])
1717c2ea22eSTetsuo Handa 			continue;
1727c2ea22eSTetsuo Handa 		matched = true;
1737c2ea22eSTetsuo Handa 		break;
1747c2ea22eSTetsuo Handa 	}
1757c2ea22eSTetsuo Handa 	return matched;
1767c2ea22eSTetsuo Handa }
177059d84dbSTetsuo Handa 
178059d84dbSTetsuo Handa /**
179059d84dbSTetsuo Handa  * tomoyo_address_matches_group - Check whether the given address matches members of the given address group.
180059d84dbSTetsuo Handa  *
181059d84dbSTetsuo Handa  * @is_ipv6: True if @address is an IPv6 address.
182059d84dbSTetsuo Handa  * @address: An IPv4 or IPv6 address.
183059d84dbSTetsuo Handa  * @group:   Pointer to "struct tomoyo_address_group".
184059d84dbSTetsuo Handa  *
185059d84dbSTetsuo Handa  * Returns true if @address matches addresses in @group group, false otherwise.
186059d84dbSTetsuo Handa  *
187059d84dbSTetsuo Handa  * Caller holds tomoyo_read_lock().
188059d84dbSTetsuo Handa  */
tomoyo_address_matches_group(const bool is_ipv6,const __be32 * address,const struct tomoyo_group * group)189059d84dbSTetsuo Handa bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
190059d84dbSTetsuo Handa 				  const struct tomoyo_group *group)
191059d84dbSTetsuo Handa {
192059d84dbSTetsuo Handa 	struct tomoyo_address_group *member;
193059d84dbSTetsuo Handa 	bool matched = false;
194059d84dbSTetsuo Handa 	const u8 size = is_ipv6 ? 16 : 4;
195059d84dbSTetsuo Handa 
196*6bd5ce60STetsuo Handa 	list_for_each_entry_rcu(member, &group->member_list, head.list,
197*6bd5ce60STetsuo Handa 				srcu_read_lock_held(&tomoyo_ss)) {
198059d84dbSTetsuo Handa 		if (member->head.is_deleted)
199059d84dbSTetsuo Handa 			continue;
200059d84dbSTetsuo Handa 		if (member->address.is_ipv6 != is_ipv6)
201059d84dbSTetsuo Handa 			continue;
202059d84dbSTetsuo Handa 		if (memcmp(&member->address.ip[0], address, size) > 0 ||
203059d84dbSTetsuo Handa 		    memcmp(address, &member->address.ip[1], size) > 0)
204059d84dbSTetsuo Handa 			continue;
205059d84dbSTetsuo Handa 		matched = true;
206059d84dbSTetsuo Handa 		break;
207059d84dbSTetsuo Handa 	}
208059d84dbSTetsuo Handa 	return matched;
209059d84dbSTetsuo Handa }
210