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