group.c 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * security/tomoyo/group.c
 *
 * Copyright (C) 2005-2010  NTT DATA CORPORATION
 */

#include <linux/slab.h>
#include "common.h"

static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
				const struct tomoyo_acl_head *b)
{
	return container_of(a, struct tomoyo_path_group, head)->member_name ==
		container_of(b, struct tomoyo_path_group, head)->member_name;
}

static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
				  const struct tomoyo_acl_head *b)
{
	return !memcmp(&container_of(a, struct tomoyo_number_group, head)
		       ->number,
		       &container_of(b, struct tomoyo_number_group, head)
		       ->number,
		       sizeof(container_of(a, struct tomoyo_number_group, head)
			      ->number));
}

/**
 * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
 *
31
 * @param: Pointer to "struct tomoyo_acl_param".
32 33 34 35
 * @type:      Type of this group.
 *
 * Returns 0 on success, negative value otherwise.
 */
36
int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
37
{
38
	struct tomoyo_group *group = tomoyo_get_group(param, type);
39 40 41
	int error = -EINVAL;
	if (!group)
		return -ENOMEM;
42
	param->list = &group->member_list;
43 44
	if (type == TOMOYO_PATH_GROUP) {
		struct tomoyo_path_group e = { };
45
		e.member_name = tomoyo_get_name(tomoyo_read_token(param));
46 47 48 49
		if (!e.member_name) {
			error = -ENOMEM;
			goto out;
		}
50 51
		error = tomoyo_update_policy(&e.head, sizeof(e), param,
					  tomoyo_same_path_group);
52 53 54
		tomoyo_put_name(e.member_name);
	} else if (type == TOMOYO_NUMBER_GROUP) {
		struct tomoyo_number_group e = { };
55 56
		if (param->data[0] == '@' ||
		    !tomoyo_parse_number_union(param, &e.number))
57
			goto out;
58 59
		error = tomoyo_update_policy(&e.head, sizeof(e), param,
					  tomoyo_same_number_group);
60 61
		/*
		 * tomoyo_put_number_union() is not needed because
62
		 * param->data[0] != '@'.
63 64
		 */
	}
65
out:
66 67 68 69 70 71 72 73 74 75
	tomoyo_put_group(group);
	return error;
}

/**
 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
 *
 * @pathname:        The name of pathname.
 * @group:           Pointer to "struct tomoyo_path_group".
 *
76 77
 * Returns matched member's pathname if @pathname matches pathnames in @group,
 * NULL otherwise.
78 79 80
 *
 * Caller holds tomoyo_read_lock().
 */
81 82 83
const struct tomoyo_path_info *
tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
			  const struct tomoyo_group *group)
84 85 86 87 88 89 90
{
	struct tomoyo_path_group *member;
	list_for_each_entry_rcu(member, &group->member_list, head.list) {
		if (member->head.is_deleted)
			continue;
		if (!tomoyo_path_matches_pattern(pathname, member->member_name))
			continue;
91
		return member->member_name;
92
	}
93
	return NULL;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

/**
 * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
 *
 * @min:   Min number.
 * @max:   Max number.
 * @group: Pointer to "struct tomoyo_number_group".
 *
 * Returns true if @min and @max partially overlaps @group, false otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
bool tomoyo_number_matches_group(const unsigned long min,
				 const unsigned long max,
				 const struct tomoyo_group *group)
{
	struct tomoyo_number_group *member;
	bool matched = false;
	list_for_each_entry_rcu(member, &group->member_list, head.list) {
		if (member->head.is_deleted)
			continue;
		if (min > member->number.values[1] ||
		    max < member->number.values[0])
			continue;
		matched = true;
		break;
	}
	return matched;
}