Commit | Line | Data |
---|---|---|
847b173e TH |
1 | /* |
2 | * security/tomoyo/gc.c | |
3 | * | |
4 | * Implementation of the Domain-Based Mandatory Access Control. | |
5 | * | |
6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION | |
7 | * | |
8 | */ | |
9 | ||
10 | #include "common.h" | |
11 | #include <linux/kthread.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
847b173e | 13 | |
d2f8b234 | 14 | enum tomoyo_policy_id { |
7762fbff TH |
15 | TOMOYO_ID_PATH_GROUP, |
16 | TOMOYO_ID_PATH_GROUP_MEMBER, | |
4c3e9e2d TH |
17 | TOMOYO_ID_NUMBER_GROUP, |
18 | TOMOYO_ID_NUMBER_GROUP_MEMBER, | |
847b173e TH |
19 | TOMOYO_ID_DOMAIN_INITIALIZER, |
20 | TOMOYO_ID_DOMAIN_KEEPER, | |
1084307c | 21 | TOMOYO_ID_AGGREGATOR, |
847b173e TH |
22 | TOMOYO_ID_ALIAS, |
23 | TOMOYO_ID_GLOBALLY_READABLE, | |
24 | TOMOYO_ID_PATTERN, | |
25 | TOMOYO_ID_NO_REWRITE, | |
26 | TOMOYO_ID_MANAGER, | |
27 | TOMOYO_ID_NAME, | |
28 | TOMOYO_ID_ACL, | |
d2f8b234 TH |
29 | TOMOYO_ID_DOMAIN, |
30 | TOMOYO_MAX_POLICY | |
847b173e TH |
31 | }; |
32 | ||
33 | struct tomoyo_gc_entry { | |
34 | struct list_head list; | |
35 | int type; | |
e79acf0e | 36 | struct list_head *element; |
847b173e TH |
37 | }; |
38 | static LIST_HEAD(tomoyo_gc_queue); | |
39 | static DEFINE_MUTEX(tomoyo_gc_mutex); | |
40 | ||
41 | /* Caller holds tomoyo_policy_lock mutex. */ | |
e79acf0e | 42 | static bool tomoyo_add_to_gc(const int type, struct list_head *element) |
847b173e TH |
43 | { |
44 | struct tomoyo_gc_entry *entry = kzalloc(sizeof(*entry), GFP_ATOMIC); | |
45 | if (!entry) | |
46 | return false; | |
47 | entry->type = type; | |
48 | entry->element = element; | |
49 | list_add(&entry->list, &tomoyo_gc_queue); | |
e79acf0e | 50 | list_del_rcu(element); |
847b173e TH |
51 | return true; |
52 | } | |
53 | ||
e79acf0e | 54 | static void tomoyo_del_allow_read(struct list_head *element) |
847b173e | 55 | { |
e79acf0e TH |
56 | struct tomoyo_globally_readable_file_entry *ptr = |
57 | container_of(element, typeof(*ptr), head.list); | |
847b173e TH |
58 | tomoyo_put_name(ptr->filename); |
59 | } | |
60 | ||
e79acf0e | 61 | static void tomoyo_del_file_pattern(struct list_head *element) |
847b173e | 62 | { |
e79acf0e TH |
63 | struct tomoyo_pattern_entry *ptr = |
64 | container_of(element, typeof(*ptr), head.list); | |
847b173e TH |
65 | tomoyo_put_name(ptr->pattern); |
66 | } | |
67 | ||
e79acf0e | 68 | static void tomoyo_del_no_rewrite(struct list_head *element) |
847b173e | 69 | { |
e79acf0e TH |
70 | struct tomoyo_no_rewrite_entry *ptr = |
71 | container_of(element, typeof(*ptr), head.list); | |
847b173e TH |
72 | tomoyo_put_name(ptr->pattern); |
73 | } | |
74 | ||
e79acf0e | 75 | static void tomoyo_del_domain_initializer(struct list_head *element) |
847b173e | 76 | { |
e79acf0e TH |
77 | struct tomoyo_domain_initializer_entry *ptr = |
78 | container_of(element, typeof(*ptr), head.list); | |
847b173e TH |
79 | tomoyo_put_name(ptr->domainname); |
80 | tomoyo_put_name(ptr->program); | |
81 | } | |
82 | ||
e79acf0e | 83 | static void tomoyo_del_domain_keeper(struct list_head *element) |
847b173e | 84 | { |
e79acf0e TH |
85 | struct tomoyo_domain_keeper_entry *ptr = |
86 | container_of(element, typeof(*ptr), head.list); | |
847b173e TH |
87 | tomoyo_put_name(ptr->domainname); |
88 | tomoyo_put_name(ptr->program); | |
89 | } | |
90 | ||
e79acf0e | 91 | static void tomoyo_del_aggregator(struct list_head *element) |
1084307c | 92 | { |
e79acf0e TH |
93 | struct tomoyo_aggregator_entry *ptr = |
94 | container_of(element, typeof(*ptr), head.list); | |
1084307c TH |
95 | tomoyo_put_name(ptr->original_name); |
96 | tomoyo_put_name(ptr->aggregated_name); | |
97 | } | |
98 | ||
e79acf0e | 99 | static void tomoyo_del_alias(struct list_head *element) |
847b173e | 100 | { |
e79acf0e TH |
101 | struct tomoyo_alias_entry *ptr = |
102 | container_of(element, typeof(*ptr), head.list); | |
847b173e TH |
103 | tomoyo_put_name(ptr->original_name); |
104 | tomoyo_put_name(ptr->aliased_name); | |
105 | } | |
106 | ||
e79acf0e | 107 | static void tomoyo_del_manager(struct list_head *element) |
847b173e | 108 | { |
e79acf0e TH |
109 | struct tomoyo_policy_manager_entry *ptr = |
110 | container_of(element, typeof(*ptr), head.list); | |
847b173e TH |
111 | tomoyo_put_name(ptr->manager); |
112 | } | |
113 | ||
e79acf0e | 114 | static void tomoyo_del_acl(struct list_head *element) |
847b173e | 115 | { |
e79acf0e TH |
116 | struct tomoyo_acl_info *acl = |
117 | container_of(element, typeof(*acl), list); | |
847b173e | 118 | switch (acl->type) { |
7ef61233 | 119 | case TOMOYO_TYPE_PATH_ACL: |
847b173e | 120 | { |
7ef61233 | 121 | struct tomoyo_path_acl *entry |
847b173e | 122 | = container_of(acl, typeof(*entry), head); |
7762fbff | 123 | tomoyo_put_name_union(&entry->name); |
847b173e TH |
124 | } |
125 | break; | |
7ef61233 | 126 | case TOMOYO_TYPE_PATH2_ACL: |
847b173e | 127 | { |
7ef61233 | 128 | struct tomoyo_path2_acl *entry |
847b173e | 129 | = container_of(acl, typeof(*entry), head); |
7762fbff TH |
130 | tomoyo_put_name_union(&entry->name1); |
131 | tomoyo_put_name_union(&entry->name2); | |
847b173e TH |
132 | } |
133 | break; | |
a1f9bb6a TH |
134 | case TOMOYO_TYPE_PATH_NUMBER_ACL: |
135 | { | |
136 | struct tomoyo_path_number_acl *entry | |
137 | = container_of(acl, typeof(*entry), head); | |
138 | tomoyo_put_name_union(&entry->name); | |
139 | tomoyo_put_number_union(&entry->number); | |
140 | } | |
141 | break; | |
75093152 | 142 | case TOMOYO_TYPE_MKDEV_ACL: |
a1f9bb6a | 143 | { |
75093152 | 144 | struct tomoyo_mkdev_acl *entry |
a1f9bb6a TH |
145 | = container_of(acl, typeof(*entry), head); |
146 | tomoyo_put_name_union(&entry->name); | |
147 | tomoyo_put_number_union(&entry->mode); | |
148 | tomoyo_put_number_union(&entry->major); | |
149 | tomoyo_put_number_union(&entry->minor); | |
150 | } | |
151 | break; | |
2106ccd9 TH |
152 | case TOMOYO_TYPE_MOUNT_ACL: |
153 | { | |
154 | struct tomoyo_mount_acl *entry | |
155 | = container_of(acl, typeof(*entry), head); | |
156 | tomoyo_put_name_union(&entry->dev_name); | |
157 | tomoyo_put_name_union(&entry->dir_name); | |
158 | tomoyo_put_name_union(&entry->fs_type); | |
159 | tomoyo_put_number_union(&entry->flags); | |
160 | } | |
161 | break; | |
847b173e TH |
162 | } |
163 | } | |
164 | ||
e79acf0e | 165 | static bool tomoyo_del_domain(struct list_head *element) |
847b173e | 166 | { |
e79acf0e TH |
167 | struct tomoyo_domain_info *domain = |
168 | container_of(element, typeof(*domain), list); | |
847b173e TH |
169 | struct tomoyo_acl_info *acl; |
170 | struct tomoyo_acl_info *tmp; | |
171 | /* | |
172 | * Since we don't protect whole execve() operation using SRCU, | |
173 | * we need to recheck domain->users at this point. | |
174 | * | |
175 | * (1) Reader starts SRCU section upon execve(). | |
176 | * (2) Reader traverses tomoyo_domain_list and finds this domain. | |
177 | * (3) Writer marks this domain as deleted. | |
178 | * (4) Garbage collector removes this domain from tomoyo_domain_list | |
179 | * because this domain is marked as deleted and used by nobody. | |
180 | * (5) Reader saves reference to this domain into | |
181 | * "struct linux_binprm"->cred->security . | |
182 | * (6) Reader finishes SRCU section, although execve() operation has | |
183 | * not finished yet. | |
184 | * (7) Garbage collector waits for SRCU synchronization. | |
185 | * (8) Garbage collector kfree() this domain because this domain is | |
186 | * used by nobody. | |
187 | * (9) Reader finishes execve() operation and restores this domain from | |
188 | * "struct linux_binprm"->cred->security. | |
189 | * | |
190 | * By updating domain->users at (5), we can solve this race problem | |
191 | * by rechecking domain->users at (8). | |
192 | */ | |
193 | if (atomic_read(&domain->users)) | |
194 | return false; | |
195 | list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) { | |
e79acf0e | 196 | tomoyo_del_acl(&acl->list); |
847b173e TH |
197 | tomoyo_memory_free(acl); |
198 | } | |
199 | tomoyo_put_name(domain->domainname); | |
200 | return true; | |
201 | } | |
202 | ||
203 | ||
e79acf0e | 204 | static void tomoyo_del_name(struct list_head *element) |
847b173e | 205 | { |
e79acf0e TH |
206 | const struct tomoyo_name_entry *ptr = |
207 | container_of(element, typeof(*ptr), list); | |
847b173e TH |
208 | } |
209 | ||
e79acf0e | 210 | static void tomoyo_del_path_group_member(struct list_head *element) |
7762fbff | 211 | { |
e79acf0e TH |
212 | struct tomoyo_path_group_member *member = |
213 | container_of(element, typeof(*member), head.list); | |
7762fbff TH |
214 | tomoyo_put_name(member->member_name); |
215 | } | |
216 | ||
e79acf0e | 217 | static void tomoyo_del_path_group(struct list_head *element) |
7762fbff | 218 | { |
e79acf0e TH |
219 | struct tomoyo_path_group *group = |
220 | container_of(element, typeof(*group), list); | |
7762fbff TH |
221 | tomoyo_put_name(group->group_name); |
222 | } | |
223 | ||
e79acf0e | 224 | static void tomoyo_del_number_group_member(struct list_head *element) |
4c3e9e2d | 225 | { |
e79acf0e TH |
226 | struct tomoyo_number_group_member *member = |
227 | container_of(element, typeof(*member), head.list); | |
4c3e9e2d TH |
228 | } |
229 | ||
e79acf0e | 230 | static void tomoyo_del_number_group(struct list_head *element) |
4c3e9e2d | 231 | { |
e79acf0e TH |
232 | struct tomoyo_number_group *group = |
233 | container_of(element, typeof(*group), list); | |
4c3e9e2d TH |
234 | tomoyo_put_name(group->group_name); |
235 | } | |
236 | ||
d2f8b234 TH |
237 | static struct list_head *tomoyo_policy_list[TOMOYO_MAX_POLICY] = { |
238 | [TOMOYO_ID_GLOBALLY_READABLE] = &tomoyo_globally_readable_list, | |
239 | [TOMOYO_ID_PATTERN] = &tomoyo_pattern_list, | |
240 | [TOMOYO_ID_NO_REWRITE] = &tomoyo_no_rewrite_list, | |
241 | [TOMOYO_ID_DOMAIN_INITIALIZER] = &tomoyo_domain_initializer_list, | |
242 | [TOMOYO_ID_DOMAIN_KEEPER] = &tomoyo_domain_keeper_list, | |
243 | [TOMOYO_ID_AGGREGATOR] = &tomoyo_aggregator_list, | |
244 | [TOMOYO_ID_ALIAS] = &tomoyo_alias_list, | |
245 | [TOMOYO_ID_MANAGER] = &tomoyo_policy_manager_list, | |
246 | }; | |
247 | ||
248 | static bool tomoyo_collect_member(struct list_head *member_list, int id) | |
249 | { | |
250 | struct tomoyo_acl_head *member; | |
251 | list_for_each_entry(member, member_list, list) { | |
252 | if (!member->is_deleted) | |
253 | continue; | |
254 | if (!tomoyo_add_to_gc(id, &member->list)) | |
255 | return false; | |
d2f8b234 TH |
256 | } |
257 | return true; | |
258 | } | |
259 | ||
260 | static bool tomoyo_collect_acl(struct tomoyo_domain_info *domain) | |
261 | { | |
262 | struct tomoyo_acl_info *acl; | |
263 | list_for_each_entry(acl, &domain->acl_info_list, list) { | |
264 | if (!acl->is_deleted) | |
265 | continue; | |
266 | if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list)) | |
267 | return false; | |
d2f8b234 TH |
268 | } |
269 | return true; | |
270 | } | |
271 | ||
847b173e TH |
272 | static void tomoyo_collect_entry(void) |
273 | { | |
d2f8b234 | 274 | int i; |
29282381 TH |
275 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
276 | return; | |
d2f8b234 TH |
277 | for (i = 0; i < TOMOYO_MAX_POLICY; i++) { |
278 | if (tomoyo_policy_list[i]) | |
279 | if (!tomoyo_collect_member(tomoyo_policy_list[i], i)) | |
280 | goto unlock; | |
847b173e TH |
281 | } |
282 | { | |
283 | struct tomoyo_domain_info *domain; | |
284 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { | |
d2f8b234 TH |
285 | if (!tomoyo_collect_acl(domain)) |
286 | goto unlock; | |
847b173e TH |
287 | if (!domain->is_deleted || atomic_read(&domain->users)) |
288 | continue; | |
289 | /* | |
290 | * Nobody is referring this domain. But somebody may | |
291 | * refer this domain after successful execve(). | |
292 | * We recheck domain->users after SRCU synchronization. | |
293 | */ | |
e79acf0e | 294 | if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list)) |
d2f8b234 | 295 | goto unlock; |
847b173e TH |
296 | } |
297 | } | |
d2f8b234 TH |
298 | for (i = 0; i < TOMOYO_MAX_HASH; i++) { |
299 | struct tomoyo_name_entry *ptr; | |
300 | list_for_each_entry_rcu(ptr, &tomoyo_name_list[i], list) { | |
301 | if (atomic_read(&ptr->users)) | |
302 | continue; | |
e79acf0e | 303 | if (!tomoyo_add_to_gc(TOMOYO_ID_NAME, &ptr->list)) |
d2f8b234 | 304 | goto unlock; |
847b173e TH |
305 | } |
306 | } | |
7762fbff TH |
307 | { |
308 | struct tomoyo_path_group *group; | |
309 | list_for_each_entry_rcu(group, &tomoyo_path_group_list, list) { | |
d2f8b234 TH |
310 | tomoyo_collect_member(&group->member_list, |
311 | TOMOYO_ID_PATH_GROUP_MEMBER); | |
7762fbff TH |
312 | if (!list_empty(&group->member_list) || |
313 | atomic_read(&group->users)) | |
314 | continue; | |
e79acf0e TH |
315 | if (!tomoyo_add_to_gc(TOMOYO_ID_PATH_GROUP, |
316 | &group->list)) | |
d2f8b234 | 317 | goto unlock; |
7762fbff TH |
318 | } |
319 | } | |
4c3e9e2d TH |
320 | { |
321 | struct tomoyo_number_group *group; | |
d2f8b234 TH |
322 | list_for_each_entry_rcu(group, &tomoyo_number_group_list, |
323 | list) { | |
324 | tomoyo_collect_member(&group->member_list, | |
325 | TOMOYO_ID_NUMBER_GROUP_MEMBER); | |
4c3e9e2d TH |
326 | if (!list_empty(&group->member_list) || |
327 | atomic_read(&group->users)) | |
328 | continue; | |
e79acf0e TH |
329 | if (!tomoyo_add_to_gc(TOMOYO_ID_NUMBER_GROUP, |
330 | &group->list)) | |
d2f8b234 | 331 | goto unlock; |
4c3e9e2d TH |
332 | } |
333 | } | |
d2f8b234 | 334 | unlock: |
29282381 | 335 | mutex_unlock(&tomoyo_policy_lock); |
847b173e TH |
336 | } |
337 | ||
338 | static void tomoyo_kfree_entry(void) | |
339 | { | |
340 | struct tomoyo_gc_entry *p; | |
341 | struct tomoyo_gc_entry *tmp; | |
342 | ||
343 | list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) { | |
e79acf0e | 344 | struct list_head *element = p->element; |
847b173e TH |
345 | switch (p->type) { |
346 | case TOMOYO_ID_DOMAIN_INITIALIZER: | |
e79acf0e | 347 | tomoyo_del_domain_initializer(element); |
847b173e TH |
348 | break; |
349 | case TOMOYO_ID_DOMAIN_KEEPER: | |
e79acf0e | 350 | tomoyo_del_domain_keeper(element); |
847b173e | 351 | break; |
1084307c | 352 | case TOMOYO_ID_AGGREGATOR: |
e79acf0e | 353 | tomoyo_del_aggregator(element); |
1084307c | 354 | break; |
847b173e | 355 | case TOMOYO_ID_ALIAS: |
e79acf0e | 356 | tomoyo_del_alias(element); |
847b173e TH |
357 | break; |
358 | case TOMOYO_ID_GLOBALLY_READABLE: | |
e79acf0e | 359 | tomoyo_del_allow_read(element); |
847b173e TH |
360 | break; |
361 | case TOMOYO_ID_PATTERN: | |
e79acf0e | 362 | tomoyo_del_file_pattern(element); |
847b173e TH |
363 | break; |
364 | case TOMOYO_ID_NO_REWRITE: | |
e79acf0e | 365 | tomoyo_del_no_rewrite(element); |
847b173e TH |
366 | break; |
367 | case TOMOYO_ID_MANAGER: | |
e79acf0e | 368 | tomoyo_del_manager(element); |
847b173e TH |
369 | break; |
370 | case TOMOYO_ID_NAME: | |
e79acf0e | 371 | tomoyo_del_name(element); |
847b173e TH |
372 | break; |
373 | case TOMOYO_ID_ACL: | |
e79acf0e | 374 | tomoyo_del_acl(element); |
847b173e TH |
375 | break; |
376 | case TOMOYO_ID_DOMAIN: | |
e79acf0e | 377 | if (!tomoyo_del_domain(element)) |
847b173e TH |
378 | continue; |
379 | break; | |
7762fbff | 380 | case TOMOYO_ID_PATH_GROUP_MEMBER: |
e79acf0e | 381 | tomoyo_del_path_group_member(element); |
7762fbff TH |
382 | break; |
383 | case TOMOYO_ID_PATH_GROUP: | |
e79acf0e | 384 | tomoyo_del_path_group(element); |
7762fbff | 385 | break; |
4c3e9e2d | 386 | case TOMOYO_ID_NUMBER_GROUP_MEMBER: |
e79acf0e | 387 | tomoyo_del_number_group_member(element); |
4c3e9e2d TH |
388 | break; |
389 | case TOMOYO_ID_NUMBER_GROUP: | |
e79acf0e | 390 | tomoyo_del_number_group(element); |
847b173e TH |
391 | break; |
392 | } | |
e79acf0e | 393 | tomoyo_memory_free(element); |
847b173e TH |
394 | list_del(&p->list); |
395 | kfree(p); | |
396 | } | |
397 | } | |
398 | ||
399 | static int tomoyo_gc_thread(void *unused) | |
400 | { | |
401 | daemonize("GC for TOMOYO"); | |
402 | if (mutex_trylock(&tomoyo_gc_mutex)) { | |
403 | int i; | |
404 | for (i = 0; i < 10; i++) { | |
405 | tomoyo_collect_entry(); | |
406 | if (list_empty(&tomoyo_gc_queue)) | |
407 | break; | |
408 | synchronize_srcu(&tomoyo_ss); | |
409 | tomoyo_kfree_entry(); | |
410 | } | |
411 | mutex_unlock(&tomoyo_gc_mutex); | |
412 | } | |
413 | do_exit(0); | |
414 | } | |
415 | ||
416 | void tomoyo_run_gc(void) | |
417 | { | |
418 | struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL, | |
419 | "GC for TOMOYO"); | |
420 | if (!IS_ERR(task)) | |
421 | wake_up_process(task); | |
422 | } |