]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/tomoyo/gc.c
TOMOYO: Rename symbols.
[net-next-2.6.git] / security / tomoyo / gc.c
CommitLineData
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
e2bf6907 14struct tomoyo_gc {
847b173e
TH
15 struct list_head list;
16 int type;
e79acf0e 17 struct list_head *element;
847b173e
TH
18};
19static LIST_HEAD(tomoyo_gc_queue);
20static DEFINE_MUTEX(tomoyo_gc_mutex);
21
22/* Caller holds tomoyo_policy_lock mutex. */
e79acf0e 23static bool tomoyo_add_to_gc(const int type, struct list_head *element)
847b173e 24{
e2bf6907 25 struct tomoyo_gc *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
847b173e
TH
26 if (!entry)
27 return false;
28 entry->type = type;
29 entry->element = element;
30 list_add(&entry->list, &tomoyo_gc_queue);
e79acf0e 31 list_del_rcu(element);
847b173e
TH
32 return true;
33}
34
e79acf0e 35static void tomoyo_del_allow_read(struct list_head *element)
847b173e 36{
e2bf6907 37 struct tomoyo_readable_file *ptr =
e79acf0e 38 container_of(element, typeof(*ptr), head.list);
847b173e
TH
39 tomoyo_put_name(ptr->filename);
40}
41
e79acf0e 42static void tomoyo_del_file_pattern(struct list_head *element)
847b173e 43{
e2bf6907 44 struct tomoyo_no_pattern *ptr =
e79acf0e 45 container_of(element, typeof(*ptr), head.list);
847b173e
TH
46 tomoyo_put_name(ptr->pattern);
47}
48
e79acf0e 49static void tomoyo_del_no_rewrite(struct list_head *element)
847b173e 50{
e2bf6907 51 struct tomoyo_no_rewrite *ptr =
e79acf0e 52 container_of(element, typeof(*ptr), head.list);
847b173e
TH
53 tomoyo_put_name(ptr->pattern);
54}
55
5448ec4f 56static void tomoyo_del_transition_control(struct list_head *element)
847b173e 57{
5448ec4f 58 struct tomoyo_transition_control *ptr =
e79acf0e 59 container_of(element, typeof(*ptr), head.list);
847b173e
TH
60 tomoyo_put_name(ptr->domainname);
61 tomoyo_put_name(ptr->program);
62}
63
e79acf0e 64static void tomoyo_del_aggregator(struct list_head *element)
1084307c 65{
e2bf6907 66 struct tomoyo_aggregator *ptr =
e79acf0e 67 container_of(element, typeof(*ptr), head.list);
1084307c
TH
68 tomoyo_put_name(ptr->original_name);
69 tomoyo_put_name(ptr->aggregated_name);
70}
71
e79acf0e 72static void tomoyo_del_manager(struct list_head *element)
847b173e 73{
e2bf6907 74 struct tomoyo_manager *ptr =
e79acf0e 75 container_of(element, typeof(*ptr), head.list);
847b173e
TH
76 tomoyo_put_name(ptr->manager);
77}
78
e79acf0e 79static void tomoyo_del_acl(struct list_head *element)
847b173e 80{
e79acf0e
TH
81 struct tomoyo_acl_info *acl =
82 container_of(element, typeof(*acl), list);
847b173e 83 switch (acl->type) {
7ef61233 84 case TOMOYO_TYPE_PATH_ACL:
847b173e 85 {
7ef61233 86 struct tomoyo_path_acl *entry
847b173e 87 = container_of(acl, typeof(*entry), head);
7762fbff 88 tomoyo_put_name_union(&entry->name);
847b173e
TH
89 }
90 break;
7ef61233 91 case TOMOYO_TYPE_PATH2_ACL:
847b173e 92 {
7ef61233 93 struct tomoyo_path2_acl *entry
847b173e 94 = container_of(acl, typeof(*entry), head);
7762fbff
TH
95 tomoyo_put_name_union(&entry->name1);
96 tomoyo_put_name_union(&entry->name2);
847b173e
TH
97 }
98 break;
a1f9bb6a
TH
99 case TOMOYO_TYPE_PATH_NUMBER_ACL:
100 {
101 struct tomoyo_path_number_acl *entry
102 = container_of(acl, typeof(*entry), head);
103 tomoyo_put_name_union(&entry->name);
104 tomoyo_put_number_union(&entry->number);
105 }
106 break;
75093152 107 case TOMOYO_TYPE_MKDEV_ACL:
a1f9bb6a 108 {
75093152 109 struct tomoyo_mkdev_acl *entry
a1f9bb6a
TH
110 = container_of(acl, typeof(*entry), head);
111 tomoyo_put_name_union(&entry->name);
112 tomoyo_put_number_union(&entry->mode);
113 tomoyo_put_number_union(&entry->major);
114 tomoyo_put_number_union(&entry->minor);
115 }
116 break;
2106ccd9
TH
117 case TOMOYO_TYPE_MOUNT_ACL:
118 {
119 struct tomoyo_mount_acl *entry
120 = container_of(acl, typeof(*entry), head);
121 tomoyo_put_name_union(&entry->dev_name);
122 tomoyo_put_name_union(&entry->dir_name);
123 tomoyo_put_name_union(&entry->fs_type);
124 tomoyo_put_number_union(&entry->flags);
125 }
126 break;
847b173e
TH
127 }
128}
129
e79acf0e 130static bool tomoyo_del_domain(struct list_head *element)
847b173e 131{
e79acf0e
TH
132 struct tomoyo_domain_info *domain =
133 container_of(element, typeof(*domain), list);
847b173e
TH
134 struct tomoyo_acl_info *acl;
135 struct tomoyo_acl_info *tmp;
136 /*
137 * Since we don't protect whole execve() operation using SRCU,
138 * we need to recheck domain->users at this point.
139 *
140 * (1) Reader starts SRCU section upon execve().
141 * (2) Reader traverses tomoyo_domain_list and finds this domain.
142 * (3) Writer marks this domain as deleted.
143 * (4) Garbage collector removes this domain from tomoyo_domain_list
144 * because this domain is marked as deleted and used by nobody.
145 * (5) Reader saves reference to this domain into
146 * "struct linux_binprm"->cred->security .
147 * (6) Reader finishes SRCU section, although execve() operation has
148 * not finished yet.
149 * (7) Garbage collector waits for SRCU synchronization.
150 * (8) Garbage collector kfree() this domain because this domain is
151 * used by nobody.
152 * (9) Reader finishes execve() operation and restores this domain from
153 * "struct linux_binprm"->cred->security.
154 *
155 * By updating domain->users at (5), we can solve this race problem
156 * by rechecking domain->users at (8).
157 */
158 if (atomic_read(&domain->users))
159 return false;
160 list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
e79acf0e 161 tomoyo_del_acl(&acl->list);
847b173e
TH
162 tomoyo_memory_free(acl);
163 }
164 tomoyo_put_name(domain->domainname);
165 return true;
166}
167
168
e79acf0e 169static void tomoyo_del_name(struct list_head *element)
847b173e 170{
e2bf6907 171 const struct tomoyo_name *ptr =
e79acf0e 172 container_of(element, typeof(*ptr), list);
847b173e
TH
173}
174
a98aa4de 175static void tomoyo_del_path_group(struct list_head *element)
7762fbff 176{
a98aa4de 177 struct tomoyo_path_group *member =
e79acf0e 178 container_of(element, typeof(*member), head.list);
7762fbff
TH
179 tomoyo_put_name(member->member_name);
180}
181
a98aa4de 182static void tomoyo_del_group(struct list_head *element)
7762fbff 183{
a98aa4de 184 struct tomoyo_group *group =
e79acf0e 185 container_of(element, typeof(*group), list);
7762fbff
TH
186 tomoyo_put_name(group->group_name);
187}
188
e79acf0e 189static void tomoyo_del_number_group(struct list_head *element)
4c3e9e2d 190{
a98aa4de
TH
191 struct tomoyo_number_group *member =
192 container_of(element, typeof(*member), head.list);
4c3e9e2d
TH
193}
194
d2f8b234
TH
195static bool tomoyo_collect_member(struct list_head *member_list, int id)
196{
197 struct tomoyo_acl_head *member;
198 list_for_each_entry(member, member_list, list) {
199 if (!member->is_deleted)
200 continue;
201 if (!tomoyo_add_to_gc(id, &member->list))
202 return false;
d2f8b234
TH
203 }
204 return true;
205}
206
207static bool tomoyo_collect_acl(struct tomoyo_domain_info *domain)
208{
209 struct tomoyo_acl_info *acl;
210 list_for_each_entry(acl, &domain->acl_info_list, list) {
211 if (!acl->is_deleted)
212 continue;
213 if (!tomoyo_add_to_gc(TOMOYO_ID_ACL, &acl->list))
214 return false;
d2f8b234
TH
215 }
216 return true;
217}
218
847b173e
TH
219static void tomoyo_collect_entry(void)
220{
d2f8b234 221 int i;
29282381
TH
222 if (mutex_lock_interruptible(&tomoyo_policy_lock))
223 return;
d2f8b234 224 for (i = 0; i < TOMOYO_MAX_POLICY; i++) {
a230f9e7
TH
225 if (!tomoyo_collect_member(&tomoyo_policy_list[i], i))
226 goto unlock;
847b173e
TH
227 }
228 {
229 struct tomoyo_domain_info *domain;
230 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
d2f8b234
TH
231 if (!tomoyo_collect_acl(domain))
232 goto unlock;
847b173e
TH
233 if (!domain->is_deleted || atomic_read(&domain->users))
234 continue;
235 /*
236 * Nobody is referring this domain. But somebody may
237 * refer this domain after successful execve().
238 * We recheck domain->users after SRCU synchronization.
239 */
e79acf0e 240 if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list))
d2f8b234 241 goto unlock;
847b173e
TH
242 }
243 }
d2f8b234 244 for (i = 0; i < TOMOYO_MAX_HASH; i++) {
e2bf6907 245 struct tomoyo_name *ptr;
d2f8b234
TH
246 list_for_each_entry_rcu(ptr, &tomoyo_name_list[i], list) {
247 if (atomic_read(&ptr->users))
248 continue;
e79acf0e 249 if (!tomoyo_add_to_gc(TOMOYO_ID_NAME, &ptr->list))
d2f8b234 250 goto unlock;
847b173e
TH
251 }
252 }
7c2ea22e
TH
253 for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
254 struct list_head *list = &tomoyo_group_list[i];
255 int id;
a98aa4de 256 struct tomoyo_group *group;
7c2ea22e
TH
257 switch (i) {
258 case 0:
259 id = TOMOYO_ID_PATH_GROUP;
260 break;
261 default:
262 id = TOMOYO_ID_NUMBER_GROUP;
263 break;
7762fbff 264 }
7c2ea22e
TH
265 list_for_each_entry(group, list, list) {
266 if (!tomoyo_collect_member(&group->member_list, id))
267 goto unlock;
4c3e9e2d
TH
268 if (!list_empty(&group->member_list) ||
269 atomic_read(&group->users))
270 continue;
7c2ea22e 271 if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, &group->list))
d2f8b234 272 goto unlock;
4c3e9e2d
TH
273 }
274 }
d2f8b234 275 unlock:
29282381 276 mutex_unlock(&tomoyo_policy_lock);
847b173e
TH
277}
278
279static void tomoyo_kfree_entry(void)
280{
e2bf6907
TH
281 struct tomoyo_gc *p;
282 struct tomoyo_gc *tmp;
847b173e
TH
283
284 list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
e79acf0e 285 struct list_head *element = p->element;
847b173e 286 switch (p->type) {
5448ec4f
TH
287 case TOMOYO_ID_TRANSITION_CONTROL:
288 tomoyo_del_transition_control(element);
847b173e 289 break;
1084307c 290 case TOMOYO_ID_AGGREGATOR:
e79acf0e 291 tomoyo_del_aggregator(element);
1084307c 292 break;
847b173e 293 case TOMOYO_ID_GLOBALLY_READABLE:
e79acf0e 294 tomoyo_del_allow_read(element);
847b173e
TH
295 break;
296 case TOMOYO_ID_PATTERN:
e79acf0e 297 tomoyo_del_file_pattern(element);
847b173e
TH
298 break;
299 case TOMOYO_ID_NO_REWRITE:
e79acf0e 300 tomoyo_del_no_rewrite(element);
847b173e
TH
301 break;
302 case TOMOYO_ID_MANAGER:
e79acf0e 303 tomoyo_del_manager(element);
847b173e
TH
304 break;
305 case TOMOYO_ID_NAME:
e79acf0e 306 tomoyo_del_name(element);
847b173e
TH
307 break;
308 case TOMOYO_ID_ACL:
e79acf0e 309 tomoyo_del_acl(element);
847b173e
TH
310 break;
311 case TOMOYO_ID_DOMAIN:
e79acf0e 312 if (!tomoyo_del_domain(element))
847b173e
TH
313 continue;
314 break;
7762fbff 315 case TOMOYO_ID_PATH_GROUP:
e79acf0e 316 tomoyo_del_path_group(element);
7762fbff 317 break;
a98aa4de
TH
318 case TOMOYO_ID_GROUP:
319 tomoyo_del_group(element);
4c3e9e2d
TH
320 break;
321 case TOMOYO_ID_NUMBER_GROUP:
e79acf0e 322 tomoyo_del_number_group(element);
847b173e
TH
323 break;
324 }
e79acf0e 325 tomoyo_memory_free(element);
847b173e
TH
326 list_del(&p->list);
327 kfree(p);
328 }
329}
330
331static int tomoyo_gc_thread(void *unused)
332{
333 daemonize("GC for TOMOYO");
334 if (mutex_trylock(&tomoyo_gc_mutex)) {
335 int i;
336 for (i = 0; i < 10; i++) {
337 tomoyo_collect_entry();
338 if (list_empty(&tomoyo_gc_queue))
339 break;
340 synchronize_srcu(&tomoyo_ss);
341 tomoyo_kfree_entry();
342 }
343 mutex_unlock(&tomoyo_gc_mutex);
344 }
345 do_exit(0);
346}
347
348void tomoyo_run_gc(void)
349{
350 struct task_struct *task = kthread_create(tomoyo_gc_thread, NULL,
351 "GC for TOMOYO");
352 if (!IS_ERR(task))
353 wake_up_process(task);
354}