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