]>
Commit | Line | Data |
---|---|---|
4c3e9e2d TH |
1 | /* |
2 | * security/tomoyo/number_group.c | |
3 | * | |
4 | * Copyright (C) 2005-2009 NTT DATA CORPORATION | |
5 | */ | |
6 | ||
7 | #include <linux/slab.h> | |
8 | #include "common.h" | |
9 | ||
4c3e9e2d | 10 | /** |
a98aa4de | 11 | * tomoyo_get_group - Allocate memory for "struct tomoyo_number_group". |
4c3e9e2d TH |
12 | * |
13 | * @group_name: The name of number group. | |
14 | * | |
15 | * Returns pointer to "struct tomoyo_number_group" on success, | |
16 | * NULL otherwise. | |
17 | */ | |
a98aa4de | 18 | struct tomoyo_group *tomoyo_get_number_group(const char *group_name) |
4c3e9e2d | 19 | { |
a98aa4de TH |
20 | struct tomoyo_group *entry = NULL; |
21 | struct tomoyo_group *group = NULL; | |
4c3e9e2d TH |
22 | const struct tomoyo_path_info *saved_group_name; |
23 | int error = -ENOMEM; | |
75093152 | 24 | if (!tomoyo_correct_word(group_name)) |
4c3e9e2d TH |
25 | return NULL; |
26 | saved_group_name = tomoyo_get_name(group_name); | |
27 | if (!saved_group_name) | |
28 | return NULL; | |
29 | entry = kzalloc(sizeof(*entry), GFP_NOFS); | |
30 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) | |
31 | goto out; | |
a230f9e7 TH |
32 | list_for_each_entry_rcu(group, &tomoyo_group_list[TOMOYO_NUMBER_GROUP], |
33 | list) { | |
4c3e9e2d TH |
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); | |
a230f9e7 TH |
45 | list_add_tail_rcu(&entry->list, |
46 | &tomoyo_group_list[TOMOYO_NUMBER_GROUP]); | |
4c3e9e2d TH |
47 | group = entry; |
48 | entry = NULL; | |
49 | error = 0; | |
50 | } | |
51 | mutex_unlock(&tomoyo_policy_lock); | |
52 | out: | |
53 | tomoyo_put_name(saved_group_name); | |
54 | kfree(entry); | |
55 | return !error ? group : NULL; | |
56 | } | |
57 | ||
36f5e1ff TH |
58 | static bool tomoyo_same_number_group(const struct tomoyo_acl_head *a, |
59 | const struct tomoyo_acl_head *b) | |
60 | { | |
a98aa4de | 61 | return !memcmp(&container_of(a, struct tomoyo_number_group, |
36f5e1ff | 62 | head)->number, |
a98aa4de | 63 | &container_of(b, struct tomoyo_number_group, |
36f5e1ff TH |
64 | head)->number, |
65 | sizeof(container_of(a, | |
a98aa4de | 66 | struct tomoyo_number_group, |
36f5e1ff TH |
67 | head)->number)); |
68 | } | |
69 | ||
4c3e9e2d TH |
70 | /** |
71 | * tomoyo_write_number_group_policy - Write "struct tomoyo_number_group" list. | |
72 | * | |
73 | * @data: String to parse. | |
74 | * @is_delete: True if it is a delete request. | |
75 | * | |
76 | * Returns 0 on success, nagative value otherwise. | |
77 | */ | |
78 | int tomoyo_write_number_group_policy(char *data, const bool is_delete) | |
79 | { | |
a98aa4de TH |
80 | struct tomoyo_group *group; |
81 | struct tomoyo_number_group e = { }; | |
36f5e1ff | 82 | int error; |
4c3e9e2d TH |
83 | char *w[2]; |
84 | if (!tomoyo_tokenize(data, w, sizeof(w))) | |
85 | return -EINVAL; | |
36f5e1ff TH |
86 | if (w[1][0] == '@' || !tomoyo_parse_number_union(w[1], &e.number) || |
87 | e.number.values[0] > e.number.values[1]) | |
4c3e9e2d | 88 | return -EINVAL; |
4c3e9e2d TH |
89 | group = tomoyo_get_number_group(w[0]); |
90 | if (!group) | |
91 | return -ENOMEM; | |
36f5e1ff TH |
92 | error = tomoyo_update_policy(&e.head, sizeof(e), is_delete, |
93 | &group->member_list, | |
94 | tomoyo_same_number_group); | |
a98aa4de | 95 | tomoyo_put_group(group); |
4c3e9e2d TH |
96 | return error; |
97 | } | |
98 | ||
4c3e9e2d TH |
99 | /** |
100 | * tomoyo_number_matches_group - Check whether the given number matches members of the given number group. | |
101 | * | |
102 | * @min: Min number. | |
103 | * @max: Max number. | |
104 | * @group: Pointer to "struct tomoyo_number_group". | |
105 | * | |
106 | * Returns true if @min and @max partially overlaps @group, false otherwise. | |
107 | * | |
108 | * Caller holds tomoyo_read_lock(). | |
109 | */ | |
110 | bool tomoyo_number_matches_group(const unsigned long min, | |
111 | const unsigned long max, | |
a98aa4de | 112 | const struct tomoyo_group *group) |
4c3e9e2d | 113 | { |
a98aa4de | 114 | struct tomoyo_number_group *member; |
4c3e9e2d | 115 | bool matched = false; |
82e0f001 TH |
116 | list_for_each_entry_rcu(member, &group->member_list, head.list) { |
117 | if (member->head.is_deleted) | |
4c3e9e2d TH |
118 | continue; |
119 | if (min > member->number.values[1] || | |
120 | max < member->number.values[0]) | |
121 | continue; | |
122 | matched = true; | |
123 | break; | |
124 | } | |
125 | return matched; | |
126 | } |