3 |
* |
* |
4 |
* Implementation of the Domain-Based Mandatory Access Control. |
* Implementation of the Domain-Based Mandatory Access Control. |
5 |
* |
* |
6 |
* Copyright (C) 2005-2007 NTT DATA CORPORATION |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
* |
* |
8 |
* Version: 1.4.2 2007/07/13 |
* Version: 1.6.2 2008/06/25 |
9 |
* |
* |
10 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
11 |
* See README.ccs for ChangeLog. |
* See README.ccs for ChangeLog. |
12 |
* |
* |
13 |
*/ |
*/ |
|
/***** TOMOYO Linux start. *****/ |
|
14 |
|
|
15 |
#include <linux/ccs_common.h> |
#include <linux/ccs_common.h> |
16 |
#include <linux/tomoyo.h> |
#include <linux/tomoyo.h> |
17 |
#include <linux/realpath.h> |
#include <linux/realpath.h> |
18 |
#include <net/ip.h> |
#include <linux/net.h> |
19 |
|
#include <linux/inet.h> |
20 |
|
#include <linux/in.h> |
21 |
|
#include <linux/in6.h> |
22 |
|
|
23 |
/************************* VARIABLES *************************/ |
/** |
24 |
|
* audit_network_log - Audit network log. |
25 |
extern struct semaphore domain_acl_lock; |
* |
26 |
|
* @is_ipv6: True if @address is an IPv6 address. |
27 |
/************************* AUDIT FUNCTIONS *************************/ |
* @operation: The name of operation. |
28 |
|
* @address: An IPv4 or IPv6 address. |
29 |
#ifdef CONFIG_TOMOYO_AUDIT |
* @port: Port number. |
30 |
static int AuditNetworkLog(const int is_ipv6, const char *operation, const u32 *address, const u16 port, const int is_granted) |
* @is_granted: True if this is a granted log. |
31 |
|
* @profile: Profile number used. |
32 |
|
* @mode: Access control mode used. |
33 |
|
* |
34 |
|
* Returns 0 on success, negative value otherwise. |
35 |
|
*/ |
36 |
|
static int audit_network_log(const bool is_ipv6, const char *operation, |
37 |
|
const char *address, const u16 port, |
38 |
|
const bool is_granted, |
39 |
|
const u8 profile, const u8 mode) |
40 |
{ |
{ |
41 |
char *buf; |
char *buf; |
42 |
int len = 256; |
int len = 256; |
43 |
if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM; |
int len2; |
44 |
if ((buf = InitAuditLog(&len)) == NULL) return -ENOMEM; |
if (ccs_can_save_audit_log(is_granted) < 0) |
45 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation); |
return -ENOMEM; |
46 |
if (is_ipv6) { |
buf = ccs_init_audit_log(&len, profile, mode, NULL); |
47 |
print_ipv6(buf + strlen(buf), len - strlen(buf), (const u16 *) address); |
if (!buf) |
48 |
} else { |
return -ENOMEM; |
49 |
u32 ip = *address; |
len2 = strlen(buf); |
50 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip)); |
snprintf(buf + len2, len - len2 - 1, KEYWORD_ALLOW_NETWORK "%s %s %u\n", |
51 |
} |
operation, address, port); |
52 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, " %u\n", port); |
return ccs_write_audit_log(buf, is_granted); |
|
return WriteAuditLog(buf, is_granted); |
|
53 |
} |
} |
|
#else |
|
|
static inline void AuditNetworkLog(const int is_ipv6, const char *operation, const u32 *address, const u16 port, const int is_granted) {} |
|
|
#endif |
|
54 |
|
|
55 |
/************************* ADDRESS GROUP HANDLER *************************/ |
/** |
56 |
|
* save_ipv6_address - Keep the given IPv6 address on the RAM. |
57 |
|
* |
58 |
|
* @addr: Pointer to "struct in6_addr". |
59 |
|
* |
60 |
|
* Returns pointer to "struct in6_addr" on success, NULL otherwise. |
61 |
|
* |
62 |
|
* The RAM is shared, so NEVER try to modify or kfree() the returned address. |
63 |
|
*/ |
64 |
|
static const struct in6_addr *save_ipv6_address(const struct in6_addr *addr) |
65 |
|
{ |
66 |
|
static const u8 block_size = 16; |
67 |
|
struct addr_list { |
68 |
|
struct in6_addr addr[block_size]; |
69 |
|
struct list1_head list; |
70 |
|
u32 in_use_count; |
71 |
|
}; |
72 |
|
static LIST1_HEAD(address_list); |
73 |
|
struct addr_list *ptr; |
74 |
|
static DEFINE_MUTEX(lock); |
75 |
|
u8 i = block_size; |
76 |
|
if (!addr) |
77 |
|
return NULL; |
78 |
|
mutex_lock(&lock); |
79 |
|
list1_for_each_entry(ptr, &address_list, list) { |
80 |
|
for (i = 0; i < ptr->in_use_count; i++) { |
81 |
|
if (!memcmp(&ptr->addr[i], addr, sizeof(*addr))) |
82 |
|
goto ok; |
83 |
|
} |
84 |
|
if (i < block_size) |
85 |
|
break; |
86 |
|
} |
87 |
|
if (i == block_size) { |
88 |
|
ptr = ccs_alloc_element(sizeof(*ptr)); |
89 |
|
if (!ptr) |
90 |
|
goto ok; |
91 |
|
list1_add_tail_mb(&ptr->list, &address_list); |
92 |
|
i = 0; |
93 |
|
} |
94 |
|
ptr->addr[ptr->in_use_count++] = *addr; |
95 |
|
ok: |
96 |
|
mutex_unlock(&lock); |
97 |
|
return ptr ? &ptr->addr[i] : NULL; |
98 |
|
} |
99 |
|
|
100 |
static struct address_group_entry *group_list = NULL; |
/* The list for "struct address_group_entry". */ |
101 |
|
static LIST1_HEAD(address_group_list); |
102 |
|
|
103 |
static int AddAddressGroupEntry(const char *group_name, const u8 is_ipv6, const u16 *min_address, const u16 *max_address, const int is_delete) |
/** |
104 |
|
* update_address_group_entry - Update "struct address_group_entry" list. |
105 |
|
* |
106 |
|
* @group_name: The name of address group. |
107 |
|
* @is_ipv6: True if @min_address and @max_address are IPv6 addresses. |
108 |
|
* @min_address: Start of IPv4 or IPv6 address range. |
109 |
|
* @max_address: End of IPv4 or IPv6 address range. |
110 |
|
* @is_delete: True if it is a delete request. |
111 |
|
* |
112 |
|
* Returns 0 on success, negative value otherwise. |
113 |
|
*/ |
114 |
|
static int update_address_group_entry(const char *group_name, |
115 |
|
const bool is_ipv6, |
116 |
|
const u16 *min_address, |
117 |
|
const u16 *max_address, |
118 |
|
const bool is_delete) |
119 |
{ |
{ |
120 |
static DECLARE_MUTEX(lock); |
static DEFINE_MUTEX(lock); |
121 |
struct address_group_entry *new_group, *group; |
struct address_group_entry *new_group; |
122 |
struct address_group_member *new_member, *member; |
struct address_group_entry *group; |
123 |
|
struct address_group_member *new_member; |
124 |
|
struct address_group_member *member; |
125 |
const struct path_info *saved_group_name; |
const struct path_info *saved_group_name; |
126 |
|
const struct in6_addr *saved_min_address = NULL; |
127 |
|
const struct in6_addr *saved_max_address = NULL; |
128 |
int error = -ENOMEM; |
int error = -ENOMEM; |
129 |
if (!IsCorrectPath(group_name, 0, 0, 0, __FUNCTION__) || !group_name[0]) return -EINVAL; |
bool found = false; |
130 |
if ((saved_group_name = SaveName(group_name)) == NULL) return -ENOMEM; |
if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) || |
131 |
down(&lock); |
!group_name[0]) |
132 |
for (group = group_list; group; group = group->next) { |
return -EINVAL; |
133 |
if (saved_group_name != group->group_name) continue; |
saved_group_name = ccs_save_name(group_name); |
134 |
for (member = group->first_member; member; member = member->next) { |
if (!saved_group_name) |
135 |
if (member->is_ipv6 != is_ipv6) continue; |
return -ENOMEM; |
136 |
|
if (!is_ipv6) |
137 |
|
goto not_ipv6; |
138 |
|
saved_min_address |
139 |
|
= save_ipv6_address((struct in6_addr *) min_address); |
140 |
|
saved_max_address |
141 |
|
= save_ipv6_address((struct in6_addr *) max_address); |
142 |
|
if (!saved_min_address || !saved_max_address) |
143 |
|
return -ENOMEM; |
144 |
|
not_ipv6: |
145 |
|
mutex_lock(&lock); |
146 |
|
list1_for_each_entry(group, &address_group_list, list) { |
147 |
|
if (saved_group_name != group->group_name) |
148 |
|
continue; |
149 |
|
list1_for_each_entry(member, &group->address_group_member_list, |
150 |
|
list) { |
151 |
|
if (member->is_ipv6 != is_ipv6) |
152 |
|
continue; |
153 |
if (is_ipv6) { |
if (is_ipv6) { |
154 |
if (memcmp(member->min.ipv6, min_address, 16) || memcmp(member->max.ipv6, max_address, 16)) continue; |
if (member->min.ipv6 != saved_min_address || |
155 |
|
member->max.ipv6 != saved_max_address) |
156 |
|
continue; |
157 |
} else { |
} else { |
158 |
if (member->min.ipv4 != * (u32 *) min_address || member->max.ipv4 != * (u32 *) max_address) continue; |
if (member->min.ipv4 != *(u32 *) min_address || |
159 |
|
member->max.ipv4 != *(u32 *) max_address) |
160 |
|
continue; |
161 |
} |
} |
162 |
member->is_deleted = is_delete; |
member->is_deleted = is_delete; |
163 |
error = 0; |
error = 0; |
164 |
goto out; |
goto out; |
165 |
} |
} |
166 |
|
found = true; |
167 |
break; |
break; |
168 |
} |
} |
169 |
if (is_delete) { |
if (is_delete) { |
170 |
error = -ENOENT; |
error = -ENOENT; |
171 |
goto out; |
goto out; |
172 |
} |
} |
173 |
if (!group) { |
if (!found) { |
174 |
if ((new_group = alloc_element(sizeof(*new_group))) == NULL) goto out; |
new_group = ccs_alloc_element(sizeof(*new_group)); |
175 |
|
if (!new_group) |
176 |
|
goto out; |
177 |
|
INIT_LIST1_HEAD(&new_group->address_group_member_list); |
178 |
new_group->group_name = saved_group_name; |
new_group->group_name = saved_group_name; |
179 |
mb(); /* Instead of using spinlock. */ |
list1_add_tail_mb(&new_group->list, &address_group_list); |
|
if ((group = group_list) != NULL) { |
|
|
while (group->next) group = group->next; group->next = new_group; |
|
|
} else { |
|
|
group_list= new_group; |
|
|
} |
|
180 |
group = new_group; |
group = new_group; |
181 |
} |
} |
182 |
if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out; |
new_member = ccs_alloc_element(sizeof(*new_member)); |
183 |
|
if (!new_member) |
184 |
|
goto out; |
185 |
new_member->is_ipv6 = is_ipv6; |
new_member->is_ipv6 = is_ipv6; |
186 |
if (is_ipv6) { |
if (is_ipv6) { |
187 |
memmove(new_member->min.ipv6, min_address, 16); |
new_member->min.ipv6 = saved_min_address; |
188 |
memmove(new_member->max.ipv6, max_address, 16); |
new_member->max.ipv6 = saved_max_address; |
189 |
} else { |
} else { |
190 |
new_member->min.ipv4 = * (u32 *) min_address; |
new_member->min.ipv4 = *(u32 *) min_address; |
191 |
new_member->max.ipv4 = * (u32 *) max_address; |
new_member->max.ipv4 = *(u32 *) max_address; |
|
} |
|
|
mb(); /* Instead of using spinlock. */ |
|
|
if ((member = group->first_member) != NULL) { |
|
|
while (member->next) member = member->next; member->next = new_member; |
|
|
} else { |
|
|
group->first_member = new_member; |
|
192 |
} |
} |
193 |
|
list1_add_tail_mb(&new_member->list, &group->address_group_member_list); |
194 |
error = 0; |
error = 0; |
195 |
out: |
out: |
196 |
up(&lock); |
mutex_unlock(&lock); |
197 |
|
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
198 |
return error; |
return error; |
199 |
} |
} |
200 |
|
|
201 |
int AddAddressGroupPolicy(char *data, const int is_delete) |
/** |
202 |
|
* ccs_write_address_group_policy - Write "struct address_group_entry" list. |
203 |
|
* |
204 |
|
* @data: String to parse. |
205 |
|
* @is_delete: True if it is a delete request. |
206 |
|
* |
207 |
|
* Returns 0 on success, negative value otherwise. |
208 |
|
*/ |
209 |
|
int ccs_write_address_group_policy(char *data, const bool is_delete) |
210 |
{ |
{ |
211 |
int count, is_ipv6; |
u8 count; |
212 |
u16 min_address[8], max_address[8]; |
bool is_ipv6; |
213 |
|
u16 min_address[8]; |
214 |
|
u16 max_address[8]; |
215 |
char *cp = strchr(data, ' '); |
char *cp = strchr(data, ' '); |
216 |
if (!cp) return -EINVAL; |
if (!cp) |
217 |
|
return -EINVAL; |
218 |
*cp++ = '\0'; |
*cp++ = '\0'; |
219 |
if ((count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
220 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
221 |
&min_address[4], &min_address[5], &min_address[6], &min_address[7], |
&min_address[0], &min_address[1], |
222 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3], |
&min_address[2], &min_address[3], |
223 |
&max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) { |
&min_address[4], &min_address[5], |
224 |
int i; |
&min_address[6], &min_address[7], |
225 |
|
&max_address[0], &max_address[1], |
226 |
|
&max_address[2], &max_address[3], |
227 |
|
&max_address[4], &max_address[5], |
228 |
|
&max_address[6], &max_address[7]); |
229 |
|
if (count == 8 || count == 16) { |
230 |
|
u8 i; |
231 |
for (i = 0; i < 8; i++) { |
for (i = 0; i < 8; i++) { |
232 |
min_address[i] = htons(min_address[i]); |
min_address[i] = htons(min_address[i]); |
233 |
max_address[i] = htons(max_address[i]); |
max_address[i] = htons(max_address[i]); |
234 |
} |
} |
235 |
if (count == 8) memmove(max_address, min_address, sizeof(min_address)); |
if (count == 8) |
236 |
is_ipv6 = 1; |
memmove(max_address, min_address, sizeof(min_address)); |
237 |
} else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
is_ipv6 = true; |
238 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
goto ok; |
239 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) { |
} |
240 |
u32 ip = ((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]); |
count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
241 |
* (u32 *) min_address = ip; |
&min_address[0], &min_address[1], |
242 |
if (count == 8) ip = ((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]); |
&min_address[2], &min_address[3], |
243 |
* (u32 *) max_address = ip; |
&max_address[0], &max_address[1], |
244 |
is_ipv6 = 0; |
&max_address[2], &max_address[3]); |
245 |
} else { |
if (count == 4 || count == 8) { |
246 |
return -EINVAL; |
u32 ip = ((((u8) min_address[0]) << 24) |
247 |
|
+ (((u8) min_address[1]) << 16) |
248 |
|
+ (((u8) min_address[2]) << 8) |
249 |
|
+ (u8) min_address[3]); |
250 |
|
*(u32 *) min_address = ip; |
251 |
|
if (count == 8) |
252 |
|
ip = ((((u8) max_address[0]) << 24) |
253 |
|
+ (((u8) max_address[1]) << 16) |
254 |
|
+ (((u8) max_address[2]) << 8) |
255 |
|
+ (u8) max_address[3]); |
256 |
|
*(u32 *) max_address = ip; |
257 |
|
is_ipv6 = false; |
258 |
|
goto ok; |
259 |
} |
} |
260 |
return AddAddressGroupEntry(data, is_ipv6, min_address, max_address, is_delete); |
return -EINVAL; |
261 |
|
ok: |
262 |
|
return update_address_group_entry(data, is_ipv6, |
263 |
|
min_address, max_address, is_delete); |
264 |
} |
} |
265 |
|
|
266 |
static struct address_group_entry *FindOrAssignNewAddressGroup(const char *group_name) |
/** |
267 |
|
* find_or_assign_new_address_group - Create address group. |
268 |
|
* |
269 |
|
* @group_name: The name of address group. |
270 |
|
* |
271 |
|
* Returns pointer to "struct address_group_entry" on success, NULL otherwise. |
272 |
|
*/ |
273 |
|
static struct address_group_entry * |
274 |
|
find_or_assign_new_address_group(const char *group_name) |
275 |
{ |
{ |
276 |
int i; |
u8 i; |
277 |
struct address_group_entry *group; |
struct address_group_entry *group; |
278 |
for (i = 0; i <= 1; i++) { |
for (i = 0; i <= 1; i++) { |
279 |
for (group = group_list; group; group = group->next) { |
list1_for_each_entry(group, &address_group_list, list) { |
280 |
if (strcmp(group_name, group->group_name->name) == 0) return group; |
if (!strcmp(group_name, group->group_name->name)) |
281 |
|
return group; |
282 |
} |
} |
283 |
if (i == 0) { |
if (!i) { |
284 |
const u16 dummy[2] = { 0, 0 }; |
const u16 dummy[2] = { 0, 0 }; |
285 |
AddAddressGroupEntry(group_name, 0, dummy, dummy, 0); |
update_address_group_entry(group_name, false, |
286 |
AddAddressGroupEntry(group_name, 0, dummy, dummy, 1); |
dummy, dummy, false); |
287 |
|
update_address_group_entry(group_name, false, |
288 |
|
dummy, dummy, true); |
289 |
} |
} |
290 |
} |
} |
291 |
return NULL; |
return NULL; |
292 |
} |
} |
293 |
|
|
294 |
static int AddressMatchesToGroup(const u8 is_ipv6, const u32 *address, const struct address_group_entry *group) |
/** |
295 |
|
* address_matches_to_group - Check whether the given address matches members of the given address group. |
296 |
|
* |
297 |
|
* @is_ipv6: True if @address is an IPv6 address. |
298 |
|
* @address: An IPv4 or IPv6 address. |
299 |
|
* @group: Pointer to "struct address_group_entry". |
300 |
|
* |
301 |
|
* Returns true if @address matches addresses in @group group, false otherwise. |
302 |
|
*/ |
303 |
|
static bool address_matches_to_group(const bool is_ipv6, const u32 *address, |
304 |
|
const struct address_group_entry *group) |
305 |
{ |
{ |
306 |
struct address_group_member *member; |
struct address_group_member *member; |
307 |
const u32 ip = ntohl(*address); |
const u32 ip = ntohl(*address); |
308 |
for (member = group->first_member; member; member = member->next) { |
list1_for_each_entry(member, &group->address_group_member_list, list) { |
309 |
if (member->is_deleted) continue; |
if (member->is_deleted) |
310 |
|
continue; |
311 |
if (member->is_ipv6) { |
if (member->is_ipv6) { |
312 |
if (is_ipv6 && memcmp(member->min.ipv6, address, 16) <= 0 && memcmp(address, member->max.ipv6, 16) <= 0) return 1; |
if (is_ipv6 && |
313 |
|
memcmp(member->min.ipv6, address, 16) <= 0 && |
314 |
|
memcmp(address, member->max.ipv6, 16) <= 0) |
315 |
|
return true; |
316 |
} else { |
} else { |
317 |
if (!is_ipv6 && member->min.ipv4 <= ip && ip <= member->max.ipv4) return 1; |
if (!is_ipv6 && |
318 |
|
member->min.ipv4 <= ip && ip <= member->max.ipv4) |
319 |
|
return true; |
320 |
} |
} |
321 |
} |
} |
322 |
return 0; |
return false; |
323 |
} |
} |
324 |
|
|
325 |
int ReadAddressGroupPolicy(struct io_buffer *head) |
/** |
326 |
|
* ccs_read_address_group_policy - Read "struct address_group_entry" list. |
327 |
|
* |
328 |
|
* @head: Pointer to "struct ccs_io_buffer". |
329 |
|
* |
330 |
|
* Returns true on success, false otherwise. |
331 |
|
*/ |
332 |
|
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
333 |
{ |
{ |
334 |
struct address_group_entry *group = head->read_var1; |
struct list1_head *gpos; |
335 |
struct address_group_member *member = head->read_var2; |
struct list1_head *mpos; |
336 |
if (!group) group = group_list; |
list1_for_each_cookie(gpos, head->read_var1, &address_group_list) { |
337 |
while (group) { |
struct address_group_entry *group; |
338 |
head->read_var1 = group; |
group = list1_entry(gpos, struct address_group_entry, list); |
339 |
if (!member) member = group->first_member; |
list1_for_each_cookie(mpos, head->read_var2, |
340 |
while (member) { |
&group->address_group_member_list) { |
341 |
head->read_var2 = member; |
char buf[128]; |
342 |
if (!member->is_deleted) { |
struct address_group_member *member; |
343 |
char buf[128]; |
member = list1_entry(mpos, struct address_group_member, |
344 |
if (member->is_ipv6) { |
list); |
345 |
const u16 *min_address = member->min.ipv6, *max_address = member->max.ipv6; |
if (member->is_deleted) |
346 |
print_ipv6(buf, sizeof(buf), min_address); |
continue; |
347 |
if (memcmp(min_address, max_address, 16)) { |
if (member->is_ipv6) { |
348 |
char *cp = strchr(buf, '\0'); |
const struct in6_addr *min_address |
349 |
*cp++ = '-'; |
= member->min.ipv6; |
350 |
print_ipv6(cp, sizeof(buf) - strlen(buf), max_address); |
const struct in6_addr *max_address |
351 |
} |
= member->max.ipv6; |
352 |
} else { |
ccs_print_ipv6(buf, sizeof(buf), min_address); |
353 |
const u32 min_address = member->min.ipv4, max_address = member->max.ipv4; |
if (min_address != max_address) { |
354 |
memset(buf, 0, sizeof(buf)); |
int len; |
355 |
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(min_address)); |
char *cp = strchr(buf, '\0'); |
356 |
if (min_address != max_address) { |
*cp++ = '-'; |
357 |
const int len = strlen(buf); |
len = strlen(buf); |
358 |
snprintf(buf + len, sizeof(buf) - 1 - len, "-%u.%u.%u.%u", HIPQUAD(max_address)); |
ccs_print_ipv6(cp, sizeof(buf) - len, |
359 |
} |
max_address); |
360 |
|
} |
361 |
|
} else { |
362 |
|
const u32 min_address = member->min.ipv4; |
363 |
|
const u32 max_address = member->max.ipv4; |
364 |
|
memset(buf, 0, sizeof(buf)); |
365 |
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", |
366 |
|
HIPQUAD(min_address)); |
367 |
|
if (min_address != max_address) { |
368 |
|
const int len = strlen(buf); |
369 |
|
snprintf(buf + len, |
370 |
|
sizeof(buf) - 1 - len, |
371 |
|
"-%u.%u.%u.%u", |
372 |
|
HIPQUAD(max_address)); |
373 |
} |
} |
|
if (io_printf(head, KEYWORD_ADDRESS_GROUP "%s %s\n", group->group_name->name, buf)) break; |
|
374 |
} |
} |
375 |
member = member->next; |
if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP |
376 |
|
"%s %s\n", group->group_name->name, |
377 |
|
buf)) |
378 |
|
goto out; |
379 |
} |
} |
|
if (member) break; |
|
|
head->read_var2 = NULL; |
|
|
group = group->next; |
|
380 |
} |
} |
381 |
return group ? -ENOMEM : 0; |
return true; |
382 |
|
out: |
383 |
|
return false; |
384 |
} |
} |
385 |
|
|
386 |
/************************* NETWORK NETWORK ACL HANDLER *************************/ |
#if !defined(NIP6) |
387 |
|
#define NIP6(addr) \ |
388 |
|
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
389 |
|
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
390 |
|
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
391 |
|
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
392 |
|
#endif |
393 |
|
|
394 |
char *print_ipv6(char *buffer, const int buffer_len, const u16 *ip) |
/** |
395 |
|
* ccs_print_ipv6 - Print an IPv6 address. |
396 |
|
* |
397 |
|
* @buffer: Buffer to write to. |
398 |
|
* @buffer_len: Size of @buffer. |
399 |
|
* @ip: Pointer to "struct in6_addr". |
400 |
|
* |
401 |
|
* Returns nothing. |
402 |
|
*/ |
403 |
|
void ccs_print_ipv6(char *buffer, const int buffer_len, |
404 |
|
const struct in6_addr *ip) |
405 |
{ |
{ |
406 |
memset(buffer, 0, buffer_len); |
memset(buffer, 0, buffer_len); |
407 |
snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", ntohs(ip[0]), ntohs(ip[1]), ntohs(ip[2]), ntohs(ip[3]), ntohs(ip[4]), ntohs(ip[5]), ntohs(ip[6]), ntohs(ip[7])); |
snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip)); |
|
return buffer; |
|
408 |
} |
} |
409 |
|
|
410 |
const char *network2keyword(const unsigned int operation) |
/** |
411 |
|
* ccs_net2keyword - Convert network operation index to network operation name. |
412 |
|
* |
413 |
|
* @operation: Type of operation. |
414 |
|
* |
415 |
|
* Returns the name of operation. |
416 |
|
*/ |
417 |
|
const char *ccs_net2keyword(const u8 operation) |
418 |
{ |
{ |
419 |
const char *keyword = "unknown"; |
const char *keyword = "unknown"; |
420 |
switch (operation) { |
switch (operation) { |
446 |
return keyword; |
return keyword; |
447 |
} |
} |
448 |
|
|
449 |
static int AddNetworkEntry(const u8 operation, const u8 record_type, const struct address_group_entry *group, const u32 *min_address, const u32 *max_address, const u16 min_port, const u16 max_port, struct domain_info *domain, const u8 is_add, const struct condition_list *condition) |
/** |
450 |
|
* update_network_entry - Update "struct ip_network_acl_record" list. |
451 |
|
* |
452 |
|
* @operation: Type of operation. |
453 |
|
* @record_type: Type of address. |
454 |
|
* @group: Pointer to "struct address_group_entry". May be NULL. |
455 |
|
* @min_address: Start of IPv4 or IPv6 address range. |
456 |
|
* @max_address: End of IPv4 or IPv6 address range. |
457 |
|
* @min_port: Start of port number range. |
458 |
|
* @max_port: End of port number range. |
459 |
|
* @domain: Pointer to "struct domain_info". |
460 |
|
* @condition: Pointer to "struct condition_list". May be NULL. |
461 |
|
* @is_delete: True if it is a delete request. |
462 |
|
* |
463 |
|
* Returns 0 on success, negative value otherwise. |
464 |
|
*/ |
465 |
|
static int update_network_entry(const u8 operation, const u8 record_type, |
466 |
|
const struct address_group_entry *group, |
467 |
|
const u32 *min_address, const u32 *max_address, |
468 |
|
const u16 min_port, const u16 max_port, |
469 |
|
struct domain_info *domain, |
470 |
|
const struct condition_list *condition, |
471 |
|
const bool is_delete) |
472 |
{ |
{ |
473 |
struct acl_info *ptr; |
struct acl_info *ptr; |
474 |
|
struct ip_network_acl_record *acl; |
475 |
int error = -ENOMEM; |
int error = -ENOMEM; |
476 |
const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/ |
/* using host byte order to allow u32 comparison than memcmp().*/ |
477 |
if (!domain) return -EINVAL; |
const u32 min_ip = ntohl(*min_address); |
478 |
down(&domain_acl_lock); |
const u32 max_ip = ntohl(*max_address); |
479 |
if (is_add) { |
const struct in6_addr *saved_min_address = NULL; |
480 |
if ((ptr = domain->first_acl_ptr) == NULL) goto first_entry; |
const struct in6_addr *saved_max_address = NULL; |
481 |
while (1) { |
if (!domain) |
482 |
struct ip_network_acl_record *new_ptr; |
return -EINVAL; |
483 |
if (ptr->type == TYPE_IP_NETWORK_ACL && ptr->u.b[0] == operation && ptr->u.b[1] == record_type && ptr->cond == condition && ((struct ip_network_acl_record *) ptr)->min_port == min_port && max_port == ((struct ip_network_acl_record *) ptr)->max_port) { |
if (record_type != IP_RECORD_TYPE_IPv6) |
484 |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
goto not_ipv6; |
485 |
if (((struct ip_network_acl_record *) ptr)->u.group == group) { |
saved_min_address = save_ipv6_address((struct in6_addr *) min_address); |
486 |
ptr->is_deleted = 0; |
saved_max_address = save_ipv6_address((struct in6_addr *) max_address); |
487 |
/* Found. Nothing to do. */ |
if (!saved_min_address || !saved_max_address) |
488 |
error = 0; |
return -ENOMEM; |
489 |
break; |
not_ipv6: |
490 |
} |
mutex_lock(&domain_acl_lock); |
491 |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
if (is_delete) |
492 |
if (((struct ip_network_acl_record *) ptr)->u.ipv4.min == min_ip && max_ip == ((struct ip_network_acl_record *) ptr)->u.ipv4.max) { |
goto delete; |
493 |
ptr->is_deleted = 0; |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
494 |
/* Found. Nothing to do. */ |
if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL) |
495 |
error = 0; |
continue; |
496 |
break; |
if (ccs_get_condition_part(ptr) != condition) |
497 |
} |
continue; |
498 |
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
acl = container_of(ptr, struct ip_network_acl_record, head); |
499 |
if (memcmp(((struct ip_network_acl_record *) ptr)->u.ipv6.min, min_address, 16) == 0 && memcmp(max_address, ((struct ip_network_acl_record *) ptr)->u.ipv6.max, 16) == 0) { |
if (acl->operation_type != operation || |
500 |
ptr->is_deleted = 0; |
acl->record_type != record_type || |
501 |
/* Found. Nothing to do. */ |
acl->min_port != min_port || max_port != acl->max_port) |
502 |
error = 0; |
continue; |
503 |
break; |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
504 |
} |
if (acl->u.group != group) |
505 |
} |
continue; |
506 |
} |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
507 |
if (ptr->next) { |
if (acl->u.ipv4.min != min_ip || |
508 |
ptr = ptr->next; |
max_ip != acl->u.ipv4.max) |
509 |
|
continue; |
510 |
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
511 |
|
if (acl->u.ipv6.min != saved_min_address || |
512 |
|
saved_max_address != acl->u.ipv6.max) |
513 |
continue; |
continue; |
|
} |
|
|
first_entry: ; |
|
|
if (is_add == 1 && TooManyDomainACL(domain)) break; |
|
|
/* Not found. Append it to the tail. */ |
|
|
if ((new_ptr = alloc_element(sizeof(*new_ptr))) == NULL) break; |
|
|
new_ptr->head.type = TYPE_IP_NETWORK_ACL; |
|
|
new_ptr->head.u.b[0] = operation; |
|
|
new_ptr->head.u.b[1] = record_type; |
|
|
new_ptr->head.cond = condition; |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
new_ptr->u.group = group; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
new_ptr->u.ipv4.min = min_ip; |
|
|
new_ptr->u.ipv4.max = max_ip; |
|
|
} else { |
|
|
memmove(new_ptr->u.ipv6.min, min_address, 16); |
|
|
memmove(new_ptr->u.ipv6.max, max_address, 16); |
|
|
} |
|
|
new_ptr->min_port = min_port; |
|
|
new_ptr->max_port = max_port; |
|
|
error = AddDomainACL(ptr, domain, (struct acl_info *) new_ptr); |
|
|
break; |
|
514 |
} |
} |
515 |
|
error = ccs_add_domain_acl(NULL, ptr); |
516 |
|
goto out; |
517 |
|
} |
518 |
|
/* Not found. Append it to the tail. */ |
519 |
|
acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition); |
520 |
|
if (!acl) |
521 |
|
goto out; |
522 |
|
acl->operation_type = operation; |
523 |
|
acl->record_type = record_type; |
524 |
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
525 |
|
acl->u.group = group; |
526 |
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
527 |
|
acl->u.ipv4.min = min_ip; |
528 |
|
acl->u.ipv4.max = max_ip; |
529 |
} else { |
} else { |
530 |
error = -ENOENT; |
acl->u.ipv6.min = saved_min_address; |
531 |
for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) { |
acl->u.ipv6.max = saved_max_address; |
532 |
if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || ptr->u.b[0] != operation || ptr->u.b[1] != record_type || ptr->cond != condition || ((struct ip_network_acl_record *) ptr)->min_port != min_port || ((struct ip_network_acl_record *) ptr)->max_port != max_port) continue; |
} |
533 |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
acl->min_port = min_port; |
534 |
if (((struct ip_network_acl_record *) ptr)->u.group != group) continue; |
acl->max_port = max_port; |
535 |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
error = ccs_add_domain_acl(domain, &acl->head); |
536 |
if (((struct ip_network_acl_record *) ptr)->u.ipv4.min != min_ip || max_ip != ((struct ip_network_acl_record *) ptr)->u.ipv4.max) continue; |
goto out; |
537 |
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
delete: |
538 |
if (memcmp(((struct ip_network_acl_record *) ptr)->u.ipv6.min, min_address, 16) || memcmp(max_address, ((struct ip_network_acl_record *) ptr)->u.ipv6.max, 16)) continue; |
error = -ENOENT; |
539 |
} |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
540 |
error = DelDomainACL(ptr); |
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
541 |
break; |
continue; |
542 |
|
if (ccs_get_condition_part(ptr) != condition) |
543 |
|
continue; |
544 |
|
acl = container_of(ptr, struct ip_network_acl_record, head); |
545 |
|
if (acl->operation_type != operation || |
546 |
|
acl->record_type != record_type || |
547 |
|
acl->min_port != min_port || max_port != acl->max_port) |
548 |
|
continue; |
549 |
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
550 |
|
if (acl->u.group != group) |
551 |
|
continue; |
552 |
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
553 |
|
if (acl->u.ipv4.min != min_ip || |
554 |
|
max_ip != acl->u.ipv4.max) |
555 |
|
continue; |
556 |
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
557 |
|
if (acl->u.ipv6.min != saved_min_address || |
558 |
|
saved_max_address != acl->u.ipv6.max) |
559 |
|
continue; |
560 |
} |
} |
561 |
|
error = ccs_del_domain_acl(ptr); |
562 |
|
break; |
563 |
} |
} |
564 |
up(&domain_acl_lock); |
out: |
565 |
|
mutex_unlock(&domain_acl_lock); |
566 |
return error; |
return error; |
567 |
} |
} |
568 |
|
|
569 |
static int CheckNetworkEntry(const int is_ipv6, const int operation, const u32 *address, const u16 port) |
/** |
570 |
|
* check_network_entry - Check permission for network operation. |
571 |
|
* |
572 |
|
* @is_ipv6: True if @address is an IPv6 address. |
573 |
|
* @operation: Type of operation. |
574 |
|
* @address: An IPv4 or IPv6 address. |
575 |
|
* @port: Port number. |
576 |
|
* |
577 |
|
* Returns 0 on success, negative value otherwise. |
578 |
|
*/ |
579 |
|
static int check_network_entry(const bool is_ipv6, const u8 operation, |
580 |
|
const u32 *address, const u16 port) |
581 |
{ |
{ |
582 |
struct domain_info * const domain = current->domain_info; |
struct domain_info * const domain = current->domain_info; |
583 |
struct acl_info *ptr; |
struct acl_info *ptr; |
584 |
const char *keyword = network2keyword(operation); |
const char *keyword = ccs_net2keyword(operation); |
585 |
const int is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_NETWORK); |
const u8 profile = current->domain_info->profile; |
586 |
const u32 ip = ntohl(*address); /* using host byte order to allow u32 comparison than memcmp().*/ |
const u8 mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_NETWORK); |
587 |
if (!CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK)) return 0; |
const bool is_enforce = (mode == 3); |
588 |
for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) { |
/* using host byte order to allow u32 comparison than memcmp().*/ |
589 |
if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || ptr->u.b[0] != operation || port < ((struct ip_network_acl_record *) ptr)->min_port || ((struct ip_network_acl_record *) ptr)->max_port < port || CheckCondition(ptr->cond, NULL)) continue; |
const u32 ip = ntohl(*address); |
590 |
if (ptr->u.b[1] == IP_RECORD_TYPE_ADDRESS_GROUP) { |
bool found = false; |
591 |
if (AddressMatchesToGroup(is_ipv6, address, ((struct ip_network_acl_record *) ptr)->u.group)) break; |
char buf[64]; |
592 |
} else if (ptr->u.b[1] == IP_RECORD_TYPE_IPv4) { |
if (!mode) |
|
if (!is_ipv6 && ((struct ip_network_acl_record *) ptr)->u.ipv4.min <= ip && ip <= ((struct ip_network_acl_record *) ptr)->u.ipv4.max) break; |
|
|
} else { |
|
|
if (is_ipv6 && memcmp(((struct ip_network_acl_record *) ptr)->u.ipv6.min, address, 16) <= 0 && memcmp(address, ((struct ip_network_acl_record *) ptr)->u.ipv6.max, 16) <= 0) break; |
|
|
} |
|
|
} |
|
|
if (ptr) { |
|
|
AuditNetworkLog(is_ipv6, keyword, address, port, 1); |
|
593 |
return 0; |
return 0; |
594 |
} |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
595 |
if (TomoyoVerboseMode()) { |
struct ip_network_acl_record *acl; |
596 |
if (is_ipv6) { |
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
597 |
char buf[64]; |
continue; |
598 |
print_ipv6(buf, sizeof(buf), (const u16 *) address); |
acl = container_of(ptr, struct ip_network_acl_record, head); |
599 |
printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain)); |
if (acl->operation_type != operation || port < acl->min_port || |
600 |
|
acl->max_port < port || !ccs_check_condition(ptr, NULL)) |
601 |
|
continue; |
602 |
|
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
603 |
|
if (!address_matches_to_group(is_ipv6, address, |
604 |
|
acl->u.group)) |
605 |
|
continue; |
606 |
|
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
607 |
|
if (is_ipv6 || |
608 |
|
ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) |
609 |
|
continue; |
610 |
} else { |
} else { |
611 |
printk("TOMOYO-%s: %s to %u.%u.%u.%u %u denied for %s\n", GetMSG(is_enforce), keyword, HIPQUAD(ip), port, GetLastName(domain)); |
if (!is_ipv6 || |
612 |
} |
memcmp(acl->u.ipv6.min, address, 16) > 0 || |
613 |
} |
memcmp(address, acl->u.ipv6.max, 16) > 0) |
614 |
AuditNetworkLog(is_ipv6, keyword, address, port, 0); |
continue; |
|
if (is_enforce) { |
|
|
if (is_ipv6) { |
|
|
char buf[64]; |
|
|
print_ipv6(buf, sizeof(buf), (const u16 *) address); |
|
|
return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port); |
|
615 |
} |
} |
616 |
return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port); |
ccs_update_condition(ptr); |
617 |
|
found = true; |
618 |
|
break; |
619 |
} |
} |
620 |
if (CheckCCSAccept(CCS_TOMOYO_MAC_FOR_NETWORK)) AddNetworkEntry(operation, is_ipv6 ? IP_RECORD_TYPE_IPv6: IP_RECORD_TYPE_IPv4, NULL, address, address, port, port, domain, 1, NULL); |
memset(buf, 0, sizeof(buf)); |
621 |
|
if (is_ipv6) |
622 |
|
ccs_print_ipv6(buf, sizeof(buf), |
623 |
|
(const struct in6_addr *) address); |
624 |
|
else |
625 |
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
626 |
|
audit_network_log(is_ipv6, keyword, buf, port, found, profile, mode); |
627 |
|
if (found) |
628 |
|
return 0; |
629 |
|
if (ccs_verbose_mode()) |
630 |
|
printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n", |
631 |
|
ccs_get_msg(is_enforce), keyword, buf, port, |
632 |
|
ccs_get_last_name(domain)); |
633 |
|
if (is_enforce) |
634 |
|
return ccs_check_supervisor(NULL, KEYWORD_ALLOW_NETWORK "%s " |
635 |
|
"%s %u\n", keyword, buf, port); |
636 |
|
if (mode == 1 && ccs_check_domain_quota(domain)) |
637 |
|
update_network_entry(operation, is_ipv6 ? |
638 |
|
IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4, |
639 |
|
NULL, address, address, port, port, domain, |
640 |
|
NULL, 0); |
641 |
return 0; |
return 0; |
642 |
} |
} |
643 |
|
|
644 |
int AddNetworkPolicy(char *data, struct domain_info *domain, const int is_delete) |
/** |
645 |
{ |
* ccs_write_network_policy - Write "struct ip_network_acl_record" list. |
646 |
u8 sock_type, operation, record_type; |
* |
647 |
u16 min_address[8], max_address[8]; |
* @data: String to parse. |
648 |
|
* @domain: Pointer to "struct domain_info". |
649 |
|
* @condition: Pointer to "struct condition_list". May be NULL. |
650 |
|
* @is_delete: True if it is a delete request. |
651 |
|
* |
652 |
|
* Returns 0 on success, negative value otherwise. |
653 |
|
*/ |
654 |
|
int ccs_write_network_policy(char *data, struct domain_info *domain, |
655 |
|
const struct condition_list *condition, |
656 |
|
const bool is_delete) |
657 |
|
{ |
658 |
|
u8 sock_type; |
659 |
|
u8 operation; |
660 |
|
u8 record_type; |
661 |
|
u16 min_address[8]; |
662 |
|
u16 max_address[8]; |
663 |
struct address_group_entry *group = NULL; |
struct address_group_entry *group = NULL; |
664 |
u16 min_port, max_port; |
u16 min_port; |
665 |
int count; |
u16 max_port; |
666 |
char *cp1 = NULL, *cp2 = NULL; |
u8 count; |
667 |
const struct condition_list *condition = NULL; |
char *cp1 = strchr(data, ' '); |
668 |
cp1 = FindConditionPart(data); |
char *cp2; |
669 |
if (cp1 && (condition = FindOrAssignNewCondition(cp1)) == NULL) goto out; |
if (!cp1) |
670 |
if ((cp1 = strchr(data, ' ')) == NULL) goto out; cp1++; |
goto out; |
671 |
if (strncmp(data, "TCP ", 4) == 0) sock_type = SOCK_STREAM; |
cp1++; |
672 |
else if (strncmp(data, "UDP ", 4) == 0) sock_type = SOCK_DGRAM; |
if (!strncmp(data, "TCP ", 4)) |
673 |
else if (strncmp(data, "RAW ", 4) == 0) sock_type = SOCK_RAW; |
sock_type = SOCK_STREAM; |
674 |
else goto out; |
else if (!strncmp(data, "UDP ", 4)) |
675 |
if ((cp2 = strchr(cp1, ' ')) == NULL) goto out; cp2++; |
sock_type = SOCK_DGRAM; |
676 |
if (strncmp(cp1, "bind ", 5) == 0) { |
else if (!strncmp(data, "RAW ", 4)) |
677 |
operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND; |
sock_type = SOCK_RAW; |
678 |
} else if (strncmp(cp1, "connect ", 8) == 0) { |
else |
679 |
operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
goto out; |
680 |
} else if (sock_type == SOCK_STREAM && strncmp(cp1, "listen ", 7) == 0) { |
cp2 = strchr(cp1, ' '); |
681 |
|
if (!cp2) |
682 |
|
goto out; |
683 |
|
cp2++; |
684 |
|
if (!strncmp(cp1, "bind ", 5)) |
685 |
|
switch (sock_type) { |
686 |
|
case SOCK_STREAM: |
687 |
|
operation = NETWORK_ACL_TCP_BIND; |
688 |
|
break; |
689 |
|
case SOCK_DGRAM: |
690 |
|
operation = NETWORK_ACL_UDP_BIND; |
691 |
|
break; |
692 |
|
default: |
693 |
|
operation = NETWORK_ACL_RAW_BIND; |
694 |
|
} |
695 |
|
else if (!strncmp(cp1, "connect ", 8)) |
696 |
|
switch (sock_type) { |
697 |
|
case SOCK_STREAM: |
698 |
|
operation = NETWORK_ACL_TCP_CONNECT; |
699 |
|
break; |
700 |
|
case SOCK_DGRAM: |
701 |
|
operation = NETWORK_ACL_UDP_CONNECT; |
702 |
|
break; |
703 |
|
default: |
704 |
|
operation = NETWORK_ACL_RAW_CONNECT; |
705 |
|
} |
706 |
|
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7)) |
707 |
operation = NETWORK_ACL_TCP_LISTEN; |
operation = NETWORK_ACL_TCP_LISTEN; |
708 |
} else if (sock_type == SOCK_STREAM && strncmp(cp1, "accept ", 7) == 0) { |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7)) |
709 |
operation = NETWORK_ACL_TCP_ACCEPT; |
operation = NETWORK_ACL_TCP_ACCEPT; |
710 |
} else { |
else |
711 |
goto out; |
goto out; |
712 |
} |
cp1 = strchr(cp2, ' '); |
713 |
if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0'; |
if (!cp1) |
714 |
if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
goto out; |
715 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
*cp1++ = '\0'; |
716 |
&min_address[4], &min_address[5], &min_address[6], &min_address[7], |
count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
717 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3], |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
718 |
&max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) { |
&min_address[0], &min_address[1], |
719 |
int i; |
&min_address[2], &min_address[3], |
720 |
|
&min_address[4], &min_address[5], |
721 |
|
&min_address[6], &min_address[7], |
722 |
|
&max_address[0], &max_address[1], |
723 |
|
&max_address[2], &max_address[3], |
724 |
|
&max_address[4], &max_address[5], |
725 |
|
&max_address[6], &max_address[7]); |
726 |
|
if (count == 8 || count == 16) { |
727 |
|
u8 i; |
728 |
for (i = 0; i < 8; i++) { |
for (i = 0; i < 8; i++) { |
729 |
min_address[i] = htons(min_address[i]); |
min_address[i] = htons(min_address[i]); |
730 |
max_address[i] = htons(max_address[i]); |
max_address[i] = htons(max_address[i]); |
731 |
} |
} |
732 |
if (count == 8) memmove(max_address, min_address, sizeof(min_address)); |
if (count == 8) |
733 |
|
memmove(max_address, min_address, sizeof(min_address)); |
734 |
record_type = IP_RECORD_TYPE_IPv6; |
record_type = IP_RECORD_TYPE_IPv6; |
735 |
} else if ((count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
goto ok; |
736 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
} |
737 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) { |
count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
738 |
u32 ip = htonl((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]); |
&min_address[0], &min_address[1], |
739 |
* (u32 *) min_address = ip; |
&min_address[2], &min_address[3], |
740 |
if (count == 8) ip = htonl((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]); |
&max_address[0], &max_address[1], |
741 |
* (u32 *) max_address = ip; |
&max_address[2], &max_address[3]); |
742 |
|
if (count == 4 || count == 8) { |
743 |
|
u32 ip = htonl((((u8) min_address[0]) << 24) |
744 |
|
+ (((u8) min_address[1]) << 16) |
745 |
|
+ (((u8) min_address[2]) << 8) |
746 |
|
+ (u8) min_address[3]); |
747 |
|
*(u32 *) min_address = ip; |
748 |
|
if (count == 8) |
749 |
|
ip = htonl((((u8) max_address[0]) << 24) |
750 |
|
+ (((u8) max_address[1]) << 16) |
751 |
|
+ (((u8) max_address[2]) << 8) |
752 |
|
+ (u8) max_address[3]); |
753 |
|
*(u32 *) max_address = ip; |
754 |
record_type = IP_RECORD_TYPE_IPv4; |
record_type = IP_RECORD_TYPE_IPv4; |
755 |
} else if (*cp2 == '@') { |
goto ok; |
|
if ((group = FindOrAssignNewAddressGroup(cp2 + 1)) == NULL) return -ENOMEM; |
|
|
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
|
|
} else { |
|
|
goto out; |
|
756 |
} |
} |
757 |
if (strchr(cp1, ' ')) goto out; |
if (*cp2 == '@') { |
758 |
if ((count = sscanf(cp1, "%hu-%hu", &min_port, &max_port)) == 1 || count == 2) { |
group = find_or_assign_new_address_group(cp2 + 1); |
759 |
if (count == 1) max_port = min_port; |
if (!group) |
760 |
return AddNetworkEntry(operation, record_type, group, (u32 *) min_address, (u32 *) max_address, min_port, max_port, domain, is_delete ? 0 : -1, condition); |
return -ENOMEM; |
761 |
|
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
762 |
|
goto ok; |
763 |
} |
} |
764 |
out: ; |
out: |
765 |
return -EINVAL; |
return -EINVAL; |
766 |
|
ok: |
767 |
|
if (strchr(cp1, ' ')) |
768 |
|
goto out; |
769 |
|
count = sscanf(cp1, "%hu-%hu", &min_port, &max_port); |
770 |
|
if (count != 1 && count != 2) |
771 |
|
goto out; |
772 |
|
if (count == 1) |
773 |
|
max_port = min_port; |
774 |
|
return update_network_entry(operation, record_type, group, |
775 |
|
(u32 *) min_address, (u32 *) max_address, |
776 |
|
min_port, max_port, domain, condition, |
777 |
|
is_delete); |
778 |
} |
} |
779 |
|
|
780 |
int CheckNetworkListenACL(const int is_ipv6, const u8 *address, const u16 port) |
/** |
781 |
|
* ccs_check_network_listen_acl - Check permission for listen() operation. |
782 |
|
* |
783 |
|
* @is_ipv6: True if @address is an IPv6 address. |
784 |
|
* @address: An IPv4 or IPv6 address. |
785 |
|
* @port: Port number. |
786 |
|
* |
787 |
|
* Returns 0 on success, negative value otherwise. |
788 |
|
*/ |
789 |
|
int ccs_check_network_listen_acl(const _Bool is_ipv6, const u8 *address, |
790 |
|
const u16 port) |
791 |
{ |
{ |
792 |
return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_LISTEN, (const u32 *) address, ntohs(port)); |
return check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN, |
793 |
|
(const u32 *) address, ntohs(port)); |
794 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkListenACL); |
|
795 |
|
|
796 |
int CheckNetworkConnectACL(const int is_ipv6, const int sock_type, const u8 *address, const u16 port) |
/** |
797 |
|
* ccs_check_network_connect_acl - Check permission for connect() operation. |
798 |
|
* |
799 |
|
* @is_ipv6: True if @address is an IPv6 address. |
800 |
|
* @sock_type: Type of socket. (TCP or UDP or RAW) |
801 |
|
* @address: An IPv4 or IPv6 address. |
802 |
|
* @port: Port number. |
803 |
|
* |
804 |
|
* Returns 0 on success, negative value otherwise. |
805 |
|
*/ |
806 |
|
int ccs_check_network_connect_acl(const _Bool is_ipv6, const int sock_type, |
807 |
|
const u8 *address, const u16 port) |
808 |
{ |
{ |
809 |
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_STREAM ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT), (const u32 *) address, ntohs(port)); |
u8 operation; |
810 |
|
switch (sock_type) { |
811 |
|
case SOCK_STREAM: |
812 |
|
operation = NETWORK_ACL_TCP_CONNECT; |
813 |
|
break; |
814 |
|
case SOCK_DGRAM: |
815 |
|
operation = NETWORK_ACL_UDP_CONNECT; |
816 |
|
break; |
817 |
|
default: |
818 |
|
operation = NETWORK_ACL_RAW_CONNECT; |
819 |
|
} |
820 |
|
return check_network_entry(is_ipv6, operation, (const u32 *) address, |
821 |
|
ntohs(port)); |
822 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkConnectACL); |
|
823 |
|
|
824 |
int CheckNetworkBindACL(const int is_ipv6, const int sock_type, const u8 *address, const u16 port) |
/** |
825 |
|
* ccs_check_network_bind_acl - Check permission for bind() operation. |
826 |
|
* |
827 |
|
* @is_ipv6: True if @address is an IPv6 address. |
828 |
|
* @sock_type: Type of socket. (TCP or UDP or RAW) |
829 |
|
* @address: An IPv4 or IPv6 address. |
830 |
|
* @port: Port number. |
831 |
|
* |
832 |
|
* Returns 0 on success, negative value otherwise. |
833 |
|
*/ |
834 |
|
int ccs_check_network_bind_acl(const _Bool is_ipv6, const int sock_type, |
835 |
|
const u8 *address, const u16 port) |
836 |
{ |
{ |
837 |
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_STREAM ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND), (const u32 *) address, ntohs(port)); |
u8 operation; |
838 |
|
switch (sock_type) { |
839 |
|
case SOCK_STREAM: |
840 |
|
operation = NETWORK_ACL_TCP_BIND; |
841 |
|
break; |
842 |
|
case SOCK_DGRAM: |
843 |
|
operation = NETWORK_ACL_UDP_BIND; |
844 |
|
break; |
845 |
|
default: |
846 |
|
operation = NETWORK_ACL_RAW_BIND; |
847 |
|
} |
848 |
|
return check_network_entry(is_ipv6, operation, (const u32 *) address, |
849 |
|
ntohs(port)); |
850 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkBindACL); |
|
851 |
|
|
852 |
int CheckNetworkAcceptACL(const int is_ipv6, const u8 *address, const u16 port) |
/** |
853 |
|
* ccs_check_network_accept_acl - Check permission for accept() operation. |
854 |
|
* |
855 |
|
* @is_ipv6: True if @address is an IPv6 address. |
856 |
|
* @address: An IPv4 or IPv6 address. |
857 |
|
* @port: Port number. |
858 |
|
* |
859 |
|
* Returns 0 on success, negative value otherwise. |
860 |
|
*/ |
861 |
|
int ccs_check_network_accept_acl(const _Bool is_ipv6, const u8 *address, |
862 |
|
const u16 port) |
863 |
{ |
{ |
864 |
return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, (const u32 *) address, ntohs(port)); |
int retval; |
865 |
|
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
866 |
|
retval = check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, |
867 |
|
(const u32 *) address, ntohs(port)); |
868 |
|
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
869 |
|
return retval; |
870 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkAcceptACL); |
|
871 |
|
|
872 |
int CheckNetworkSendMsgACL(const int is_ipv6, const int sock_type, const u8 *address, const u16 port) |
/** |
873 |
|
* ccs_check_network_sendmsg_acl - Check permission for sendmsg() operation. |
874 |
|
* |
875 |
|
* @is_ipv6: True if @address is an IPv6 address. |
876 |
|
* @sock_type: Type of socket. (UDP or RAW) |
877 |
|
* @address: An IPv4 or IPv6 address. |
878 |
|
* @port: Port number. |
879 |
|
* |
880 |
|
* Returns 0 on success, negative value otherwise. |
881 |
|
*/ |
882 |
|
int ccs_check_network_sendmsg_acl(const _Bool is_ipv6, const int sock_type, |
883 |
|
const u8 *address, const u16 port) |
884 |
{ |
{ |
885 |
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port)); |
u8 operation; |
886 |
|
if (sock_type == SOCK_DGRAM) |
887 |
|
operation = NETWORK_ACL_UDP_CONNECT; |
888 |
|
else |
889 |
|
operation = NETWORK_ACL_RAW_CONNECT; |
890 |
|
return check_network_entry(is_ipv6, operation, (const u32 *) address, |
891 |
|
ntohs(port)); |
892 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkSendMsgACL); |
|
893 |
|
|
894 |
int CheckNetworkRecvMsgACL(const int is_ipv6, const int sock_type, const u8 *address, const u16 port) |
/** |
895 |
|
* ccs_check_network_recvmsg_acl - Check permission for recvmsg() operation. |
896 |
|
* |
897 |
|
* @is_ipv6: True if @address is an IPv6 address. |
898 |
|
* @sock_type: Type of socket. (UDP or RAW) |
899 |
|
* @address: An IPv4 or IPv6 address. |
900 |
|
* @port: Port number. |
901 |
|
* |
902 |
|
* Returns 0 on success, negative value otherwise. |
903 |
|
*/ |
904 |
|
int ccs_check_network_recvmsg_acl(const _Bool is_ipv6, const int sock_type, |
905 |
|
const u8 *address, const u16 port) |
906 |
{ |
{ |
907 |
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port)); |
int retval; |
908 |
|
const u8 operation |
909 |
|
= (sock_type == SOCK_DGRAM) ? |
910 |
|
NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
911 |
|
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
912 |
|
retval = check_network_entry(is_ipv6, operation, (const u32 *) address, |
913 |
|
ntohs(port)); |
914 |
|
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
915 |
|
return retval; |
916 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkRecvMsgACL); |
|
|
|
|
|
/***** TOMOYO Linux end. *****/ |
|