4 * Implementation of the Domain-Based Mandatory Access Control.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
11 #include <linux/kthread.h>
12 #include <linux/slab.h>
15 struct list_head list
;
17 struct list_head
*element
;
19 static LIST_HEAD(tomoyo_gc_queue
);
20 static DEFINE_MUTEX(tomoyo_gc_mutex
);
22 /* Caller holds tomoyo_policy_lock mutex. */
23 static bool tomoyo_add_to_gc(const int type
, struct list_head
*element
)
25 struct tomoyo_gc
*entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
29 entry
->element
= element
;
30 list_add(&entry
->list
, &tomoyo_gc_queue
);
31 list_del_rcu(element
);
35 static void tomoyo_del_allow_read(struct list_head
*element
)
37 struct tomoyo_readable_file
*ptr
=
38 container_of(element
, typeof(*ptr
), head
.list
);
39 tomoyo_put_name(ptr
->filename
);
42 static void tomoyo_del_file_pattern(struct list_head
*element
)
44 struct tomoyo_no_pattern
*ptr
=
45 container_of(element
, typeof(*ptr
), head
.list
);
46 tomoyo_put_name(ptr
->pattern
);
49 static void tomoyo_del_no_rewrite(struct list_head
*element
)
51 struct tomoyo_no_rewrite
*ptr
=
52 container_of(element
, typeof(*ptr
), head
.list
);
53 tomoyo_put_name(ptr
->pattern
);
56 static void tomoyo_del_transition_control(struct list_head
*element
)
58 struct tomoyo_transition_control
*ptr
=
59 container_of(element
, typeof(*ptr
), head
.list
);
60 tomoyo_put_name(ptr
->domainname
);
61 tomoyo_put_name(ptr
->program
);
64 static void tomoyo_del_aggregator(struct list_head
*element
)
66 struct tomoyo_aggregator
*ptr
=
67 container_of(element
, typeof(*ptr
), head
.list
);
68 tomoyo_put_name(ptr
->original_name
);
69 tomoyo_put_name(ptr
->aggregated_name
);
72 static void tomoyo_del_manager(struct list_head
*element
)
74 struct tomoyo_manager
*ptr
=
75 container_of(element
, typeof(*ptr
), head
.list
);
76 tomoyo_put_name(ptr
->manager
);
79 static void tomoyo_del_acl(struct list_head
*element
)
81 struct tomoyo_acl_info
*acl
=
82 container_of(element
, typeof(*acl
), list
);
84 case TOMOYO_TYPE_PATH_ACL
:
86 struct tomoyo_path_acl
*entry
87 = container_of(acl
, typeof(*entry
), head
);
88 tomoyo_put_name_union(&entry
->name
);
91 case TOMOYO_TYPE_PATH2_ACL
:
93 struct tomoyo_path2_acl
*entry
94 = container_of(acl
, typeof(*entry
), head
);
95 tomoyo_put_name_union(&entry
->name1
);
96 tomoyo_put_name_union(&entry
->name2
);
99 case TOMOYO_TYPE_PATH_NUMBER_ACL
:
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
);
107 case TOMOYO_TYPE_MKDEV_ACL
:
109 struct tomoyo_mkdev_acl
*entry
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
);
117 case TOMOYO_TYPE_MOUNT_ACL
:
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
);
130 static bool tomoyo_del_domain(struct list_head
*element
)
132 struct tomoyo_domain_info
*domain
=
133 container_of(element
, typeof(*domain
), list
);
134 struct tomoyo_acl_info
*acl
;
135 struct tomoyo_acl_info
*tmp
;
137 * Since we don't protect whole execve() operation using SRCU,
138 * we need to recheck domain->users at this point.
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
149 * (7) Garbage collector waits for SRCU synchronization.
150 * (8) Garbage collector kfree() this domain because this domain is
152 * (9) Reader finishes execve() operation and restores this domain from
153 * "struct linux_binprm"->cred->security.
155 * By updating domain->users at (5), we can solve this race problem
156 * by rechecking domain->users at (8).
158 if (atomic_read(&domain
->users
))
160 list_for_each_entry_safe(acl
, tmp
, &domain
->acl_info_list
, list
) {
161 tomoyo_del_acl(&acl
->list
);
162 tomoyo_memory_free(acl
);
164 tomoyo_put_name(domain
->domainname
);
169 static void tomoyo_del_name(struct list_head
*element
)
171 const struct tomoyo_name
*ptr
=
172 container_of(element
, typeof(*ptr
), list
);
175 static void tomoyo_del_path_group(struct list_head
*element
)
177 struct tomoyo_path_group
*member
=
178 container_of(element
, typeof(*member
), head
.list
);
179 tomoyo_put_name(member
->member_name
);
182 static void tomoyo_del_group(struct list_head
*element
)
184 struct tomoyo_group
*group
=
185 container_of(element
, typeof(*group
), list
);
186 tomoyo_put_name(group
->group_name
);
189 static void tomoyo_del_number_group(struct list_head
*element
)
191 struct tomoyo_number_group
*member
=
192 container_of(element
, typeof(*member
), head
.list
);
195 static bool tomoyo_collect_member(struct list_head
*member_list
, int id
)
197 struct tomoyo_acl_head
*member
;
198 list_for_each_entry(member
, member_list
, list
) {
199 if (!member
->is_deleted
)
201 if (!tomoyo_add_to_gc(id
, &member
->list
))
207 static bool tomoyo_collect_acl(struct tomoyo_domain_info
*domain
)
209 struct tomoyo_acl_info
*acl
;
210 list_for_each_entry(acl
, &domain
->acl_info_list
, list
) {
211 if (!acl
->is_deleted
)
213 if (!tomoyo_add_to_gc(TOMOYO_ID_ACL
, &acl
->list
))
219 static void tomoyo_collect_entry(void)
222 if (mutex_lock_interruptible(&tomoyo_policy_lock
))
224 for (i
= 0; i
< TOMOYO_MAX_POLICY
; i
++) {
225 if (!tomoyo_collect_member(&tomoyo_policy_list
[i
], i
))
229 struct tomoyo_domain_info
*domain
;
230 list_for_each_entry_rcu(domain
, &tomoyo_domain_list
, list
) {
231 if (!tomoyo_collect_acl(domain
))
233 if (!domain
->is_deleted
|| atomic_read(&domain
->users
))
236 * Nobody is referring this domain. But somebody may
237 * refer this domain after successful execve().
238 * We recheck domain->users after SRCU synchronization.
240 if (!tomoyo_add_to_gc(TOMOYO_ID_DOMAIN
, &domain
->list
))
244 for (i
= 0; i
< TOMOYO_MAX_HASH
; i
++) {
245 struct tomoyo_name
*ptr
;
246 list_for_each_entry_rcu(ptr
, &tomoyo_name_list
[i
], list
) {
247 if (atomic_read(&ptr
->users
))
249 if (!tomoyo_add_to_gc(TOMOYO_ID_NAME
, &ptr
->list
))
253 for (i
= 0; i
< TOMOYO_MAX_GROUP
; i
++) {
254 struct list_head
*list
= &tomoyo_group_list
[i
];
256 struct tomoyo_group
*group
;
259 id
= TOMOYO_ID_PATH_GROUP
;
262 id
= TOMOYO_ID_NUMBER_GROUP
;
265 list_for_each_entry(group
, list
, list
) {
266 if (!tomoyo_collect_member(&group
->member_list
, id
))
268 if (!list_empty(&group
->member_list
) ||
269 atomic_read(&group
->users
))
271 if (!tomoyo_add_to_gc(TOMOYO_ID_GROUP
, &group
->list
))
276 mutex_unlock(&tomoyo_policy_lock
);
279 static void tomoyo_kfree_entry(void)
282 struct tomoyo_gc
*tmp
;
284 list_for_each_entry_safe(p
, tmp
, &tomoyo_gc_queue
, list
) {
285 struct list_head
*element
= p
->element
;
287 case TOMOYO_ID_TRANSITION_CONTROL
:
288 tomoyo_del_transition_control(element
);
290 case TOMOYO_ID_AGGREGATOR
:
291 tomoyo_del_aggregator(element
);
293 case TOMOYO_ID_GLOBALLY_READABLE
:
294 tomoyo_del_allow_read(element
);
296 case TOMOYO_ID_PATTERN
:
297 tomoyo_del_file_pattern(element
);
299 case TOMOYO_ID_NO_REWRITE
:
300 tomoyo_del_no_rewrite(element
);
302 case TOMOYO_ID_MANAGER
:
303 tomoyo_del_manager(element
);
306 tomoyo_del_name(element
);
309 tomoyo_del_acl(element
);
311 case TOMOYO_ID_DOMAIN
:
312 if (!tomoyo_del_domain(element
))
315 case TOMOYO_ID_PATH_GROUP
:
316 tomoyo_del_path_group(element
);
318 case TOMOYO_ID_GROUP
:
319 tomoyo_del_group(element
);
321 case TOMOYO_ID_NUMBER_GROUP
:
322 tomoyo_del_number_group(element
);
325 tomoyo_memory_free(element
);
331 static int tomoyo_gc_thread(void *unused
)
333 daemonize("GC for TOMOYO");
334 if (mutex_trylock(&tomoyo_gc_mutex
)) {
336 for (i
= 0; i
< 10; i
++) {
337 tomoyo_collect_entry();
338 if (list_empty(&tomoyo_gc_queue
))
340 synchronize_srcu(&tomoyo_ss
);
341 tomoyo_kfree_entry();
343 mutex_unlock(&tomoyo_gc_mutex
);
348 void tomoyo_run_gc(void)
350 struct task_struct
*task
= kthread_create(tomoyo_gc_thread
, NULL
,
353 wake_up_process(task
);