Blame view

security/tomoyo/group.c 5.63 KB
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
1
2
3
  /*
   * security/tomoyo/group.c
   *
0f2a55d5b   Tetsuo Handa   TOMOYO: Update ke...
4
   * Copyright (C) 2005-2011  NTT DATA CORPORATION
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
5
6
7
8
   */
  
  #include <linux/slab.h>
  #include "common.h"
0f2a55d5b   Tetsuo Handa   TOMOYO: Update ke...
9
10
11
12
13
14
15
16
  /**
   * tomoyo_same_path_group - Check for duplicated "struct tomoyo_path_group" entry.
   *
   * @a: Pointer to "struct tomoyo_acl_head".
   * @b: Pointer to "struct tomoyo_acl_head".
   *
   * Returns true if @a == @b, false otherwise.
   */
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
17
  static bool tomoyo_same_path_group(const struct tomoyo_acl_head *a,
0f2a55d5b   Tetsuo Handa   TOMOYO: Update ke...
18
  				   const struct tomoyo_acl_head *b)
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
19
20
21
22
  {
  	return container_of(a, struct tomoyo_path_group, head)->member_name ==
  		container_of(b, struct tomoyo_path_group, head)->member_name;
  }
0f2a55d5b   Tetsuo Handa   TOMOYO: Update ke...
23
24
25
26
27
28
29
30
  /**
   * tomoyo_same_number_group - Check for duplicated "struct tomoyo_number_group" entry.
   *
   * @a: Pointer to "struct tomoyo_acl_head".
   * @b: Pointer to "struct tomoyo_acl_head".
   *
   * Returns true if @a == @b, false otherwise.
   */
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
31
  static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a,
0f2a55d5b   Tetsuo Handa   TOMOYO: Update ke...
32
  				     const struct tomoyo_acl_head *b)
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
33
34
35
36
37
38
39
40
41
42
  {
  	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));
  }
  
  /**
059d84dbb   Tetsuo Handa   TOMOYO: Add socke...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
   * tomoyo_same_address_group - Check for duplicated "struct tomoyo_address_group" entry.
   *
   * @a: Pointer to "struct tomoyo_acl_head".
   * @b: Pointer to "struct tomoyo_acl_head".
   *
   * Returns true if @a == @b, false otherwise.
   */
  static bool tomoyo_same_address_group(const struct tomoyo_acl_head *a,
  				      const struct tomoyo_acl_head *b)
  {
  	const struct tomoyo_address_group *p1 = container_of(a, typeof(*p1),
  							     head);
  	const struct tomoyo_address_group *p2 = container_of(b, typeof(*p2),
  							     head);
  
  	return tomoyo_same_ipaddr_union(&p1->address, &p2->address);
  }
  
  /**
   * tomoyo_write_group - Write "struct tomoyo_path_group"/"struct tomoyo_number_group"/"struct tomoyo_address_group" list.
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
63
   *
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
64
   * @param: Pointer to "struct tomoyo_acl_param".
0f2a55d5b   Tetsuo Handa   TOMOYO: Update ke...
65
   * @type:  Type of this group.
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
66
67
68
   *
   * Returns 0 on success, negative value otherwise.
   */
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
69
  int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type)
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
70
  {
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
71
  	struct tomoyo_group *group = tomoyo_get_group(param, type);
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
72
  	int error = -EINVAL;
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
73
74
  	if (!group)
  		return -ENOMEM;
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
75
  	param->list = &group->member_list;
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
76
77
  	if (type == TOMOYO_PATH_GROUP) {
  		struct tomoyo_path_group e = { };
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
78
  		e.member_name = tomoyo_get_name(tomoyo_read_token(param));
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
79
80
81
82
  		if (!e.member_name) {
  			error = -ENOMEM;
  			goto out;
  		}
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
83
84
  		error = tomoyo_update_policy(&e.head, sizeof(e), param,
  					  tomoyo_same_path_group);
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
85
86
87
  		tomoyo_put_name(e.member_name);
  	} else if (type == TOMOYO_NUMBER_GROUP) {
  		struct tomoyo_number_group e = { };
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
88
89
  		if (param->data[0] == '@' ||
  		    !tomoyo_parse_number_union(param, &e.number))
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
90
  			goto out;
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
91
92
  		error = tomoyo_update_policy(&e.head, sizeof(e), param,
  					  tomoyo_same_number_group);
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
93
94
  		/*
  		 * tomoyo_put_number_union() is not needed because
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
95
  		 * param->data[0] != '@'.
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
96
  		 */
059d84dbb   Tetsuo Handa   TOMOYO: Add socke...
97
98
99
100
101
102
103
104
  	} else {
  		struct tomoyo_address_group e = { };
  
  		if (param->data[0] == '@' ||
  		    !tomoyo_parse_ipaddr_union(param, &e.address))
  			goto out;
  		error = tomoyo_update_policy(&e.head, sizeof(e), param,
  					     tomoyo_same_address_group);
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
105
  	}
a238cf5b8   Tetsuo Handa   TOMOYO: Use struc...
106
  out:
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
107
108
109
110
111
112
113
  	tomoyo_put_group(group);
  	return error;
  }
  
  /**
   * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group.
   *
0f2a55d5b   Tetsuo Handa   TOMOYO: Update ke...
114
115
   * @pathname: The name of pathname.
   * @group:    Pointer to "struct tomoyo_path_group".
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
116
   *
484ca79c6   Tetsuo Handa   TOMOYO: Use pathn...
117
118
   * Returns matched member's pathname if @pathname matches pathnames in @group,
   * NULL otherwise.
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
119
120
121
   *
   * Caller holds tomoyo_read_lock().
   */
484ca79c6   Tetsuo Handa   TOMOYO: Use pathn...
122
123
124
  const struct tomoyo_path_info *
  tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  			  const struct tomoyo_group *group)
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
125
126
  {
  	struct tomoyo_path_group *member;
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
127
128
129
130
131
  	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;
484ca79c6   Tetsuo Handa   TOMOYO: Use pathn...
132
  		return member->member_name;
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
133
  	}
484ca79c6   Tetsuo Handa   TOMOYO: Use pathn...
134
  	return NULL;
7c2ea22e3   Tetsuo Handa   TOMOYO: Merge pat...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  }
  
  /**
   * 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;
  }
059d84dbb   Tetsuo Handa   TOMOYO: Add socke...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  
  /**
   * tomoyo_address_matches_group - Check whether the given address matches members of the given address group.
   *
   * @is_ipv6: True if @address is an IPv6 address.
   * @address: An IPv4 or IPv6 address.
   * @group:   Pointer to "struct tomoyo_address_group".
   *
   * Returns true if @address matches addresses in @group group, false otherwise.
   *
   * Caller holds tomoyo_read_lock().
   */
  bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
  				  const struct tomoyo_group *group)
  {
  	struct tomoyo_address_group *member;
  	bool matched = false;
  	const u8 size = is_ipv6 ? 16 : 4;
  
  	list_for_each_entry_rcu(member, &group->member_list, head.list) {
  		if (member->head.is_deleted)
  			continue;
  		if (member->address.is_ipv6 != is_ipv6)
  			continue;
  		if (memcmp(&member->address.ip[0], address, size) > 0 ||
  		    memcmp(address, &member->address.ip[1], size) > 0)
  			continue;
  		matched = true;
  		break;
  	}
  	return matched;
  }