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-pre 2010/03/08 |
|
* |
|
|
* Version: 1.6.7-rc 2009/03/18 |
|
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/tomoyo_socket.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. |
34 |
const char *operation, const char *address, |
const char *operation, const char *address, |
35 |
const u16 port, const bool is_granted) |
const u16 port, const bool is_granted) |
36 |
{ |
{ |
37 |
return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK |
if (!is_granted) |
38 |
|
ccs_warn_log(r, "%s %s %u", operation, address, port); |
39 |
|
return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK |
40 |
"%s %s %u\n", operation, address, port); |
"%s %s %u\n", operation, address, port); |
41 |
} |
} |
42 |
|
|
43 |
/** |
/** |
|
* 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) |
|
|
{ |
|
|
static const u8 ccs_block_size = 16; |
|
|
struct ccs_addr_list { |
|
|
/* Workaround for gcc 4.3's bug. */ |
|
|
struct in6_addr addr[16]; /* = ccs_block_size */ |
|
|
struct list1_head list; |
|
|
u32 in_use_count; |
|
|
}; |
|
|
static LIST1_HEAD(ccs_address_list); |
|
|
struct ccs_addr_list *ptr; |
|
|
static DEFINE_MUTEX(lock); |
|
|
u8 i = ccs_block_size; |
|
|
if (!addr) |
|
|
return NULL; |
|
|
mutex_lock(&lock); |
|
|
list1_for_each_entry(ptr, &ccs_address_list, list) { |
|
|
for (i = 0; i < ptr->in_use_count; i++) { |
|
|
if (!memcmp(&ptr->addr[i], addr, sizeof(*addr))) |
|
|
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; |
|
|
} |
|
|
|
|
|
/** |
|
44 |
* ccs_parse_ip_address - Parse an IP address. |
* ccs_parse_ip_address - Parse an IP address. |
45 |
* |
* |
46 |
* @address: String to parse. |
* @address: String to parse. |
49 |
* |
* |
50 |
* Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise. |
* Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise. |
51 |
*/ |
*/ |
52 |
static int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
53 |
{ |
{ |
54 |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
55 |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
83 |
return 0; |
return 0; |
84 |
} |
} |
85 |
|
|
|
/** |
|
|
* ccs_write_address_group_policy - Write "struct ccs_address_group_entry" list. |
|
|
* |
|
|
* @data: String to parse. |
|
|
* @is_delete: True if it is a delete request. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
int ccs_write_address_group_policy(char *data, const bool is_delete) |
|
|
{ |
|
|
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". |
|
|
* |
|
|
* Returns true on success, false otherwise. |
|
|
*/ |
|
|
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
|
|
{ |
|
|
struct list1_head *gpos; |
|
|
struct list1_head *mpos; |
|
|
list1_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) { |
|
|
struct ccs_address_group_entry *group; |
|
|
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; |
|
|
} |
|
|
|
|
86 |
#if !defined(NIP6) |
#if !defined(NIP6) |
87 |
#define NIP6(addr) \ |
#define NIP6(addr) \ |
88 |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
89 |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
90 |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
91 |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
92 |
#endif |
#endif |
93 |
|
|
94 |
/** |
/** |
118 |
{ |
{ |
119 |
const char *keyword = "unknown"; |
const char *keyword = "unknown"; |
120 |
switch (operation) { |
switch (operation) { |
121 |
case NETWORK_ACL_UDP_BIND: |
case CCS_NETWORK_UDP_BIND: |
122 |
keyword = "UDP bind"; |
keyword = "UDP bind"; |
123 |
break; |
break; |
124 |
case NETWORK_ACL_UDP_CONNECT: |
case CCS_NETWORK_UDP_CONNECT: |
125 |
keyword = "UDP connect"; |
keyword = "UDP connect"; |
126 |
break; |
break; |
127 |
case NETWORK_ACL_TCP_BIND: |
case CCS_NETWORK_TCP_BIND: |
128 |
keyword = "TCP bind"; |
keyword = "TCP bind"; |
129 |
break; |
break; |
130 |
case NETWORK_ACL_TCP_LISTEN: |
case CCS_NETWORK_TCP_LISTEN: |
131 |
keyword = "TCP listen"; |
keyword = "TCP listen"; |
132 |
break; |
break; |
133 |
case NETWORK_ACL_TCP_CONNECT: |
case CCS_NETWORK_TCP_CONNECT: |
134 |
keyword = "TCP connect"; |
keyword = "TCP connect"; |
135 |
break; |
break; |
136 |
case NETWORK_ACL_TCP_ACCEPT: |
case CCS_NETWORK_TCP_ACCEPT: |
137 |
keyword = "TCP accept"; |
keyword = "TCP accept"; |
138 |
break; |
break; |
139 |
case NETWORK_ACL_RAW_BIND: |
case CCS_NETWORK_RAW_BIND: |
140 |
keyword = "RAW bind"; |
keyword = "RAW bind"; |
141 |
break; |
break; |
142 |
case NETWORK_ACL_RAW_CONNECT: |
case CCS_NETWORK_RAW_CONNECT: |
143 |
keyword = "RAW connect"; |
keyword = "RAW connect"; |
144 |
break; |
break; |
145 |
} |
} |
147 |
} |
} |
148 |
|
|
149 |
/** |
/** |
150 |
* ccs_update_network_entry - Update "struct ccs_ip_network_acl_record" list. |
* ccs_network_entry2 - Check permission for network operation. |
151 |
* |
* |
152 |
* @operation: Type of operation. |
* @is_ipv6: True if @address is an IPv6 address. |
153 |
* @record_type: Type of address. |
* @operation: Type of operation. |
154 |
* @group: Pointer to "struct ccs_address_group_entry". May be NULL. |
* @address: An IPv4 or IPv6 address. |
155 |
* @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 ccs_domain_info". |
|
|
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
|
|
* @is_delete: True if it is a delete request. |
|
156 |
* |
* |
157 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
158 |
|
* |
159 |
|
* Caller holds ccs_read_lock(). |
160 |
*/ |
*/ |
161 |
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, |
162 |
const struct ccs_address_group_entry *group, |
const u32 *address, const u16 port) |
|
const u32 *min_address, |
|
|
const u32 *max_address, |
|
|
const u16 min_port, const u16 max_port, |
|
|
struct ccs_domain_info *domain, |
|
|
const struct ccs_condition_list *condition, |
|
|
const bool is_delete) |
|
163 |
{ |
{ |
164 |
static DEFINE_MUTEX(lock); |
struct ccs_request_info r; |
165 |
struct ccs_acl_info *ptr; |
struct ccs_acl_info *ptr; |
166 |
struct ccs_ip_network_acl_record *acl; |
const char *keyword = ccs_net2keyword(operation); |
167 |
int error = -ENOMEM; |
const u16 perm = 1 << operation; |
168 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
/* using host byte order to allow u32 comparison than memcmp().*/ |
169 |
const u32 min_ip = ntohl(*min_address); |
const u32 ip = ntohl(*address); |
170 |
const u32 max_ip = ntohl(*max_address); |
int error; |
171 |
const struct in6_addr *saved_min_address = NULL; |
char buf[64]; |
172 |
const struct in6_addr *saved_max_address = NULL; |
if (ccs_init_request_info(&r, NULL, |
173 |
if (!domain) |
CCS_MAC_NETWORK_UDP_BIND + operation) |
174 |
return -EINVAL; |
== CCS_CONFIG_DISABLED) |
175 |
if (record_type != IP_RECORD_TYPE_IPv6) |
return 0; |
176 |
goto not_ipv6; |
memset(buf, 0, sizeof(buf)); |
177 |
saved_min_address = ccs_save_ipv6_address((struct in6_addr *) |
if (is_ipv6) |
178 |
min_address); |
ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *) |
179 |
saved_max_address = ccs_save_ipv6_address((struct in6_addr *) |
address); |
180 |
max_address); |
else |
181 |
if (!saved_min_address || !saved_max_address) |
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
182 |
return -ENOMEM; |
do { |
183 |
not_ipv6: |
error = -EPERM; |
184 |
mutex_lock(&lock); |
list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) { |
185 |
if (is_delete) |
struct ccs_ip_network_acl *acl; |
186 |
goto delete; |
if (ptr->is_deleted || |
187 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
ptr->type != CCS_TYPE_IP_NETWORK_ACL) |
188 |
if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL) |
continue; |
189 |
continue; |
acl = container_of(ptr, struct ccs_ip_network_acl, |
190 |
if (ccs_get_condition_part(ptr) != condition) |
head); |
191 |
continue; |
if (!(acl->perm & perm)) |
192 |
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
continue; |
193 |
if (acl->operation_type != operation || |
if (!ccs_compare_number_union(port, &acl->port) || |
194 |
acl->record_type != record_type || |
!ccs_condition(&r, ptr)) |
195 |
acl->min_port != min_port || max_port != acl->max_port) |
continue; |
196 |
continue; |
switch (acl->address_type) { |
197 |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
198 |
if (acl->u.group != group) |
if (!ccs_address_matches_group(is_ipv6, |
199 |
continue; |
address, |
200 |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
acl->address. |
201 |
if (acl->u.ipv4.min != min_ip || |
group)) |
202 |
max_ip != acl->u.ipv4.max) |
continue; |
203 |
continue; |
break; |
204 |
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
case CCS_IP_ADDRESS_TYPE_IPv4: |
205 |
if (acl->u.ipv6.min != saved_min_address || |
if (is_ipv6 || ip < acl->address.ipv4.min || |
206 |
saved_max_address != acl->u.ipv6.max) |
acl->address.ipv4.max < ip) |
207 |
continue; |
continue; |
208 |
} |
break; |
209 |
error = ccs_add_domain_acl(NULL, ptr); |
default: |
210 |
goto out; |
if (!is_ipv6 || |
211 |
} |
memcmp(acl->address.ipv6.min, address, 16) |
212 |
/* Not found. Append it to the tail. */ |
> 0 || |
213 |
acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition); |
memcmp(address, acl->address.ipv6.max, 16) |
214 |
if (!acl) |
> 0) |
215 |
goto out; |
continue; |
216 |
acl->operation_type = operation; |
break; |
217 |
acl->record_type = record_type; |
} |
218 |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
r.cond = ptr->cond; |
219 |
acl->u.group = group; |
error = 0; |
220 |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
break; |
|
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; |
|
221 |
} |
} |
222 |
error = ccs_del_domain_acl(ptr); |
ccs_audit_network_log(&r, keyword, buf, port, !error); |
223 |
break; |
if (!error) |
224 |
} |
break; |
225 |
out: |
error = ccs_supervisor(&r, CCS_KEYWORD_ALLOW_NETWORK |
226 |
mutex_unlock(&lock); |
"%s %s %u\n", keyword, buf, port); |
227 |
|
} while (error == CCS_RETRY_REQUEST); |
228 |
|
if (r.mode != CCS_CONFIG_ENFORCING) |
229 |
|
error = 0; |
230 |
return error; |
return error; |
231 |
} |
} |
232 |
|
|
233 |
/** |
/** |
234 |
* ccs_check_network_entry - Check permission for network operation. |
* ccs_network_entry - Check permission for network operation. |
235 |
* |
* |
236 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
237 |
* @operation: Type of operation. |
* @operation: Type of operation. |
240 |
* |
* |
241 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
242 |
*/ |
*/ |
243 |
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, |
244 |
const u32 *address, const u16 port) |
const u32 *address, const u16 port) |
245 |
{ |
{ |
246 |
struct ccs_request_info r; |
const int idx = ccs_read_lock(); |
247 |
struct ccs_acl_info *ptr; |
const int error = ccs_network_entry2(is_ipv6, operation, address, |
248 |
const char *keyword = ccs_net2keyword(operation); |
port); |
249 |
bool is_enforce; |
ccs_read_unlock(idx); |
250 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
return error; |
|
const u32 ip = ntohl(*address); |
|
|
bool found = false; |
|
|
char buf[64]; |
|
|
if (!ccs_can_sleep()) |
|
|
return 0; |
|
|
ccs_init_request_info(&r, NULL, CCS_MAC_FOR_NETWORK); |
|
|
is_enforce = (r.mode == 3); |
|
|
if (!r.mode) |
|
|
return 0; |
|
|
retry: |
|
|
list1_for_each_entry(ptr, &r.domain->acl_info_list, list) { |
|
|
struct ccs_ip_network_acl_record *acl; |
|
|
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (acl->operation_type != operation || port < acl->min_port || |
|
|
acl->max_port < port || !ccs_check_condition(&r, ptr)) |
|
|
continue; |
|
|
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
if (!ccs_address_matches_group(is_ipv6, address, |
|
|
acl->u.group)) |
|
|
continue; |
|
|
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
|
|
if (is_ipv6 || |
|
|
ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) |
|
|
continue; |
|
|
} else { |
|
|
if (!is_ipv6 || |
|
|
memcmp(acl->u.ipv6.min, address, 16) > 0 || |
|
|
memcmp(address, acl->u.ipv6.max, 16) > 0) |
|
|
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); |
|
|
else |
|
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
|
|
ccs_audit_network_log(&r, keyword, buf, port, found); |
|
|
if (found) |
|
|
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_domain_quota_ok(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; |
|
251 |
} |
} |
252 |
|
|
253 |
/** |
/** |
254 |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list. |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl" list. |
255 |
* |
* |
256 |
* @data: String to parse. |
* @data: String to parse. |
257 |
* @domain: Pointer to "struct ccs_domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
258 |
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
* @condition: Pointer to "struct ccs_condition". May be NULL. |
259 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
260 |
* |
* |
261 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
262 |
*/ |
*/ |
263 |
int ccs_write_network_policy(char *data, struct ccs_domain_info *domain, |
int ccs_write_network_policy(char *data, struct ccs_domain_info *domain, |
264 |
const struct ccs_condition_list *condition, |
struct ccs_condition *condition, |
265 |
const bool is_delete) |
const bool is_delete) |
266 |
{ |
{ |
267 |
u8 sock_type; |
struct ccs_ip_network_acl *entry = NULL; |
268 |
u8 operation; |
struct ccs_acl_info *ptr; |
269 |
u8 record_type; |
struct ccs_ip_network_acl e = { |
270 |
|
.head.type = CCS_TYPE_IP_NETWORK_ACL, |
271 |
|
.head.cond = condition, |
272 |
|
}; |
273 |
u16 min_address[8]; |
u16 min_address[8]; |
274 |
u16 max_address[8]; |
u16 max_address[8]; |
275 |
struct ccs_address_group_entry *group = NULL; |
int error = is_delete ? -ENOENT : -ENOMEM; |
276 |
u16 min_port; |
u8 sock_type; |
277 |
u16 max_port; |
char *w[4]; |
278 |
u8 count; |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
279 |
char *cp1 = strchr(data, ' '); |
return -EINVAL; |
280 |
char *cp2; |
if (!strcmp(w[0], "TCP")) |
|
if (!cp1) |
|
|
goto out; |
|
|
cp1++; |
|
|
if (!strncmp(data, "TCP ", 4)) |
|
281 |
sock_type = SOCK_STREAM; |
sock_type = SOCK_STREAM; |
282 |
else if (!strncmp(data, "UDP ", 4)) |
else if (!strcmp(w[0], "UDP")) |
283 |
sock_type = SOCK_DGRAM; |
sock_type = SOCK_DGRAM; |
284 |
else if (!strncmp(data, "RAW ", 4)) |
else if (!strcmp(w[0], "RAW")) |
285 |
sock_type = SOCK_RAW; |
sock_type = SOCK_RAW; |
286 |
else |
else |
287 |
goto out; |
return -EINVAL; |
288 |
cp2 = strchr(cp1, ' '); |
if (!strcmp(w[1], "bind")) |
|
if (!cp2) |
|
|
goto out; |
|
|
cp2++; |
|
|
if (!strncmp(cp1, "bind ", 5)) |
|
289 |
switch (sock_type) { |
switch (sock_type) { |
290 |
case SOCK_STREAM: |
case SOCK_STREAM: |
291 |
operation = NETWORK_ACL_TCP_BIND; |
e.perm = 1 << CCS_NETWORK_TCP_BIND; |
292 |
break; |
break; |
293 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
294 |
operation = NETWORK_ACL_UDP_BIND; |
e.perm = 1 << CCS_NETWORK_UDP_BIND; |
295 |
break; |
break; |
296 |
default: |
default: |
297 |
operation = NETWORK_ACL_RAW_BIND; |
e.perm = 1 << CCS_NETWORK_RAW_BIND; |
298 |
|
break; |
299 |
} |
} |
300 |
else if (!strncmp(cp1, "connect ", 8)) |
else if (!strcmp(w[1], "connect")) |
301 |
switch (sock_type) { |
switch (sock_type) { |
302 |
case SOCK_STREAM: |
case SOCK_STREAM: |
303 |
operation = NETWORK_ACL_TCP_CONNECT; |
e.perm = 1 << CCS_NETWORK_TCP_CONNECT; |
304 |
break; |
break; |
305 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
306 |
operation = NETWORK_ACL_UDP_CONNECT; |
e.perm = 1 << CCS_NETWORK_UDP_CONNECT; |
307 |
break; |
break; |
308 |
default: |
default: |
309 |
operation = NETWORK_ACL_RAW_CONNECT; |
e.perm = 1 << CCS_NETWORK_RAW_CONNECT; |
310 |
|
break; |
311 |
} |
} |
312 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen")) |
313 |
operation = NETWORK_ACL_TCP_LISTEN; |
e.perm = 1 << CCS_NETWORK_TCP_LISTEN; |
314 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept")) |
315 |
operation = NETWORK_ACL_TCP_ACCEPT; |
e.perm = 1 << CCS_NETWORK_TCP_ACCEPT; |
316 |
else |
else |
317 |
goto out; |
return -EINVAL; |
318 |
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)) { |
|
319 |
case 2: |
case 2: |
320 |
record_type = IP_RECORD_TYPE_IPv6; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv6; |
321 |
|
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
322 |
|
min_address); |
323 |
|
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
324 |
|
max_address); |
325 |
|
if (!e.address.ipv6.min || !e.address.ipv6.max) |
326 |
|
goto out; |
327 |
break; |
break; |
328 |
case 1: |
case 1: |
329 |
record_type = IP_RECORD_TYPE_IPv4; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv4; |
330 |
|
/* use host byte order to allow u32 comparison.*/ |
331 |
|
e.address.ipv4.min = ntohl(*(u32 *) min_address); |
332 |
|
e.address.ipv4.max = ntohl(*(u32 *) max_address); |
333 |
break; |
break; |
334 |
default: |
default: |
335 |
if (*cp2 != '@') |
if (w[2][0] != '@') |
336 |
goto out; |
return -EINVAL; |
337 |
group = ccs_find_or_assign_new_address_group(cp2 + 1); |
e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
338 |
if (!group) |
e.address.group = ccs_get_address_group(w[2] + 1); |
339 |
|
if (!e.address.group) |
340 |
return -ENOMEM; |
return -ENOMEM; |
|
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
|
341 |
break; |
break; |
342 |
} |
} |
343 |
if (strchr(cp1, ' ')) |
if (!ccs_parse_number_union(w[3], &e.port)) |
344 |
goto out; |
goto out; |
345 |
count = sscanf(cp1, "%hu-%hu", &min_port, &max_port); |
if (!is_delete) |
346 |
if (count != 1 && count != 2) |
entry = kmalloc(sizeof(e), GFP_KERNEL); |
347 |
goto out; |
mutex_lock(&ccs_policy_lock); |
348 |
if (count == 1) |
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
349 |
max_port = min_port; |
struct ccs_ip_network_acl *acl = |
350 |
return ccs_update_network_entry(operation, record_type, group, |
container_of(ptr, struct ccs_ip_network_acl, head); |
351 |
(u32 *) min_address, |
if (ptr->type != CCS_TYPE_IP_NETWORK_ACL || |
352 |
(u32 *) max_address, |
ptr->cond != condition || |
353 |
min_port, max_port, domain, condition, |
ccs_memcmp(acl, &e, offsetof(typeof(e), address_type), |
354 |
is_delete); |
sizeof(e))) |
355 |
|
continue; |
356 |
|
if (is_delete) { |
357 |
|
acl->perm &= ~e.perm; |
358 |
|
if (!acl->perm) |
359 |
|
ptr->is_deleted = true; |
360 |
|
} else { |
361 |
|
if (ptr->is_deleted) |
362 |
|
acl->perm = 0; |
363 |
|
acl->perm |= e.perm; |
364 |
|
ptr->is_deleted = false; |
365 |
|
} |
366 |
|
error = 0; |
367 |
|
break; |
368 |
|
} |
369 |
|
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
370 |
|
ccs_add_domain_acl(domain, &entry->head); |
371 |
|
entry = NULL; |
372 |
|
error = 0; |
373 |
|
} |
374 |
|
mutex_unlock(&ccs_policy_lock); |
375 |
out: |
out: |
376 |
return -EINVAL; |
if (w[2][0] == '@') |
377 |
|
ccs_put_address_group(e.address.group); |
378 |
|
else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) { |
379 |
|
ccs_put_ipv6_address(e.address.ipv6.min); |
380 |
|
ccs_put_ipv6_address(e.address.ipv6.max); |
381 |
|
} |
382 |
|
ccs_put_number_union(&e.port); |
383 |
|
kfree(entry); |
384 |
|
return error; |
385 |
} |
} |
386 |
|
|
387 |
/** |
/** |
388 |
* ccs_check_network_listen_acl - Check permission for listen() operation. |
* ccs_network_listen_acl - Check permission for listen() operation. |
389 |
* |
* |
390 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
391 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address. |
393 |
* |
* |
394 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
395 |
*/ |
*/ |
396 |
static inline int ccs_check_network_listen_acl(const bool is_ipv6, |
static inline int ccs_network_listen_acl(const bool is_ipv6, const u8 *address, |
397 |
const u8 *address, |
const u16 port) |
|
const u16 port) |
|
398 |
{ |
{ |
399 |
return ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN, |
return ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN, |
400 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
401 |
} |
} |
402 |
|
|
403 |
/** |
/** |
404 |
* ccs_check_network_connect_acl - Check permission for connect() operation. |
* ccs_network_connect_acl - Check permission for connect() operation. |
405 |
* |
* |
406 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
407 |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
410 |
* |
* |
411 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
412 |
*/ |
*/ |
413 |
static inline int ccs_check_network_connect_acl(const bool is_ipv6, |
static inline int ccs_network_connect_acl(const bool is_ipv6, |
414 |
const int sock_type, |
const int sock_type, |
415 |
const u8 *address, |
const u8 *address, const u16 port) |
|
const u16 port) |
|
416 |
{ |
{ |
417 |
u8 operation; |
u8 operation; |
418 |
switch (sock_type) { |
switch (sock_type) { |
419 |
case SOCK_STREAM: |
case SOCK_STREAM: |
420 |
operation = NETWORK_ACL_TCP_CONNECT; |
operation = CCS_NETWORK_TCP_CONNECT; |
421 |
break; |
break; |
422 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
423 |
operation = NETWORK_ACL_UDP_CONNECT; |
operation = CCS_NETWORK_UDP_CONNECT; |
424 |
break; |
break; |
425 |
default: |
default: |
426 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = CCS_NETWORK_RAW_CONNECT; |
427 |
} |
} |
428 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
429 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
430 |
} |
} |
431 |
|
|
432 |
/** |
/** |
433 |
* ccs_check_network_bind_acl - Check permission for bind() operation. |
* ccs_network_bind_acl - Check permission for bind() operation. |
434 |
* |
* |
435 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
436 |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
439 |
* |
* |
440 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
441 |
*/ |
*/ |
442 |
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, |
443 |
const u8 *address, const u16 port) |
const u8 *address, const u16 port) |
444 |
{ |
{ |
445 |
u8 operation; |
u8 operation; |
446 |
switch (sock_type) { |
switch (sock_type) { |
447 |
case SOCK_STREAM: |
case SOCK_STREAM: |
448 |
operation = NETWORK_ACL_TCP_BIND; |
operation = CCS_NETWORK_TCP_BIND; |
449 |
break; |
break; |
450 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
451 |
operation = NETWORK_ACL_UDP_BIND; |
operation = CCS_NETWORK_UDP_BIND; |
452 |
break; |
break; |
453 |
default: |
default: |
454 |
operation = NETWORK_ACL_RAW_BIND; |
operation = CCS_NETWORK_RAW_BIND; |
455 |
} |
} |
456 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
457 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
458 |
} |
} |
459 |
|
|
460 |
/** |
/** |
461 |
* ccs_check_network_accept_acl - Check permission for accept() operation. |
* ccs_network_accept_acl - Check permission for accept() operation. |
462 |
* |
* |
463 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
464 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address. |
466 |
* |
* |
467 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
468 |
*/ |
*/ |
469 |
static inline int ccs_check_network_accept_acl(const bool is_ipv6, |
static inline int ccs_network_accept_acl(const bool is_ipv6, const u8 *address, |
470 |
const u8 *address, |
const u16 port) |
|
const u16 port) |
|
471 |
{ |
{ |
472 |
int retval; |
int retval; |
473 |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
474 |
retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, |
retval = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT, |
475 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
476 |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
477 |
return retval; |
return retval; |
478 |
} |
} |
479 |
|
|
480 |
/** |
/** |
481 |
* ccs_check_network_sendmsg_acl - Check permission for sendmsg() operation. |
* ccs_network_sendmsg_acl - Check permission for sendmsg() operation. |
482 |
* |
* |
483 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
484 |
* @sock_type: Type of socket. (UDP or RAW) |
* @sock_type: Type of socket. (UDP or RAW) |
487 |
* |
* |
488 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
489 |
*/ |
*/ |
490 |
static inline int ccs_check_network_sendmsg_acl(const bool is_ipv6, |
static inline int ccs_network_sendmsg_acl(const bool is_ipv6, |
491 |
const int sock_type, |
const int sock_type, |
492 |
const u8 *address, |
const u8 *address, const u16 port) |
|
const u16 port) |
|
493 |
{ |
{ |
494 |
u8 operation; |
u8 operation; |
495 |
if (sock_type == SOCK_DGRAM) |
if (sock_type == SOCK_DGRAM) |
496 |
operation = NETWORK_ACL_UDP_CONNECT; |
operation = CCS_NETWORK_UDP_CONNECT; |
497 |
else |
else |
498 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = CCS_NETWORK_RAW_CONNECT; |
499 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
500 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
501 |
} |
} |
502 |
|
|
503 |
/** |
/** |
504 |
* ccs_check_network_recvmsg_acl - Check permission for recvmsg() operation. |
* ccs_network_recvmsg_acl - Check permission for recvmsg() operation. |
505 |
* |
* |
506 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
507 |
* @sock_type: Type of socket. (UDP or RAW) |
* @sock_type: Type of socket. (UDP or RAW) |
510 |
* |
* |
511 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
512 |
*/ |
*/ |
513 |
static inline int ccs_check_network_recvmsg_acl(const bool is_ipv6, |
static inline int ccs_network_recvmsg_acl(const bool is_ipv6, |
514 |
const int sock_type, |
const int sock_type, |
515 |
const u8 *address, |
const u8 *address, const u16 port) |
|
const u16 port) |
|
516 |
{ |
{ |
517 |
int retval; |
int retval; |
518 |
const u8 operation |
const u8 operation |
519 |
= (sock_type == SOCK_DGRAM) ? |
= (sock_type == SOCK_DGRAM) ? |
520 |
NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
CCS_NETWORK_UDP_CONNECT : CCS_NETWORK_RAW_CONNECT; |
521 |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
522 |
retval = ccs_check_network_entry(is_ipv6, operation, |
retval = ccs_network_entry(is_ipv6, operation, |
523 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
524 |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
525 |
return retval; |
return retval; |
526 |
} |
} |
528 |
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
529 |
|
|
530 |
/* Check permission for creating a socket. */ |
/* Check permission for creating a socket. */ |
531 |
int ccs_socket_create_permission(int family, int type, int protocol) |
static int __ccs_socket_create_permission(int family, int type, int protocol) |
532 |
{ |
{ |
533 |
int error = 0; |
int error = 0; |
534 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
558 |
} |
} |
559 |
|
|
560 |
/* Check permission for listening a TCP socket. */ |
/* Check permission for listening a TCP socket. */ |
561 |
int ccs_socket_listen_permission(struct socket *sock) |
static int __ccs_socket_listen_permission(struct socket *sock) |
562 |
{ |
{ |
563 |
int error = 0; |
int error = 0; |
564 |
char addr[MAX_SOCK_ADDR]; |
char addr[MAX_SOCK_ADDR]; |
584 |
struct sockaddr_in *addr4; |
struct sockaddr_in *addr4; |
585 |
case AF_INET6: |
case AF_INET6: |
586 |
addr6 = (struct sockaddr_in6 *) addr; |
addr6 = (struct sockaddr_in6 *) addr; |
587 |
error = ccs_check_network_listen_acl(true, |
error = ccs_network_listen_acl(true, |
588 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
589 |
addr6->sin6_port); |
addr6->sin6_port); |
590 |
break; |
break; |
591 |
case AF_INET: |
case AF_INET: |
592 |
addr4 = (struct sockaddr_in *) addr; |
addr4 = (struct sockaddr_in *) addr; |
593 |
error = ccs_check_network_listen_acl(false, |
error = ccs_network_listen_acl(false, |
594 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
595 |
addr4->sin_port); |
addr4->sin_port); |
596 |
break; |
break; |
597 |
} |
} |
598 |
return error; |
return error; |
599 |
} |
} |
600 |
|
|
601 |
/* 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. */ |
602 |
int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_connect_permission(struct socket *sock, |
603 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
604 |
{ |
{ |
605 |
int error = 0; |
int error = 0; |
606 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
627 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
628 |
else |
else |
629 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
630 |
error = ccs_check_network_connect_acl(true, type, |
error = ccs_network_connect_acl(true, type, |
631 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
632 |
port); |
port); |
633 |
break; |
break; |
634 |
case AF_INET: |
case AF_INET: |
635 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
639 |
port = addr4->sin_port; |
port = addr4->sin_port; |
640 |
else |
else |
641 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
642 |
error = ccs_check_network_connect_acl(false, type, |
error = ccs_network_connect_acl(false, type, |
643 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
644 |
port); |
port); |
645 |
break; |
break; |
646 |
} |
} |
647 |
if (type != SOCK_STREAM) |
if (type != SOCK_STREAM) |
657 |
} |
} |
658 |
|
|
659 |
/* 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. */ |
660 |
int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_bind_permission(struct socket *sock, |
661 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
662 |
{ |
{ |
663 |
int error = 0; |
int error = 0; |
664 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
685 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
686 |
else |
else |
687 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
688 |
error = ccs_check_network_bind_acl(true, type, |
error = ccs_network_bind_acl(true, type, |
689 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
690 |
port); |
port); |
691 |
break; |
break; |
692 |
case AF_INET: |
case AF_INET: |
693 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
697 |
port = addr4->sin_port; |
port = addr4->sin_port; |
698 |
else |
else |
699 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
700 |
error = ccs_check_network_bind_acl(false, type, |
error = ccs_network_bind_acl(false, type, |
701 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
702 |
port); |
port); |
703 |
break; |
break; |
704 |
} |
} |
705 |
return error; |
return error; |
710 |
* |
* |
711 |
* Currently, the LSM hook for this purpose is not provided. |
* Currently, the LSM hook for this purpose is not provided. |
712 |
*/ |
*/ |
713 |
int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr) |
static int __ccs_socket_accept_permission(struct socket *sock, |
714 |
|
struct sockaddr *addr) |
715 |
{ |
{ |
716 |
int error = 0; |
int error = 0; |
717 |
int addr_len; |
int addr_len; |
733 |
struct sockaddr_in *addr4; |
struct sockaddr_in *addr4; |
734 |
case AF_INET6: |
case AF_INET6: |
735 |
addr6 = (struct sockaddr_in6 *) addr; |
addr6 = (struct sockaddr_in6 *) addr; |
736 |
error = ccs_check_network_accept_acl(true, |
error = ccs_network_accept_acl(true, |
737 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
738 |
addr6->sin6_port); |
addr6->sin6_port); |
739 |
break; |
break; |
740 |
case AF_INET: |
case AF_INET: |
741 |
addr4 = (struct sockaddr_in *) addr; |
addr4 = (struct sockaddr_in *) addr; |
742 |
error = ccs_check_network_accept_acl(false, |
error = ccs_network_accept_acl(false, |
743 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
744 |
addr4->sin_port); |
addr4->sin_port); |
745 |
break; |
break; |
746 |
} |
} |
747 |
return error; |
return error; |
748 |
} |
} |
749 |
|
|
750 |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
751 |
int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_sendmsg_permission(struct socket *sock, |
752 |
int addr_len) |
struct msghdr *msg, int size) |
753 |
{ |
{ |
754 |
|
struct sockaddr *addr = (struct sockaddr *) msg->msg_name; |
755 |
|
const int addr_len = msg->msg_namelen; |
756 |
int error = 0; |
int error = 0; |
757 |
const int type = sock->type; |
const int type = sock->type; |
758 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
772 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
773 |
else |
else |
774 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
775 |
error = ccs_check_network_sendmsg_acl(true, type, |
error = ccs_network_sendmsg_acl(true, type, |
776 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
777 |
port); |
port); |
778 |
break; |
break; |
779 |
case AF_INET: |
case AF_INET: |
780 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
784 |
port = addr4->sin_port; |
port = addr4->sin_port; |
785 |
else |
else |
786 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
787 |
error = ccs_check_network_sendmsg_acl(false, type, |
error = ccs_network_sendmsg_acl(false, type, |
788 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, port); |
|
port); |
|
789 |
break; |
break; |
790 |
} |
} |
791 |
return error; |
return error; |
793 |
|
|
794 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
795 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
796 |
|
#if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2 |
797 |
|
|
798 |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
799 |
{ |
{ |
812 |
|
|
813 |
#endif |
#endif |
814 |
#endif |
#endif |
815 |
|
#endif |
816 |
|
|
817 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) |
818 |
|
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
819 |
|
unsigned int flags) |
820 |
|
{ |
821 |
|
/* Clear queue. */ |
822 |
|
if (flags & MSG_PEEK) { |
823 |
|
int clear = 0; |
824 |
|
spin_lock_irq(&sk->sk_receive_queue.lock); |
825 |
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
826 |
|
__skb_unlink(skb, &sk->sk_receive_queue); |
827 |
|
clear = 1; |
828 |
|
} |
829 |
|
spin_unlock_irq(&sk->sk_receive_queue.lock); |
830 |
|
if (clear) |
831 |
|
kfree_skb(skb); |
832 |
|
} |
833 |
|
skb_free_datagram(sk, skb); |
834 |
|
} |
835 |
|
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16) |
836 |
|
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
837 |
|
unsigned int flags) |
838 |
|
{ |
839 |
|
/* Clear queue. */ |
840 |
|
if (flags & MSG_PEEK) { |
841 |
|
int clear = 0; |
842 |
|
spin_lock_bh(&sk->sk_receive_queue.lock); |
843 |
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
844 |
|
__skb_unlink(skb, &sk->sk_receive_queue); |
845 |
|
clear = 1; |
846 |
|
} |
847 |
|
spin_unlock_bh(&sk->sk_receive_queue.lock); |
848 |
|
if (clear) |
849 |
|
kfree_skb(skb); |
850 |
|
} |
851 |
|
skb_free_datagram(sk, skb); |
852 |
|
} |
853 |
|
#endif |
854 |
|
|
855 |
/* |
/* |
856 |
* Check permission for receiving a datagram via a UDP or RAW socket. |
* Check permission for receiving a datagram via a UDP or RAW socket. |
857 |
* |
* |
858 |
* Currently, the LSM hook for this purpose is not provided. |
* Currently, the LSM hook for this purpose is not provided. |
859 |
*/ |
*/ |
860 |
int ccs_socket_recv_datagram_permission(struct sock *sk, struct sk_buff *skb, |
static int __ccs_socket_recvmsg_permission(struct sock *sk, |
861 |
const unsigned int flags) |
struct sk_buff *skb, |
862 |
|
const unsigned int flags) |
863 |
{ |
{ |
864 |
int error = 0; |
int error = 0; |
865 |
const unsigned int type = sk->sk_type; |
const unsigned int type = sk->sk_type; |
866 |
/* 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()) |
|
867 |
return 0; |
return 0; |
|
#endif |
|
868 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
869 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
870 |
return 0; |
return 0; |
|
if (type != SOCK_DGRAM && type != SOCK_RAW) |
|
|
return 0; |
|
871 |
|
|
872 |
switch (sk->sk_family) { |
switch (sk->sk_family) { |
873 |
struct in6_addr sin6; |
struct in6_addr sin6; |
886 |
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
887 |
port = htons(sk->sk_protocol); |
port = htons(sk->sk_protocol); |
888 |
} |
} |
889 |
error = ccs_check_network_recvmsg_acl(true, type, |
error = ccs_network_recvmsg_acl(true, type, |
890 |
(u8 *) &sin6, port); |
(u8 *) &sin6, port); |
891 |
break; |
break; |
892 |
case PF_INET: |
case PF_INET: |
893 |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
897 |
sin4.s_addr = ip_hdr(skb)->saddr; |
sin4.s_addr = ip_hdr(skb)->saddr; |
898 |
port = htons(sk->sk_protocol); |
port = htons(sk->sk_protocol); |
899 |
} |
} |
900 |
error = ccs_check_network_recvmsg_acl(false, type, |
error = ccs_network_recvmsg_acl(false, type, |
901 |
(u8 *) &sin4, port); |
(u8 *) &sin4, port); |
902 |
break; |
break; |
903 |
} |
} |
904 |
if (!error) |
if (!error) |
905 |
return 0; |
return 0; |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
|
|
lock_sock(sk); |
|
|
#endif |
|
906 |
/* |
/* |
907 |
* Remove from queue if MSG_PEEK is used so that |
* Remove from queue if MSG_PEEK is used so that |
908 |
* the head message from unwanted source in receive queue will not |
* the head message from unwanted source in receive queue will not |
909 |
* prevent the caller from picking up next message from wanted source |
* prevent the caller from picking up next message from wanted source |
910 |
* when the caller is using MSG_PEEK flag for picking up. |
* when the caller is using MSG_PEEK flag for picking up. |
911 |
*/ |
*/ |
|
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); |
|
912 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
913 |
release_sock(sk); |
if (type == SOCK_DGRAM) |
914 |
|
lock_sock(sk); |
915 |
|
#endif |
916 |
|
skb_kill_datagram(sk, skb, flags); |
917 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
918 |
|
if (type == SOCK_DGRAM) |
919 |
|
release_sock(sk); |
920 |
#endif |
#endif |
921 |
/* Hope less harmful than -EPERM. */ |
/* Hope less harmful than -EPERM. */ |
922 |
return -EAGAIN; |
return -ENOMEM; |
923 |
|
} |
924 |
|
|
925 |
|
void __init ccs_network_init(void) |
926 |
|
{ |
927 |
|
ccsecurity_ops.socket_create_permission = |
928 |
|
__ccs_socket_create_permission; |
929 |
|
ccsecurity_ops.socket_listen_permission = |
930 |
|
__ccs_socket_listen_permission; |
931 |
|
ccsecurity_ops.socket_connect_permission = |
932 |
|
__ccs_socket_connect_permission; |
933 |
|
ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission; |
934 |
|
ccsecurity_ops.socket_accept_permission = |
935 |
|
__ccs_socket_accept_permission; |
936 |
|
ccsecurity_ops.socket_sendmsg_permission = |
937 |
|
__ccs_socket_sendmsg_permission; |
938 |
|
ccsecurity_ops.socket_recvmsg_permission = |
939 |
|
__ccs_socket_recvmsg_permission; |
940 |
} |
} |