| 1 | /* |
| 2 | * security/tomoyo/path_group.c |
| 3 | * |
| 4 | * Copyright (C) 2005-2009 NTT DATA CORPORATION |
| 5 | */ |
| 6 | |
| 7 | #include <linux/slab.h> |
| 8 | #include "common.h" |
| 9 | /* The list for "struct tomoyo_path_group". */ |
| 10 | LIST_HEAD(tomoyo_path_group_list); |
| 11 | |
| 12 | /** |
| 13 | * tomoyo_get_path_group - Allocate memory for "struct tomoyo_path_group". |
| 14 | * |
| 15 | * @group_name: The name of pathname group. |
| 16 | * |
| 17 | * Returns pointer to "struct tomoyo_path_group" on success, NULL otherwise. |
| 18 | */ |
| 19 | struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name) |
| 20 | { |
| 21 | struct tomoyo_path_group *entry = NULL; |
| 22 | struct tomoyo_path_group *group = NULL; |
| 23 | const struct tomoyo_path_info *saved_group_name; |
| 24 | int error = -ENOMEM; |
| 25 | if (!tomoyo_is_correct_word(group_name)) |
| 26 | return NULL; |
| 27 | saved_group_name = tomoyo_get_name(group_name); |
| 28 | if (!saved_group_name) |
| 29 | return NULL; |
| 30 | entry = kzalloc(sizeof(*entry), GFP_NOFS); |
| 31 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 32 | goto out; |
| 33 | list_for_each_entry_rcu(group, &tomoyo_path_group_list, list) { |
| 34 | if (saved_group_name != group->group_name) |
| 35 | continue; |
| 36 | atomic_inc(&group->users); |
| 37 | error = 0; |
| 38 | break; |
| 39 | } |
| 40 | if (error && tomoyo_memory_ok(entry)) { |
| 41 | INIT_LIST_HEAD(&entry->member_list); |
| 42 | entry->group_name = saved_group_name; |
| 43 | saved_group_name = NULL; |
| 44 | atomic_set(&entry->users, 1); |
| 45 | list_add_tail_rcu(&entry->list, &tomoyo_path_group_list); |
| 46 | group = entry; |
| 47 | entry = NULL; |
| 48 | error = 0; |
| 49 | } |
| 50 | mutex_unlock(&tomoyo_policy_lock); |
| 51 | out: |
| 52 | tomoyo_put_name(saved_group_name); |
| 53 | kfree(entry); |
| 54 | return !error ? group : NULL; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * tomoyo_write_path_group_policy - Write "struct tomoyo_path_group" list. |
| 59 | * |
| 60 | * @data: String to parse. |
| 61 | * @is_delete: True if it is a delete request. |
| 62 | * |
| 63 | * Returns 0 on success, nagative value otherwise. |
| 64 | */ |
| 65 | int tomoyo_write_path_group_policy(char *data, const bool is_delete) |
| 66 | { |
| 67 | struct tomoyo_path_group *group; |
| 68 | struct tomoyo_path_group_member *member; |
| 69 | struct tomoyo_path_group_member e = { }; |
| 70 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 71 | char *w[2]; |
| 72 | if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0]) |
| 73 | return -EINVAL; |
| 74 | group = tomoyo_get_path_group(w[0]); |
| 75 | if (!group) |
| 76 | return -ENOMEM; |
| 77 | e.member_name = tomoyo_get_name(w[1]); |
| 78 | if (!e.member_name) |
| 79 | goto out; |
| 80 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) |
| 81 | goto out; |
| 82 | list_for_each_entry_rcu(member, &group->member_list, head.list) { |
| 83 | if (member->member_name != e.member_name) |
| 84 | continue; |
| 85 | member->head.is_deleted = is_delete; |
| 86 | error = 0; |
| 87 | break; |
| 88 | } |
| 89 | if (!is_delete && error) { |
| 90 | struct tomoyo_path_group_member *entry = |
| 91 | tomoyo_commit_ok(&e, sizeof(e)); |
| 92 | if (entry) { |
| 93 | list_add_tail_rcu(&entry->head.list, |
| 94 | &group->member_list); |
| 95 | error = 0; |
| 96 | } |
| 97 | } |
| 98 | mutex_unlock(&tomoyo_policy_lock); |
| 99 | out: |
| 100 | tomoyo_put_name(e.member_name); |
| 101 | tomoyo_put_path_group(group); |
| 102 | return error; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * tomoyo_read_path_group_policy - Read "struct tomoyo_path_group" list. |
| 107 | * |
| 108 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 109 | * |
| 110 | * Returns true on success, false otherwise. |
| 111 | * |
| 112 | * Caller holds tomoyo_read_lock(). |
| 113 | */ |
| 114 | bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head) |
| 115 | { |
| 116 | struct list_head *gpos; |
| 117 | struct list_head *mpos; |
| 118 | list_for_each_cookie(gpos, head->read_var1, &tomoyo_path_group_list) { |
| 119 | struct tomoyo_path_group *group; |
| 120 | group = list_entry(gpos, struct tomoyo_path_group, list); |
| 121 | list_for_each_cookie(mpos, head->read_var2, |
| 122 | &group->member_list) { |
| 123 | struct tomoyo_path_group_member *member; |
| 124 | member = list_entry(mpos, |
| 125 | struct tomoyo_path_group_member, |
| 126 | head.list); |
| 127 | if (member->head.is_deleted) |
| 128 | continue; |
| 129 | if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_PATH_GROUP |
| 130 | "%s %s\n", |
| 131 | group->group_name->name, |
| 132 | member->member_name->name)) |
| 133 | return false; |
| 134 | } |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * tomoyo_path_matches_group - Check whether the given pathname matches members of the given pathname group. |
| 141 | * |
| 142 | * @pathname: The name of pathname. |
| 143 | * @group: Pointer to "struct tomoyo_path_group". |
| 144 | * |
| 145 | * Returns true if @pathname matches pathnames in @group, false otherwise. |
| 146 | * |
| 147 | * Caller holds tomoyo_read_lock(). |
| 148 | */ |
| 149 | bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname, |
| 150 | const struct tomoyo_path_group *group) |
| 151 | { |
| 152 | struct tomoyo_path_group_member *member; |
| 153 | bool matched = false; |
| 154 | list_for_each_entry_rcu(member, &group->member_list, head.list) { |
| 155 | if (member->head.is_deleted) |
| 156 | continue; |
| 157 | if (!tomoyo_path_matches_pattern(pathname, |
| 158 | member->member_name)) |
| 159 | continue; |
| 160 | matched = true; |
| 161 | break; |
| 162 | } |
| 163 | return matched; |
| 164 | } |