1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * security/tomoyo/gc.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2005-2011 NTT DATA CORPORATION
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include "common.h"
9*4882a593Smuzhiyun #include <linux/kthread.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun /**
13*4882a593Smuzhiyun * tomoyo_memory_free - Free memory for elements.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * @ptr: Pointer to allocated memory.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Returns nothing.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Caller holds tomoyo_policy_lock mutex.
20*4882a593Smuzhiyun */
tomoyo_memory_free(void * ptr)21*4882a593Smuzhiyun static inline void tomoyo_memory_free(void *ptr)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= ksize(ptr);
24*4882a593Smuzhiyun kfree(ptr);
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /* The list for "struct tomoyo_io_buffer". */
28*4882a593Smuzhiyun static LIST_HEAD(tomoyo_io_buffer_list);
29*4882a593Smuzhiyun /* Lock for protecting tomoyo_io_buffer_list. */
30*4882a593Smuzhiyun static DEFINE_SPINLOCK(tomoyo_io_buffer_list_lock);
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /**
33*4882a593Smuzhiyun * tomoyo_struct_used_by_io_buffer - Check whether the list element is used by /sys/kernel/security/tomoyo/ users or not.
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * Returns true if @element is used by /sys/kernel/security/tomoyo/ users,
38*4882a593Smuzhiyun * false otherwise.
39*4882a593Smuzhiyun */
tomoyo_struct_used_by_io_buffer(const struct list_head * element)40*4882a593Smuzhiyun static bool tomoyo_struct_used_by_io_buffer(const struct list_head *element)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun struct tomoyo_io_buffer *head;
43*4882a593Smuzhiyun bool in_use = false;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun spin_lock(&tomoyo_io_buffer_list_lock);
46*4882a593Smuzhiyun list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
47*4882a593Smuzhiyun head->users++;
48*4882a593Smuzhiyun spin_unlock(&tomoyo_io_buffer_list_lock);
49*4882a593Smuzhiyun mutex_lock(&head->io_sem);
50*4882a593Smuzhiyun if (head->r.domain == element || head->r.group == element ||
51*4882a593Smuzhiyun head->r.acl == element || &head->w.domain->list == element)
52*4882a593Smuzhiyun in_use = true;
53*4882a593Smuzhiyun mutex_unlock(&head->io_sem);
54*4882a593Smuzhiyun spin_lock(&tomoyo_io_buffer_list_lock);
55*4882a593Smuzhiyun head->users--;
56*4882a593Smuzhiyun if (in_use)
57*4882a593Smuzhiyun break;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun spin_unlock(&tomoyo_io_buffer_list_lock);
60*4882a593Smuzhiyun return in_use;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /**
64*4882a593Smuzhiyun * tomoyo_name_used_by_io_buffer - Check whether the string is used by /sys/kernel/security/tomoyo/ users or not.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * @string: String to check.
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * Returns true if @string is used by /sys/kernel/security/tomoyo/ users,
69*4882a593Smuzhiyun * false otherwise.
70*4882a593Smuzhiyun */
tomoyo_name_used_by_io_buffer(const char * string)71*4882a593Smuzhiyun static bool tomoyo_name_used_by_io_buffer(const char *string)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun struct tomoyo_io_buffer *head;
74*4882a593Smuzhiyun const size_t size = strlen(string) + 1;
75*4882a593Smuzhiyun bool in_use = false;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun spin_lock(&tomoyo_io_buffer_list_lock);
78*4882a593Smuzhiyun list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
79*4882a593Smuzhiyun int i;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun head->users++;
82*4882a593Smuzhiyun spin_unlock(&tomoyo_io_buffer_list_lock);
83*4882a593Smuzhiyun mutex_lock(&head->io_sem);
84*4882a593Smuzhiyun for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) {
85*4882a593Smuzhiyun const char *w = head->r.w[i];
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun if (w < string || w > string + size)
88*4882a593Smuzhiyun continue;
89*4882a593Smuzhiyun in_use = true;
90*4882a593Smuzhiyun break;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun mutex_unlock(&head->io_sem);
93*4882a593Smuzhiyun spin_lock(&tomoyo_io_buffer_list_lock);
94*4882a593Smuzhiyun head->users--;
95*4882a593Smuzhiyun if (in_use)
96*4882a593Smuzhiyun break;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun spin_unlock(&tomoyo_io_buffer_list_lock);
99*4882a593Smuzhiyun return in_use;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /**
103*4882a593Smuzhiyun * tomoyo_del_transition_control - Delete members in "struct tomoyo_transition_control".
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Returns nothing.
108*4882a593Smuzhiyun */
tomoyo_del_transition_control(struct list_head * element)109*4882a593Smuzhiyun static inline void tomoyo_del_transition_control(struct list_head *element)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun struct tomoyo_transition_control *ptr =
112*4882a593Smuzhiyun container_of(element, typeof(*ptr), head.list);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun tomoyo_put_name(ptr->domainname);
115*4882a593Smuzhiyun tomoyo_put_name(ptr->program);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun * tomoyo_del_aggregator - Delete members in "struct tomoyo_aggregator".
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun * Returns nothing.
124*4882a593Smuzhiyun */
tomoyo_del_aggregator(struct list_head * element)125*4882a593Smuzhiyun static inline void tomoyo_del_aggregator(struct list_head *element)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun struct tomoyo_aggregator *ptr =
128*4882a593Smuzhiyun container_of(element, typeof(*ptr), head.list);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun tomoyo_put_name(ptr->original_name);
131*4882a593Smuzhiyun tomoyo_put_name(ptr->aggregated_name);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /**
135*4882a593Smuzhiyun * tomoyo_del_manager - Delete members in "struct tomoyo_manager".
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
138*4882a593Smuzhiyun *
139*4882a593Smuzhiyun * Returns nothing.
140*4882a593Smuzhiyun */
tomoyo_del_manager(struct list_head * element)141*4882a593Smuzhiyun static inline void tomoyo_del_manager(struct list_head *element)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct tomoyo_manager *ptr =
144*4882a593Smuzhiyun container_of(element, typeof(*ptr), head.list);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun tomoyo_put_name(ptr->manager);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /**
150*4882a593Smuzhiyun * tomoyo_del_acl - Delete members in "struct tomoyo_acl_info".
151*4882a593Smuzhiyun *
152*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * Returns nothing.
155*4882a593Smuzhiyun */
tomoyo_del_acl(struct list_head * element)156*4882a593Smuzhiyun static void tomoyo_del_acl(struct list_head *element)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun struct tomoyo_acl_info *acl =
159*4882a593Smuzhiyun container_of(element, typeof(*acl), list);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun tomoyo_put_condition(acl->cond);
162*4882a593Smuzhiyun switch (acl->type) {
163*4882a593Smuzhiyun case TOMOYO_TYPE_PATH_ACL:
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun struct tomoyo_path_acl *entry
166*4882a593Smuzhiyun = container_of(acl, typeof(*entry), head);
167*4882a593Smuzhiyun tomoyo_put_name_union(&entry->name);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun break;
170*4882a593Smuzhiyun case TOMOYO_TYPE_PATH2_ACL:
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun struct tomoyo_path2_acl *entry
173*4882a593Smuzhiyun = container_of(acl, typeof(*entry), head);
174*4882a593Smuzhiyun tomoyo_put_name_union(&entry->name1);
175*4882a593Smuzhiyun tomoyo_put_name_union(&entry->name2);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun break;
178*4882a593Smuzhiyun case TOMOYO_TYPE_PATH_NUMBER_ACL:
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun struct tomoyo_path_number_acl *entry
181*4882a593Smuzhiyun = container_of(acl, typeof(*entry), head);
182*4882a593Smuzhiyun tomoyo_put_name_union(&entry->name);
183*4882a593Smuzhiyun tomoyo_put_number_union(&entry->number);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun break;
186*4882a593Smuzhiyun case TOMOYO_TYPE_MKDEV_ACL:
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun struct tomoyo_mkdev_acl *entry
189*4882a593Smuzhiyun = container_of(acl, typeof(*entry), head);
190*4882a593Smuzhiyun tomoyo_put_name_union(&entry->name);
191*4882a593Smuzhiyun tomoyo_put_number_union(&entry->mode);
192*4882a593Smuzhiyun tomoyo_put_number_union(&entry->major);
193*4882a593Smuzhiyun tomoyo_put_number_union(&entry->minor);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun break;
196*4882a593Smuzhiyun case TOMOYO_TYPE_MOUNT_ACL:
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun struct tomoyo_mount_acl *entry
199*4882a593Smuzhiyun = container_of(acl, typeof(*entry), head);
200*4882a593Smuzhiyun tomoyo_put_name_union(&entry->dev_name);
201*4882a593Smuzhiyun tomoyo_put_name_union(&entry->dir_name);
202*4882a593Smuzhiyun tomoyo_put_name_union(&entry->fs_type);
203*4882a593Smuzhiyun tomoyo_put_number_union(&entry->flags);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun break;
206*4882a593Smuzhiyun case TOMOYO_TYPE_ENV_ACL:
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun struct tomoyo_env_acl *entry =
209*4882a593Smuzhiyun container_of(acl, typeof(*entry), head);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun tomoyo_put_name(entry->env);
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun break;
214*4882a593Smuzhiyun case TOMOYO_TYPE_INET_ACL:
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun struct tomoyo_inet_acl *entry =
217*4882a593Smuzhiyun container_of(acl, typeof(*entry), head);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun tomoyo_put_group(entry->address.group);
220*4882a593Smuzhiyun tomoyo_put_number_union(&entry->port);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun break;
223*4882a593Smuzhiyun case TOMOYO_TYPE_UNIX_ACL:
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun struct tomoyo_unix_acl *entry =
226*4882a593Smuzhiyun container_of(acl, typeof(*entry), head);
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun tomoyo_put_name_union(&entry->name);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun break;
231*4882a593Smuzhiyun case TOMOYO_TYPE_MANUAL_TASK_ACL:
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun struct tomoyo_task_acl *entry =
234*4882a593Smuzhiyun container_of(acl, typeof(*entry), head);
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun tomoyo_put_name(entry->domainname);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun break;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun * tomoyo_del_domain - Delete members in "struct tomoyo_domain_info".
244*4882a593Smuzhiyun *
245*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
246*4882a593Smuzhiyun *
247*4882a593Smuzhiyun * Returns nothing.
248*4882a593Smuzhiyun *
249*4882a593Smuzhiyun * Caller holds tomoyo_policy_lock mutex.
250*4882a593Smuzhiyun */
tomoyo_del_domain(struct list_head * element)251*4882a593Smuzhiyun static inline void tomoyo_del_domain(struct list_head *element)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun struct tomoyo_domain_info *domain =
254*4882a593Smuzhiyun container_of(element, typeof(*domain), list);
255*4882a593Smuzhiyun struct tomoyo_acl_info *acl;
256*4882a593Smuzhiyun struct tomoyo_acl_info *tmp;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun /*
259*4882a593Smuzhiyun * Since this domain is referenced from neither
260*4882a593Smuzhiyun * "struct tomoyo_io_buffer" nor "struct cred"->security, we can delete
261*4882a593Smuzhiyun * elements without checking for is_deleted flag.
262*4882a593Smuzhiyun */
263*4882a593Smuzhiyun list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
264*4882a593Smuzhiyun tomoyo_del_acl(&acl->list);
265*4882a593Smuzhiyun tomoyo_memory_free(acl);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun tomoyo_put_name(domain->domainname);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /**
271*4882a593Smuzhiyun * tomoyo_del_condition - Delete members in "struct tomoyo_condition".
272*4882a593Smuzhiyun *
273*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
274*4882a593Smuzhiyun *
275*4882a593Smuzhiyun * Returns nothing.
276*4882a593Smuzhiyun */
tomoyo_del_condition(struct list_head * element)277*4882a593Smuzhiyun void tomoyo_del_condition(struct list_head *element)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun struct tomoyo_condition *cond = container_of(element, typeof(*cond),
280*4882a593Smuzhiyun head.list);
281*4882a593Smuzhiyun const u16 condc = cond->condc;
282*4882a593Smuzhiyun const u16 numbers_count = cond->numbers_count;
283*4882a593Smuzhiyun const u16 names_count = cond->names_count;
284*4882a593Smuzhiyun const u16 argc = cond->argc;
285*4882a593Smuzhiyun const u16 envc = cond->envc;
286*4882a593Smuzhiyun unsigned int i;
287*4882a593Smuzhiyun const struct tomoyo_condition_element *condp
288*4882a593Smuzhiyun = (const struct tomoyo_condition_element *) (cond + 1);
289*4882a593Smuzhiyun struct tomoyo_number_union *numbers_p
290*4882a593Smuzhiyun = (struct tomoyo_number_union *) (condp + condc);
291*4882a593Smuzhiyun struct tomoyo_name_union *names_p
292*4882a593Smuzhiyun = (struct tomoyo_name_union *) (numbers_p + numbers_count);
293*4882a593Smuzhiyun const struct tomoyo_argv *argv
294*4882a593Smuzhiyun = (const struct tomoyo_argv *) (names_p + names_count);
295*4882a593Smuzhiyun const struct tomoyo_envp *envp
296*4882a593Smuzhiyun = (const struct tomoyo_envp *) (argv + argc);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun for (i = 0; i < numbers_count; i++)
299*4882a593Smuzhiyun tomoyo_put_number_union(numbers_p++);
300*4882a593Smuzhiyun for (i = 0; i < names_count; i++)
301*4882a593Smuzhiyun tomoyo_put_name_union(names_p++);
302*4882a593Smuzhiyun for (i = 0; i < argc; argv++, i++)
303*4882a593Smuzhiyun tomoyo_put_name(argv->value);
304*4882a593Smuzhiyun for (i = 0; i < envc; envp++, i++) {
305*4882a593Smuzhiyun tomoyo_put_name(envp->name);
306*4882a593Smuzhiyun tomoyo_put_name(envp->value);
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /**
311*4882a593Smuzhiyun * tomoyo_del_name - Delete members in "struct tomoyo_name".
312*4882a593Smuzhiyun *
313*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
314*4882a593Smuzhiyun *
315*4882a593Smuzhiyun * Returns nothing.
316*4882a593Smuzhiyun */
tomoyo_del_name(struct list_head * element)317*4882a593Smuzhiyun static inline void tomoyo_del_name(struct list_head *element)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun /* Nothing to do. */
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /**
323*4882a593Smuzhiyun * tomoyo_del_path_group - Delete members in "struct tomoyo_path_group".
324*4882a593Smuzhiyun *
325*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
326*4882a593Smuzhiyun *
327*4882a593Smuzhiyun * Returns nothing.
328*4882a593Smuzhiyun */
tomoyo_del_path_group(struct list_head * element)329*4882a593Smuzhiyun static inline void tomoyo_del_path_group(struct list_head *element)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun struct tomoyo_path_group *member =
332*4882a593Smuzhiyun container_of(element, typeof(*member), head.list);
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun tomoyo_put_name(member->member_name);
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /**
338*4882a593Smuzhiyun * tomoyo_del_group - Delete "struct tomoyo_group".
339*4882a593Smuzhiyun *
340*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
341*4882a593Smuzhiyun *
342*4882a593Smuzhiyun * Returns nothing.
343*4882a593Smuzhiyun */
tomoyo_del_group(struct list_head * element)344*4882a593Smuzhiyun static inline void tomoyo_del_group(struct list_head *element)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun struct tomoyo_group *group =
347*4882a593Smuzhiyun container_of(element, typeof(*group), head.list);
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun tomoyo_put_name(group->group_name);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun /**
353*4882a593Smuzhiyun * tomoyo_del_address_group - Delete members in "struct tomoyo_address_group".
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * Returns nothing.
358*4882a593Smuzhiyun */
tomoyo_del_address_group(struct list_head * element)359*4882a593Smuzhiyun static inline void tomoyo_del_address_group(struct list_head *element)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun /* Nothing to do. */
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /**
365*4882a593Smuzhiyun * tomoyo_del_number_group - Delete members in "struct tomoyo_number_group".
366*4882a593Smuzhiyun *
367*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
368*4882a593Smuzhiyun *
369*4882a593Smuzhiyun * Returns nothing.
370*4882a593Smuzhiyun */
tomoyo_del_number_group(struct list_head * element)371*4882a593Smuzhiyun static inline void tomoyo_del_number_group(struct list_head *element)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun /* Nothing to do. */
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /**
377*4882a593Smuzhiyun * tomoyo_try_to_gc - Try to kfree() an entry.
378*4882a593Smuzhiyun *
379*4882a593Smuzhiyun * @type: One of values in "enum tomoyo_policy_id".
380*4882a593Smuzhiyun * @element: Pointer to "struct list_head".
381*4882a593Smuzhiyun *
382*4882a593Smuzhiyun * Returns nothing.
383*4882a593Smuzhiyun *
384*4882a593Smuzhiyun * Caller holds tomoyo_policy_lock mutex.
385*4882a593Smuzhiyun */
tomoyo_try_to_gc(const enum tomoyo_policy_id type,struct list_head * element)386*4882a593Smuzhiyun static void tomoyo_try_to_gc(const enum tomoyo_policy_id type,
387*4882a593Smuzhiyun struct list_head *element)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun /*
390*4882a593Smuzhiyun * __list_del_entry() guarantees that the list element became no longer
391*4882a593Smuzhiyun * reachable from the list which the element was originally on (e.g.
392*4882a593Smuzhiyun * tomoyo_domain_list). Also, synchronize_srcu() guarantees that the
393*4882a593Smuzhiyun * list element became no longer referenced by syscall users.
394*4882a593Smuzhiyun */
395*4882a593Smuzhiyun __list_del_entry(element);
396*4882a593Smuzhiyun mutex_unlock(&tomoyo_policy_lock);
397*4882a593Smuzhiyun synchronize_srcu(&tomoyo_ss);
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun * However, there are two users which may still be using the list
400*4882a593Smuzhiyun * element. We need to defer until both users forget this element.
401*4882a593Smuzhiyun *
402*4882a593Smuzhiyun * Don't kfree() until "struct tomoyo_io_buffer"->r.{domain,group,acl}
403*4882a593Smuzhiyun * and "struct tomoyo_io_buffer"->w.domain forget this element.
404*4882a593Smuzhiyun */
405*4882a593Smuzhiyun if (tomoyo_struct_used_by_io_buffer(element))
406*4882a593Smuzhiyun goto reinject;
407*4882a593Smuzhiyun switch (type) {
408*4882a593Smuzhiyun case TOMOYO_ID_TRANSITION_CONTROL:
409*4882a593Smuzhiyun tomoyo_del_transition_control(element);
410*4882a593Smuzhiyun break;
411*4882a593Smuzhiyun case TOMOYO_ID_MANAGER:
412*4882a593Smuzhiyun tomoyo_del_manager(element);
413*4882a593Smuzhiyun break;
414*4882a593Smuzhiyun case TOMOYO_ID_AGGREGATOR:
415*4882a593Smuzhiyun tomoyo_del_aggregator(element);
416*4882a593Smuzhiyun break;
417*4882a593Smuzhiyun case TOMOYO_ID_GROUP:
418*4882a593Smuzhiyun tomoyo_del_group(element);
419*4882a593Smuzhiyun break;
420*4882a593Smuzhiyun case TOMOYO_ID_PATH_GROUP:
421*4882a593Smuzhiyun tomoyo_del_path_group(element);
422*4882a593Smuzhiyun break;
423*4882a593Smuzhiyun case TOMOYO_ID_ADDRESS_GROUP:
424*4882a593Smuzhiyun tomoyo_del_address_group(element);
425*4882a593Smuzhiyun break;
426*4882a593Smuzhiyun case TOMOYO_ID_NUMBER_GROUP:
427*4882a593Smuzhiyun tomoyo_del_number_group(element);
428*4882a593Smuzhiyun break;
429*4882a593Smuzhiyun case TOMOYO_ID_CONDITION:
430*4882a593Smuzhiyun tomoyo_del_condition(element);
431*4882a593Smuzhiyun break;
432*4882a593Smuzhiyun case TOMOYO_ID_NAME:
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun * Don't kfree() until all "struct tomoyo_io_buffer"->r.w[]
435*4882a593Smuzhiyun * forget this element.
436*4882a593Smuzhiyun */
437*4882a593Smuzhiyun if (tomoyo_name_used_by_io_buffer
438*4882a593Smuzhiyun (container_of(element, typeof(struct tomoyo_name),
439*4882a593Smuzhiyun head.list)->entry.name))
440*4882a593Smuzhiyun goto reinject;
441*4882a593Smuzhiyun tomoyo_del_name(element);
442*4882a593Smuzhiyun break;
443*4882a593Smuzhiyun case TOMOYO_ID_ACL:
444*4882a593Smuzhiyun tomoyo_del_acl(element);
445*4882a593Smuzhiyun break;
446*4882a593Smuzhiyun case TOMOYO_ID_DOMAIN:
447*4882a593Smuzhiyun /*
448*4882a593Smuzhiyun * Don't kfree() until all "struct cred"->security forget this
449*4882a593Smuzhiyun * element.
450*4882a593Smuzhiyun */
451*4882a593Smuzhiyun if (atomic_read(&container_of
452*4882a593Smuzhiyun (element, typeof(struct tomoyo_domain_info),
453*4882a593Smuzhiyun list)->users))
454*4882a593Smuzhiyun goto reinject;
455*4882a593Smuzhiyun break;
456*4882a593Smuzhiyun case TOMOYO_MAX_POLICY:
457*4882a593Smuzhiyun break;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun mutex_lock(&tomoyo_policy_lock);
460*4882a593Smuzhiyun if (type == TOMOYO_ID_DOMAIN)
461*4882a593Smuzhiyun tomoyo_del_domain(element);
462*4882a593Smuzhiyun tomoyo_memory_free(element);
463*4882a593Smuzhiyun return;
464*4882a593Smuzhiyun reinject:
465*4882a593Smuzhiyun /*
466*4882a593Smuzhiyun * We can safely reinject this element here bacause
467*4882a593Smuzhiyun * (1) Appending list elements and removing list elements are protected
468*4882a593Smuzhiyun * by tomoyo_policy_lock mutex.
469*4882a593Smuzhiyun * (2) Only this function removes list elements and this function is
470*4882a593Smuzhiyun * exclusively executed by tomoyo_gc_mutex mutex.
471*4882a593Smuzhiyun * are true.
472*4882a593Smuzhiyun */
473*4882a593Smuzhiyun mutex_lock(&tomoyo_policy_lock);
474*4882a593Smuzhiyun list_add_rcu(element, element->prev);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /**
478*4882a593Smuzhiyun * tomoyo_collect_member - Delete elements with "struct tomoyo_acl_head".
479*4882a593Smuzhiyun *
480*4882a593Smuzhiyun * @id: One of values in "enum tomoyo_policy_id".
481*4882a593Smuzhiyun * @member_list: Pointer to "struct list_head".
482*4882a593Smuzhiyun *
483*4882a593Smuzhiyun * Returns nothing.
484*4882a593Smuzhiyun */
tomoyo_collect_member(const enum tomoyo_policy_id id,struct list_head * member_list)485*4882a593Smuzhiyun static void tomoyo_collect_member(const enum tomoyo_policy_id id,
486*4882a593Smuzhiyun struct list_head *member_list)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun struct tomoyo_acl_head *member;
489*4882a593Smuzhiyun struct tomoyo_acl_head *tmp;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun list_for_each_entry_safe(member, tmp, member_list, list) {
492*4882a593Smuzhiyun if (!member->is_deleted)
493*4882a593Smuzhiyun continue;
494*4882a593Smuzhiyun member->is_deleted = TOMOYO_GC_IN_PROGRESS;
495*4882a593Smuzhiyun tomoyo_try_to_gc(id, &member->list);
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /**
500*4882a593Smuzhiyun * tomoyo_collect_acl - Delete elements in "struct tomoyo_domain_info".
501*4882a593Smuzhiyun *
502*4882a593Smuzhiyun * @list: Pointer to "struct list_head".
503*4882a593Smuzhiyun *
504*4882a593Smuzhiyun * Returns nothing.
505*4882a593Smuzhiyun */
tomoyo_collect_acl(struct list_head * list)506*4882a593Smuzhiyun static void tomoyo_collect_acl(struct list_head *list)
507*4882a593Smuzhiyun {
508*4882a593Smuzhiyun struct tomoyo_acl_info *acl;
509*4882a593Smuzhiyun struct tomoyo_acl_info *tmp;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun list_for_each_entry_safe(acl, tmp, list, list) {
512*4882a593Smuzhiyun if (!acl->is_deleted)
513*4882a593Smuzhiyun continue;
514*4882a593Smuzhiyun acl->is_deleted = TOMOYO_GC_IN_PROGRESS;
515*4882a593Smuzhiyun tomoyo_try_to_gc(TOMOYO_ID_ACL, &acl->list);
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun /**
520*4882a593Smuzhiyun * tomoyo_collect_entry - Try to kfree() deleted elements.
521*4882a593Smuzhiyun *
522*4882a593Smuzhiyun * Returns nothing.
523*4882a593Smuzhiyun */
tomoyo_collect_entry(void)524*4882a593Smuzhiyun static void tomoyo_collect_entry(void)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun int i;
527*4882a593Smuzhiyun enum tomoyo_policy_id id;
528*4882a593Smuzhiyun struct tomoyo_policy_namespace *ns;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun mutex_lock(&tomoyo_policy_lock);
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun struct tomoyo_domain_info *domain;
533*4882a593Smuzhiyun struct tomoyo_domain_info *tmp;
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun list_for_each_entry_safe(domain, tmp, &tomoyo_domain_list,
536*4882a593Smuzhiyun list) {
537*4882a593Smuzhiyun tomoyo_collect_acl(&domain->acl_info_list);
538*4882a593Smuzhiyun if (!domain->is_deleted || atomic_read(&domain->users))
539*4882a593Smuzhiyun continue;
540*4882a593Smuzhiyun tomoyo_try_to_gc(TOMOYO_ID_DOMAIN, &domain->list);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
544*4882a593Smuzhiyun for (id = 0; id < TOMOYO_MAX_POLICY; id++)
545*4882a593Smuzhiyun tomoyo_collect_member(id, &ns->policy_list[id]);
546*4882a593Smuzhiyun for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++)
547*4882a593Smuzhiyun tomoyo_collect_acl(&ns->acl_group[i]);
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun struct tomoyo_shared_acl_head *ptr;
551*4882a593Smuzhiyun struct tomoyo_shared_acl_head *tmp;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun list_for_each_entry_safe(ptr, tmp, &tomoyo_condition_list,
554*4882a593Smuzhiyun list) {
555*4882a593Smuzhiyun if (atomic_read(&ptr->users) > 0)
556*4882a593Smuzhiyun continue;
557*4882a593Smuzhiyun atomic_set(&ptr->users, TOMOYO_GC_IN_PROGRESS);
558*4882a593Smuzhiyun tomoyo_try_to_gc(TOMOYO_ID_CONDITION, &ptr->list);
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
562*4882a593Smuzhiyun for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
563*4882a593Smuzhiyun struct list_head *list = &ns->group_list[i];
564*4882a593Smuzhiyun struct tomoyo_group *group;
565*4882a593Smuzhiyun struct tomoyo_group *tmp;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun switch (i) {
568*4882a593Smuzhiyun case 0:
569*4882a593Smuzhiyun id = TOMOYO_ID_PATH_GROUP;
570*4882a593Smuzhiyun break;
571*4882a593Smuzhiyun case 1:
572*4882a593Smuzhiyun id = TOMOYO_ID_NUMBER_GROUP;
573*4882a593Smuzhiyun break;
574*4882a593Smuzhiyun default:
575*4882a593Smuzhiyun id = TOMOYO_ID_ADDRESS_GROUP;
576*4882a593Smuzhiyun break;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun list_for_each_entry_safe(group, tmp, list, head.list) {
579*4882a593Smuzhiyun tomoyo_collect_member(id, &group->member_list);
580*4882a593Smuzhiyun if (!list_empty(&group->member_list) ||
581*4882a593Smuzhiyun atomic_read(&group->head.users) > 0)
582*4882a593Smuzhiyun continue;
583*4882a593Smuzhiyun atomic_set(&group->head.users,
584*4882a593Smuzhiyun TOMOYO_GC_IN_PROGRESS);
585*4882a593Smuzhiyun tomoyo_try_to_gc(TOMOYO_ID_GROUP,
586*4882a593Smuzhiyun &group->head.list);
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun for (i = 0; i < TOMOYO_MAX_HASH; i++) {
591*4882a593Smuzhiyun struct list_head *list = &tomoyo_name_list[i];
592*4882a593Smuzhiyun struct tomoyo_shared_acl_head *ptr;
593*4882a593Smuzhiyun struct tomoyo_shared_acl_head *tmp;
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun list_for_each_entry_safe(ptr, tmp, list, list) {
596*4882a593Smuzhiyun if (atomic_read(&ptr->users) > 0)
597*4882a593Smuzhiyun continue;
598*4882a593Smuzhiyun atomic_set(&ptr->users, TOMOYO_GC_IN_PROGRESS);
599*4882a593Smuzhiyun tomoyo_try_to_gc(TOMOYO_ID_NAME, &ptr->list);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun mutex_unlock(&tomoyo_policy_lock);
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun /**
606*4882a593Smuzhiyun * tomoyo_gc_thread - Garbage collector thread function.
607*4882a593Smuzhiyun *
608*4882a593Smuzhiyun * @unused: Unused.
609*4882a593Smuzhiyun *
610*4882a593Smuzhiyun * Returns 0.
611*4882a593Smuzhiyun */
tomoyo_gc_thread(void * unused)612*4882a593Smuzhiyun static int tomoyo_gc_thread(void *unused)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun /* Garbage collector thread is exclusive. */
615*4882a593Smuzhiyun static DEFINE_MUTEX(tomoyo_gc_mutex);
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun if (!mutex_trylock(&tomoyo_gc_mutex))
618*4882a593Smuzhiyun goto out;
619*4882a593Smuzhiyun tomoyo_collect_entry();
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun struct tomoyo_io_buffer *head;
622*4882a593Smuzhiyun struct tomoyo_io_buffer *tmp;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun spin_lock(&tomoyo_io_buffer_list_lock);
625*4882a593Smuzhiyun list_for_each_entry_safe(head, tmp, &tomoyo_io_buffer_list,
626*4882a593Smuzhiyun list) {
627*4882a593Smuzhiyun if (head->users)
628*4882a593Smuzhiyun continue;
629*4882a593Smuzhiyun list_del(&head->list);
630*4882a593Smuzhiyun kfree(head->read_buf);
631*4882a593Smuzhiyun kfree(head->write_buf);
632*4882a593Smuzhiyun kfree(head);
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun spin_unlock(&tomoyo_io_buffer_list_lock);
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun mutex_unlock(&tomoyo_gc_mutex);
637*4882a593Smuzhiyun out:
638*4882a593Smuzhiyun /* This acts as do_exit(0). */
639*4882a593Smuzhiyun return 0;
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun /**
643*4882a593Smuzhiyun * tomoyo_notify_gc - Register/unregister /sys/kernel/security/tomoyo/ users.
644*4882a593Smuzhiyun *
645*4882a593Smuzhiyun * @head: Pointer to "struct tomoyo_io_buffer".
646*4882a593Smuzhiyun * @is_register: True if register, false if unregister.
647*4882a593Smuzhiyun *
648*4882a593Smuzhiyun * Returns nothing.
649*4882a593Smuzhiyun */
tomoyo_notify_gc(struct tomoyo_io_buffer * head,const bool is_register)650*4882a593Smuzhiyun void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun bool is_write = false;
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun spin_lock(&tomoyo_io_buffer_list_lock);
655*4882a593Smuzhiyun if (is_register) {
656*4882a593Smuzhiyun head->users = 1;
657*4882a593Smuzhiyun list_add(&head->list, &tomoyo_io_buffer_list);
658*4882a593Smuzhiyun } else {
659*4882a593Smuzhiyun is_write = head->write_buf != NULL;
660*4882a593Smuzhiyun if (!--head->users) {
661*4882a593Smuzhiyun list_del(&head->list);
662*4882a593Smuzhiyun kfree(head->read_buf);
663*4882a593Smuzhiyun kfree(head->write_buf);
664*4882a593Smuzhiyun kfree(head);
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun spin_unlock(&tomoyo_io_buffer_list_lock);
668*4882a593Smuzhiyun if (is_write)
669*4882a593Smuzhiyun kthread_run(tomoyo_gc_thread, NULL, "GC for TOMOYO");
670*4882a593Smuzhiyun }
671