blob: a877e4c3b1019d962da68ad792816d8e6aa28e1d [file] [log] [blame]
Tetsuo Handa847b1732010-02-11 09:43:54 +09001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Tetsuo Handa847b1732010-02-11 09:43:54 +090013
Tetsuo Handae2bf6902010-06-25 11:16:00 +090014struct tomoyo_gc {
Tetsuo Handa847b1732010-02-11 09:43:54 +090015 struct list_head list;
16 int type;
Tetsuo Handae79acf02010-06-16 16:31:50 +090017 struct list_head *element;
Tetsuo Handa847b1732010-02-11 09:43:54 +090018};
19static LIST_HEAD(tomoyo_gc_queue);
20static DEFINE_MUTEX(tomoyo_gc_mutex);
21
22/* Caller holds tomoyo_policy_lock mutex. */
Tetsuo Handae79acf02010-06-16 16:31:50 +090023static bool tomoyo_add_to_gc(const int type, struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090024{
Tetsuo Handae2bf6902010-06-25 11:16:00 +090025 struct tomoyo_gc *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
Tetsuo Handa847b1732010-02-11 09:43:54 +090026 if (!entry)
27 return false;
28 entry->type = type;
29 entry->element = element;
30 list_add(&entry->list, &tomoyo_gc_queue);
Tetsuo Handae79acf02010-06-16 16:31:50 +090031 list_del_rcu(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +090032 return true;
33}
34
Tetsuo Handae79acf02010-06-16 16:31:50 +090035static void tomoyo_del_allow_read(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090036{
Tetsuo Handae2bf6902010-06-25 11:16:00 +090037 struct tomoyo_readable_file *ptr =
Tetsuo Handae79acf02010-06-16 16:31:50 +090038 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090039 tomoyo_put_name(ptr->filename);
40}
41
Tetsuo Handae79acf02010-06-16 16:31:50 +090042static void tomoyo_del_file_pattern(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090043{
Tetsuo Handae2bf6902010-06-25 11:16:00 +090044 struct tomoyo_no_pattern *ptr =
Tetsuo Handae79acf02010-06-16 16:31:50 +090045 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090046 tomoyo_put_name(ptr->pattern);
47}
48
Tetsuo Handae79acf02010-06-16 16:31:50 +090049static void tomoyo_del_no_rewrite(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090050{
Tetsuo Handae2bf6902010-06-25 11:16:00 +090051 struct tomoyo_no_rewrite *ptr =
Tetsuo Handae79acf02010-06-16 16:31:50 +090052 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090053 tomoyo_put_name(ptr->pattern);
54}
55
Tetsuo Handa5448ec42010-06-21 11:14:39 +090056static void tomoyo_del_transition_control(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090057{
Tetsuo Handa5448ec42010-06-21 11:14:39 +090058 struct tomoyo_transition_control *ptr =
Tetsuo Handae79acf02010-06-16 16:31:50 +090059 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090060 tomoyo_put_name(ptr->domainname);
61 tomoyo_put_name(ptr->program);
62}
63
Tetsuo Handae79acf02010-06-16 16:31:50 +090064static void tomoyo_del_aggregator(struct list_head *element)
Tetsuo Handa10843072010-06-03 20:38:03 +090065{
Tetsuo Handae2bf6902010-06-25 11:16:00 +090066 struct tomoyo_aggregator *ptr =
Tetsuo Handae79acf02010-06-16 16:31:50 +090067 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa10843072010-06-03 20:38:03 +090068 tomoyo_put_name(ptr->original_name);
69 tomoyo_put_name(ptr->aggregated_name);
70}
71
Tetsuo Handae79acf02010-06-16 16:31:50 +090072static void tomoyo_del_manager(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090073{
Tetsuo Handae2bf6902010-06-25 11:16:00 +090074 struct tomoyo_manager *ptr =
Tetsuo Handae79acf02010-06-16 16:31:50 +090075 container_of(element, typeof(*ptr), head.list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090076 tomoyo_put_name(ptr->manager);
77}
78
Tetsuo Handae79acf02010-06-16 16:31:50 +090079static void tomoyo_del_acl(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +090080{
Tetsuo Handae79acf02010-06-16 16:31:50 +090081 struct tomoyo_acl_info *acl =
82 container_of(element, typeof(*acl), list);
Tetsuo Handa847b1732010-02-11 09:43:54 +090083 switch (acl->type) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +090084 case TOMOYO_TYPE_PATH_ACL:
Tetsuo Handa847b1732010-02-11 09:43:54 +090085 {
Tetsuo Handa7ef61232010-02-16 08:03:30 +090086 struct tomoyo_path_acl *entry
Tetsuo Handa847b1732010-02-11 09:43:54 +090087 = container_of(acl, typeof(*entry), head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090088 tomoyo_put_name_union(&entry->name);
Tetsuo Handa847b1732010-02-11 09:43:54 +090089 }
90 break;
Tetsuo Handa7ef61232010-02-16 08:03:30 +090091 case TOMOYO_TYPE_PATH2_ACL:
Tetsuo Handa847b1732010-02-11 09:43:54 +090092 {
Tetsuo Handa7ef61232010-02-16 08:03:30 +090093 struct tomoyo_path2_acl *entry
Tetsuo Handa847b1732010-02-11 09:43:54 +090094 = container_of(acl, typeof(*entry), head);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090095 tomoyo_put_name_union(&entry->name1);
96 tomoyo_put_name_union(&entry->name2);
Tetsuo Handa847b1732010-02-11 09:43:54 +090097 }
98 break;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +090099 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;
Tetsuo Handa75093152010-06-16 16:23:55 +0900107 case TOMOYO_TYPE_MKDEV_ACL:
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900108 {
Tetsuo Handa75093152010-06-16 16:23:55 +0900109 struct tomoyo_mkdev_acl *entry
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900110 = 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;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900117 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;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900127 }
128}
129
Tetsuo Handae79acf02010-06-16 16:31:50 +0900130static bool tomoyo_del_domain(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +0900131{
Tetsuo Handae79acf02010-06-16 16:31:50 +0900132 struct tomoyo_domain_info *domain =
133 container_of(element, typeof(*domain), list);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900134 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) {
Tetsuo Handae79acf02010-06-16 16:31:50 +0900161 tomoyo_del_acl(&acl->list);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900162 tomoyo_memory_free(acl);
163 }
164 tomoyo_put_name(domain->domainname);
165 return true;
166}
167
168
Tetsuo Handae79acf02010-06-16 16:31:50 +0900169static void tomoyo_del_name(struct list_head *element)
Tetsuo Handa847b1732010-02-11 09:43:54 +0900170{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900171 const struct tomoyo_name *ptr =
Tetsuo Handae79acf02010-06-16 16:31:50 +0900172 container_of(element, typeof(*ptr), list);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900173}
174
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900175static void tomoyo_del_path_group(struct list_head *element)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900176{
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900177 struct tomoyo_path_group *member =
Tetsuo Handae79acf02010-06-16 16:31:50 +0900178 container_of(element, typeof(*member), head.list);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900179 tomoyo_put_name(member->member_name);
180}
181
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900182static void tomoyo_del_group(struct list_head *element)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900183{
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900184 struct tomoyo_group *group =
Tetsuo Handae79acf02010-06-16 16:31:50 +0900185 container_of(element, typeof(*group), list);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900186 tomoyo_put_name(group->group_name);
187}
188
Tetsuo Handae79acf02010-06-16 16:31:50 +0900189static void tomoyo_del_number_group(struct list_head *element)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900190{
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900191 struct tomoyo_number_group *member =
192 container_of(element, typeof(*member), head.list);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900193}
194
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900195static 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;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900203 }
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;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900215 }
216 return true;
217}
218
Tetsuo Handa847b1732010-02-11 09:43:54 +0900219static void tomoyo_collect_entry(void)
220{
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900221 int i;
Tetsuo Handa29282382010-05-06 00:18:15 +0900222 if (mutex_lock_interruptible(&tomoyo_policy_lock))
223 return;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900224 for (i = 0; i < TOMOYO_MAX_POLICY; i++) {
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900225 if (!tomoyo_collect_member(&tomoyo_policy_list[i], i))
226 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900227 }
228 {
229 struct tomoyo_domain_info *domain;
230 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900231 if (!tomoyo_collect_acl(domain))
232 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900233 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 */
Tetsuo Handae79acf02010-06-16 16:31:50 +0900240 if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN, &domain->list))
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900241 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900242 }
243 }
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900244 for (i = 0; i < TOMOYO_MAX_HASH; i++) {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900245 struct tomoyo_name *ptr;
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900246 list_for_each_entry_rcu(ptr, &tomoyo_name_list[i], list) {
247 if (atomic_read(&ptr->users))
248 continue;
Tetsuo Handae79acf02010-06-16 16:31:50 +0900249 if (!tomoyo_add_to_gc(TOMOYO_ID_NAME, &ptr->list))
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900250 goto unlock;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900251 }
252 }
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900253 for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
254 struct list_head *list = &tomoyo_group_list[i];
255 int id;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900256 struct tomoyo_group *group;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900257 switch (i) {
258 case 0:
259 id = TOMOYO_ID_PATH_GROUP;
260 break;
261 default:
262 id = TOMOYO_ID_NUMBER_GROUP;
263 break;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900264 }
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900265 list_for_each_entry(group, list, list) {
266 if (!tomoyo_collect_member(&group->member_list, id))
267 goto unlock;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900268 if (!list_empty(&group->member_list) ||
269 atomic_read(&group->users))
270 continue;
Tetsuo Handa7c2ea222010-06-17 16:55:58 +0900271 if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP, &group->list))
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900272 goto unlock;
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900273 }
274 }
Tetsuo Handad2f8b232010-06-15 10:10:37 +0900275 unlock:
Tetsuo Handa29282382010-05-06 00:18:15 +0900276 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900277}
278
279static void tomoyo_kfree_entry(void)
280{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900281 struct tomoyo_gc *p;
282 struct tomoyo_gc *tmp;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900283
284 list_for_each_entry_safe(p, tmp, &tomoyo_gc_queue, list) {
Tetsuo Handae79acf02010-06-16 16:31:50 +0900285 struct list_head *element = p->element;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900286 switch (p->type) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900287 case TOMOYO_ID_TRANSITION_CONTROL:
288 tomoyo_del_transition_control(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900289 break;
Tetsuo Handa10843072010-06-03 20:38:03 +0900290 case TOMOYO_ID_AGGREGATOR:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900291 tomoyo_del_aggregator(element);
Tetsuo Handa10843072010-06-03 20:38:03 +0900292 break;
Tetsuo Handa847b1732010-02-11 09:43:54 +0900293 case TOMOYO_ID_GLOBALLY_READABLE:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900294 tomoyo_del_allow_read(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900295 break;
296 case TOMOYO_ID_PATTERN:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900297 tomoyo_del_file_pattern(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900298 break;
299 case TOMOYO_ID_NO_REWRITE:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900300 tomoyo_del_no_rewrite(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900301 break;
302 case TOMOYO_ID_MANAGER:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900303 tomoyo_del_manager(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900304 break;
305 case TOMOYO_ID_NAME:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900306 tomoyo_del_name(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900307 break;
308 case TOMOYO_ID_ACL:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900309 tomoyo_del_acl(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900310 break;
311 case TOMOYO_ID_DOMAIN:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900312 if (!tomoyo_del_domain(element))
Tetsuo Handa847b1732010-02-11 09:43:54 +0900313 continue;
314 break;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900315 case TOMOYO_ID_PATH_GROUP:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900316 tomoyo_del_path_group(element);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900317 break;
Tetsuo Handaa98aa4d2010-06-17 16:52:29 +0900318 case TOMOYO_ID_GROUP:
319 tomoyo_del_group(element);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900320 break;
321 case TOMOYO_ID_NUMBER_GROUP:
Tetsuo Handae79acf02010-06-16 16:31:50 +0900322 tomoyo_del_number_group(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900323 break;
324 }
Tetsuo Handae79acf02010-06-16 16:31:50 +0900325 tomoyo_memory_free(element);
Tetsuo Handa847b1732010-02-11 09:43:54 +0900326 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}