blob: 5fb0e1298400e040bcc724f1196a4c9e02154f4c [file] [log] [blame]
Tetsuo Handa7c2ea222010-06-17 16:55:58 +09001/*
2 * security/tomoyo/group.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handa7c2ea222010-06-17 16:55:58 +09005 */
6
7#include <linux/slab.h>
8#include "common.h"
9
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090010/**
11 * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry.
12 *
13 * @a: Pointer to "struct tomoyo_acl_head".
14 * @b: Pointer to "struct tomoyo_acl_head".
15 *
16 * Returns true if @a == @b, false otherwise.
17 */
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090018static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090019 const struct tomoyo_acl_head *b)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090020{
21 return container_of(a, struct tomoyo_path_group, head)->member_name ==
22 container_of(b, struct tomoyo_path_group, head)->member_name;
23}
24
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090025/**
26 * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry.
27 *
28 * @a: Pointer to "struct tomoyo_acl_head".
29 * @b: Pointer to "struct tomoyo_acl_head".
30 *
31 * Returns true if @a == @b, false otherwise.
32 */
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090033static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090034 const struct tomoyo_acl_head *b)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090035{
36 return !memcmp(&container_of(a, struct tomoyo_number_group, head)
37 ->number,
38 &container_of(b, struct tomoyo_number_group, head)
39 ->number,
40 sizeof(container_of(a, struct tomoyo_number_group, head)
41 ->number));
42}
43
44/**
45 * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group" list.
46 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +090047 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090048 * @type: Type of this group.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090049 *
50 * Returns 0 on success, negative value otherwise.
51 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +090052int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090053{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090054 struct tomoyo_group *group = tomoyo_get_group(param, type);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090055 int error = -EINVAL;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090056 if (!group)
57 return -ENOMEM;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090058 param->list = &group->member_list;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090059 if (type == TOMOYO_PATH_GROUP) {
60 struct tomoyo_path_group e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +090061 e.member_name = tomoyo_get_name(tomoyo_read_token(param));
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090062 if (!e.member_name) {
63 error = -ENOMEM;
64 goto out;
65 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +090066 error = tomoyo_update_policy(&e.head, sizeof(e), param,
67 tomoyo_same_path_group);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090068 tomoyo_put_name(e.member_name);
69 } else if (type == TOMOYO_NUMBER_GROUP) {
70 struct tomoyo_number_group e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +090071 if (param->data[0] == '@' ||
72 !tomoyo_parse_number_union(param, &e.number))
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090073 goto out;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090074 error = tomoyo_update_policy(&e.head, sizeof(e), param,
75 tomoyo_same_number_group);
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090076 /*
77 * tomoyo_put_number_union() is not needed because
Tetsuo Handaa238cf52011-06-26 23:17:10 +090078 * param->data[0] != '@'.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090079 */
80 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +090081out:
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090082 tomoyo_put_group(group);
83 return error;
84}
85
86/**
87 * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
88 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090089 * @pathname: The name of pathname.
90 * @group: Pointer to "struct tomoyo_path_group".
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090091 *
Tetsuo Handa484ca792010-07-29 14:29:55 +090092 * Returns matched member's pathname if @pathname matches pathnames in @group,
93 * NULL otherwise.
Tetsuo Handa7c2ea222010-06-17 16:55:58 +090094 *
95 * Caller holds tomoyo_read_lock().
96 */
Tetsuo Handa484ca792010-07-29 14:29:55 +090097const struct tomoyo_path_info *
98tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
99 const struct tomoyo_group *group)
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900100{
101 struct tomoyo_path_group *member;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900102 list_for_each_entry_rcu(member, &group->member_list, head.list) {
103 if (member->head.is_deleted)
104 continue;
105 if (!tomoyo_path_matches_pattern(pathname, member->member_name))
106 continue;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900107 return member->member_name;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900108 }
Tetsuo Handa484ca792010-07-29 14:29:55 +0900109 return NULL;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900110}
111
112/**
113 * tomoyo_number_matches_group - Check whether the given number matches members of the given number group.
114 *
115 * @min: Min number.
116 * @max: Max number.
117 * @group: Pointer to "struct tomoyo_number_group".
118 *
119 * Returns true if @min and @max partially overlaps @group, false otherwise.
120 *
121 * Caller holds tomoyo_read_lock().
122 */
123bool tomoyo_number_matches_group(const unsigned long min,
124 const unsigned long max,
125 const struct tomoyo_group *group)
126{
127 struct tomoyo_number_group *member;
128 bool matched = false;
129 list_for_each_entry_rcu(member, &group->member_list, head.list) {
130 if (member->head.is_deleted)
131 continue;
132 if (min > member->number.values[1] ||
133 max < member->number.values[0])
134 continue;
135 matched = true;
136 break;
137 }
138 return matched;
139}