This patch contains code for handling numeric values grouping. To be able to specify value ranges more flexibly, I want to add numeric values grouping support into TOMOYO 2.3.0. number_group UIDS_ALLOWED_TO_EXECUTE_SHELL 100-2000 number_group UIDS_ALLOWED_TO_EXECUTE_SHELL 10000-20000 + allow_execute /bin/sh if task.uid=@UIDS_ALLOWED_TO_EXECUTE_SHELL number_group PERMISSIONS_FOR_CREATE 0644 number_group PERMISSIONS_FOR_CREATE 0600 + allow_create /tmp/file @PERMISSIONS_FOR_CREATE Signed-off-by: Tetsuo Handa --- security/tomoyo/number_group.c | 212 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) --- /dev/null +++ security-testing-2.6/security/tomoyo/number_group.c @@ -0,0 +1,212 @@ +/* + * security/tomoyo/number_group.c + * + * Copyright (C) 2005-2009 NTT DATA CORPORATION + */ +#include "internal.h" + +/* The list for "struct tomoyo_number_group". */ +LIST_HEAD(tomoyo_number_group_list); + +/** + * tomoyo_get_number_group - Allocate memory for "struct tomoyo_number_group". + * + * @group_name: The name of number group. + * + * Returns pointer to "struct tomoyo_number_group" on success, + * NULL otherwise. + */ +struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name) +{ + struct tomoyo_number_group *entry = NULL; + struct tomoyo_number_group *group; + const struct tomoyo_path_info *saved_group_name; + int error = -ENOMEM; + if (!tomoyo_is_correct_path(group_name, 0, 0, 0) || + !group_name[0]) + return NULL; + saved_group_name = tomoyo_get_name(group_name); + if (!saved_group_name) + return NULL; + entry = kzalloc(sizeof(*entry), GFP_KERNEL); + mutex_lock(&tomoyo_policy_lock); + list_for_each_entry_rcu(group, &tomoyo_number_group_list, list) { + if (saved_group_name != group->group_name) + continue; + atomic_inc(&group->users); + error = 0; + break; + } + if (error && tomoyo_memory_ok(entry, sizeof(*entry))) { + INIT_LIST_HEAD(&entry->member_list); + entry->group_name = saved_group_name; + saved_group_name = NULL; + atomic_set(&entry->users, 1); + list_add_tail_rcu(&entry->list, &tomoyo_number_group_list); + group = entry; + entry = NULL; + error = 0; + } + mutex_unlock(&tomoyo_policy_lock); + tomoyo_put_name(saved_group_name); + kfree(entry); + return !error ? group : NULL; +} + +/** + * tomoyo_put_number_group - Delete memory for "struct tomoyo_number_group". + * + * @group: Pointer to "struct tomoyo_number_group". + */ +void tomoyo_put_number_group(struct tomoyo_number_group *group) +{ + struct tomoyo_number_group_member *member; + struct tomoyo_number_group_member *next_member; + LIST_HEAD(q); + bool can_delete_group = false; + if (!group) + return; + mutex_lock(&tomoyo_policy_lock); + if (atomic_dec_and_test(&group->users)) { + list_for_each_entry_safe(member, next_member, + &group->member_list, list) { + if (!member->is_deleted) + break; + list_del(&member->list); + list_add(&member->list, &q); + } + if (list_empty(&group->member_list)) { + list_del(&group->list); + can_delete_group = true; + } + } + mutex_unlock(&tomoyo_policy_lock); + list_for_each_entry_safe(member, next_member, &q, list) { + list_del(&member->list); + tomoyo_memory_free(member, sizeof(*member)); + } + if (can_delete_group) { + tomoyo_put_name(group->group_name); + tomoyo_memory_free(group, sizeof(*group)); + } +} + +/** + * tomoyo_write_number_group_policy - Write "struct tomoyo_number_group" list. + * + * @data: String to parse. + * @is_delete: True if it is a delete request. + * + * Returns 0 on success, nagative value otherwise. + */ +int tomoyo_write_number_group_policy(char *data, const bool is_delete) +{ + struct tomoyo_number_group *group; + struct tomoyo_number_group_member *entry = NULL; + struct tomoyo_number_group_member e = { }; + struct tomoyo_number_group_member *member; + int error = is_delete ? -ENOENT : -ENOMEM; + char *w[2]; + if (!tomoyo_tokenize(data, w, sizeof(w))) + return -EINVAL; + if (!tomoyo_parse_number_union(w[1], &e.number)) + return -EINVAL; + if (e.number.is_group || e.number.values[0] > e.number.values[1]) { + tomoyo_put_number_union(&e.number); + return -EINVAL; + } + group = tomoyo_get_number_group(w[0]); + if (!group) + return -ENOMEM; + if (!is_delete) + entry = kmalloc(sizeof(e), GFP_KERNEL); + mutex_lock(&tomoyo_policy_lock); + list_for_each_entry_rcu(member, &group->member_list, list) { + if (memcmp(&member->number, &e.number, sizeof(e.number))) + continue; + member->is_deleted = is_delete; + error = 0; + break; + } + if (!is_delete && error && tomoyo_commit_ok(entry, &e, sizeof(e))) { + list_add_tail_rcu(&entry->list, &group->member_list); + entry = NULL; + error = 0; + } + mutex_unlock(&tomoyo_policy_lock); + tomoyo_put_number_group(group); + kfree(entry); + return error; +} + +/** + * tomoyo_read_number_group_policy - Read "struct tomoyo_number_group" list. + * + * @head: Pointer to "struct tomoyo_io_buffer". + * + * Returns true on success, false otherwise. + * + * Caller holds tomoyo_read_lock(). + */ +bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head) +{ + struct list_head *gpos; + struct list_head *mpos; + bool done = true; + list_for_each_cookie(gpos, head->read_var1, + &tomoyo_number_group_list) { + struct tomoyo_number_group *group; + const char *name; + group = list_entry(gpos, struct tomoyo_number_group, list); + name = group->group_name->name; + list_for_each_cookie(mpos, head->read_var2, + &group->member_list) { + int pos; + const struct tomoyo_number_group_member *member + = list_entry(mpos, + struct tomoyo_number_group_member, + list); + if (member->is_deleted) + continue; + pos = head->read_avail; + if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_NUMBER_GROUP + "%s", name) || + !tomoyo_print_number_union(head, &member->number) + || !tomoyo_io_printf(head, "\n")) { + head->read_avail = pos; + done = false; + break; + } + } + } + return done; +} + +/** + * tomoyo_number_matches_group - Check whether the given number matches members of the given number group. + * + * @min: Min number. + * @max: Max number. + * @group: Pointer to "struct tomoyo_number_group". + * + * Returns true if @min and @max partially overlaps @group, false otherwise. + * + * Caller holds tomoyo_read_lock(). + */ +bool tomoyo_number_matches_group(const unsigned long min, + const unsigned long max, + const struct tomoyo_number_group *group) +{ + struct tomoyo_number_group_member *member; + bool matched = false; + list_for_each_entry_rcu(member, &group->member_list, list) { + if (member->is_deleted) + continue; + if (min > member->number.values[1] || + max < member->number.values[0]) + continue; + matched = true; + break; + } + return matched; +} -- -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/