3 |
* |
* |
4 |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
5 |
* |
* |
6 |
* Version: 1.7.0-pre 2009/08/08 |
* Version: 1.7.0-pre 2009/08/24 |
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. |
19 |
#include <net/udp.h> |
#include <net/udp.h> |
20 |
#include "internal.h" |
#include "internal.h" |
21 |
|
|
|
/* 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 |
|
|
}; |
|
|
|
|
22 |
/** |
/** |
23 |
* ccs_audit_network_log - Audit network log. |
* ccs_audit_network_log - Audit network log. |
24 |
* |
* |
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 |
if (!is_granted && ccs_verbose_mode(r->domain)) |
if (!is_granted) |
38 |
printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n", |
ccs_warn_log(r, "%s %s %u", operation, address, port); |
39 |
ccs_get_msg(r->mode == 3), operation, address, port, |
return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK |
|
ccs_get_last_name(r->domain)); |
|
|
return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK |
|
40 |
"%s %s %u\n", operation, address, port); |
"%s %s %u\n", operation, address, port); |
41 |
} |
} |
42 |
|
|
|
/* The list for "struct ccs_address_group_entry". */ |
|
|
LIST_HEAD(ccs_address_group_list); |
|
|
|
|
|
/** |
|
|
* ccs_get_address_group - Allocate memory for "struct ccs_address_group_entry". |
|
|
* |
|
|
* @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_get_address_group(const char * |
|
|
group_name) |
|
|
{ |
|
|
struct ccs_address_group_entry *entry = NULL; |
|
|
struct ccs_address_group_entry *group; |
|
|
const struct ccs_path_info *saved_group_name; |
|
|
int error = -ENOMEM; |
|
|
if (!ccs_is_correct_path(group_name, 0, 0, 0) || |
|
|
!group_name[0]) |
|
|
return NULL; |
|
|
saved_group_name = ccs_get_name(group_name); |
|
|
if (!saved_group_name) |
|
|
return NULL; |
|
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
|
|
mutex_lock(&ccs_policy_lock); |
|
|
list_for_each_entry_rcu(group, &ccs_address_group_list, list) { |
|
|
if (saved_group_name != group->group_name) |
|
|
continue; |
|
|
atomic_inc(&group->users); |
|
|
error = 0; |
|
|
break; |
|
|
} |
|
|
if (error && ccs_memory_ok(entry, sizeof(*entry))) { |
|
|
INIT_LIST_HEAD(&entry->address_group_member_list); |
|
|
entry->group_name = saved_group_name; |
|
|
saved_group_name = NULL; |
|
|
atomic_set(&entry->users, 1); |
|
|
list_add_tail_rcu(&entry->list, &ccs_address_group_list); |
|
|
group = entry; |
|
|
entry = NULL; |
|
|
error = 0; |
|
|
} |
|
|
mutex_unlock(&ccs_policy_lock); |
|
|
ccs_put_name(saved_group_name); |
|
|
kfree(entry); |
|
|
return !error ? group : NULL; |
|
|
} |
|
|
|
|
|
/** |
|
|
* 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) |
|
|
{ |
|
|
struct ccs_address_group_entry *group; |
|
|
struct ccs_address_group_member *entry = NULL; |
|
|
struct ccs_address_group_member *member; |
|
|
const struct in6_addr *saved_min_address = NULL; |
|
|
const struct in6_addr *saved_max_address = NULL; |
|
|
int error = is_delete ? -ENOENT : -ENOMEM; |
|
|
const u32 min_ipv4_address = ntohl(*(u32 *) min_address); |
|
|
const u32 max_ipv4_address = ntohl(*(u32 *) max_address); |
|
|
group = ccs_get_address_group(group_name); |
|
|
if (!group) |
|
|
return -ENOMEM; |
|
|
if (is_ipv6) { |
|
|
saved_min_address |
|
|
= ccs_get_ipv6_address((struct in6_addr *) |
|
|
min_address); |
|
|
saved_max_address |
|
|
= ccs_get_ipv6_address((struct in6_addr *) |
|
|
max_address); |
|
|
if (!saved_min_address || !saved_max_address) |
|
|
goto out; |
|
|
} |
|
|
if (!is_delete) |
|
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
|
|
mutex_lock(&ccs_policy_lock); |
|
|
list_for_each_entry_rcu(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 != min_ipv4_address || |
|
|
member->max.ipv4 != max_ipv4_address) |
|
|
continue; |
|
|
} |
|
|
member->is_deleted = is_delete; |
|
|
error = 0; |
|
|
break; |
|
|
} |
|
|
if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) { |
|
|
entry->is_ipv6 = is_ipv6; |
|
|
if (is_ipv6) { |
|
|
entry->min.ipv6 = saved_min_address; |
|
|
saved_min_address = NULL; |
|
|
entry->max.ipv6 = saved_max_address; |
|
|
saved_max_address = NULL; |
|
|
} else { |
|
|
entry->min.ipv4 = min_ipv4_address; |
|
|
entry->max.ipv4 = max_ipv4_address; |
|
|
} |
|
|
list_add_tail_rcu(&entry->list, |
|
|
&group->address_group_member_list); |
|
|
entry = NULL; |
|
|
error = 0; |
|
|
} |
|
|
mutex_unlock(&ccs_policy_lock); |
|
|
out: |
|
|
ccs_put_ipv6_address(saved_min_address); |
|
|
ccs_put_ipv6_address(saved_max_address); |
|
|
ccs_put_address_group(group); |
|
|
return error; |
|
|
} |
|
|
|
|
43 |
/** |
/** |
44 |
* ccs_parse_ip_address - Parse an IP address. |
* ccs_parse_ip_address - Parse an IP address. |
45 |
* |
* |
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) |
|
|
{ |
|
|
char *w[2]; |
|
|
bool is_ipv6; |
|
|
u16 min_address[8]; |
|
|
u16 max_address[8]; |
|
|
if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0]) |
|
|
return -EINVAL; |
|
|
switch (ccs_parse_ip_address(w[1], 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(w[0], is_ipv6, min_address, |
|
|
max_address, is_delete); |
|
|
} |
|
|
|
|
|
/** |
|
|
* 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. |
|
|
* |
|
|
* Caller holds ccs_read_lock(). |
|
|
*/ |
|
|
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); |
|
|
bool matched = false; |
|
|
ccs_check_read_lock(); |
|
|
list_for_each_entry_rcu(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) { |
|
|
matched = true; |
|
|
break; |
|
|
} |
|
|
} else { |
|
|
if (!is_ipv6 && |
|
|
member->min.ipv4 <= ip && ip <= member->max.ipv4) { |
|
|
matched = true; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
return matched; |
|
|
} |
|
|
|
|
|
/** |
|
|
* 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. |
|
|
* |
|
|
* Caller holds ccs_read_lock(). |
|
|
*/ |
|
|
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
|
|
{ |
|
|
struct list_head *gpos; |
|
|
struct list_head *mpos; |
|
|
bool done = true; |
|
|
ccs_check_read_lock(); |
|
|
list_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) { |
|
|
struct ccs_address_group_entry *group; |
|
|
group = list_entry(gpos, struct ccs_address_group_entry, list); |
|
|
list_for_each_cookie(mpos, head->read_var2, |
|
|
&group->address_group_member_list) { |
|
|
char buf[128]; |
|
|
struct ccs_address_group_member *member; |
|
|
member = list_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)); |
|
|
} |
|
|
} |
|
|
done = ccs_io_printf(head, KEYWORD_ADDRESS_GROUP |
|
|
"%s %s\n", group->group_name->name, |
|
|
buf); |
|
|
if (!done) |
|
|
break; |
|
|
} |
|
|
if (!done) |
|
|
break; |
|
|
} |
|
|
return done; |
|
|
} |
|
|
|
|
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. |
|
* |
|
|
* @operation: Type of operation. |
|
|
* @record_type: Type of address. |
|
|
* @group: Name of group. May be NULL. |
|
|
* @min_address: Start of IPv4 or IPv6 address range. |
|
|
* @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". May be NULL. |
|
|
* @is_delete: True if it is a delete request. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int ccs_update_network_entry(const u8 operation, const u8 record_type, |
|
|
const char *address_group_name, |
|
|
const char *port_group_name, |
|
|
const u32 *min_address, |
|
|
const u32 *max_address, |
|
|
const u16 min_port, const u16 max_port, |
|
|
struct ccs_domain_info *domain, |
|
|
struct ccs_condition *condition, |
|
|
const bool is_delete) |
|
|
{ |
|
|
static const u8 offset = offsetof(struct ccs_ip_network_acl_record, |
|
|
head.cond); |
|
|
struct ccs_acl_info *ptr; |
|
|
struct ccs_ip_network_acl_record e; |
|
|
struct ccs_ip_network_acl_record *entry = NULL; |
|
|
int error = is_delete ? -ENOENT : -ENOMEM; |
|
|
memset(&e, 0, sizeof(e)); |
|
|
e.head.type = TYPE_IP_NETWORK_ACL; |
|
|
e.head.cond = condition; |
|
|
e.operation_type = operation; |
|
|
e.record_type = record_type; |
|
|
if (!domain) |
|
|
return -EINVAL; |
|
|
if (address_group_name) { |
|
|
e.address.group = ccs_get_address_group(address_group_name); |
|
|
if (!e.address.group) |
|
|
return -ENOMEM; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
|
|
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
|
|
min_address); |
|
|
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
|
|
max_address); |
|
|
if (!e.address.ipv6.min || !e.address.ipv6.max) |
|
|
goto out; |
|
|
} else { |
|
|
/* use host byte order to allow u32 comparison than memcmp().*/ |
|
|
e.address.ipv4.min = ntohl(*min_address); |
|
|
e.address.ipv4.max = ntohl(*max_address); |
|
|
} |
|
|
if (port_group_name) { |
|
|
if (!ccs_check_and_save_number(port_group_name, &e.port)) |
|
|
goto out; |
|
|
} else { |
|
|
e.port.values[0] = min_port; |
|
|
e.port.values[1] = max_port; |
|
|
} |
|
|
if (is_delete) |
|
|
goto delete; |
|
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
|
|
mutex_lock(&ccs_policy_lock); |
|
|
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
|
|
struct ccs_ip_network_acl_record *acl; |
|
|
if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
//if (ptr->cond != condition) |
|
|
//continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (memcmp(((char *) acl) + offset, ((char *) &e) + offset, |
|
|
sizeof(e) - offset)) |
|
|
continue; |
|
|
error = ccs_add_domain_acl(NULL, ptr); |
|
|
break; |
|
|
} |
|
|
if (error && ccs_memory_ok(entry, sizeof(*entry))) { |
|
|
memmove(entry, &e, sizeof(e)); |
|
|
memset(&e, 0, sizeof(e)); |
|
|
error = ccs_add_domain_acl(domain, &entry->head); |
|
|
entry = NULL; |
|
|
} |
|
|
mutex_unlock(&ccs_policy_lock); |
|
|
goto out; |
|
|
delete: |
|
|
mutex_lock(&ccs_policy_lock); |
|
|
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
|
|
struct ccs_ip_network_acl_record *acl; |
|
|
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
//if (ptr->cond != condition) |
|
|
//continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (memcmp(((char *) acl) + offset, ((char *) &e) + offset, |
|
|
sizeof(e) - offset)) |
|
|
continue; |
|
|
error = ccs_del_domain_acl(ptr); |
|
|
break; |
|
|
} |
|
|
mutex_unlock(&ccs_policy_lock); |
|
|
out: |
|
|
if (address_group_name) |
|
|
ccs_put_address_group(e.address.group); |
|
|
else if (record_type == IP_RECORD_TYPE_IPv6) { |
|
|
ccs_put_ipv6_address(e.address.ipv6.min); |
|
|
ccs_put_ipv6_address(e.address.ipv6.max); |
|
|
} |
|
|
if (port_group_name) |
|
|
ccs_put_number_group(e.port.group); |
|
|
kfree(entry); |
|
|
return error; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_check_network_entry2 - Check permission for network operation. |
|
151 |
* |
* |
152 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
153 |
* @operation: Type of operation. |
* @operation: Type of operation. |
158 |
* |
* |
159 |
* Caller holds ccs_read_lock(). |
* Caller holds ccs_read_lock(). |
160 |
*/ |
*/ |
161 |
static int ccs_check_network_entry2(const bool is_ipv6, const u8 operation, |
static int ccs_network_entry2(const bool is_ipv6, const u8 operation, |
162 |
const u32 *address, const u16 port) |
const u32 *address, const u16 port) |
163 |
{ |
{ |
164 |
struct ccs_request_info r; |
struct ccs_request_info r; |
165 |
struct ccs_acl_info *ptr; |
struct ccs_acl_info *ptr; |
166 |
const char *keyword = ccs_net2keyword(operation); |
const char *keyword = ccs_net2keyword(operation); |
167 |
bool is_enforce; |
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 ip = ntohl(*address); |
const u32 ip = ntohl(*address); |
170 |
bool found = false; |
int error; |
171 |
char buf[64]; |
char buf[64]; |
172 |
ccs_check_read_lock(); |
ccs_assert_read_lock(); |
173 |
if (!ccs_can_sleep()) |
if (ccs_init_request_info(&r, NULL, |
174 |
|
CCS_MAC_NETWORK_UDP_BIND + operation) |
175 |
|
== CCS_MAC_MODE_DISABLED) |
176 |
return 0; |
return 0; |
|
ccs_init_request_info(&r, NULL, CCS_MAC_FOR_NETWORK); |
|
|
is_enforce = (r.mode == 3); |
|
|
if (!r.mode) |
|
|
return 0; |
|
|
retry: |
|
|
list_for_each_entry_rcu(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) |
|
|
continue; |
|
|
if (!ccs_compare_number_union(port, &acl->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->address.group)) |
|
|
continue; |
|
|
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
|
|
if (is_ipv6 || |
|
|
ip < acl->address.ipv4.min || |
|
|
acl->address.ipv4.max < ip) |
|
|
continue; |
|
|
} else { |
|
|
if (!is_ipv6 || |
|
|
memcmp(acl->address.ipv6.min, address, 16) > 0 || |
|
|
memcmp(address, acl->address.ipv6.max, 16) > 0) |
|
|
continue; |
|
|
} |
|
|
r.cond = ptr->cond; |
|
|
found = true; |
|
|
break; |
|
|
} |
|
177 |
memset(buf, 0, sizeof(buf)); |
memset(buf, 0, sizeof(buf)); |
178 |
if (is_ipv6) |
if (is_ipv6) |
179 |
ccs_print_ipv6(buf, sizeof(buf), |
ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *) |
180 |
(const struct in6_addr *) address); |
address); |
181 |
else |
else |
182 |
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
183 |
ccs_audit_network_log(&r, keyword, buf, port, found); |
do { |
184 |
if (found) |
error = -EPERM; |
185 |
return 0; |
list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) { |
186 |
if (is_enforce) { |
struct ccs_ip_network_acl *acl; |
187 |
int err = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK |
if (ptr->is_deleted || |
188 |
"%s %s %u\n", keyword, buf, |
ptr->type != CCS_TYPE_IP_NETWORK_ACL) |
189 |
port); |
continue; |
190 |
if (err == 1) |
acl = container_of(ptr, struct ccs_ip_network_acl, |
191 |
goto retry; |
head); |
192 |
return err; |
if (!(acl->perm & perm)) |
193 |
} else if (ccs_domain_quota_ok(&r)) { |
continue; |
194 |
struct ccs_condition *cond = ccs_handler_cond(); |
if (!ccs_compare_number_union(port, &acl->port) || |
195 |
ccs_update_network_entry(operation, is_ipv6 ? |
!ccs_condition(&r, ptr)) |
196 |
IP_RECORD_TYPE_IPv6 : |
continue; |
197 |
IP_RECORD_TYPE_IPv4, |
switch (acl->address_type) { |
198 |
NULL, NULL, address, address, port, |
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
199 |
port, r.domain, cond, false); |
if (!ccs_address_matches_group(is_ipv6, |
200 |
ccs_put_condition(cond); |
address, |
201 |
} |
acl->address. |
202 |
return 0; |
group)) |
203 |
|
continue; |
204 |
|
break; |
205 |
|
case CCS_IP_ADDRESS_TYPE_IPv4: |
206 |
|
if (is_ipv6 || ip < acl->address.ipv4.min || |
207 |
|
acl->address.ipv4.max < ip) |
208 |
|
continue; |
209 |
|
break; |
210 |
|
default: |
211 |
|
if (!is_ipv6 || |
212 |
|
memcmp(acl->address.ipv6.min, address, 16) |
213 |
|
> 0 || |
214 |
|
memcmp(address, acl->address.ipv6.max, 16) |
215 |
|
> 0) |
216 |
|
continue; |
217 |
|
break; |
218 |
|
} |
219 |
|
r.cond = ptr->cond; |
220 |
|
error = 0; |
221 |
|
break; |
222 |
|
} |
223 |
|
ccs_audit_network_log(&r, keyword, buf, port, !error); |
224 |
|
if (!error) |
225 |
|
break; |
226 |
|
error = ccs_supervisor(&r, CCS_KEYWORD_ALLOW_NETWORK |
227 |
|
"%s %s %u\n", keyword, buf, port); |
228 |
|
} while (error == 1); |
229 |
|
if (r.mode != CCS_MAC_MODE_ENFORCING) |
230 |
|
error = 0; |
231 |
|
return error; |
232 |
} |
} |
233 |
|
|
234 |
/** |
/** |
235 |
* ccs_check_network_entry - Check permission for network operation. |
* ccs_network_entry - Check permission for network operation. |
236 |
* |
* |
237 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
238 |
* @operation: Type of operation. |
* @operation: Type of operation. |
241 |
* |
* |
242 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
243 |
*/ |
*/ |
244 |
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, |
245 |
const u32 *address, const u16 port) |
const u32 *address, const u16 port) |
246 |
{ |
{ |
247 |
const int idx = ccs_read_lock(); |
const int idx = ccs_read_lock(); |
248 |
const int error = ccs_check_network_entry2(is_ipv6, operation, |
const int error = ccs_network_entry2(is_ipv6, operation, |
249 |
address, port); |
address, port); |
250 |
ccs_read_unlock(idx); |
ccs_read_unlock(idx); |
251 |
return error; |
return error; |
252 |
} |
} |
253 |
|
|
254 |
/** |
/** |
255 |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list. |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl" list. |
256 |
* |
* |
257 |
* @data: String to parse. |
* @data: String to parse. |
258 |
* @domain: Pointer to "struct ccs_domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
265 |
struct ccs_condition *condition, |
struct ccs_condition *condition, |
266 |
const bool is_delete) |
const bool is_delete) |
267 |
{ |
{ |
268 |
char *w[4]; |
struct ccs_ip_network_acl *entry = NULL; |
269 |
u8 sock_type; |
struct ccs_acl_info *ptr; |
270 |
u8 operation; |
struct ccs_ip_network_acl e = { |
271 |
u8 record_type; |
.head.type = CCS_TYPE_IP_NETWORK_ACL, |
272 |
|
.head.cond = condition, |
273 |
|
}; |
274 |
u16 min_address[8]; |
u16 min_address[8]; |
275 |
u16 max_address[8]; |
u16 max_address[8]; |
276 |
const char *address_group_name = NULL; |
int error = is_delete ? -ENOENT : -ENOMEM; |
277 |
const char *port_group_name = NULL; |
u8 sock_type; |
278 |
u16 min_port; |
char *w[4]; |
|
u16 max_port; |
|
279 |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
280 |
return -EINVAL; |
return -EINVAL; |
281 |
if (!strcmp(w[0], "TCP")) |
if (!strcmp(w[0], "TCP")) |
285 |
else if (!strcmp(w[0], "RAW")) |
else if (!strcmp(w[0], "RAW")) |
286 |
sock_type = SOCK_RAW; |
sock_type = SOCK_RAW; |
287 |
else |
else |
288 |
goto out; |
return -EINVAL; |
289 |
if (!strcmp(w[1], "bind")) |
if (!strcmp(w[1], "bind")) |
290 |
switch (sock_type) { |
switch (sock_type) { |
291 |
case SOCK_STREAM: |
case SOCK_STREAM: |
292 |
operation = NETWORK_ACL_TCP_BIND; |
e.perm = 1 << CCS_NETWORK_TCP_BIND; |
293 |
break; |
break; |
294 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
295 |
operation = NETWORK_ACL_UDP_BIND; |
e.perm = 1 << CCS_NETWORK_UDP_BIND; |
296 |
break; |
break; |
297 |
default: |
default: |
298 |
operation = NETWORK_ACL_RAW_BIND; |
e.perm = 1 << CCS_NETWORK_RAW_BIND; |
299 |
|
break; |
300 |
} |
} |
301 |
else if (!strcmp(w[1], "connect")) |
else if (!strcmp(w[1], "connect")) |
302 |
switch (sock_type) { |
switch (sock_type) { |
303 |
case SOCK_STREAM: |
case SOCK_STREAM: |
304 |
operation = NETWORK_ACL_TCP_CONNECT; |
e.perm = 1 << CCS_NETWORK_TCP_CONNECT; |
305 |
break; |
break; |
306 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
307 |
operation = NETWORK_ACL_UDP_CONNECT; |
e.perm = 1 << CCS_NETWORK_UDP_CONNECT; |
308 |
break; |
break; |
309 |
default: |
default: |
310 |
operation = NETWORK_ACL_RAW_CONNECT; |
e.perm = 1 << CCS_NETWORK_RAW_CONNECT; |
311 |
|
break; |
312 |
} |
} |
313 |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen")) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen")) |
314 |
operation = NETWORK_ACL_TCP_LISTEN; |
e.perm = 1 << CCS_NETWORK_TCP_LISTEN; |
315 |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept")) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept")) |
316 |
operation = NETWORK_ACL_TCP_ACCEPT; |
e.perm = 1 << CCS_NETWORK_TCP_ACCEPT; |
317 |
else |
else |
318 |
goto out; |
return -EINVAL; |
319 |
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
320 |
case 2: |
case 2: |
321 |
record_type = IP_RECORD_TYPE_IPv6; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv6; |
322 |
|
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
323 |
|
min_address); |
324 |
|
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
325 |
|
max_address); |
326 |
|
if (!e.address.ipv6.min || !e.address.ipv6.max) |
327 |
|
goto out; |
328 |
break; |
break; |
329 |
case 1: |
case 1: |
330 |
record_type = IP_RECORD_TYPE_IPv4; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv4; |
331 |
|
/* use host byte order to allow u32 comparison.*/ |
332 |
|
e.address.ipv4.min = ntohl(*(u32 *) min_address); |
333 |
|
e.address.ipv4.max = ntohl(*(u32 *) max_address); |
334 |
break; |
break; |
335 |
default: |
default: |
336 |
if (w[2][0] != '@') |
if (w[2][0] != '@') |
337 |
goto out; |
return -EINVAL; |
338 |
address_group_name = w[2] + 1; |
e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
339 |
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
e.address.group = ccs_get_address_group(w[2] + 1); |
340 |
|
if (!e.address.group) |
341 |
|
return -ENOMEM; |
342 |
break; |
break; |
343 |
} |
} |
344 |
switch (sscanf(w[3], "%hu-%hu", &min_port, &max_port)) { |
if (!ccs_parse_number_union(w[3], &e.port)) |
345 |
case 2: |
goto out; |
346 |
break; |
if (!is_delete) |
347 |
case 1: |
entry = kmalloc(sizeof(e), GFP_KERNEL); |
348 |
max_port = min_port; |
mutex_lock(&ccs_policy_lock); |
349 |
break; |
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
350 |
default: |
struct ccs_ip_network_acl *acl = |
351 |
if (w[3][0] != '@') |
container_of(ptr, struct ccs_ip_network_acl, |
352 |
goto out; |
head); |
353 |
port_group_name = w[3]; |
if (ptr->type != CCS_TYPE_IP_NETWORK_ACL || |
354 |
|
ptr->cond != condition || |
355 |
|
ccs_memcmp(acl, &e, offsetof(typeof(e), address_type), |
356 |
|
sizeof(e))) |
357 |
|
continue; |
358 |
|
if (is_delete) { |
359 |
|
acl->perm &= ~e.perm; |
360 |
|
if (!acl->perm) |
361 |
|
ptr->is_deleted = true; |
362 |
|
} else { |
363 |
|
if (ptr->is_deleted) |
364 |
|
acl->perm = 0; |
365 |
|
acl->perm |= e.perm; |
366 |
|
ptr->is_deleted = false; |
367 |
|
} |
368 |
|
error = 0; |
369 |
break; |
break; |
370 |
} |
} |
371 |
return ccs_update_network_entry(operation, record_type, |
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
372 |
address_group_name, port_group_name, |
ccs_add_domain_acl(domain, &entry->head); |
373 |
(u32 *) min_address, |
entry = NULL; |
374 |
(u32 *) max_address, |
error = 0; |
375 |
min_port, max_port, domain, condition, |
} |
376 |
is_delete); |
mutex_unlock(&ccs_policy_lock); |
377 |
out: |
out: |
378 |
return -EINVAL; |
if (w[2][0] == '@') |
379 |
|
ccs_put_address_group(e.address.group); |
380 |
|
else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) { |
381 |
|
ccs_put_ipv6_address(e.address.ipv6.min); |
382 |
|
ccs_put_ipv6_address(e.address.ipv6.max); |
383 |
|
} |
384 |
|
ccs_put_number_union(&e.port); |
385 |
|
kfree(entry); |
386 |
|
return error; |
387 |
} |
} |
388 |
|
|
389 |
/** |
/** |
390 |
* ccs_check_network_listen_acl - Check permission for listen() operation. |
* ccs_network_listen_acl - Check permission for listen() operation. |
391 |
* |
* |
392 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
393 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address. |
395 |
* |
* |
396 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
397 |
*/ |
*/ |
398 |
static inline int ccs_check_network_listen_acl(const bool is_ipv6, |
static inline int ccs_network_listen_acl(const bool is_ipv6, |
399 |
const u8 *address, |
const u8 *address, |
400 |
const u16 port) |
const u16 port) |
401 |
{ |
{ |
402 |
return ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN, |
return ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN, |
403 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
404 |
} |
} |
405 |
|
|
406 |
/** |
/** |
407 |
* ccs_check_network_connect_acl - Check permission for connect() operation. |
* ccs_network_connect_acl - Check permission for connect() operation. |
408 |
* |
* |
409 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
410 |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
413 |
* |
* |
414 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
415 |
*/ |
*/ |
416 |
static inline int ccs_check_network_connect_acl(const bool is_ipv6, |
static inline int ccs_network_connect_acl(const bool is_ipv6, |
417 |
const int sock_type, |
const int sock_type, |
418 |
const u8 *address, |
const u8 *address, |
419 |
const u16 port) |
const u16 port) |
420 |
{ |
{ |
421 |
u8 operation; |
u8 operation; |
422 |
switch (sock_type) { |
switch (sock_type) { |
423 |
case SOCK_STREAM: |
case SOCK_STREAM: |
424 |
operation = NETWORK_ACL_TCP_CONNECT; |
operation = CCS_NETWORK_TCP_CONNECT; |
425 |
break; |
break; |
426 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
427 |
operation = NETWORK_ACL_UDP_CONNECT; |
operation = CCS_NETWORK_UDP_CONNECT; |
428 |
break; |
break; |
429 |
default: |
default: |
430 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = CCS_NETWORK_RAW_CONNECT; |
431 |
} |
} |
432 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
433 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
434 |
} |
} |
435 |
|
|
436 |
/** |
/** |
437 |
* ccs_check_network_bind_acl - Check permission for bind() operation. |
* ccs_network_bind_acl - Check permission for bind() operation. |
438 |
* |
* |
439 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
440 |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
443 |
* |
* |
444 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
445 |
*/ |
*/ |
446 |
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, |
447 |
const u8 *address, const u16 port) |
const u8 *address, const u16 port) |
448 |
{ |
{ |
449 |
u8 operation; |
u8 operation; |
450 |
switch (sock_type) { |
switch (sock_type) { |
451 |
case SOCK_STREAM: |
case SOCK_STREAM: |
452 |
operation = NETWORK_ACL_TCP_BIND; |
operation = CCS_NETWORK_TCP_BIND; |
453 |
break; |
break; |
454 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
455 |
operation = NETWORK_ACL_UDP_BIND; |
operation = CCS_NETWORK_UDP_BIND; |
456 |
break; |
break; |
457 |
default: |
default: |
458 |
operation = NETWORK_ACL_RAW_BIND; |
operation = CCS_NETWORK_RAW_BIND; |
459 |
} |
} |
460 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
461 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
462 |
} |
} |
463 |
|
|
464 |
/** |
/** |
465 |
* ccs_check_network_accept_acl - Check permission for accept() operation. |
* ccs_network_accept_acl - Check permission for accept() operation. |
466 |
* |
* |
467 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
468 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address. |
470 |
* |
* |
471 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
472 |
*/ |
*/ |
473 |
static inline int ccs_check_network_accept_acl(const bool is_ipv6, |
static inline int ccs_network_accept_acl(const bool is_ipv6, |
474 |
const u8 *address, |
const u8 *address, |
475 |
const u16 port) |
const u16 port) |
476 |
{ |
{ |
477 |
int retval; |
int retval; |
478 |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
479 |
retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, |
retval = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT, |
480 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
481 |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
482 |
return retval; |
return retval; |
483 |
} |
} |
484 |
|
|
485 |
/** |
/** |
486 |
* ccs_check_network_sendmsg_acl - Check permission for sendmsg() operation. |
* ccs_network_sendmsg_acl - Check permission for sendmsg() operation. |
487 |
* |
* |
488 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
489 |
* @sock_type: Type of socket. (UDP or RAW) |
* @sock_type: Type of socket. (UDP or RAW) |
492 |
* |
* |
493 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
494 |
*/ |
*/ |
495 |
static inline int ccs_check_network_sendmsg_acl(const bool is_ipv6, |
static inline int ccs_network_sendmsg_acl(const bool is_ipv6, |
496 |
const int sock_type, |
const int sock_type, |
497 |
const u8 *address, |
const u8 *address, |
498 |
const u16 port) |
const u16 port) |
499 |
{ |
{ |
500 |
u8 operation; |
u8 operation; |
501 |
if (sock_type == SOCK_DGRAM) |
if (sock_type == SOCK_DGRAM) |
502 |
operation = NETWORK_ACL_UDP_CONNECT; |
operation = CCS_NETWORK_UDP_CONNECT; |
503 |
else |
else |
504 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = CCS_NETWORK_RAW_CONNECT; |
505 |
return ccs_check_network_entry(is_ipv6, operation, |
return ccs_network_entry(is_ipv6, operation, |
506 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
507 |
} |
} |
508 |
|
|
509 |
/** |
/** |
510 |
* ccs_check_network_recvmsg_acl - Check permission for recvmsg() operation. |
* ccs_network_recvmsg_acl - Check permission for recvmsg() operation. |
511 |
* |
* |
512 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
513 |
* @sock_type: Type of socket. (UDP or RAW) |
* @sock_type: Type of socket. (UDP or RAW) |
516 |
* |
* |
517 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
518 |
*/ |
*/ |
519 |
static inline int ccs_check_network_recvmsg_acl(const bool is_ipv6, |
static inline int ccs_network_recvmsg_acl(const bool is_ipv6, |
520 |
const int sock_type, |
const int sock_type, |
521 |
const u8 *address, |
const u8 *address, |
522 |
const u16 port) |
const u16 port) |
523 |
{ |
{ |
524 |
int retval; |
int retval; |
525 |
const u8 operation |
const u8 operation |
526 |
= (sock_type == SOCK_DGRAM) ? |
= (sock_type == SOCK_DGRAM) ? |
527 |
NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
CCS_NETWORK_UDP_CONNECT : CCS_NETWORK_RAW_CONNECT; |
528 |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
529 |
retval = ccs_check_network_entry(is_ipv6, operation, |
retval = ccs_network_entry(is_ipv6, operation, |
530 |
(const u32 *) address, ntohs(port)); |
(const u32 *) address, ntohs(port)); |
531 |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
532 |
return retval; |
return retval; |
533 |
} |
} |
591 |
struct sockaddr_in *addr4; |
struct sockaddr_in *addr4; |
592 |
case AF_INET6: |
case AF_INET6: |
593 |
addr6 = (struct sockaddr_in6 *) addr; |
addr6 = (struct sockaddr_in6 *) addr; |
594 |
error = ccs_check_network_listen_acl(true, |
error = ccs_network_listen_acl(true, |
595 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
596 |
addr6->sin6_port); |
addr6->sin6_port); |
597 |
break; |
break; |
598 |
case AF_INET: |
case AF_INET: |
599 |
addr4 = (struct sockaddr_in *) addr; |
addr4 = (struct sockaddr_in *) addr; |
600 |
error = ccs_check_network_listen_acl(false, |
error = ccs_network_listen_acl(false, |
601 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
602 |
addr4->sin_port); |
addr4->sin_port); |
603 |
break; |
break; |
604 |
} |
} |
605 |
return error; |
return error; |
634 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
635 |
else |
else |
636 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
637 |
error = ccs_check_network_connect_acl(true, type, |
error = ccs_network_connect_acl(true, type, |
638 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
639 |
port); |
port); |
640 |
break; |
break; |
641 |
case AF_INET: |
case AF_INET: |
642 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
646 |
port = addr4->sin_port; |
port = addr4->sin_port; |
647 |
else |
else |
648 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
649 |
error = ccs_check_network_connect_acl(false, type, |
error = ccs_network_connect_acl(false, type, |
650 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
651 |
port); |
port); |
652 |
break; |
break; |
653 |
} |
} |
654 |
if (type != SOCK_STREAM) |
if (type != SOCK_STREAM) |
692 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
693 |
else |
else |
694 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
695 |
error = ccs_check_network_bind_acl(true, type, |
error = ccs_network_bind_acl(true, type, |
696 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
697 |
port); |
port); |
698 |
break; |
break; |
699 |
case AF_INET: |
case AF_INET: |
700 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
704 |
port = addr4->sin_port; |
port = addr4->sin_port; |
705 |
else |
else |
706 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
707 |
error = ccs_check_network_bind_acl(false, type, |
error = ccs_network_bind_acl(false, type, |
708 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
709 |
port); |
port); |
710 |
break; |
break; |
711 |
} |
} |
712 |
return error; |
return error; |
739 |
struct sockaddr_in *addr4; |
struct sockaddr_in *addr4; |
740 |
case AF_INET6: |
case AF_INET6: |
741 |
addr6 = (struct sockaddr_in6 *) addr; |
addr6 = (struct sockaddr_in6 *) addr; |
742 |
error = ccs_check_network_accept_acl(true, |
error = ccs_network_accept_acl(true, |
743 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
744 |
addr6->sin6_port); |
addr6->sin6_port); |
745 |
break; |
break; |
746 |
case AF_INET: |
case AF_INET: |
747 |
addr4 = (struct sockaddr_in *) addr; |
addr4 = (struct sockaddr_in *) addr; |
748 |
error = ccs_check_network_accept_acl(false, |
error = ccs_network_accept_acl(false, |
749 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
750 |
addr4->sin_port); |
addr4->sin_port); |
751 |
break; |
break; |
752 |
} |
} |
753 |
return error; |
return error; |
776 |
port = addr6->sin6_port; |
port = addr6->sin6_port; |
777 |
else |
else |
778 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
779 |
error = ccs_check_network_sendmsg_acl(true, type, |
error = ccs_network_sendmsg_acl(true, type, |
780 |
addr6->sin6_addr.s6_addr, |
addr6->sin6_addr.s6_addr, |
781 |
port); |
port); |
782 |
break; |
break; |
783 |
case AF_INET: |
case AF_INET: |
784 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
788 |
port = addr4->sin_port; |
port = addr4->sin_port; |
789 |
else |
else |
790 |
port = htons(sock->sk->sk_protocol); |
port = htons(sock->sk->sk_protocol); |
791 |
error = ccs_check_network_sendmsg_acl(false, type, |
error = ccs_network_sendmsg_acl(false, type, |
792 |
(u8 *) &addr4->sin_addr, |
(u8 *) &addr4->sin_addr, |
793 |
port); |
port); |
794 |
break; |
break; |
795 |
} |
} |
796 |
return error; |
return error; |
888 |
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
889 |
port = htons(sk->sk_protocol); |
port = htons(sk->sk_protocol); |
890 |
} |
} |
891 |
error = ccs_check_network_recvmsg_acl(true, type, |
error = ccs_network_recvmsg_acl(true, type, |
892 |
(u8 *) &sin6, port); |
(u8 *) &sin6, port); |
893 |
break; |
break; |
894 |
case PF_INET: |
case PF_INET: |
895 |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
899 |
sin4.s_addr = ip_hdr(skb)->saddr; |
sin4.s_addr = ip_hdr(skb)->saddr; |
900 |
port = htons(sk->sk_protocol); |
port = htons(sk->sk_protocol); |
901 |
} |
} |
902 |
error = ccs_check_network_recvmsg_acl(false, type, |
error = ccs_network_recvmsg_acl(false, type, |
903 |
(u8 *) &sin4, port); |
(u8 *) &sin4, port); |
904 |
break; |
break; |
905 |
} |
} |
906 |
if (!error) |
if (!error) |