5 |
* |
* |
6 |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
7 |
* |
* |
8 |
* Version: 1.6.6-pre 2009/01/05 |
* Version: 1.7.0-pre 2009/07/03 |
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 |
*/ |
*/ |
14 |
|
|
|
#include <linux/ccs_common.h> |
|
|
#include <linux/tomoyo.h> |
|
|
#include <linux/realpath.h> |
|
15 |
#include <linux/net.h> |
#include <linux/net.h> |
16 |
#include <linux/inet.h> |
#include <linux/inet.h> |
17 |
#include <linux/in.h> |
#include <linux/in.h> |
19 |
#include <net/ip.h> |
#include <net/ip.h> |
20 |
#include <net/ipv6.h> |
#include <net/ipv6.h> |
21 |
#include <net/udp.h> |
#include <net/udp.h> |
22 |
|
#include <linux/ccs_common.h> |
23 |
|
#include <linux/tomoyo.h> |
24 |
|
#include <linux/tomoyo_socket.h> |
25 |
|
|
26 |
/* Index numbers for Network Controls. */ |
/* Index numbers for Network Controls. */ |
27 |
enum ccs_network_acl_index { |
enum ccs_network_acl_index { |
50 |
const char *operation, const char *address, |
const char *operation, const char *address, |
51 |
const u16 port, const bool is_granted) |
const u16 port, const bool is_granted) |
52 |
{ |
{ |
53 |
|
if (!is_granted && ccs_verbose_mode(r->domain)) |
54 |
|
printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n", |
55 |
|
ccs_get_msg(r->mode == 3), operation, address, port, |
56 |
|
ccs_get_last_name(r->domain)); |
57 |
return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK |
return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK |
58 |
"%s %s %u\n", operation, address, port); |
"%s %s %u\n", operation, address, port); |
59 |
} |
} |
60 |
|
|
61 |
|
/* The list for "struct ccs_address_group_entry". */ |
62 |
|
LIST_HEAD(ccs_address_group_list); |
63 |
|
|
64 |
/** |
/** |
65 |
* ccs_save_ipv6_address - Keep the given IPv6 address on the RAM. |
* ccs_get_address_group - Allocate memory for "struct ccs_address_group_entry". |
|
* |
|
|
* @addr: Pointer to "struct in6_addr". |
|
66 |
* |
* |
67 |
* Returns pointer to "struct in6_addr" on success, NULL otherwise. |
* @group_name: The name of address group. |
68 |
* |
* |
69 |
* The RAM is shared, so NEVER try to modify or kfree() the returned address. |
* Returns pointer to "struct ccs_address_group_entry" on success, |
70 |
|
* NULL otherwise. |
71 |
*/ |
*/ |
72 |
static const struct in6_addr *ccs_save_ipv6_address(const struct in6_addr *addr) |
static struct ccs_address_group_entry *ccs_get_address_group(const char * |
73 |
{ |
group_name) |
74 |
static const u8 ccs_block_size = 16; |
{ |
75 |
struct ccs_addr_list { |
struct ccs_address_group_entry *entry = NULL; |
76 |
/* Workaround for gcc 4.3's bug. */ |
struct ccs_address_group_entry *group; |
77 |
struct in6_addr addr[16]; /* = ccs_block_size */ |
const struct ccs_path_info *saved_group_name; |
78 |
struct list1_head list; |
int error = -ENOMEM; |
79 |
u32 in_use_count; |
if (!ccs_is_correct_path(group_name, 0, 0, 0) || |
80 |
}; |
!group_name[0]) |
|
static LIST1_HEAD(ccs_address_list); |
|
|
struct ccs_addr_list *ptr; |
|
|
static DEFINE_MUTEX(lock); |
|
|
u8 i = ccs_block_size; |
|
|
if (!addr) |
|
81 |
return NULL; |
return NULL; |
82 |
mutex_lock(&lock); |
saved_group_name = ccs_get_name(group_name); |
83 |
list1_for_each_entry(ptr, &ccs_address_list, list) { |
if (!saved_group_name) |
84 |
for (i = 0; i < ptr->in_use_count; i++) { |
return NULL; |
85 |
if (!memcmp(&ptr->addr[i], addr, sizeof(*addr))) |
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
86 |
goto ok; |
mutex_lock(&ccs_policy_lock); |
87 |
} |
list_for_each_entry_rcu(group, &ccs_address_group_list, list) { |
88 |
if (i < ccs_block_size) |
if (saved_group_name != group->group_name) |
89 |
break; |
continue; |
90 |
|
atomic_inc(&group->users); |
91 |
|
error = 0; |
92 |
|
break; |
93 |
} |
} |
94 |
if (i == ccs_block_size) { |
if (error && ccs_memory_ok(entry, sizeof(*entry))) { |
95 |
ptr = ccs_alloc_element(sizeof(*ptr)); |
INIT_LIST_HEAD(&entry->address_group_member_list); |
96 |
if (!ptr) |
entry->group_name = saved_group_name; |
97 |
goto ok; |
saved_group_name = NULL; |
98 |
list1_add_tail_mb(&ptr->list, &ccs_address_list); |
atomic_set(&entry->users, 1); |
99 |
i = 0; |
list_add_tail_rcu(&entry->list, &ccs_address_group_list); |
100 |
} |
group = entry; |
101 |
ptr->addr[ptr->in_use_count++] = *addr; |
entry = NULL; |
102 |
ok: |
error = 0; |
103 |
mutex_unlock(&lock); |
} |
104 |
return ptr ? &ptr->addr[i] : NULL; |
mutex_unlock(&ccs_policy_lock); |
105 |
|
ccs_put_name(saved_group_name); |
106 |
|
kfree(entry); |
107 |
|
return !error ? group : NULL; |
108 |
} |
} |
109 |
|
|
|
/* The list for "struct ccs_address_group_entry". */ |
|
|
static LIST1_HEAD(ccs_address_group_list); |
|
|
|
|
110 |
/** |
/** |
111 |
* ccs_update_address_group_entry - Update "struct ccs_address_group_entry" list. |
* ccs_update_address_group_entry - Update "struct ccs_address_group_entry" list. |
112 |
* |
* |
124 |
const u16 *max_address, |
const u16 *max_address, |
125 |
const bool is_delete) |
const bool is_delete) |
126 |
{ |
{ |
|
static DEFINE_MUTEX(lock); |
|
|
struct ccs_address_group_entry *new_group; |
|
127 |
struct ccs_address_group_entry *group; |
struct ccs_address_group_entry *group; |
128 |
struct ccs_address_group_member *new_member; |
struct ccs_address_group_member *entry = NULL; |
129 |
struct ccs_address_group_member *member; |
struct ccs_address_group_member *member; |
|
const struct ccs_path_info *saved_group_name; |
|
130 |
const struct in6_addr *saved_min_address = NULL; |
const struct in6_addr *saved_min_address = NULL; |
131 |
const struct in6_addr *saved_max_address = NULL; |
const struct in6_addr *saved_max_address = NULL; |
132 |
int error = -ENOMEM; |
int error = is_delete ? -ENOENT : -ENOMEM; |
133 |
bool found = false; |
const u32 min_ipv4_address = ntohl(*(u32 *) min_address); |
134 |
if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) || |
const u32 max_ipv4_address = ntohl(*(u32 *) max_address); |
135 |
!group_name[0]) |
group = ccs_get_address_group(group_name); |
136 |
return -EINVAL; |
if (!group) |
|
saved_group_name = ccs_save_name(group_name); |
|
|
if (!saved_group_name) |
|
|
return -ENOMEM; |
|
|
if (!is_ipv6) |
|
|
goto not_ipv6; |
|
|
saved_min_address |
|
|
= ccs_save_ipv6_address((struct in6_addr *) min_address); |
|
|
saved_max_address |
|
|
= ccs_save_ipv6_address((struct in6_addr *) max_address); |
|
|
if (!saved_min_address || !saved_max_address) |
|
137 |
return -ENOMEM; |
return -ENOMEM; |
138 |
not_ipv6: |
if (is_ipv6) { |
139 |
mutex_lock(&lock); |
saved_min_address |
140 |
list1_for_each_entry(group, &ccs_address_group_list, list) { |
= ccs_get_ipv6_address((struct in6_addr *) |
141 |
if (saved_group_name != group->group_name) |
min_address); |
142 |
|
saved_max_address |
143 |
|
= ccs_get_ipv6_address((struct in6_addr *) |
144 |
|
max_address); |
145 |
|
if (!saved_min_address || !saved_max_address) |
146 |
|
goto out; |
147 |
|
} |
148 |
|
if (!is_delete) |
149 |
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
150 |
|
mutex_lock(&ccs_policy_lock); |
151 |
|
list_for_each_entry_rcu(member, &group->address_group_member_list, |
152 |
|
list) { |
153 |
|
if (member->is_ipv6 != is_ipv6) |
154 |
continue; |
continue; |
155 |
list1_for_each_entry(member, |
if (is_ipv6) { |
156 |
&group->address_group_member_list, |
if (member->min.ipv6 != saved_min_address || |
157 |
list) { |
member->max.ipv6 != saved_max_address) |
158 |
if (member->is_ipv6 != is_ipv6) |
continue; |
159 |
|
} else { |
160 |
|
if (member->min.ipv4 != min_ipv4_address || |
161 |
|
member->max.ipv4 != max_ipv4_address) |
162 |
continue; |
continue; |
|
if (is_ipv6) { |
|
|
if (member->min.ipv6 != saved_min_address || |
|
|
member->max.ipv6 != saved_max_address) |
|
|
continue; |
|
|
} else { |
|
|
if (member->min.ipv4 != *(u32 *) min_address || |
|
|
member->max.ipv4 != *(u32 *) max_address) |
|
|
continue; |
|
|
} |
|
|
member->is_deleted = is_delete; |
|
|
error = 0; |
|
|
goto out; |
|
163 |
} |
} |
164 |
found = true; |
member->is_deleted = is_delete; |
165 |
|
error = 0; |
166 |
break; |
break; |
167 |
} |
} |
168 |
if (is_delete) { |
if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) { |
169 |
error = -ENOENT; |
entry->is_ipv6 = is_ipv6; |
170 |
goto out; |
if (is_ipv6) { |
171 |
} |
entry->min.ipv6 = saved_min_address; |
172 |
if (!found) { |
saved_min_address = NULL; |
173 |
new_group = ccs_alloc_element(sizeof(*new_group)); |
entry->max.ipv6 = saved_max_address; |
174 |
if (!new_group) |
saved_max_address = NULL; |
175 |
goto out; |
} else { |
176 |
INIT_LIST1_HEAD(&new_group->address_group_member_list); |
entry->min.ipv4 = min_ipv4_address; |
177 |
new_group->group_name = saved_group_name; |
entry->max.ipv4 = max_ipv4_address; |
178 |
list1_add_tail_mb(&new_group->list, &ccs_address_group_list); |
} |
179 |
group = new_group; |
list_add_tail_rcu(&entry->list, |
180 |
|
&group->address_group_member_list); |
181 |
|
entry = NULL; |
182 |
|
error = 0; |
183 |
} |
} |
184 |
new_member = ccs_alloc_element(sizeof(*new_member)); |
mutex_unlock(&ccs_policy_lock); |
|
if (!new_member) |
|
|
goto out; |
|
|
new_member->is_ipv6 = is_ipv6; |
|
|
if (is_ipv6) { |
|
|
new_member->min.ipv6 = saved_min_address; |
|
|
new_member->max.ipv6 = saved_max_address; |
|
|
} else { |
|
|
new_member->min.ipv4 = *(u32 *) min_address; |
|
|
new_member->max.ipv4 = *(u32 *) max_address; |
|
|
} |
|
|
list1_add_tail_mb(&new_member->list, |
|
|
&group->address_group_member_list); |
|
|
error = 0; |
|
185 |
out: |
out: |
186 |
mutex_unlock(&lock); |
ccs_put_ipv6_address(saved_min_address); |
187 |
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
ccs_put_ipv6_address(saved_max_address); |
188 |
|
ccs_put_address_group(group); |
189 |
return error; |
return error; |
190 |
} |
} |
191 |
|
|
242 |
*/ |
*/ |
243 |
int ccs_write_address_group_policy(char *data, const bool is_delete) |
int ccs_write_address_group_policy(char *data, const bool is_delete) |
244 |
{ |
{ |
245 |
|
char *w[2]; |
246 |
bool is_ipv6; |
bool is_ipv6; |
247 |
u16 min_address[8]; |
u16 min_address[8]; |
248 |
u16 max_address[8]; |
u16 max_address[8]; |
249 |
char *cp = strchr(data, ' '); |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0]) |
250 |
if (!cp) |
return -EINVAL; |
251 |
return -EINVAL; |
switch (ccs_parse_ip_address(w[1], min_address, max_address)) { |
|
*cp++ = '\0'; |
|
|
switch (ccs_parse_ip_address(cp, min_address, max_address)) { |
|
252 |
case 2: |
case 2: |
253 |
is_ipv6 = true; |
is_ipv6 = true; |
254 |
break; |
break; |
258 |
default: |
default: |
259 |
return -EINVAL; |
return -EINVAL; |
260 |
} |
} |
261 |
return ccs_update_address_group_entry(data, is_ipv6, min_address, |
return ccs_update_address_group_entry(w[0], is_ipv6, min_address, |
262 |
max_address, is_delete); |
max_address, is_delete); |
263 |
} |
} |
264 |
|
|
265 |
/** |
/** |
266 |
* ccs_find_or_assign_new_address_group - Create address group. |
* ccs_address_matches_group - Check whether the given address matches members of the given address group. |
|
* |
|
|
* @group_name: The name of address group. |
|
|
* |
|
|
* Returns pointer to "struct ccs_address_group_entry" on success, |
|
|
* NULL otherwise. |
|
|
*/ |
|
|
static struct ccs_address_group_entry * |
|
|
ccs_find_or_assign_new_address_group(const char *group_name) |
|
|
{ |
|
|
u8 i; |
|
|
struct ccs_address_group_entry *group; |
|
|
for (i = 0; i <= 1; i++) { |
|
|
list1_for_each_entry(group, &ccs_address_group_list, list) { |
|
|
if (!strcmp(group_name, group->group_name->name)) |
|
|
return group; |
|
|
} |
|
|
if (!i) { |
|
|
const u16 dummy[2] = { 0, 0 }; |
|
|
ccs_update_address_group_entry(group_name, false, |
|
|
dummy, dummy, false); |
|
|
ccs_update_address_group_entry(group_name, false, |
|
|
dummy, dummy, true); |
|
|
} |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_address_matches_to_group - Check whether the given address matches members of the given address group. |
|
267 |
* |
* |
268 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
269 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address. |
270 |
* @group: Pointer to "struct ccs_address_group_entry". |
* @group: Pointer to "struct ccs_address_group_entry". |
271 |
* |
* |
272 |
* Returns true if @address matches addresses in @group group, false otherwise. |
* Returns true if @address matches addresses in @group group, false otherwise. |
273 |
|
* |
274 |
|
* Caller holds ccs_read_lock(). |
275 |
*/ |
*/ |
276 |
static bool ccs_address_matches_to_group(const bool is_ipv6, const u32 *address, |
static bool ccs_address_matches_group(const bool is_ipv6, const u32 *address, |
277 |
const struct ccs_address_group_entry * |
const struct ccs_address_group_entry * |
278 |
group) |
group) |
279 |
{ |
{ |
280 |
struct ccs_address_group_member *member; |
struct ccs_address_group_member *member; |
281 |
const u32 ip = ntohl(*address); |
const u32 ip = ntohl(*address); |
282 |
list1_for_each_entry(member, &group->address_group_member_list, |
bool matched = false; |
283 |
list) { |
ccs_check_read_lock(); |
284 |
|
list_for_each_entry_rcu(member, &group->address_group_member_list, |
285 |
|
list) { |
286 |
if (member->is_deleted) |
if (member->is_deleted) |
287 |
continue; |
continue; |
288 |
if (member->is_ipv6) { |
if (member->is_ipv6) { |
289 |
if (is_ipv6 && |
if (is_ipv6 && |
290 |
memcmp(member->min.ipv6, address, 16) <= 0 && |
memcmp(member->min.ipv6, address, 16) <= 0 && |
291 |
memcmp(address, member->max.ipv6, 16) <= 0) |
memcmp(address, member->max.ipv6, 16) <= 0) { |
292 |
return true; |
matched = true; |
293 |
|
break; |
294 |
|
} |
295 |
} else { |
} else { |
296 |
if (!is_ipv6 && |
if (!is_ipv6 && |
297 |
member->min.ipv4 <= ip && ip <= member->max.ipv4) |
member->min.ipv4 <= ip && ip <= member->max.ipv4) { |
298 |
return true; |
matched = true; |
299 |
|
break; |
300 |
|
} |
301 |
} |
} |
302 |
} |
} |
303 |
return false; |
return matched; |
304 |
} |
} |
305 |
|
|
306 |
/** |
/** |
309 |
* @head: Pointer to "struct ccs_io_buffer". |
* @head: Pointer to "struct ccs_io_buffer". |
310 |
* |
* |
311 |
* Returns true on success, false otherwise. |
* Returns true on success, false otherwise. |
312 |
|
* |
313 |
|
* Caller holds ccs_read_lock(). |
314 |
*/ |
*/ |
315 |
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
316 |
{ |
{ |
317 |
struct list1_head *gpos; |
struct list_head *gpos; |
318 |
struct list1_head *mpos; |
struct list_head *mpos; |
319 |
list1_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) { |
bool done = true; |
320 |
|
ccs_check_read_lock(); |
321 |
|
list_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) { |
322 |
struct ccs_address_group_entry *group; |
struct ccs_address_group_entry *group; |
323 |
group = list1_entry(gpos, struct ccs_address_group_entry, list); |
group = list_entry(gpos, struct ccs_address_group_entry, list); |
324 |
list1_for_each_cookie(mpos, head->read_var2, |
list_for_each_cookie(mpos, head->read_var2, |
325 |
&group->address_group_member_list) { |
&group->address_group_member_list) { |
326 |
char buf[128]; |
char buf[128]; |
327 |
struct ccs_address_group_member *member; |
struct ccs_address_group_member *member; |
328 |
member = list1_entry(mpos, |
member = list_entry(mpos, |
329 |
struct ccs_address_group_member, |
struct ccs_address_group_member, |
330 |
list); |
list); |
331 |
if (member->is_deleted) |
if (member->is_deleted) |
358 |
HIPQUAD(max_address)); |
HIPQUAD(max_address)); |
359 |
} |
} |
360 |
} |
} |
361 |
if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP |
done = ccs_io_printf(head, KEYWORD_ADDRESS_GROUP |
362 |
"%s %s\n", group->group_name->name, |
"%s %s\n", group->group_name->name, |
363 |
buf)) |
buf); |
364 |
goto out; |
if (!done) |
365 |
|
break; |
366 |
} |
} |
367 |
|
if (!done) |
368 |
|
break; |
369 |
} |
} |
370 |
return true; |
return done; |
|
out: |
|
|
return false; |
|
371 |
} |
} |
372 |
|
|
373 |
#if !defined(NIP6) |
#if !defined(NIP6) |
438 |
* |
* |
439 |
* @operation: Type of operation. |
* @operation: Type of operation. |
440 |
* @record_type: Type of address. |
* @record_type: Type of address. |
441 |
* @group: Pointer to "struct ccs_address_group_entry". May be NULL. |
* @group: Name of group. May be NULL. |
442 |
* @min_address: Start of IPv4 or IPv6 address range. |
* @min_address: Start of IPv4 or IPv6 address range. |
443 |
* @max_address: End of IPv4 or IPv6 address range. |
* @max_address: End of IPv4 or IPv6 address range. |
444 |
* @min_port: Start of port number range. |
* @min_port: Start of port number range. |
445 |
* @max_port: End of port number range. |
* @max_port: End of port number range. |
446 |
* @domain: Pointer to "struct domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
447 |
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
* @condition: Pointer to "struct ccs_condition". May be NULL. |
448 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
449 |
* |
* |
450 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
451 |
*/ |
*/ |
452 |
static int ccs_update_network_entry(const u8 operation, const u8 record_type, |
static int ccs_update_network_entry(const u8 operation, const u8 record_type, |
453 |
const struct ccs_address_group_entry *group, |
const char *group_name, |
454 |
const u32 *min_address, |
const u32 *min_address, |
455 |
const u32 *max_address, |
const u32 *max_address, |
456 |
const u16 min_port, const u16 max_port, |
const u16 min_port, const u16 max_port, |
457 |
struct domain_info *domain, |
struct ccs_domain_info *domain, |
458 |
const struct ccs_condition_list *condition, |
struct ccs_condition *condition, |
459 |
const bool is_delete) |
const bool is_delete) |
460 |
{ |
{ |
461 |
static DEFINE_MUTEX(lock); |
struct ccs_ip_network_acl_record *entry = NULL; |
462 |
struct ccs_acl_info *ptr; |
struct ccs_acl_info *ptr; |
463 |
struct ccs_ip_network_acl_record *acl; |
int error = is_delete ? -ENOENT : -ENOMEM; |
|
int error = -ENOMEM; |
|
464 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
/* using host byte order to allow u32 comparison than memcmp().*/ |
465 |
const u32 min_ip = ntohl(*min_address); |
const u32 min_ip = ntohl(*min_address); |
466 |
const u32 max_ip = ntohl(*max_address); |
const u32 max_ip = ntohl(*max_address); |
467 |
const struct in6_addr *saved_min_address = NULL; |
const struct in6_addr *saved_min_address = NULL; |
468 |
const struct in6_addr *saved_max_address = NULL; |
const struct in6_addr *saved_max_address = NULL; |
469 |
|
struct ccs_address_group_entry *group = NULL; |
470 |
if (!domain) |
if (!domain) |
471 |
return -EINVAL; |
return -EINVAL; |
472 |
if (record_type != IP_RECORD_TYPE_IPv6) |
if (group_name) { |
473 |
goto not_ipv6; |
group = ccs_get_address_group(group_name); |
474 |
saved_min_address = ccs_save_ipv6_address((struct in6_addr *) |
if (!group) |
475 |
min_address); |
return -ENOMEM; |
476 |
saved_max_address = ccs_save_ipv6_address((struct in6_addr *) |
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
477 |
max_address); |
saved_min_address = ccs_get_ipv6_address((struct in6_addr *) |
478 |
if (!saved_min_address || !saved_max_address) |
min_address); |
479 |
return -ENOMEM; |
saved_max_address = ccs_get_ipv6_address((struct in6_addr *) |
480 |
not_ipv6: |
max_address); |
481 |
mutex_lock(&lock); |
if (!saved_min_address || !saved_max_address) |
482 |
|
goto out; |
483 |
|
} |
484 |
if (is_delete) |
if (is_delete) |
485 |
goto delete; |
goto delete; |
486 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
487 |
|
mutex_lock(&ccs_policy_lock); |
488 |
|
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
489 |
|
struct ccs_ip_network_acl_record *acl; |
490 |
if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL) |
if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL) |
491 |
continue; |
continue; |
492 |
if (ccs_get_condition_part(ptr) != condition) |
if (ptr->cond != condition) |
493 |
continue; |
continue; |
494 |
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
495 |
if (acl->operation_type != operation || |
if (acl->operation_type != operation || |
509 |
continue; |
continue; |
510 |
} |
} |
511 |
error = ccs_add_domain_acl(NULL, ptr); |
error = ccs_add_domain_acl(NULL, ptr); |
512 |
goto out; |
break; |
513 |
} |
} |
514 |
/* Not found. Append it to the tail. */ |
if (error && ccs_memory_ok(entry, sizeof(*entry))) { |
515 |
acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition); |
entry->head.type = TYPE_IP_NETWORK_ACL; |
516 |
if (!acl) |
entry->head.cond = condition; |
517 |
goto out; |
entry->operation_type = operation; |
518 |
acl->operation_type = operation; |
entry->record_type = record_type; |
519 |
acl->record_type = record_type; |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
520 |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
entry->u.group = group; |
521 |
acl->u.group = group; |
group = NULL; |
522 |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
523 |
acl->u.ipv4.min = min_ip; |
entry->u.ipv4.min = min_ip; |
524 |
acl->u.ipv4.max = max_ip; |
entry->u.ipv4.max = max_ip; |
525 |
} else { |
} else { |
526 |
acl->u.ipv6.min = saved_min_address; |
entry->u.ipv6.min = saved_min_address; |
527 |
acl->u.ipv6.max = saved_max_address; |
saved_min_address = NULL; |
528 |
} |
entry->u.ipv6.max = saved_max_address; |
529 |
acl->min_port = min_port; |
saved_max_address = NULL; |
530 |
acl->max_port = max_port; |
} |
531 |
error = ccs_add_domain_acl(domain, &acl->head); |
entry->min_port = min_port; |
532 |
|
entry->max_port = max_port; |
533 |
|
error = ccs_add_domain_acl(domain, &entry->head); |
534 |
|
entry = NULL; |
535 |
|
} |
536 |
|
mutex_unlock(&ccs_policy_lock); |
537 |
goto out; |
goto out; |
538 |
delete: |
delete: |
539 |
error = -ENOENT; |
mutex_lock(&ccs_policy_lock); |
540 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
541 |
|
struct ccs_ip_network_acl_record *acl; |
542 |
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
543 |
continue; |
continue; |
544 |
if (ccs_get_condition_part(ptr) != condition) |
if (ptr->cond != condition) |
545 |
continue; |
continue; |
546 |
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
547 |
if (acl->operation_type != operation || |
if (acl->operation_type != operation || |
563 |
error = ccs_del_domain_acl(ptr); |
error = ccs_del_domain_acl(ptr); |
564 |
break; |
break; |
565 |
} |
} |
566 |
|
mutex_unlock(&ccs_policy_lock); |
567 |
out: |
out: |
568 |
mutex_unlock(&lock); |
ccs_put_ipv6_address(saved_min_address); |
569 |
|
ccs_put_ipv6_address(saved_max_address); |
570 |
|
ccs_put_address_group(group); |
571 |
|
kfree(entry); |
572 |
return error; |
return error; |
573 |
} |
} |
574 |
|
|
575 |
/** |
/** |
576 |
* ccs_check_network_entry - Check permission for network operation. |
* ccs_check_network_entry2 - Check permission for network operation. |
577 |
* |
* |
578 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
579 |
* @operation: Type of operation. |
* @operation: Type of operation. |
581 |
* @port: Port number. |
* @port: Port number. |
582 |
* |
* |
583 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
584 |
|
* |
585 |
|
* Caller holds ccs_read_lock(). |
586 |
*/ |
*/ |
587 |
static int ccs_check_network_entry(const bool is_ipv6, const u8 operation, |
static int ccs_check_network_entry2(const bool is_ipv6, const u8 operation, |
588 |
const u32 *address, const u16 port) |
const u32 *address, const u16 port) |
589 |
{ |
{ |
590 |
struct ccs_request_info r; |
struct ccs_request_info r; |
591 |
struct ccs_acl_info *ptr; |
struct ccs_acl_info *ptr; |
595 |
const u32 ip = ntohl(*address); |
const u32 ip = ntohl(*address); |
596 |
bool found = false; |
bool found = false; |
597 |
char buf[64]; |
char buf[64]; |
598 |
|
ccs_check_read_lock(); |
599 |
if (!ccs_can_sleep()) |
if (!ccs_can_sleep()) |
600 |
return 0; |
return 0; |
601 |
ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_NETWORK); |
ccs_init_request_info(&r, NULL, CCS_MAC_FOR_NETWORK); |
602 |
is_enforce = (r.mode == 3); |
is_enforce = (r.mode == 3); |
603 |
if (!r.mode) |
if (!r.mode) |
604 |
return 0; |
return 0; |
605 |
retry: |
retry: |
606 |
list1_for_each_entry(ptr, &r.domain->acl_info_list, list) { |
list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) { |
607 |
struct ccs_ip_network_acl_record *acl; |
struct ccs_ip_network_acl_record *acl; |
608 |
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
609 |
continue; |
continue; |
612 |
acl->max_port < port || !ccs_check_condition(&r, ptr)) |
acl->max_port < port || !ccs_check_condition(&r, ptr)) |
613 |
continue; |
continue; |
614 |
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
615 |
if (!ccs_address_matches_to_group(is_ipv6, address, |
if (!ccs_address_matches_group(is_ipv6, address, |
616 |
acl->u.group)) |
acl->u.group)) |
617 |
continue; |
continue; |
618 |
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
619 |
if (is_ipv6 || |
if (is_ipv6 || |
625 |
memcmp(address, acl->u.ipv6.max, 16) > 0) |
memcmp(address, acl->u.ipv6.max, 16) > 0) |
626 |
continue; |
continue; |
627 |
} |
} |
628 |
r.cond = ccs_get_condition_part(ptr); |
r.cond = ptr->cond; |
629 |
found = true; |
found = true; |
630 |
break; |
break; |
631 |
} |
} |
638 |
ccs_audit_network_log(&r, keyword, buf, port, found); |
ccs_audit_network_log(&r, keyword, buf, port, found); |
639 |
if (found) |
if (found) |
640 |
return 0; |
return 0; |
|
if (ccs_verbose_mode(r.domain)) |
|
|
printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n", |
|
|
ccs_get_msg(is_enforce), keyword, buf, port, |
|
|
ccs_get_last_name(r.domain)); |
|
641 |
if (is_enforce) { |
if (is_enforce) { |
642 |
int error = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK |
int err = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK |
643 |
"%s %s %u\n", keyword, buf, |
"%s %s %u\n", keyword, buf, |
644 |
port); |
port); |
645 |
if (error == 1) |
if (err == 1) |
646 |
goto retry; |
goto retry; |
647 |
return error; |
return err; |
648 |
} |
} else if (ccs_domain_quota_ok(&r)) { |
649 |
if (r.mode == 1 && ccs_check_domain_quota(r.domain)) |
struct ccs_condition *cond = ccs_handler_cond(); |
650 |
ccs_update_network_entry(operation, is_ipv6 ? |
ccs_update_network_entry(operation, is_ipv6 ? |
651 |
IP_RECORD_TYPE_IPv6 : |
IP_RECORD_TYPE_IPv6 : |
652 |
IP_RECORD_TYPE_IPv4, |
IP_RECORD_TYPE_IPv4, |
653 |
NULL, address, address, port, port, |
NULL, address, address, port, port, |
654 |
r.domain, ccs_handler_cond(), 0); |
r.domain, cond, false); |
655 |
|
ccs_put_condition(cond); |
656 |
|
} |
657 |
return 0; |
return 0; |
658 |
} |
} |
659 |
|
|
660 |
/** |
/** |
661 |
|
* ccs_check_network_entry - Check permission for network operation. |
662 |
|
* |
663 |
|
* @is_ipv6: True if @address is an IPv6 address. |
664 |
|
* @operation: Type of operation. |
665 |
|
* @address: An IPv4 or IPv6 address. |
666 |
|
* @port: Port number. |
667 |
|
* |
668 |
|
* Returns 0 on success, negative value otherwise. |
669 |
|
*/ |
670 |
|
static int ccs_check_network_entry(const bool is_ipv6, const u8 operation, |
671 |
|
const u32 *address, const u16 port) |
672 |
|
{ |
673 |
|
const int idx = ccs_read_lock(); |
674 |
|
const int error = ccs_check_network_entry2(is_ipv6, operation, |
675 |
|
address, port); |
676 |
|
ccs_read_unlock(idx); |
677 |
|
return error; |
678 |
|
} |
679 |
|
|
680 |
|
/** |
681 |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list. |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list. |
682 |
* |
* |
683 |
* @data: String to parse. |
* @data: String to parse. |
684 |
* @domain: Pointer to "struct domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
685 |
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
* @condition: Pointer to "struct ccs_condition". May be NULL. |
686 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
687 |
* |
* |
688 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
689 |
*/ |
*/ |
690 |
int ccs_write_network_policy(char *data, struct domain_info *domain, |
int ccs_write_network_policy(char *data, struct ccs_domain_info *domain, |
691 |
const struct ccs_condition_list *condition, |
struct ccs_condition *condition, |
692 |
const bool is_delete) |
const bool is_delete) |
693 |
{ |
{ |
694 |
|
char *w[4]; |
695 |
u8 sock_type; |
u8 sock_type; |
696 |
u8 operation; |
u8 operation; |
697 |
u8 record_type; |
u8 record_type; |
698 |
u16 min_address[8]; |
u16 min_address[8]; |
699 |
u16 max_address[8]; |
u16 max_address[8]; |
700 |
struct ccs_address_group_entry *group = NULL; |
const char *group_name = NULL; |
701 |
u16 min_port; |
u16 min_port; |
702 |
u16 max_port; |
u16 max_port; |
703 |
u8 count; |
u8 count; |
704 |
char *cp1 = strchr(data, ' '); |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
705 |
char *cp2; |
return -EINVAL; |
706 |
if (!cp1) |
if (!strcmp(w[0], "TCP")) |
|
goto out; |
|
|
cp1++; |
|
|
if (!strncmp(data, "TCP ", 4)) |
|
707 |
sock_type = SOCK_STREAM; |
sock_type = SOCK_STREAM; |
708 |
else if (!strncmp(data, "UDP ", 4)) |
else if (!strcmp(w[0], "UDP")) |
709 |
sock_type = SOCK_DGRAM; |
sock_type = SOCK_DGRAM; |
710 |
else if (!strncmp(data, "RAW ", 4)) |
else if (!strcmp(w[0], "RAW")) |
711 |
sock_type = SOCK_RAW; |
sock_type = SOCK_RAW; |
712 |
else |
else |
713 |
goto out; |
goto out; |
714 |
cp2 = strchr(cp1, ' '); |
if (!strcmp(w[1], "bind")) |
|
if (!cp2) |
|
|
goto out; |
|
|
cp2++; |
|
|
if (!strncmp(cp1, "bind ", 5)) |
|
715 |
switch (sock_type) { |
switch (sock_type) { |
716 |
case SOCK_STREAM: |
case SOCK_STREAM: |
717 |
operation = NETWORK_ACL_TCP_BIND; |
operation = NETWORK_ACL_TCP_BIND; |
722 |
default: |
default: |
723 |
operation = NETWORK_ACL_RAW_BIND; |
operation = NETWORK_ACL_RAW_BIND; |
724 |
} |
} |
725 |
else if (!strncmp(cp1, "connect ", 8)) |
else if (!strcmp(w[1], "connect")) |
726 |
switch (sock_type) { |
switch (sock_type) { |
727 |
case SOCK_STREAM: |
case SOCK_STREAM: |
728 |
operation = NETWORK_ACL_TCP_CONNECT; |
operation = NETWORK_ACL_TCP_CONNECT; |
733 |
default: |
default: |
734 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = NETWORK_ACL_RAW_CONNECT; |
735 |
} |
} |
736 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen")) |
737 |
operation = NETWORK_ACL_TCP_LISTEN; |
operation = NETWORK_ACL_TCP_LISTEN; |
738 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept")) |
739 |
operation = NETWORK_ACL_TCP_ACCEPT; |
operation = NETWORK_ACL_TCP_ACCEPT; |
740 |
else |
else |
741 |
goto out; |
goto out; |
742 |
cp1 = strchr(cp2, ' '); |
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
|
if (!cp1) |
|
|
goto out; |
|
|
*cp1++ = '\0'; |
|
|
switch (ccs_parse_ip_address(cp2, min_address, max_address)) { |
|
743 |
case 2: |
case 2: |
744 |
record_type = IP_RECORD_TYPE_IPv6; |
record_type = IP_RECORD_TYPE_IPv6; |
745 |
break; |
break; |
747 |
record_type = IP_RECORD_TYPE_IPv4; |
record_type = IP_RECORD_TYPE_IPv4; |
748 |
break; |
break; |
749 |
default: |
default: |
750 |
if (*cp2 != '@') |
if (w[2][0] != '@') |
751 |
goto out; |
goto out; |
752 |
group = ccs_find_or_assign_new_address_group(cp2 + 1); |
group_name = w[2] + 1; |
|
if (!group) |
|
|
return -ENOMEM; |
|
753 |
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
754 |
break; |
break; |
755 |
} |
} |
756 |
if (strchr(cp1, ' ')) |
count = sscanf(w[3], "%hu-%hu", &min_port, &max_port); |
|
goto out; |
|
|
count = sscanf(cp1, "%hu-%hu", &min_port, &max_port); |
|
757 |
if (count != 1 && count != 2) |
if (count != 1 && count != 2) |
758 |
goto out; |
goto out; |
759 |
if (count == 1) |
if (count == 1) |
760 |
max_port = min_port; |
max_port = min_port; |
761 |
return ccs_update_network_entry(operation, record_type, group, |
return ccs_update_network_entry(operation, record_type, group_name, |
762 |
(u32 *) min_address, |
(u32 *) min_address, |
763 |
(u32 *) max_address, |
(u32 *) max_address, |
764 |
min_port, max_port, domain, condition, |
min_port, max_port, domain, condition, |
856 |
const u16 port) |
const u16 port) |
857 |
{ |
{ |
858 |
int retval; |
int retval; |
859 |
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
860 |
retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, |
retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, |
861 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
862 |
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
863 |
return retval; |
return retval; |
864 |
} |
} |
865 |
|
|
906 |
const u8 operation |
const u8 operation |
907 |
= (sock_type == SOCK_DGRAM) ? |
= (sock_type == SOCK_DGRAM) ? |
908 |
NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
909 |
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
910 |
retval = ccs_check_network_entry(is_ipv6, operation, |
retval = ccs_check_network_entry(is_ipv6, operation, |
911 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
912 |
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
913 |
return retval; |
return retval; |
914 |
} |
} |
915 |
|
|
922 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
923 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
924 |
return 0; |
return 0; |
925 |
if (family == PF_PACKET && !ccs_capable(TOMOYO_USE_PACKET_SOCKET)) |
if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET)) |
926 |
return -EPERM; |
return -EPERM; |
927 |
if (family == PF_ROUTE && !ccs_capable(TOMOYO_USE_ROUTE_SOCKET)) |
if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET)) |
928 |
return -EPERM; |
return -EPERM; |
929 |
if (family != PF_INET && family != PF_INET6) |
if (family != PF_INET && family != PF_INET6) |
930 |
return 0; |
return 0; |
931 |
switch (type) { |
switch (type) { |
932 |
case SOCK_STREAM: |
case SOCK_STREAM: |
933 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CREATE)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_CREATE)) |
934 |
error = -EPERM; |
error = -EPERM; |
935 |
break; |
break; |
936 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
937 |
if (!ccs_capable(TOMOYO_USE_INET_DGRAM_SOCKET)) |
if (!ccs_capable(CCS_USE_INET_DGRAM_SOCKET)) |
938 |
error = -EPERM; |
error = -EPERM; |
939 |
break; |
break; |
940 |
case SOCK_RAW: |
case SOCK_RAW: |
941 |
if (!ccs_capable(TOMOYO_USE_INET_RAW_SOCKET)) |
if (!ccs_capable(CCS_USE_INET_RAW_SOCKET)) |
942 |
error = -EPERM; |
error = -EPERM; |
943 |
break; |
break; |
944 |
} |
} |
963 |
default: |
default: |
964 |
return 0; |
return 0; |
965 |
} |
} |
966 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_LISTEN)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN)) |
967 |
return -EPERM; |
return -EPERM; |
968 |
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
969 |
return -EPERM; |
return -EPERM; |
1037 |
switch (sock->sk->sk_family) { |
switch (sock->sk->sk_family) { |
1038 |
case PF_INET: |
case PF_INET: |
1039 |
case PF_INET6: |
case PF_INET6: |
1040 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CONNECT)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT)) |
1041 |
error = -EPERM; |
error = -EPERM; |
1042 |
break; |
break; |
1043 |
} |
} |
1198 |
#endif |
#endif |
1199 |
#endif |
#endif |
1200 |
|
|
1201 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) |
1202 |
|
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
1203 |
|
unsigned int flags) |
1204 |
|
{ |
1205 |
|
/* Clear queue. */ |
1206 |
|
if (flags & MSG_PEEK) { |
1207 |
|
int clear = 0; |
1208 |
|
spin_lock_irq(&sk->sk_receive_queue.lock); |
1209 |
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
1210 |
|
__skb_unlink(skb, &sk->sk_receive_queue); |
1211 |
|
clear = 1; |
1212 |
|
} |
1213 |
|
spin_unlock_irq(&sk->sk_receive_queue.lock); |
1214 |
|
if (clear) |
1215 |
|
kfree_skb(skb); |
1216 |
|
} |
1217 |
|
skb_free_datagram(sk, skb); |
1218 |
|
} |
1219 |
|
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16) |
1220 |
|
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
1221 |
|
unsigned int flags) |
1222 |
|
{ |
1223 |
|
/* Clear queue. */ |
1224 |
|
if (flags & MSG_PEEK) { |
1225 |
|
int clear = 0; |
1226 |
|
spin_lock_bh(&sk->sk_receive_queue.lock); |
1227 |
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
1228 |
|
__skb_unlink(skb, &sk->sk_receive_queue); |
1229 |
|
clear = 1; |
1230 |
|
} |
1231 |
|
spin_unlock_bh(&sk->sk_receive_queue.lock); |
1232 |
|
if (clear) |
1233 |
|
kfree_skb(skb); |
1234 |
|
} |
1235 |
|
skb_free_datagram(sk, skb); |
1236 |
|
} |
1237 |
|
#endif |
1238 |
|
|
1239 |
/* |
/* |
1240 |
* Check permission for receiving a datagram via a UDP or RAW socket. |
* Check permission for receiving a datagram via a UDP or RAW socket. |
1241 |
* |
* |
1242 |
* Currently, the LSM hook for this purpose is not provided. |
* Currently, the LSM hook for this purpose is not provided. |
1243 |
*/ |
*/ |
1244 |
int ccs_socket_recv_datagram_permission(struct sock *sk, struct sk_buff *skb, |
int ccs_socket_recvmsg_permission(struct sock *sk, struct sk_buff *skb, |
1245 |
const unsigned int flags) |
const unsigned int flags) |
1246 |
{ |
{ |
1247 |
int error = 0; |
int error = 0; |
1248 |
const unsigned int type = sk->sk_type; |
const unsigned int type = sk->sk_type; |
1249 |
/* Nothing to do if I didn't receive a datagram. */ |
if (type != SOCK_DGRAM && type != SOCK_RAW) |
|
if (!skb) |
|
|
return 0; |
|
|
/* Nothing to do if I can't sleep. */ |
|
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
|
|
if (in_interrupt()) |
|
|
return 0; |
|
|
#else |
|
|
if (in_atomic()) |
|
1250 |
return 0; |
return 0; |
|
#endif |
|
1251 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
1252 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
1253 |
return 0; |
return 0; |
|
if (type != SOCK_DGRAM && type != SOCK_RAW) |
|
|
return 0; |
|
1254 |
|
|
1255 |
switch (sk->sk_family) { |
switch (sk->sk_family) { |
1256 |
struct in6_addr sin6; |
struct in6_addr sin6; |
1286 |
} |
} |
1287 |
if (!error) |
if (!error) |
1288 |
return 0; |
return 0; |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
|
|
lock_sock(sk); |
|
|
#endif |
|
1289 |
/* |
/* |
1290 |
* Remove from queue if MSG_PEEK is used so that |
* Remove from queue if MSG_PEEK is used so that |
1291 |
* the head message from unwanted source in receive queue will not |
* the head message from unwanted source in receive queue will not |
1292 |
* prevent the caller from picking up next message from wanted source |
* prevent the caller from picking up next message from wanted source |
1293 |
* when the caller is using MSG_PEEK flag for picking up. |
* when the caller is using MSG_PEEK flag for picking up. |
1294 |
*/ |
*/ |
|
if (flags & MSG_PEEK) { |
|
|
unsigned long cpu_flags; |
|
|
/***** CRITICAL SECTION START *****/ |
|
|
spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags); |
|
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
|
|
__skb_unlink(skb, &sk->sk_receive_queue); |
|
|
atomic_dec(&skb->users); |
|
|
} |
|
|
spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags); |
|
|
/***** CRITICAL SECTION END *****/ |
|
|
} |
|
|
/* Drop reference count. */ |
|
|
skb_free_datagram(sk, skb); |
|
1295 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
1296 |
release_sock(sk); |
if (type == SOCK_DGRAM) |
1297 |
|
lock_sock(sk); |
1298 |
|
#endif |
1299 |
|
skb_kill_datagram(sk, skb, flags); |
1300 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
1301 |
|
if (type == SOCK_DGRAM) |
1302 |
|
release_sock(sk); |
1303 |
#endif |
#endif |
1304 |
/* Hope less harmful than -EPERM. */ |
/* Hope less harmful than -EPERM. */ |
1305 |
return -EAGAIN; |
return -ENOMEM; |
1306 |
} |
} |
1307 |
|
EXPORT_SYMBOL(ccs_socket_recvmsg_permission); |