1 |
/* |
/* |
2 |
* fs/tomoyo_network.c |
* security/ccsecurity/network.c |
3 |
* |
* |
4 |
* Implementation of the Domain-Based Mandatory Access Control. |
* Copyright (C) 2005-2010 NTT DATA CORPORATION |
5 |
* |
* |
6 |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
* Version: 1.7.2 2010/04/01 |
|
* |
|
|
* Version: 1.6.6-pre 2009/01/05 |
|
7 |
* |
* |
8 |
* 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. |
9 |
* See README.ccs for ChangeLog. |
* See README.ccs for ChangeLog. |
10 |
* |
* |
11 |
*/ |
*/ |
12 |
|
|
|
#include <linux/ccs_common.h> |
|
|
#include <linux/tomoyo.h> |
|
|
#include <linux/realpath.h> |
|
13 |
#include <linux/net.h> |
#include <linux/net.h> |
14 |
#include <linux/inet.h> |
#include <linux/inet.h> |
15 |
#include <linux/in.h> |
#include <linux/in.h> |
17 |
#include <net/ip.h> |
#include <net/ip.h> |
18 |
#include <net/ipv6.h> |
#include <net/ipv6.h> |
19 |
#include <net/udp.h> |
#include <net/udp.h> |
20 |
|
#include "internal.h" |
|
/* Index numbers for Network Controls. */ |
|
|
enum ccs_network_acl_index { |
|
|
NETWORK_ACL_UDP_BIND, |
|
|
NETWORK_ACL_UDP_CONNECT, |
|
|
NETWORK_ACL_TCP_BIND, |
|
|
NETWORK_ACL_TCP_LISTEN, |
|
|
NETWORK_ACL_TCP_CONNECT, |
|
|
NETWORK_ACL_TCP_ACCEPT, |
|
|
NETWORK_ACL_RAW_BIND, |
|
|
NETWORK_ACL_RAW_CONNECT |
|
|
}; |
|
21 |
|
|
22 |
/** |
/** |
23 |
* ccs_audit_network_log - Audit network log. |
* ccs_audit_network_log - Audit network log. |
24 |
* |
* |
25 |
* @r: Pointer to "struct ccs_request_info". |
* @r: Pointer to "struct ccs_request_info". |
|
* @operation: The name of operation. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* @is_granted: True if this is a granted log. |
|
26 |
* |
* |
27 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
28 |
*/ |
*/ |
29 |
static int ccs_audit_network_log(struct ccs_request_info *r, |
static int ccs_audit_network_log(struct ccs_request_info *r) |
|
const char *operation, const char *address, |
|
|
const u16 port, const bool is_granted) |
|
|
{ |
|
|
return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK |
|
|
"%s %s %u\n", operation, address, port); |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_save_ipv6_address - Keep the given IPv6 address on the RAM. |
|
|
* |
|
|
* @addr: Pointer to "struct in6_addr". |
|
|
* |
|
|
* Returns pointer to "struct in6_addr" on success, NULL otherwise. |
|
|
* |
|
|
* The RAM is shared, so NEVER try to modify or kfree() the returned address. |
|
|
*/ |
|
|
static const struct in6_addr *ccs_save_ipv6_address(const struct in6_addr *addr) |
|
30 |
{ |
{ |
31 |
static const u8 ccs_block_size = 16; |
char buf[128]; |
32 |
struct ccs_addr_list { |
const char *operation = ccs_net2keyword(r->param.network.operation); |
33 |
/* Workaround for gcc 4.3's bug. */ |
const u32 *address = r->param.network.address; |
34 |
struct in6_addr addr[16]; /* = ccs_block_size */ |
const u16 port = r->param.network.port; |
35 |
struct list1_head list; |
if (r->param.network.is_ipv6) |
36 |
u32 in_use_count; |
ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *) |
37 |
}; |
address, (const struct in6_addr *) address); |
38 |
static LIST1_HEAD(ccs_address_list); |
else |
39 |
struct ccs_addr_list *ptr; |
ccs_print_ipv4(buf, sizeof(buf), r->param.network.ip, |
40 |
static DEFINE_MUTEX(lock); |
r->param.network.ip); |
41 |
u8 i = ccs_block_size; |
ccs_write_log(r, CCS_KEYWORD_ALLOW_NETWORK "%s %s %u\n", operation, |
42 |
if (!addr) |
buf, port); |
43 |
return NULL; |
if (r->granted) |
44 |
mutex_lock(&lock); |
return 0; |
45 |
list1_for_each_entry(ptr, &ccs_address_list, list) { |
ccs_warn_log(r, "%s %s %u", operation, buf, port); |
46 |
for (i = 0; i < ptr->in_use_count; i++) { |
return ccs_supervisor(r, CCS_KEYWORD_ALLOW_NETWORK "%s %s %u\n", |
47 |
if (!memcmp(&ptr->addr[i], addr, sizeof(*addr))) |
operation, buf, port); |
|
goto ok; |
|
|
} |
|
|
if (i < ccs_block_size) |
|
|
break; |
|
|
} |
|
|
if (i == ccs_block_size) { |
|
|
ptr = ccs_alloc_element(sizeof(*ptr)); |
|
|
if (!ptr) |
|
|
goto ok; |
|
|
list1_add_tail_mb(&ptr->list, &ccs_address_list); |
|
|
i = 0; |
|
|
} |
|
|
ptr->addr[ptr->in_use_count++] = *addr; |
|
|
ok: |
|
|
mutex_unlock(&lock); |
|
|
return ptr ? &ptr->addr[i] : NULL; |
|
|
} |
|
|
|
|
|
/* The list for "struct ccs_address_group_entry". */ |
|
|
static LIST1_HEAD(ccs_address_group_list); |
|
|
|
|
|
/** |
|
|
* ccs_update_address_group_entry - Update "struct ccs_address_group_entry" list. |
|
|
* |
|
|
* @group_name: The name of address group. |
|
|
* @is_ipv6: True if @min_address and @max_address are IPv6 addresses. |
|
|
* @min_address: Start of IPv4 or IPv6 address range. |
|
|
* @max_address: End of IPv4 or IPv6 address range. |
|
|
* @is_delete: True if it is a delete request. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int ccs_update_address_group_entry(const char *group_name, |
|
|
const bool is_ipv6, |
|
|
const u16 *min_address, |
|
|
const u16 *max_address, |
|
|
const bool is_delete) |
|
|
{ |
|
|
static DEFINE_MUTEX(lock); |
|
|
struct ccs_address_group_entry *new_group; |
|
|
struct ccs_address_group_entry *group; |
|
|
struct ccs_address_group_member *new_member; |
|
|
struct ccs_address_group_member *member; |
|
|
const struct ccs_path_info *saved_group_name; |
|
|
const struct in6_addr *saved_min_address = NULL; |
|
|
const struct in6_addr *saved_max_address = NULL; |
|
|
int error = -ENOMEM; |
|
|
bool found = false; |
|
|
if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) || |
|
|
!group_name[0]) |
|
|
return -EINVAL; |
|
|
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) |
|
|
return -ENOMEM; |
|
|
not_ipv6: |
|
|
mutex_lock(&lock); |
|
|
list1_for_each_entry(group, &ccs_address_group_list, list) { |
|
|
if (saved_group_name != group->group_name) |
|
|
continue; |
|
|
list1_for_each_entry(member, |
|
|
&group->address_group_member_list, |
|
|
list) { |
|
|
if (member->is_ipv6 != is_ipv6) |
|
|
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; |
|
|
} |
|
|
found = true; |
|
|
break; |
|
|
} |
|
|
if (is_delete) { |
|
|
error = -ENOENT; |
|
|
goto out; |
|
|
} |
|
|
if (!found) { |
|
|
new_group = ccs_alloc_element(sizeof(*new_group)); |
|
|
if (!new_group) |
|
|
goto out; |
|
|
INIT_LIST1_HEAD(&new_group->address_group_member_list); |
|
|
new_group->group_name = saved_group_name; |
|
|
list1_add_tail_mb(&new_group->list, &ccs_address_group_list); |
|
|
group = new_group; |
|
|
} |
|
|
new_member = ccs_alloc_element(sizeof(*new_member)); |
|
|
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; |
|
|
out: |
|
|
mutex_unlock(&lock); |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
|
|
return error; |
|
48 |
} |
} |
49 |
|
|
50 |
/** |
/** |
54 |
* @min: Pointer to store min address. |
* @min: Pointer to store min address. |
55 |
* @max: Pointer to store max address. |
* @max: Pointer to store max address. |
56 |
* |
* |
57 |
* Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise. |
* Returns CCS_IP_ADDRESS_TYPE_IPv6 if @address is an IPv6, |
58 |
|
* CCS_IP_ADDRESS_TYPE_IPv4 if @address is an IPv4, |
59 |
|
* CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP otherwise. |
60 |
*/ |
*/ |
61 |
static int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
62 |
{ |
{ |
63 |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
64 |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
74 |
min[i] = htons(min[i]); |
min[i] = htons(min[i]); |
75 |
max[i] = htons(max[i]); |
max[i] = htons(max[i]); |
76 |
} |
} |
77 |
return 2; |
return CCS_IP_ADDRESS_TYPE_IPv6; |
78 |
} |
} |
79 |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
80 |
&min[0], &min[1], &min[2], &min[3], |
&min[0], &min[1], &min[2], &min[3], |
84 |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
85 |
memmove(min, &ip, sizeof(ip)); |
memmove(min, &ip, sizeof(ip)); |
86 |
if (count == 8) |
if (count == 8) |
87 |
ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16) |
ip = htonl((((u8) max[0]) << 24) |
88 |
|
+ (((u8) max[1]) << 16) |
89 |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
90 |
memmove(max, &ip, sizeof(ip)); |
memmove(max, &ip, sizeof(ip)); |
91 |
return 1; |
return CCS_IP_ADDRESS_TYPE_IPv4; |
92 |
} |
} |
93 |
return 0; |
return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
94 |
} |
} |
95 |
|
|
96 |
/** |
/** |
97 |
* ccs_write_address_group_policy - Write "struct ccs_address_group_entry" list. |
* ccs_print_ipv4 - Print an IPv4 address. |
|
* |
|
|
* @data: String to parse. |
|
|
* @is_delete: True if it is a delete request. |
|
98 |
* |
* |
99 |
* Returns 0 on success, negative value otherwise. |
* @buffer: Buffer to write to. |
100 |
*/ |
* @buffer_len: Size of @buffer. |
101 |
int ccs_write_address_group_policy(char *data, const bool is_delete) |
* @min_ip: Min address in host byte order. |
102 |
{ |
* @max_ip: Max address in host byte order. |
|
bool is_ipv6; |
|
|
u16 min_address[8]; |
|
|
u16 max_address[8]; |
|
|
char *cp = strchr(data, ' '); |
|
|
if (!cp) |
|
|
return -EINVAL; |
|
|
*cp++ = '\0'; |
|
|
switch (ccs_parse_ip_address(cp, min_address, max_address)) { |
|
|
case 2: |
|
|
is_ipv6 = true; |
|
|
break; |
|
|
case 1: |
|
|
is_ipv6 = false; |
|
|
break; |
|
|
default: |
|
|
return -EINVAL; |
|
|
} |
|
|
return ccs_update_address_group_entry(data, is_ipv6, min_address, |
|
|
max_address, is_delete); |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_find_or_assign_new_address_group - Create 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_group - Check whether the given address matches members of the given address group. |
|
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @group: Pointer to "struct ccs_address_group_entry". |
|
|
* |
|
|
* Returns true if @address matches addresses in @group group, false otherwise. |
|
|
*/ |
|
|
static bool ccs_address_matches_group(const bool is_ipv6, const u32 *address, |
|
|
const struct ccs_address_group_entry * |
|
|
group) |
|
|
{ |
|
|
struct ccs_address_group_member *member; |
|
|
const u32 ip = ntohl(*address); |
|
|
list1_for_each_entry(member, &group->address_group_member_list, |
|
|
list) { |
|
|
if (member->is_deleted) |
|
|
continue; |
|
|
if (member->is_ipv6) { |
|
|
if (is_ipv6 && |
|
|
memcmp(member->min.ipv6, address, 16) <= 0 && |
|
|
memcmp(address, member->max.ipv6, 16) <= 0) |
|
|
return true; |
|
|
} else { |
|
|
if (!is_ipv6 && |
|
|
member->min.ipv4 <= ip && ip <= member->max.ipv4) |
|
|
return true; |
|
|
} |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_read_address_group_policy - Read "struct ccs_address_group_entry" list. |
|
|
* |
|
|
* @head: Pointer to "struct ccs_io_buffer". |
|
103 |
* |
* |
104 |
* Returns true on success, false otherwise. |
* Returns nothing. |
105 |
*/ |
*/ |
106 |
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
void ccs_print_ipv4(char *buffer, const int buffer_len, |
107 |
|
const u32 min_ip, const u32 max_ip) |
108 |
{ |
{ |
109 |
struct list1_head *gpos; |
memset(buffer, 0, buffer_len); |
110 |
struct list1_head *mpos; |
snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u", |
111 |
list1_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) { |
HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-', |
112 |
struct ccs_address_group_entry *group; |
HIPQUAD(max_ip)); |
|
group = list1_entry(gpos, struct ccs_address_group_entry, list); |
|
|
list1_for_each_cookie(mpos, head->read_var2, |
|
|
&group->address_group_member_list) { |
|
|
char buf[128]; |
|
|
struct ccs_address_group_member *member; |
|
|
member = list1_entry(mpos, |
|
|
struct ccs_address_group_member, |
|
|
list); |
|
|
if (member->is_deleted) |
|
|
continue; |
|
|
if (member->is_ipv6) { |
|
|
const struct in6_addr *min_address |
|
|
= member->min.ipv6; |
|
|
const struct in6_addr *max_address |
|
|
= member->max.ipv6; |
|
|
ccs_print_ipv6(buf, sizeof(buf), min_address); |
|
|
if (min_address != max_address) { |
|
|
int len; |
|
|
char *cp = buf + strlen(buf); |
|
|
*cp++ = '-'; |
|
|
len = strlen(buf); |
|
|
ccs_print_ipv6(cp, sizeof(buf) - len, |
|
|
max_address); |
|
|
} |
|
|
} else { |
|
|
const u32 min_address = member->min.ipv4; |
|
|
const u32 max_address = member->max.ipv4; |
|
|
memset(buf, 0, sizeof(buf)); |
|
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", |
|
|
HIPQUAD(min_address)); |
|
|
if (min_address != max_address) { |
|
|
const int len = strlen(buf); |
|
|
snprintf(buf + len, |
|
|
sizeof(buf) - 1 - len, |
|
|
"-%u.%u.%u.%u", |
|
|
HIPQUAD(max_address)); |
|
|
} |
|
|
} |
|
|
if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP |
|
|
"%s %s\n", group->group_name->name, |
|
|
buf)) |
|
|
goto out; |
|
|
} |
|
|
} |
|
|
return true; |
|
|
out: |
|
|
return false; |
|
113 |
} |
} |
114 |
|
|
115 |
#if !defined(NIP6) |
#if !defined(NIP6) |
116 |
#define NIP6(addr) \ |
#define NIP6(addr) \ |
117 |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
118 |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
119 |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
120 |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
121 |
#endif |
#endif |
122 |
|
|
123 |
/** |
/** |
125 |
* |
* |
126 |
* @buffer: Buffer to write to. |
* @buffer: Buffer to write to. |
127 |
* @buffer_len: Size of @buffer. |
* @buffer_len: Size of @buffer. |
128 |
* @ip: Pointer to "struct in6_addr". |
* @min_ip: Pointer to "struct in6_addr". |
129 |
|
* @max_ip: Pointer to "struct in6_addr". |
130 |
* |
* |
131 |
* Returns nothing. |
* Returns nothing. |
132 |
*/ |
*/ |
133 |
void ccs_print_ipv6(char *buffer, const int buffer_len, |
void ccs_print_ipv6(char *buffer, const int buffer_len, |
134 |
const struct in6_addr *ip) |
const struct in6_addr *min_ip, |
135 |
|
const struct in6_addr *max_ip) |
136 |
{ |
{ |
137 |
memset(buffer, 0, buffer_len); |
memset(buffer, 0, buffer_len); |
138 |
snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip)); |
snprintf(buffer, buffer_len - 1, |
139 |
|
"%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x", |
140 |
|
NIP6(*min_ip), min_ip == max_ip ? '\0' : '-', |
141 |
|
NIP6(*max_ip)); |
142 |
} |
} |
143 |
|
|
144 |
/** |
/** |
152 |
{ |
{ |
153 |
const char *keyword = "unknown"; |
const char *keyword = "unknown"; |
154 |
switch (operation) { |
switch (operation) { |
155 |
case NETWORK_ACL_UDP_BIND: |
case CCS_NETWORK_UDP_BIND: |
156 |
keyword = "UDP bind"; |
keyword = "UDP bind"; |
157 |
break; |
break; |
158 |
case NETWORK_ACL_UDP_CONNECT: |
case CCS_NETWORK_UDP_CONNECT: |
159 |
keyword = "UDP connect"; |
keyword = "UDP connect"; |
160 |
break; |
break; |
161 |
case NETWORK_ACL_TCP_BIND: |
case CCS_NETWORK_TCP_BIND: |
162 |
keyword = "TCP bind"; |
keyword = "TCP bind"; |
163 |
break; |
break; |
164 |
case NETWORK_ACL_TCP_LISTEN: |
case CCS_NETWORK_TCP_LISTEN: |
165 |
keyword = "TCP listen"; |
keyword = "TCP listen"; |
166 |
break; |
break; |
167 |
case NETWORK_ACL_TCP_CONNECT: |
case CCS_NETWORK_TCP_CONNECT: |
168 |
keyword = "TCP connect"; |
keyword = "TCP connect"; |
169 |
break; |
break; |
170 |
case NETWORK_ACL_TCP_ACCEPT: |
case CCS_NETWORK_TCP_ACCEPT: |
171 |
keyword = "TCP accept"; |
keyword = "TCP accept"; |
172 |
break; |
break; |
173 |
case NETWORK_ACL_RAW_BIND: |
case CCS_NETWORK_RAW_BIND: |
174 |
keyword = "RAW bind"; |
keyword = "RAW bind"; |
175 |
break; |
break; |
176 |
case NETWORK_ACL_RAW_CONNECT: |
case CCS_NETWORK_RAW_CONNECT: |
177 |
keyword = "RAW connect"; |
keyword = "RAW connect"; |
178 |
break; |
break; |
179 |
} |
} |
180 |
return keyword; |
return keyword; |
181 |
} |
} |
182 |
|
|
183 |
|
static bool ccs_check_network_acl(const struct ccs_request_info *r, |
184 |
|
const struct ccs_acl_info *ptr) |
185 |
|
{ |
186 |
|
const struct ccs_ip_network_acl *acl = |
187 |
|
container_of(ptr, typeof(*acl), head); |
188 |
|
bool ret; |
189 |
|
if (!(acl->perm & (1 << r->param.network.operation)) || |
190 |
|
!ccs_compare_number_union(r->param.network.port, &acl->port)) |
191 |
|
return false; |
192 |
|
switch (acl->address_type) { |
193 |
|
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
194 |
|
ret = ccs_address_matches_group(r->param.network.is_ipv6, |
195 |
|
r->param.network.address, |
196 |
|
acl->address.group); |
197 |
|
break; |
198 |
|
case CCS_IP_ADDRESS_TYPE_IPv4: |
199 |
|
ret = !r->param.network.is_ipv6 && |
200 |
|
acl->address.ipv4.min <= r->param.network.ip && |
201 |
|
r->param.network.ip <= acl->address.ipv4.max; |
202 |
|
break; |
203 |
|
default: |
204 |
|
ret = r->param.network.is_ipv6 && |
205 |
|
memcmp(acl->address.ipv6.min, r->param.network.address, |
206 |
|
16) <= 0 && |
207 |
|
memcmp(r->param.network.address, acl->address.ipv6.max, |
208 |
|
16) <= 0; |
209 |
|
break; |
210 |
|
} |
211 |
|
return ret; |
212 |
|
} |
213 |
|
|
214 |
/** |
/** |
215 |
* ccs_update_network_entry - Update "struct ccs_ip_network_acl_record" list. |
* ccs_network_entry2 - Check permission for network operation. |
216 |
* |
* |
217 |
* @operation: Type of operation. |
* @is_ipv6: True if @address is an IPv6 address. |
218 |
* @record_type: Type of address. |
* @operation: Type of operation. |
219 |
* @group: Pointer to "struct ccs_address_group_entry". May be NULL. |
* @address: An IPv4 or IPv6 address. |
220 |
* @min_address: Start of IPv4 or IPv6 address range. |
* @port: Port number. |
|
* @max_address: End of IPv4 or IPv6 address range. |
|
|
* @min_port: Start of port number range. |
|
|
* @max_port: End of port number range. |
|
|
* @domain: Pointer to "struct domain_info". |
|
|
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
|
|
* @is_delete: True if it is a delete request. |
|
221 |
* |
* |
222 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
223 |
|
* |
224 |
|
* Caller holds ccs_read_lock(). |
225 |
*/ |
*/ |
226 |
static int ccs_update_network_entry(const u8 operation, const u8 record_type, |
static int ccs_network_entry2(const bool is_ipv6, const u8 operation, |
227 |
const struct ccs_address_group_entry *group, |
const u32 *address, const u16 port) |
228 |
const u32 *min_address, |
{ |
229 |
const u32 *max_address, |
struct ccs_request_info r; |
230 |
const u16 min_port, const u16 max_port, |
int error; |
231 |
struct domain_info *domain, |
if (ccs_init_request_info(&r, CCS_MAC_NETWORK_UDP_BIND + operation) |
232 |
const struct ccs_condition_list *condition, |
== CCS_CONFIG_DISABLED) |
233 |
const bool is_delete) |
return 0; |
234 |
{ |
r.param_type = CCS_TYPE_IP_NETWORK_ACL; |
235 |
static DEFINE_MUTEX(lock); |
r.param.network.operation = operation; |
236 |
struct ccs_acl_info *ptr; |
r.param.network.is_ipv6 = is_ipv6; |
237 |
struct ccs_ip_network_acl_record *acl; |
r.param.network.address = address; |
238 |
int error = -ENOMEM; |
r.param.network.port = port; |
239 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
/* using host byte order to allow u32 comparison than memcmp().*/ |
240 |
const u32 min_ip = ntohl(*min_address); |
r.param.network.ip = ntohl(*address); |
241 |
const u32 max_ip = ntohl(*max_address); |
do { |
242 |
const struct in6_addr *saved_min_address = NULL; |
ccs_check_acl(&r, ccs_check_network_acl); |
243 |
const struct in6_addr *saved_max_address = NULL; |
error = ccs_audit_network_log(&r); |
244 |
if (!domain) |
} while (error == CCS_RETRY_REQUEST); |
245 |
return -EINVAL; |
if (r.mode != CCS_CONFIG_ENFORCING) |
246 |
if (record_type != IP_RECORD_TYPE_IPv6) |
error = 0; |
|
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) |
|
|
return -ENOMEM; |
|
|
not_ipv6: |
|
|
mutex_lock(&lock); |
|
|
if (is_delete) |
|
|
goto delete; |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
|
|
if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
if (ccs_get_condition_part(ptr) != condition) |
|
|
continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (acl->operation_type != operation || |
|
|
acl->record_type != record_type || |
|
|
acl->min_port != min_port || max_port != acl->max_port) |
|
|
continue; |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
if (acl->u.group != group) |
|
|
continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
if (acl->u.ipv4.min != min_ip || |
|
|
max_ip != acl->u.ipv4.max) |
|
|
continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
|
|
if (acl->u.ipv6.min != saved_min_address || |
|
|
saved_max_address != acl->u.ipv6.max) |
|
|
continue; |
|
|
} |
|
|
error = ccs_add_domain_acl(NULL, ptr); |
|
|
goto out; |
|
|
} |
|
|
/* Not found. Append it to the tail. */ |
|
|
acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition); |
|
|
if (!acl) |
|
|
goto out; |
|
|
acl->operation_type = operation; |
|
|
acl->record_type = record_type; |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
acl->u.group = group; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
acl->u.ipv4.min = min_ip; |
|
|
acl->u.ipv4.max = max_ip; |
|
|
} else { |
|
|
acl->u.ipv6.min = saved_min_address; |
|
|
acl->u.ipv6.max = saved_max_address; |
|
|
} |
|
|
acl->min_port = min_port; |
|
|
acl->max_port = max_port; |
|
|
error = ccs_add_domain_acl(domain, &acl->head); |
|
|
goto out; |
|
|
delete: |
|
|
error = -ENOENT; |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
|
|
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
if (ccs_get_condition_part(ptr) != condition) |
|
|
continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (acl->operation_type != operation || |
|
|
acl->record_type != record_type || |
|
|
acl->min_port != min_port || max_port != acl->max_port) |
|
|
continue; |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
if (acl->u.group != group) |
|
|
continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
if (acl->u.ipv4.min != min_ip || |
|
|
max_ip != acl->u.ipv4.max) |
|
|
continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
|
|
if (acl->u.ipv6.min != saved_min_address || |
|
|
saved_max_address != acl->u.ipv6.max) |
|
|
continue; |
|
|
} |
|
|
error = ccs_del_domain_acl(ptr); |
|
|
break; |
|
|
} |
|
|
out: |
|
|
mutex_unlock(&lock); |
|
247 |
return error; |
return error; |
248 |
} |
} |
249 |
|
|
250 |
/** |
/** |
251 |
* ccs_check_network_entry - Check permission for network operation. |
* ccs_network_entry - Check permission for network operation. |
252 |
* |
* |
253 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
254 |
* @operation: Type of operation. |
* @operation: Type of operation. |
257 |
* |
* |
258 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
259 |
*/ |
*/ |
260 |
static int ccs_check_network_entry(const bool is_ipv6, const u8 operation, |
static int ccs_network_entry(const bool is_ipv6, const u8 operation, |
261 |
const u32 *address, const u16 port) |
const u32 *address, const u16 port) |
262 |
{ |
{ |
263 |
struct ccs_request_info r; |
const int idx = ccs_read_lock(); |
264 |
struct ccs_acl_info *ptr; |
const int error = ccs_network_entry2(is_ipv6, operation, address, |
265 |
const char *keyword = ccs_net2keyword(operation); |
port); |
266 |
bool is_enforce; |
ccs_read_unlock(idx); |
267 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
return error; |
268 |
const u32 ip = ntohl(*address); |
} |
269 |
bool found = false; |
|
270 |
char buf[64]; |
static bool ccs_same_ip_network_acl(const struct ccs_acl_info *a, |
271 |
if (!ccs_can_sleep()) |
const struct ccs_acl_info *b) |
272 |
return 0; |
{ |
273 |
ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_NETWORK); |
const struct ccs_ip_network_acl *p1 = container_of(a, typeof(*p1), |
274 |
is_enforce = (r.mode == 3); |
head); |
275 |
if (!r.mode) |
const struct ccs_ip_network_acl *p2 = container_of(b, typeof(*p2), |
276 |
return 0; |
head); |
277 |
retry: |
return ccs_same_acl_head(&p1->head, &p2->head) |
278 |
list1_for_each_entry(ptr, &r.domain->acl_info_list, list) { |
&& p1->address_type == p2->address_type && |
279 |
struct ccs_ip_network_acl_record *acl; |
p1->address.ipv4.min == p2->address.ipv4.min && |
280 |
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
p1->address.ipv6.min == p2->address.ipv6.min && |
281 |
continue; |
p1->address.ipv4.max == p2->address.ipv4.max && |
282 |
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
p1->address.ipv6.max == p2->address.ipv6.max && |
283 |
if (acl->operation_type != operation || port < acl->min_port || |
p1->address.group == p2->address.group && |
284 |
acl->max_port < port || !ccs_check_condition(&r, ptr)) |
ccs_same_number_union(&p1->port, &p2->port); |
285 |
continue; |
} |
286 |
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
287 |
if (!ccs_address_matches_group(is_ipv6, address, |
static bool ccs_merge_ip_network_acl(struct ccs_acl_info *a, |
288 |
acl->u.group)) |
struct ccs_acl_info *b, |
289 |
continue; |
const bool is_delete) |
290 |
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
{ |
291 |
if (is_ipv6 || |
u8 * const a_perm = &container_of(a, struct ccs_ip_network_acl, head) |
292 |
ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) |
->perm; |
293 |
continue; |
u8 perm = *a_perm; |
294 |
} else { |
const u8 b_perm = container_of(b, struct ccs_ip_network_acl, head) |
295 |
if (!is_ipv6 || |
->perm; |
296 |
memcmp(acl->u.ipv6.min, address, 16) > 0 || |
if (is_delete) |
297 |
memcmp(address, acl->u.ipv6.max, 16) > 0) |
perm &= ~b_perm; |
|
continue; |
|
|
} |
|
|
r.cond = ccs_get_condition_part(ptr); |
|
|
found = true; |
|
|
break; |
|
|
} |
|
|
memset(buf, 0, sizeof(buf)); |
|
|
if (is_ipv6) |
|
|
ccs_print_ipv6(buf, sizeof(buf), |
|
|
(const struct in6_addr *) address); |
|
298 |
else |
else |
299 |
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
perm |= b_perm; |
300 |
ccs_audit_network_log(&r, keyword, buf, port, found); |
*a_perm = perm; |
301 |
if (found) |
return !perm; |
|
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)); |
|
|
if (is_enforce) { |
|
|
int error = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK |
|
|
"%s %s %u\n", keyword, buf, |
|
|
port); |
|
|
if (error == 1) |
|
|
goto retry; |
|
|
return error; |
|
|
} |
|
|
if (r.mode == 1 && ccs_check_domain_quota(r.domain)) |
|
|
ccs_update_network_entry(operation, is_ipv6 ? |
|
|
IP_RECORD_TYPE_IPv6 : |
|
|
IP_RECORD_TYPE_IPv4, |
|
|
NULL, address, address, port, port, |
|
|
r.domain, ccs_handler_cond(), 0); |
|
|
return 0; |
|
302 |
} |
} |
303 |
|
|
304 |
/** |
/** |
305 |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list. |
* ccs_write_network - Write "struct ccs_ip_network_acl" list. |
306 |
* |
* |
307 |
* @data: String to parse. |
* @data: String to parse. |
308 |
* @domain: Pointer to "struct domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
309 |
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
* @condition: Pointer to "struct ccs_condition". May be NULL. |
310 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
311 |
* |
* |
312 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
313 |
*/ |
*/ |
314 |
int ccs_write_network_policy(char *data, struct domain_info *domain, |
int ccs_write_network(char *data, struct ccs_domain_info *domain, |
315 |
const struct ccs_condition_list *condition, |
struct ccs_condition *condition, const bool is_delete) |
|
const bool is_delete) |
|
316 |
{ |
{ |
317 |
u8 sock_type; |
struct ccs_ip_network_acl e = { |
318 |
u8 operation; |
.head.type = CCS_TYPE_IP_NETWORK_ACL, |
319 |
u8 record_type; |
.head.cond = condition, |
320 |
|
}; |
321 |
u16 min_address[8]; |
u16 min_address[8]; |
322 |
u16 max_address[8]; |
u16 max_address[8]; |
323 |
struct ccs_address_group_entry *group = NULL; |
int error = is_delete ? -ENOENT : -ENOMEM; |
324 |
u16 min_port; |
u8 sock_type; |
325 |
u16 max_port; |
char *w[4]; |
326 |
u8 count; |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
327 |
char *cp1 = strchr(data, ' '); |
return -EINVAL; |
328 |
char *cp2; |
if (!strcmp(w[0], "TCP")) |
|
if (!cp1) |
|
|
goto out; |
|
|
cp1++; |
|
|
if (!strncmp(data, "TCP ", 4)) |
|
329 |
sock_type = SOCK_STREAM; |
sock_type = SOCK_STREAM; |
330 |
else if (!strncmp(data, "UDP ", 4)) |
else if (!strcmp(w[0], "UDP")) |
331 |
sock_type = SOCK_DGRAM; |
sock_type = SOCK_DGRAM; |
332 |
else if (!strncmp(data, "RAW ", 4)) |
else if (!strcmp(w[0], "RAW")) |
333 |
sock_type = SOCK_RAW; |
sock_type = SOCK_RAW; |
334 |
else |
else |
335 |
goto out; |
return -EINVAL; |
336 |
cp2 = strchr(cp1, ' '); |
if (!strcmp(w[1], "bind")) |
|
if (!cp2) |
|
|
goto out; |
|
|
cp2++; |
|
|
if (!strncmp(cp1, "bind ", 5)) |
|
337 |
switch (sock_type) { |
switch (sock_type) { |
338 |
case SOCK_STREAM: |
case SOCK_STREAM: |
339 |
operation = NETWORK_ACL_TCP_BIND; |
e.perm = 1 << CCS_NETWORK_TCP_BIND; |
340 |
break; |
break; |
341 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
342 |
operation = NETWORK_ACL_UDP_BIND; |
e.perm = 1 << CCS_NETWORK_UDP_BIND; |
343 |
break; |
break; |
344 |
default: |
default: |
345 |
operation = NETWORK_ACL_RAW_BIND; |
e.perm = 1 << CCS_NETWORK_RAW_BIND; |
346 |
|
break; |
347 |
} |
} |
348 |
else if (!strncmp(cp1, "connect ", 8)) |
else if (!strcmp(w[1], "connect")) |
349 |
switch (sock_type) { |
switch (sock_type) { |
350 |
case SOCK_STREAM: |
case SOCK_STREAM: |
351 |
operation = NETWORK_ACL_TCP_CONNECT; |
e.perm = 1 << CCS_NETWORK_TCP_CONNECT; |
352 |
break; |
break; |
353 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
354 |
operation = NETWORK_ACL_UDP_CONNECT; |
e.perm = 1 << CCS_NETWORK_UDP_CONNECT; |
355 |
break; |
break; |
356 |
default: |
default: |
357 |
operation = NETWORK_ACL_RAW_CONNECT; |
e.perm = 1 << CCS_NETWORK_RAW_CONNECT; |
358 |
|
break; |
359 |
} |
} |
360 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen")) |
361 |
operation = NETWORK_ACL_TCP_LISTEN; |
e.perm = 1 << CCS_NETWORK_TCP_LISTEN; |
362 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept")) |
363 |
operation = NETWORK_ACL_TCP_ACCEPT; |
e.perm = 1 << CCS_NETWORK_TCP_ACCEPT; |
364 |
else |
else |
365 |
goto out; |
return -EINVAL; |
366 |
cp1 = strchr(cp2, ' '); |
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
367 |
if (!cp1) |
case CCS_IP_ADDRESS_TYPE_IPv6: |
368 |
goto out; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv6; |
369 |
*cp1++ = '\0'; |
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
370 |
switch (ccs_parse_ip_address(cp2, min_address, max_address)) { |
min_address); |
371 |
case 2: |
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
372 |
record_type = IP_RECORD_TYPE_IPv6; |
max_address); |
373 |
|
if (!e.address.ipv6.min || !e.address.ipv6.max) |
374 |
|
goto out; |
375 |
break; |
break; |
376 |
case 1: |
case CCS_IP_ADDRESS_TYPE_IPv4: |
377 |
record_type = IP_RECORD_TYPE_IPv4; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv4; |
378 |
|
/* use host byte order to allow u32 comparison.*/ |
379 |
|
e.address.ipv4.min = ntohl(*(u32 *) min_address); |
380 |
|
e.address.ipv4.max = ntohl(*(u32 *) max_address); |
381 |
break; |
break; |
382 |
default: |
default: |
383 |
if (*cp2 != '@') |
if (w[2][0] != '@') |
384 |
goto out; |
return -EINVAL; |
385 |
group = ccs_find_or_assign_new_address_group(cp2 + 1); |
e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
386 |
if (!group) |
e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP); |
387 |
|
if (!e.address.group) |
388 |
return -ENOMEM; |
return -ENOMEM; |
|
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
|
389 |
break; |
break; |
390 |
} |
} |
391 |
if (strchr(cp1, ' ')) |
if (!ccs_parse_number_union(w[3], &e.port)) |
|
goto out; |
|
|
count = sscanf(cp1, "%hu-%hu", &min_port, &max_port); |
|
|
if (count != 1 && count != 2) |
|
392 |
goto out; |
goto out; |
393 |
if (count == 1) |
error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain, |
394 |
max_port = min_port; |
ccs_same_ip_network_acl, |
395 |
return ccs_update_network_entry(operation, record_type, group, |
ccs_merge_ip_network_acl); |
|
(u32 *) min_address, |
|
|
(u32 *) max_address, |
|
|
min_port, max_port, domain, condition, |
|
|
is_delete); |
|
396 |
out: |
out: |
397 |
return -EINVAL; |
if (w[2][0] == '@') |
398 |
|
ccs_put_group(e.address.group); |
399 |
|
else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) { |
400 |
|
ccs_put_ipv6_address(e.address.ipv6.min); |
401 |
|
ccs_put_ipv6_address(e.address.ipv6.max); |
402 |
|
} |
403 |
|
ccs_put_number_union(&e.port); |
404 |
|
return error; |
405 |
} |
} |
406 |
|
|
407 |
/** |
/** |
408 |
* ccs_check_network_listen_acl - Check permission for listen() operation. |
* ccs_network_listen_acl - Check permission for listen() operation. |
409 |
* |
* |
410 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
411 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address. |
413 |
* |
* |
414 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
415 |
*/ |
*/ |
416 |
static inline int ccs_check_network_listen_acl(const bool is_ipv6, |
static int ccs_network_listen_acl(const bool is_ipv6, const u8 *address, |
417 |
const u8 *address, |
const u16 port) |
|
const u16 port) |
|
418 |
{ |
{ |
419 |
return ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN, |
return ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN, |
420 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
421 |
} |
} |
422 |
|
|
423 |
/** |
/** |
424 |
* ccs_check_network_connect_acl - Check permission for connect() operation. |
* ccs_network_connect_acl - Check permission for connect() operation. |
425 |
* |
* |
426 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
427 |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
430 |
* |
* |
431 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
432 |
*/ |
*/ |
433 |
static inline int ccs_check_network_connect_acl(const bool is_ipv6, |
static int ccs_network_connect_acl(const bool is_ipv6, const int sock_type, |
434 |
const int sock_type, |
const u8 *address, const u16 port) |
|
const u8 *address, |
|
|
const u16 port) |
|
435 |
{ |
{ |
436 |
u8 operation; |
u8 operation; |
437 |
switch (sock_type) { |
switch (sock_type) { |
438 |
case SOCK_STREAM: |
case SOCK_STREAM: |
439 |
operation = NETWORK_ACL_TCP_CONNECT; |
operation = CCS_NETWORK_TCP_CONNECT; |
440 |
break; |
break; |
441 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
442 |
operation = NETWORK_ACL_UDP_CONNECT; |
operation = CCS_NETWORK_UDP_CONNECT; |
443 |
break; |
break; |
444 |
default: |
default: |
445 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = CCS_NETWORK_RAW_CONNECT; |
446 |
} |
} |
447 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
448 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
449 |
} |
} |
450 |
|
|
451 |
/** |
/** |
452 |
* ccs_check_network_bind_acl - Check permission for bind() operation. |
* ccs_network_bind_acl - Check permission for bind() operation. |
453 |
* |
* |
454 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
455 |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
458 |
* |
* |
459 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
460 |
*/ |
*/ |
461 |
static int ccs_check_network_bind_acl(const bool is_ipv6, const int sock_type, |
static int ccs_network_bind_acl(const bool is_ipv6, const int sock_type, |
462 |
const u8 *address, const u16 port) |
const u8 *address, const u16 port) |
463 |
{ |
{ |
464 |
u8 operation; |
u8 operation; |
465 |
switch (sock_type) { |
switch (sock_type) { |
466 |
case SOCK_STREAM: |
case SOCK_STREAM: |
467 |
operation = NETWORK_ACL_TCP_BIND; |
operation = CCS_NETWORK_TCP_BIND; |
468 |
break; |
break; |
469 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
470 |
operation = NETWORK_ACL_UDP_BIND; |
operation = CCS_NETWORK_UDP_BIND; |
471 |
break; |
break; |
472 |
default: |
default: |
473 |
operation = NETWORK_ACL_RAW_BIND; |
operation = CCS_NETWORK_RAW_BIND; |
474 |
} |
} |
475 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
476 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
477 |
} |
} |
478 |
|
|
479 |
/** |
/** |
480 |
* ccs_check_network_accept_acl - Check permission for accept() operation. |
* ccs_network_accept_acl - Check permission for accept() operation. |
481 |
* |
* |
482 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
483 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address. |
485 |
* |
* |
486 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
487 |
*/ |
*/ |
488 |
static inline int ccs_check_network_accept_acl(const bool is_ipv6, |
static int ccs_network_accept_acl(const bool is_ipv6, const u8 *address, |
489 |
const u8 *address, |
const u16 port) |
|
const u16 port) |
|
490 |
{ |
{ |
491 |
int retval; |
int retval; |
492 |
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
493 |
retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, |
retval = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT, |
494 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
495 |
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
496 |
return retval; |
return retval; |
497 |
} |
} |
498 |
|
|
499 |
/** |
/** |
500 |
* ccs_check_network_sendmsg_acl - Check permission for sendmsg() operation. |
* ccs_network_sendmsg_acl - Check permission for sendmsg() operation. |
501 |
* |
* |
502 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
503 |
* @sock_type: Type of socket. (UDP or RAW) |
* @sock_type: Type of socket. (UDP or RAW) |
506 |
* |
* |
507 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
508 |
*/ |
*/ |
509 |
static inline int ccs_check_network_sendmsg_acl(const bool is_ipv6, |
static int ccs_network_sendmsg_acl(const bool is_ipv6, const int sock_type, |
510 |
const int sock_type, |
const u8 *address, const u16 port) |
|
const u8 *address, |
|
|
const u16 port) |
|
511 |
{ |
{ |
512 |
u8 operation; |
u8 operation; |
513 |
if (sock_type == SOCK_DGRAM) |
if (sock_type == SOCK_DGRAM) |
514 |
operation = NETWORK_ACL_UDP_CONNECT; |
operation = CCS_NETWORK_UDP_CONNECT; |
515 |
else |
else |
516 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = CCS_NETWORK_RAW_CONNECT; |
517 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
518 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
519 |
} |
} |
520 |
|
|
521 |
/** |
/** |
522 |
* ccs_check_network_recvmsg_acl - Check permission for recvmsg() operation. |
* ccs_network_recvmsg_acl - Check permission for recvmsg() operation. |
523 |
* |
* |
524 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
525 |
* @sock_type: Type of socket. (UDP or RAW) |
* @sock_type: Type of socket. (UDP or RAW) |
528 |
* |
* |
529 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
530 |
*/ |
*/ |
531 |
static inline int ccs_check_network_recvmsg_acl(const bool is_ipv6, |
static int ccs_network_recvmsg_acl(const bool is_ipv6, const int sock_type, |
532 |
const int sock_type, |
const u8 *address, const u16 port) |
|
const u8 *address, |
|
|
const u16 port) |
|
533 |
{ |
{ |
534 |
int retval; |
int retval; |
535 |
const u8 operation |
const u8 operation |
536 |
= (sock_type == SOCK_DGRAM) ? |
= (sock_type == SOCK_DGRAM) ? |
537 |
NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
CCS_NETWORK_UDP_CONNECT : CCS_NETWORK_RAW_CONNECT; |
538 |
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
539 |
retval = ccs_check_network_entry(is_ipv6, operation, |
retval = ccs_network_entry(is_ipv6, operation, |
540 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
541 |
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
542 |
return retval; |
return retval; |
543 |
} |
} |
544 |
|
|
545 |
|
#ifndef CONFIG_NET |
546 |
|
|
547 |
|
void __init ccs_network_init(void) |
548 |
|
{ |
549 |
|
} |
550 |
|
|
551 |
|
#else |
552 |
|
|
553 |
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
554 |
|
|
555 |
/* Check permission for creating a socket. */ |
/* Check permission for creating a socket. */ |
556 |
int ccs_socket_create_permission(int family, int type, int protocol) |
static int __ccs_socket_create_permission(int family, int type, int protocol) |
557 |
{ |
{ |
558 |
int error = 0; |
int error = 0; |
559 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
560 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
561 |
return 0; |
return 0; |
562 |
if (family == PF_PACKET && !ccs_capable(TOMOYO_USE_PACKET_SOCKET)) |
if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET)) |
563 |
return -EPERM; |
return -EPERM; |
564 |
if (family == PF_ROUTE && !ccs_capable(TOMOYO_USE_ROUTE_SOCKET)) |
if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET)) |
565 |
return -EPERM; |
return -EPERM; |
566 |
if (family != PF_INET && family != PF_INET6) |
if (family != PF_INET && family != PF_INET6) |
567 |
return 0; |
return 0; |
568 |
switch (type) { |
switch (type) { |
569 |
case SOCK_STREAM: |
case SOCK_STREAM: |
570 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CREATE)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_CREATE)) |
571 |
error = -EPERM; |
error = -EPERM; |
572 |
break; |
break; |
573 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
574 |
if (!ccs_capable(TOMOYO_USE_INET_DGRAM_SOCKET)) |
if (!ccs_capable(CCS_USE_INET_DGRAM_SOCKET)) |
575 |
error = -EPERM; |
error = -EPERM; |
576 |
break; |
break; |
577 |
case SOCK_RAW: |
case SOCK_RAW: |
578 |
if (!ccs_capable(TOMOYO_USE_INET_RAW_SOCKET)) |
if (!ccs_capable(CCS_USE_INET_RAW_SOCKET)) |
579 |
error = -EPERM; |
error = -EPERM; |
580 |
break; |
break; |
581 |
} |
} |
583 |
} |
} |
584 |
|
|
585 |
/* Check permission for listening a TCP socket. */ |
/* Check permission for listening a TCP socket. */ |
586 |
int ccs_socket_listen_permission(struct socket *sock) |
static int __ccs_socket_listen_permission(struct socket *sock) |
587 |
{ |
{ |
588 |
int error = 0; |
int error = 0; |
589 |
char addr[MAX_SOCK_ADDR]; |
char addr[MAX_SOCK_ADDR]; |
600 |
default: |
default: |
601 |
return 0; |
return 0; |
602 |
} |
} |
603 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_LISTEN)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN)) |
604 |
return -EPERM; |
return -EPERM; |
605 |
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
606 |
return -EPERM; |
return -EPERM; |
609 |
struct sockaddr_in *addr4; |
struct sockaddr_in *addr4; |
610 |
case AF_INET6: |
case AF_INET6: |
611 |
addr6 = (struct sockaddr_in6 *) addr; |
addr6 = (struct sockaddr_in6 *) addr; |
612 |
error = ccs_check_network_listen_acl(true, |
error = ccs_network_listen_acl(true, |
613 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
614 |
addr6->sin6_port); |
addr6->sin6_port); |
615 |
break; |
break; |
616 |
case AF_INET: |
case AF_INET: |
617 |
addr4 = (struct sockaddr_in *) addr; |
addr4 = (struct sockaddr_in *) addr; |
618 |
error = ccs_check_network_listen_acl(false, |
error = ccs_network_listen_acl(false, |
619 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
620 |
addr4->sin_port); |
addr4->sin_port); |
621 |
break; |
break; |
622 |
} |
} |
623 |
return error; |
return error; |
624 |
} |
} |
625 |
|
|
626 |
/* Check permission for setting the remote IP address/port pair of a socket. */ |
/* Check permission for setting the remote IP address/port pair of a socket. */ |
627 |
int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_connect_permission(struct socket *sock, |
628 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
629 |
{ |
{ |
630 |
int error = 0; |
int error = 0; |
631 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
652 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
653 |
else |
else |
654 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
655 |
error = ccs_check_network_connect_acl(true, type, |
error = ccs_network_connect_acl(true, type, |
656 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
657 |
port); |
port); |
658 |
break; |
break; |
659 |
case AF_INET: |
case AF_INET: |
660 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
664 |
port = addr4->sin_port; |
port = addr4->sin_port; |
665 |
else |
else |
666 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
667 |
error = ccs_check_network_connect_acl(false, type, |
error = ccs_network_connect_acl(false, type, |
668 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
669 |
port); |
port); |
670 |
break; |
break; |
671 |
} |
} |
672 |
if (type != SOCK_STREAM) |
if (type != SOCK_STREAM) |
674 |
switch (sock->sk->sk_family) { |
switch (sock->sk->sk_family) { |
675 |
case PF_INET: |
case PF_INET: |
676 |
case PF_INET6: |
case PF_INET6: |
677 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CONNECT)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT)) |
678 |
error = -EPERM; |
error = -EPERM; |
679 |
break; |
break; |
680 |
} |
} |
682 |
} |
} |
683 |
|
|
684 |
/* Check permission for setting the local IP address/port pair of a socket. */ |
/* Check permission for setting the local IP address/port pair of a socket. */ |
685 |
int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_bind_permission(struct socket *sock, |
686 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
687 |
{ |
{ |
688 |
int error = 0; |
int error = 0; |
689 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
710 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
711 |
else |
else |
712 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
713 |
error = ccs_check_network_bind_acl(true, type, |
error = ccs_network_bind_acl(true, type, |
714 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
715 |
port); |
port); |
716 |
break; |
break; |
717 |
case AF_INET: |
case AF_INET: |
718 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
722 |
port = addr4->sin_port; |
port = addr4->sin_port; |
723 |
else |
else |
724 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
725 |
error = ccs_check_network_bind_acl(false, type, |
error = ccs_network_bind_acl(false, type, |
726 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
727 |
port); |
port); |
728 |
break; |
break; |
729 |
} |
} |
730 |
return error; |
return error; |
735 |
* |
* |
736 |
* Currently, the LSM hook for this purpose is not provided. |
* Currently, the LSM hook for this purpose is not provided. |
737 |
*/ |
*/ |
738 |
int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr) |
static int __ccs_socket_accept_permission(struct socket *sock, |
739 |
|
struct sockaddr *addr) |
740 |
{ |
{ |
741 |
int error = 0; |
int error = 0; |
742 |
int addr_len; |
int addr_len; |
758 |
struct sockaddr_in *addr4; |
struct sockaddr_in *addr4; |
759 |
case AF_INET6: |
case AF_INET6: |
760 |
addr6 = (struct sockaddr_in6 *) addr; |
addr6 = (struct sockaddr_in6 *) addr; |
761 |
error = ccs_check_network_accept_acl(true, |
error = ccs_network_accept_acl(true, |
762 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
763 |
addr6->sin6_port); |
addr6->sin6_port); |
764 |
break; |
break; |
765 |
case AF_INET: |
case AF_INET: |
766 |
addr4 = (struct sockaddr_in *) addr; |
addr4 = (struct sockaddr_in *) addr; |
767 |
error = ccs_check_network_accept_acl(false, |
error = ccs_network_accept_acl(false, |
768 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
769 |
addr4->sin_port); |
addr4->sin_port); |
770 |
break; |
break; |
771 |
} |
} |
772 |
return error; |
return error; |
773 |
} |
} |
774 |
|
|
775 |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
776 |
int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_sendmsg_permission(struct socket *sock, |
777 |
int addr_len) |
struct msghdr *msg, int size) |
778 |
{ |
{ |
779 |
|
struct sockaddr *addr = (struct sockaddr *) msg->msg_name; |
780 |
|
const int addr_len = msg->msg_namelen; |
781 |
int error = 0; |
int error = 0; |
782 |
const int type = sock->type; |
const int type = sock->type; |
783 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
797 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
798 |
else |
else |
799 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
800 |
error = ccs_check_network_sendmsg_acl(true, type, |
error = ccs_network_sendmsg_acl(true, type, |
801 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
802 |
port); |
port); |
803 |
break; |
break; |
804 |
case AF_INET: |
case AF_INET: |
805 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
809 |
port = addr4->sin_port; |
port = addr4->sin_port; |
810 |
else |
else |
811 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
812 |
error = ccs_check_network_sendmsg_acl(false, type, |
error = ccs_network_sendmsg_acl(false, type, |
813 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, port); |
|
port); |
|
814 |
break; |
break; |
815 |
} |
} |
816 |
return error; |
return error; |
818 |
|
|
819 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
820 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
821 |
|
#if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2 |
822 |
|
|
823 |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
824 |
{ |
{ |
837 |
|
|
838 |
#endif |
#endif |
839 |
#endif |
#endif |
840 |
|
#endif |
841 |
|
|
842 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) |
843 |
|
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
844 |
|
unsigned int flags) |
845 |
|
{ |
846 |
|
/* Clear queue. */ |
847 |
|
if (flags & MSG_PEEK) { |
848 |
|
int clear = 0; |
849 |
|
spin_lock_irq(&sk->sk_receive_queue.lock); |
850 |
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
851 |
|
__skb_unlink(skb, &sk->sk_receive_queue); |
852 |
|
clear = 1; |
853 |
|
} |
854 |
|
spin_unlock_irq(&sk->sk_receive_queue.lock); |
855 |
|
if (clear) |
856 |
|
kfree_skb(skb); |
857 |
|
} |
858 |
|
skb_free_datagram(sk, skb); |
859 |
|
} |
860 |
|
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16) |
861 |
|
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
862 |
|
unsigned int flags) |
863 |
|
{ |
864 |
|
/* Clear queue. */ |
865 |
|
if (flags & MSG_PEEK) { |
866 |
|
int clear = 0; |
867 |
|
spin_lock_bh(&sk->sk_receive_queue.lock); |
868 |
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
869 |
|
__skb_unlink(skb, &sk->sk_receive_queue); |
870 |
|
clear = 1; |
871 |
|
} |
872 |
|
spin_unlock_bh(&sk->sk_receive_queue.lock); |
873 |
|
if (clear) |
874 |
|
kfree_skb(skb); |
875 |
|
} |
876 |
|
skb_free_datagram(sk, skb); |
877 |
|
} |
878 |
|
#endif |
879 |
|
|
880 |
/* |
/* |
881 |
* Check permission for receiving a datagram via a UDP or RAW socket. |
* Check permission for receiving a datagram via a UDP or RAW socket. |
882 |
* |
* |
883 |
* Currently, the LSM hook for this purpose is not provided. |
* Currently, the LSM hook for this purpose is not provided. |
884 |
*/ |
*/ |
885 |
int ccs_socket_recv_datagram_permission(struct sock *sk, struct sk_buff *skb, |
static int __ccs_socket_recvmsg_permission(struct sock *sk, |
886 |
const unsigned int flags) |
struct sk_buff *skb, |
887 |
|
const unsigned int flags) |
888 |
{ |
{ |
889 |
int error = 0; |
int error = 0; |
890 |
const unsigned int type = sk->sk_type; |
const unsigned int type = sk->sk_type; |
891 |
/* 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()) |
|
892 |
return 0; |
return 0; |
|
#endif |
|
893 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
894 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
895 |
return 0; |
return 0; |
|
if (type != SOCK_DGRAM && type != SOCK_RAW) |
|
|
return 0; |
|
896 |
|
|
897 |
switch (sk->sk_family) { |
switch (sk->sk_family) { |
898 |
struct in6_addr sin6; |
struct in6_addr sin6; |
911 |
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
912 |
port = htons(sk->sk_protocol); |
port = htons(sk->sk_protocol); |
913 |
} |
} |
914 |
error = ccs_check_network_recvmsg_acl(true, type, |
error = ccs_network_recvmsg_acl(true, type, |
915 |
(u8 *) &sin6, port); |
(u8 *) &sin6, port); |
916 |
break; |
break; |
917 |
case PF_INET: |
case PF_INET: |
918 |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
922 |
sin4.s_addr = ip_hdr(skb)->saddr; |
sin4.s_addr = ip_hdr(skb)->saddr; |
923 |
port = htons(sk->sk_protocol); |
port = htons(sk->sk_protocol); |
924 |
} |
} |
925 |
error = ccs_check_network_recvmsg_acl(false, type, |
error = ccs_network_recvmsg_acl(false, type, |
926 |
(u8 *) &sin4, port); |
(u8 *) &sin4, port); |
927 |
break; |
break; |
928 |
} |
} |
929 |
if (!error) |
if (!error) |
930 |
return 0; |
return 0; |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
|
|
lock_sock(sk); |
|
|
#endif |
|
931 |
/* |
/* |
932 |
* Remove from queue if MSG_PEEK is used so that |
* Remove from queue if MSG_PEEK is used so that |
933 |
* the head message from unwanted source in receive queue will not |
* the head message from unwanted source in receive queue will not |
934 |
* prevent the caller from picking up next message from wanted source |
* prevent the caller from picking up next message from wanted source |
935 |
* when the caller is using MSG_PEEK flag for picking up. |
* when the caller is using MSG_PEEK flag for picking up. |
936 |
*/ |
*/ |
|
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); |
|
937 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
938 |
release_sock(sk); |
if (type == SOCK_DGRAM) |
939 |
|
lock_sock(sk); |
940 |
|
#endif |
941 |
|
skb_kill_datagram(sk, skb, flags); |
942 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
943 |
|
if (type == SOCK_DGRAM) |
944 |
|
release_sock(sk); |
945 |
#endif |
#endif |
946 |
/* Hope less harmful than -EPERM. */ |
/* Hope less harmful than -EPERM. */ |
947 |
return -EAGAIN; |
return -ENOMEM; |
948 |
} |
} |
949 |
|
|
950 |
|
void __init ccs_network_init(void) |
951 |
|
{ |
952 |
|
ccsecurity_ops.socket_create_permission = |
953 |
|
__ccs_socket_create_permission; |
954 |
|
ccsecurity_ops.socket_listen_permission = |
955 |
|
__ccs_socket_listen_permission; |
956 |
|
ccsecurity_ops.socket_connect_permission = |
957 |
|
__ccs_socket_connect_permission; |
958 |
|
ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission; |
959 |
|
ccsecurity_ops.socket_accept_permission = |
960 |
|
__ccs_socket_accept_permission; |
961 |
|
ccsecurity_ops.socket_sendmsg_permission = |
962 |
|
__ccs_socket_sendmsg_permission; |
963 |
|
ccsecurity_ops.socket_recvmsg_permission = |
964 |
|
__ccs_socket_recvmsg_permission; |
965 |
|
} |
966 |
|
|
967 |
|
#endif |