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.8.0-pre 2010/08/01 |
|
* |
|
|
* Version: 1.6.6-pre 2009/01/05 |
|
7 |
* |
* |
8 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
9 |
* See README.ccs for ChangeLog. |
* See README.ccs for ChangeLog. |
10 |
* |
* |
11 |
*/ |
*/ |
12 |
|
|
|
#include <linux/ccs_common.h> |
|
|
#include <linux/tomoyo.h> |
|
|
#include <linux/realpath.h> |
|
13 |
#include <linux/net.h> |
#include <linux/net.h> |
14 |
#include <linux/inet.h> |
#include <linux/inet.h> |
15 |
#include <linux/in.h> |
#include <linux/in.h> |
17 |
#include <net/ip.h> |
#include <net/ip.h> |
18 |
#include <net/ipv6.h> |
#include <net/ipv6.h> |
19 |
#include <net/udp.h> |
#include <net/udp.h> |
20 |
|
#include "internal.h" |
21 |
|
|
22 |
/* Index numbers for Network Controls. */ |
struct ccs_addr_info { |
23 |
enum ccs_network_acl_index { |
u8 protocol; |
24 |
NETWORK_ACL_UDP_BIND, |
u8 operation; |
25 |
NETWORK_ACL_UDP_CONNECT, |
u16 port; /* In network byte order. */ |
26 |
NETWORK_ACL_TCP_BIND, |
const u32 *address; /* In network byte order. */ |
27 |
NETWORK_ACL_TCP_LISTEN, |
bool is_ipv6; |
|
NETWORK_ACL_TCP_CONNECT, |
|
|
NETWORK_ACL_TCP_ACCEPT, |
|
|
NETWORK_ACL_RAW_BIND, |
|
|
NETWORK_ACL_RAW_CONNECT |
|
28 |
}; |
}; |
29 |
|
|
30 |
/** |
const char *ccs_net_protocol_keyword[CCS_MAX_NETWORK_PROTOCOL] = { |
31 |
* ccs_audit_network_log - Audit network log. |
[CCS_NETWORK_TCP_PROTOCOL] = "TCP", |
32 |
* |
[CCS_NETWORK_UDP_PROTOCOL] = "UDP", |
33 |
* @r: Pointer to "struct ccs_request_info". |
[CCS_NETWORK_RAW_PROTOCOL] = "RAW", |
34 |
* @operation: The name of operation. |
}; |
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* @is_granted: True if this is a granted log. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int ccs_audit_network_log(struct ccs_request_info *r, |
|
|
const char *operation, const char *address, |
|
|
const u16 port, const bool is_granted) |
|
|
{ |
|
|
return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK |
|
|
"%s %s %u\n", operation, address, port); |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_save_ipv6_address - Keep the given IPv6 address on the RAM. |
|
|
* |
|
|
* @addr: Pointer to "struct in6_addr". |
|
|
* |
|
|
* Returns pointer to "struct in6_addr" on success, NULL otherwise. |
|
|
* |
|
|
* The RAM is shared, so NEVER try to modify or kfree() the returned address. |
|
|
*/ |
|
|
static const struct in6_addr *ccs_save_ipv6_address(const struct in6_addr *addr) |
|
|
{ |
|
|
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; |
|
|
} |
|
35 |
|
|
36 |
/* The list for "struct ccs_address_group_entry". */ |
const char *ccs_net_keyword[CCS_MAX_NETWORK_OPERATION] = { |
37 |
static LIST1_HEAD(ccs_address_group_list); |
[CCS_NETWORK_BIND] = "bind", |
38 |
|
[CCS_NETWORK_LISTEN] = "listen", |
39 |
|
[CCS_NETWORK_CONNECT] = "connect", |
40 |
|
[CCS_NETWORK_ACCEPT] = "accept", |
41 |
|
[CCS_NETWORK_SEND] = "send", |
42 |
|
[CCS_NETWORK_RECV] = "recv", |
43 |
|
}; |
44 |
|
|
45 |
/** |
/** |
46 |
* ccs_update_address_group_entry - Update "struct ccs_address_group_entry" list. |
* ccs_audit_network_log - Audit network log. |
47 |
* |
* |
48 |
* @group_name: The name of address group. |
* @r: Pointer to "struct ccs_request_info". |
|
* @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. |
|
49 |
* |
* |
50 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
51 |
*/ |
*/ |
52 |
static int ccs_update_address_group_entry(const char *group_name, |
static int ccs_audit_network_log(struct ccs_request_info *r) |
53 |
const bool is_ipv6, |
{ |
54 |
const u16 *min_address, |
char buf[128]; |
55 |
const u16 *max_address, |
const char *protocol = |
56 |
const bool is_delete) |
ccs_net_protocol_keyword[r->param.network.protocol]; |
57 |
{ |
const char *operation = ccs_net_keyword[r->param.network.operation]; |
58 |
static DEFINE_MUTEX(lock); |
const u32 *address = r->param.network.address; |
59 |
struct ccs_address_group_entry *new_group; |
const u16 port = r->param.network.port; |
60 |
struct ccs_address_group_entry *group; |
if (r->param.network.is_ipv6) |
61 |
struct ccs_address_group_member *new_member; |
ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *) |
62 |
struct ccs_address_group_member *member; |
address, (const struct in6_addr *) address); |
63 |
const struct ccs_path_info *saved_group_name; |
else |
64 |
const struct in6_addr *saved_min_address = NULL; |
ccs_print_ipv4(buf, sizeof(buf), r->param.network.ip, |
65 |
const struct in6_addr *saved_max_address = NULL; |
r->param.network.ip); |
66 |
int error = -ENOMEM; |
ccs_write_log(r, "network %s %s %s %u\n", protocol, operation, buf, |
67 |
bool found = false; |
port); |
68 |
if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) || |
if (r->granted) |
69 |
!group_name[0]) |
return 0; |
70 |
return -EINVAL; |
ccs_warn_log(r, "network %s %s %s %u", protocol, operation, buf, port); |
71 |
saved_group_name = ccs_save_name(group_name); |
return ccs_supervisor(r, "network %s %s %s %u\n", protocol, operation, |
72 |
if (!saved_group_name) |
buf, port); |
|
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; |
|
73 |
} |
} |
74 |
|
|
75 |
/** |
/** |
79 |
* @min: Pointer to store min address. |
* @min: Pointer to store min address. |
80 |
* @max: Pointer to store max address. |
* @max: Pointer to store max address. |
81 |
* |
* |
82 |
* Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise. |
* Returns CCS_IP_ADDRESS_TYPE_IPv6 if @address is an IPv6, |
83 |
|
* CCS_IP_ADDRESS_TYPE_IPv4 if @address is an IPv4, |
84 |
|
* CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP otherwise. |
85 |
*/ |
*/ |
86 |
static int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
87 |
{ |
{ |
88 |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
89 |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
99 |
min[i] = htons(min[i]); |
min[i] = htons(min[i]); |
100 |
max[i] = htons(max[i]); |
max[i] = htons(max[i]); |
101 |
} |
} |
102 |
return 2; |
return CCS_IP_ADDRESS_TYPE_IPv6; |
103 |
} |
} |
104 |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
105 |
&min[0], &min[1], &min[2], &min[3], |
&min[0], &min[1], &min[2], &min[3], |
109 |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
110 |
memmove(min, &ip, sizeof(ip)); |
memmove(min, &ip, sizeof(ip)); |
111 |
if (count == 8) |
if (count == 8) |
112 |
ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16) |
ip = htonl((((u8) max[0]) << 24) |
113 |
|
+ (((u8) max[1]) << 16) |
114 |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
115 |
memmove(max, &ip, sizeof(ip)); |
memmove(max, &ip, sizeof(ip)); |
116 |
return 1; |
return CCS_IP_ADDRESS_TYPE_IPv4; |
117 |
} |
} |
118 |
return 0; |
return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
|
} |
|
|
|
|
|
/** |
|
|
* 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; |
|
119 |
} |
} |
120 |
|
|
121 |
/** |
/** |
122 |
* ccs_address_matches_to_group - Check whether the given address matches members of the given address group. |
* ccs_print_ipv4 - Print an IPv4 address. |
123 |
* |
* |
124 |
* @is_ipv6: True if @address is an IPv6 address. |
* @buffer: Buffer to write to. |
125 |
* @address: An IPv4 or IPv6 address. |
* @buffer_len: Size of @buffer. |
126 |
* @group: Pointer to "struct ccs_address_group_entry". |
* @min_ip: Min address in host byte order. |
127 |
* |
* @max_ip: Max address in host byte order. |
|
* Returns true if @address matches addresses in @group group, false otherwise. |
|
|
*/ |
|
|
static bool ccs_address_matches_to_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". |
|
128 |
* |
* |
129 |
* Returns true on success, false otherwise. |
* Returns nothing. |
130 |
*/ |
*/ |
131 |
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
void ccs_print_ipv4(char *buffer, const int buffer_len, |
132 |
|
const u32 min_ip, const u32 max_ip) |
133 |
{ |
{ |
134 |
struct list1_head *gpos; |
memset(buffer, 0, buffer_len); |
135 |
struct list1_head *mpos; |
snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u", |
136 |
list1_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) { |
HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-', |
137 |
struct ccs_address_group_entry *group; |
HIPQUAD(max_ip)); |
|
group = list1_entry(gpos, struct ccs_address_group_entry, list); |
|
|
list1_for_each_cookie(mpos, head->read_var2, |
|
|
&group->address_group_member_list) { |
|
|
char buf[128]; |
|
|
struct ccs_address_group_member *member; |
|
|
member = list1_entry(mpos, |
|
|
struct ccs_address_group_member, |
|
|
list); |
|
|
if (member->is_deleted) |
|
|
continue; |
|
|
if (member->is_ipv6) { |
|
|
const struct in6_addr *min_address |
|
|
= member->min.ipv6; |
|
|
const struct in6_addr *max_address |
|
|
= member->max.ipv6; |
|
|
ccs_print_ipv6(buf, sizeof(buf), min_address); |
|
|
if (min_address != max_address) { |
|
|
int len; |
|
|
char *cp = buf + strlen(buf); |
|
|
*cp++ = '-'; |
|
|
len = strlen(buf); |
|
|
ccs_print_ipv6(cp, sizeof(buf) - len, |
|
|
max_address); |
|
|
} |
|
|
} else { |
|
|
const u32 min_address = member->min.ipv4; |
|
|
const u32 max_address = member->max.ipv4; |
|
|
memset(buf, 0, sizeof(buf)); |
|
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", |
|
|
HIPQUAD(min_address)); |
|
|
if (min_address != max_address) { |
|
|
const int len = strlen(buf); |
|
|
snprintf(buf + len, |
|
|
sizeof(buf) - 1 - len, |
|
|
"-%u.%u.%u.%u", |
|
|
HIPQUAD(max_address)); |
|
|
} |
|
|
} |
|
|
if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP |
|
|
"%s %s\n", group->group_name->name, |
|
|
buf)) |
|
|
goto out; |
|
|
} |
|
|
} |
|
|
return true; |
|
|
out: |
|
|
return false; |
|
138 |
} |
} |
139 |
|
|
140 |
#if !defined(NIP6) |
#if !defined(NIP6) |
141 |
#define NIP6(addr) \ |
#define NIP6(addr) \ |
142 |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
143 |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
144 |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
145 |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
146 |
#endif |
#endif |
147 |
|
|
148 |
/** |
/** |
150 |
* |
* |
151 |
* @buffer: Buffer to write to. |
* @buffer: Buffer to write to. |
152 |
* @buffer_len: Size of @buffer. |
* @buffer_len: Size of @buffer. |
153 |
* @ip: Pointer to "struct in6_addr". |
* @min_ip: Pointer to "struct in6_addr". |
154 |
|
* @max_ip: Pointer to "struct in6_addr". |
155 |
* |
* |
156 |
* Returns nothing. |
* Returns nothing. |
157 |
*/ |
*/ |
158 |
void ccs_print_ipv6(char *buffer, const int buffer_len, |
void ccs_print_ipv6(char *buffer, const int buffer_len, |
159 |
const struct in6_addr *ip) |
const struct in6_addr *min_ip, |
160 |
|
const struct in6_addr *max_ip) |
161 |
{ |
{ |
162 |
memset(buffer, 0, buffer_len); |
memset(buffer, 0, buffer_len); |
163 |
snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip)); |
snprintf(buffer, buffer_len - 1, |
164 |
} |
"%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x", |
165 |
|
NIP6(*min_ip), min_ip == max_ip ? '\0' : '-', |
166 |
/** |
NIP6(*max_ip)); |
167 |
* ccs_net2keyword - Convert network operation index to network operation name. |
} |
168 |
* |
|
169 |
* @operation: Type of operation. |
static bool ccs_check_network_acl(struct ccs_request_info *r, |
170 |
* |
const struct ccs_acl_info *ptr) |
171 |
* Returns the name of operation. |
{ |
172 |
*/ |
const struct ccs_ip_network_acl *acl = |
173 |
const char *ccs_net2keyword(const u8 operation) |
container_of(ptr, typeof(*acl), head); |
174 |
{ |
bool ret; |
175 |
const char *keyword = "unknown"; |
if (!(acl->perm & (1 << r->param.network.operation)) || |
176 |
switch (operation) { |
!ccs_compare_number_union(r->param.network.port, &acl->port)) |
177 |
case NETWORK_ACL_UDP_BIND: |
return false; |
178 |
keyword = "UDP bind"; |
switch (acl->address_type) { |
179 |
break; |
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
180 |
case NETWORK_ACL_UDP_CONNECT: |
ret = ccs_address_matches_group(r->param.network.is_ipv6, |
181 |
keyword = "UDP connect"; |
r->param.network.address, |
182 |
break; |
acl->address.group); |
183 |
case NETWORK_ACL_TCP_BIND: |
break; |
184 |
keyword = "TCP bind"; |
case CCS_IP_ADDRESS_TYPE_IPv4: |
185 |
break; |
ret = !r->param.network.is_ipv6 && |
186 |
case NETWORK_ACL_TCP_LISTEN: |
acl->address.ipv4.min <= r->param.network.ip && |
187 |
keyword = "TCP listen"; |
r->param.network.ip <= acl->address.ipv4.max; |
188 |
break; |
break; |
189 |
case NETWORK_ACL_TCP_CONNECT: |
default: |
190 |
keyword = "TCP connect"; |
ret = r->param.network.is_ipv6 && |
191 |
break; |
memcmp(acl->address.ipv6.min, r->param.network.address, |
192 |
case NETWORK_ACL_TCP_ACCEPT: |
16) <= 0 && |
193 |
keyword = "TCP accept"; |
memcmp(r->param.network.address, acl->address.ipv6.max, |
194 |
break; |
16) <= 0; |
195 |
case NETWORK_ACL_RAW_BIND: |
break; |
196 |
keyword = "RAW bind"; |
} |
197 |
break; |
return ret; |
198 |
case NETWORK_ACL_RAW_CONNECT: |
} |
199 |
keyword = "RAW connect"; |
|
200 |
break; |
static const u8 |
201 |
} |
ccs_net2mac[CCS_MAX_NETWORK_PROTOCOL][CCS_MAX_NETWORK_OPERATION] = { |
202 |
return keyword; |
[CCS_NETWORK_TCP_PROTOCOL] = { |
203 |
} |
[CCS_NETWORK_BIND] = CCS_MAC_NETWORK_TCP_BIND, |
204 |
|
[CCS_NETWORK_LISTEN] = CCS_MAC_NETWORK_TCP_LISTEN, |
205 |
|
[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_TCP_CONNECT, |
206 |
|
[CCS_NETWORK_ACCEPT] = CCS_MAC_NETWORK_TCP_ACCEPT, |
207 |
|
}, |
208 |
|
[CCS_NETWORK_UDP_PROTOCOL] = { |
209 |
|
[CCS_NETWORK_BIND] = CCS_MAC_NETWORK_UDP_BIND, |
210 |
|
[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UDP_SEND, |
211 |
|
[CCS_NETWORK_SEND] = CCS_MAC_NETWORK_UDP_SEND, |
212 |
|
[CCS_NETWORK_RECV] = CCS_MAC_NETWORK_UDP_RECV, |
213 |
|
}, |
214 |
|
[CCS_NETWORK_RAW_PROTOCOL] = { |
215 |
|
[CCS_NETWORK_BIND] = CCS_MAC_NETWORK_RAW_BIND, |
216 |
|
[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_RAW_SEND, |
217 |
|
[CCS_NETWORK_SEND] = CCS_MAC_NETWORK_RAW_SEND, |
218 |
|
[CCS_NETWORK_RECV] = CCS_MAC_NETWORK_RAW_RECV, |
219 |
|
}, |
220 |
|
}; |
221 |
|
|
222 |
/** |
/** |
223 |
* ccs_update_network_entry - Update "struct ccs_ip_network_acl_record" list. |
* ccs_network_entry - Check permission for network operation. |
224 |
* |
* |
225 |
* @operation: Type of operation. |
* @address: Pointer to "struct ccs_ip_address". |
|
* @record_type: Type of address. |
|
|
* @group: Pointer to "struct ccs_address_group_entry". 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 domain_info". |
|
|
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
|
|
* @is_delete: True if it is a delete request. |
|
226 |
* |
* |
227 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
228 |
*/ |
*/ |
229 |
static int ccs_update_network_entry(const u8 operation, const u8 record_type, |
static int ccs_network_entry(const struct ccs_addr_info *address) |
230 |
const struct ccs_address_group_entry *group, |
{ |
231 |
const u32 *min_address, |
const int idx = ccs_read_lock(); |
232 |
const u32 *max_address, |
struct ccs_request_info r; |
233 |
const u16 min_port, const u16 max_port, |
int error = 0; |
234 |
struct domain_info *domain, |
const u8 type = ccs_net2mac[address->protocol][address->operation]; |
235 |
const struct ccs_condition_list *condition, |
if (!type || ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) { |
236 |
const bool is_delete) |
r.param_type = CCS_TYPE_IP_NETWORK_ACL; |
237 |
{ |
r.param.network.protocol = address->protocol; |
238 |
static DEFINE_MUTEX(lock); |
r.param.network.operation = address->operation; |
239 |
struct ccs_acl_info *ptr; |
r.param.network.is_ipv6 = address->is_ipv6; |
240 |
struct ccs_ip_network_acl_record *acl; |
r.param.network.address = address->address; |
241 |
int error = -ENOMEM; |
r.param.network.port = ntohs(address->port); |
242 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
/* use host byte order to allow u32 comparison than memcmp().*/ |
243 |
const u32 min_ip = ntohl(*min_address); |
r.param.network.ip = ntohl(*address->address); |
244 |
const u32 max_ip = ntohl(*max_address); |
do { |
245 |
const struct in6_addr *saved_min_address = NULL; |
ccs_check_acl(&r, ccs_check_network_acl); |
246 |
const struct in6_addr *saved_max_address = NULL; |
error = ccs_audit_network_log(&r); |
247 |
if (!domain) |
} while (error == CCS_RETRY_REQUEST); |
|
return -EINVAL; |
|
|
if (record_type != IP_RECORD_TYPE_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); |
|
|
if (is_delete) |
|
|
goto delete; |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
|
|
if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
if (ccs_get_condition_part(ptr) != condition) |
|
|
continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (acl->operation_type != operation || |
|
|
acl->record_type != record_type || |
|
|
acl->min_port != min_port || max_port != acl->max_port) |
|
|
continue; |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
if (acl->u.group != group) |
|
|
continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
if (acl->u.ipv4.min != min_ip || |
|
|
max_ip != acl->u.ipv4.max) |
|
|
continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
|
|
if (acl->u.ipv6.min != saved_min_address || |
|
|
saved_max_address != acl->u.ipv6.max) |
|
|
continue; |
|
|
} |
|
|
error = ccs_add_domain_acl(NULL, ptr); |
|
|
goto out; |
|
|
} |
|
|
/* Not found. Append it to the tail. */ |
|
|
acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition); |
|
|
if (!acl) |
|
|
goto out; |
|
|
acl->operation_type = operation; |
|
|
acl->record_type = record_type; |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
acl->u.group = group; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
acl->u.ipv4.min = min_ip; |
|
|
acl->u.ipv4.max = max_ip; |
|
|
} else { |
|
|
acl->u.ipv6.min = saved_min_address; |
|
|
acl->u.ipv6.max = saved_max_address; |
|
|
} |
|
|
acl->min_port = min_port; |
|
|
acl->max_port = max_port; |
|
|
error = ccs_add_domain_acl(domain, &acl->head); |
|
|
goto out; |
|
|
delete: |
|
|
error = -ENOENT; |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
|
|
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
if (ccs_get_condition_part(ptr) != condition) |
|
|
continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (acl->operation_type != operation || |
|
|
acl->record_type != record_type || |
|
|
acl->min_port != min_port || max_port != acl->max_port) |
|
|
continue; |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
if (acl->u.group != group) |
|
|
continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
if (acl->u.ipv4.min != min_ip || |
|
|
max_ip != acl->u.ipv4.max) |
|
|
continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
|
|
if (acl->u.ipv6.min != saved_min_address || |
|
|
saved_max_address != acl->u.ipv6.max) |
|
|
continue; |
|
|
} |
|
|
error = ccs_del_domain_acl(ptr); |
|
|
break; |
|
248 |
} |
} |
249 |
out: |
ccs_read_unlock(idx); |
|
mutex_unlock(&lock); |
|
250 |
return error; |
return error; |
251 |
} |
} |
252 |
|
|
253 |
/** |
static bool ccs_same_ip_network_acl(const struct ccs_acl_info *a, |
254 |
* ccs_check_network_entry - Check permission for network operation. |
const struct ccs_acl_info *b) |
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @operation: Type of operation. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int ccs_check_network_entry(const bool is_ipv6, const u8 operation, |
|
|
const u32 *address, const u16 port) |
|
255 |
{ |
{ |
256 |
struct ccs_request_info r; |
const struct ccs_ip_network_acl *p1 = container_of(a, typeof(*p1), |
257 |
struct ccs_acl_info *ptr; |
head); |
258 |
const char *keyword = ccs_net2keyword(operation); |
const struct ccs_ip_network_acl *p2 = container_of(b, typeof(*p2), |
259 |
bool is_enforce; |
head); |
260 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
return ccs_same_acl_head(&p1->head, &p2->head) |
261 |
const u32 ip = ntohl(*address); |
&& p1->protocol == p2->protocol |
262 |
bool found = false; |
&& p1->address_type == p2->address_type && |
263 |
char buf[64]; |
p1->address.ipv4.min == p2->address.ipv4.min && |
264 |
if (!ccs_can_sleep()) |
p1->address.ipv6.min == p2->address.ipv6.min && |
265 |
return 0; |
p1->address.ipv4.max == p2->address.ipv4.max && |
266 |
ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_NETWORK); |
p1->address.ipv6.max == p2->address.ipv6.max && |
267 |
is_enforce = (r.mode == 3); |
p1->address.group == p2->address.group && |
268 |
if (!r.mode) |
ccs_same_number_union(&p1->port, &p2->port); |
269 |
return 0; |
} |
270 |
retry: |
|
271 |
list1_for_each_entry(ptr, &r.domain->acl_info_list, list) { |
static bool ccs_merge_ip_network_acl(struct ccs_acl_info *a, |
272 |
struct ccs_ip_network_acl_record *acl; |
struct ccs_acl_info *b, |
273 |
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
const bool is_delete) |
274 |
continue; |
{ |
275 |
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
u8 * const a_perm = &container_of(a, struct ccs_ip_network_acl, head) |
276 |
if (acl->operation_type != operation || port < acl->min_port || |
->perm; |
277 |
acl->max_port < port || !ccs_check_condition(&r, ptr)) |
u8 perm = *a_perm; |
278 |
continue; |
const u8 b_perm = container_of(b, struct ccs_ip_network_acl, head) |
279 |
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
->perm; |
280 |
if (!ccs_address_matches_to_group(is_ipv6, address, |
if (is_delete) |
281 |
acl->u.group)) |
perm &= ~b_perm; |
|
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); |
|
282 |
else |
else |
283 |
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
perm |= b_perm; |
284 |
ccs_audit_network_log(&r, keyword, buf, port, found); |
*a_perm = perm; |
285 |
if (found) |
return !perm; |
|
return 0; |
|
|
if (ccs_verbose_mode(r.domain)) |
|
|
printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n", |
|
|
ccs_get_msg(is_enforce), keyword, buf, port, |
|
|
ccs_get_last_name(r.domain)); |
|
|
if (is_enforce) { |
|
|
int error = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK |
|
|
"%s %s %u\n", keyword, buf, |
|
|
port); |
|
|
if (error == 1) |
|
|
goto retry; |
|
|
return error; |
|
|
} |
|
|
if (r.mode == 1 && ccs_check_domain_quota(r.domain)) |
|
|
ccs_update_network_entry(operation, is_ipv6 ? |
|
|
IP_RECORD_TYPE_IPv6 : |
|
|
IP_RECORD_TYPE_IPv4, |
|
|
NULL, address, address, port, port, |
|
|
r.domain, ccs_handler_cond(), 0); |
|
|
return 0; |
|
286 |
} |
} |
287 |
|
|
288 |
/** |
/** |
289 |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list. |
* ccs_write_network - Write "struct ccs_ip_network_acl" list. |
290 |
* |
* |
291 |
* @data: String to parse. |
* @data: String to parse. |
292 |
* @domain: Pointer to "struct domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
293 |
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
* @condition: Pointer to "struct ccs_condition". Maybe NULL. |
294 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
295 |
* |
* |
296 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
297 |
*/ |
*/ |
298 |
int ccs_write_network_policy(char *data, struct domain_info *domain, |
int ccs_write_network(char *data, struct ccs_domain_info *domain, |
299 |
const struct ccs_condition_list *condition, |
struct ccs_condition *condition, const bool is_delete) |
|
const bool is_delete) |
|
300 |
{ |
{ |
301 |
u8 sock_type; |
struct ccs_ip_network_acl e = { |
302 |
u8 operation; |
.head.type = CCS_TYPE_IP_NETWORK_ACL, |
303 |
u8 record_type; |
.head.cond = condition, |
304 |
|
}; |
305 |
u16 min_address[8]; |
u16 min_address[8]; |
306 |
u16 max_address[8]; |
u16 max_address[8]; |
307 |
struct ccs_address_group_entry *group = NULL; |
int error = is_delete ? -ENOENT : -ENOMEM; |
308 |
u16 min_port; |
u8 type; |
309 |
u16 max_port; |
char *w[4]; |
310 |
u8 count; |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
311 |
char *cp1 = strchr(data, ' '); |
return -EINVAL; |
312 |
char *cp2; |
for (e.protocol = 0; e.protocol < CCS_MAX_NETWORK_PROTOCOL; |
313 |
if (!cp1) |
e.protocol++) |
314 |
goto out; |
if (!strcmp(w[0], ccs_net_protocol_keyword[e.protocol])) |
315 |
cp1++; |
break; |
316 |
if (!strncmp(data, "TCP ", 4)) |
for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++) |
317 |
sock_type = SOCK_STREAM; |
if (ccs_permstr(w[1], ccs_net_keyword[type])) |
318 |
else if (!strncmp(data, "UDP ", 4)) |
e.perm |= 1 << type; |
319 |
sock_type = SOCK_DGRAM; |
if (e.protocol == CCS_MAX_NETWORK_PROTOCOL || !e.perm) |
320 |
else if (!strncmp(data, "RAW ", 4)) |
return -EINVAL; |
321 |
sock_type = SOCK_RAW; |
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
322 |
else |
case CCS_IP_ADDRESS_TYPE_IPv6: |
323 |
goto out; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv6; |
324 |
cp2 = strchr(cp1, ' '); |
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
325 |
if (!cp2) |
min_address); |
326 |
goto out; |
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
327 |
cp2++; |
max_address); |
328 |
if (!strncmp(cp1, "bind ", 5)) |
if (!e.address.ipv6.min || !e.address.ipv6.max) |
329 |
switch (sock_type) { |
goto out; |
|
case SOCK_STREAM: |
|
|
operation = NETWORK_ACL_TCP_BIND; |
|
|
break; |
|
|
case SOCK_DGRAM: |
|
|
operation = NETWORK_ACL_UDP_BIND; |
|
|
break; |
|
|
default: |
|
|
operation = NETWORK_ACL_RAW_BIND; |
|
|
} |
|
|
else if (!strncmp(cp1, "connect ", 8)) |
|
|
switch (sock_type) { |
|
|
case SOCK_STREAM: |
|
|
operation = NETWORK_ACL_TCP_CONNECT; |
|
|
break; |
|
|
case SOCK_DGRAM: |
|
|
operation = NETWORK_ACL_UDP_CONNECT; |
|
|
break; |
|
|
default: |
|
|
operation = NETWORK_ACL_RAW_CONNECT; |
|
|
} |
|
|
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7)) |
|
|
operation = NETWORK_ACL_TCP_LISTEN; |
|
|
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7)) |
|
|
operation = NETWORK_ACL_TCP_ACCEPT; |
|
|
else |
|
|
goto out; |
|
|
cp1 = strchr(cp2, ' '); |
|
|
if (!cp1) |
|
|
goto out; |
|
|
*cp1++ = '\0'; |
|
|
switch (ccs_parse_ip_address(cp2, min_address, max_address)) { |
|
|
case 2: |
|
|
record_type = IP_RECORD_TYPE_IPv6; |
|
330 |
break; |
break; |
331 |
case 1: |
case CCS_IP_ADDRESS_TYPE_IPv4: |
332 |
record_type = IP_RECORD_TYPE_IPv4; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv4; |
333 |
|
/* use host byte order to allow u32 comparison.*/ |
334 |
|
e.address.ipv4.min = ntohl(*(u32 *) min_address); |
335 |
|
e.address.ipv4.max = ntohl(*(u32 *) max_address); |
336 |
break; |
break; |
337 |
default: |
default: |
338 |
if (*cp2 != '@') |
if (w[2][0] != '@') |
339 |
goto out; |
return -EINVAL; |
340 |
group = ccs_find_or_assign_new_address_group(cp2 + 1); |
e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
341 |
if (!group) |
e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP); |
342 |
|
if (!e.address.group) |
343 |
return -ENOMEM; |
return -ENOMEM; |
|
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
|
344 |
break; |
break; |
345 |
} |
} |
346 |
if (strchr(cp1, ' ')) |
if (!ccs_parse_number_union(w[3], &e.port)) |
347 |
goto out; |
goto out; |
348 |
count = sscanf(cp1, "%hu-%hu", &min_port, &max_port); |
error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain, |
349 |
if (count != 1 && count != 2) |
ccs_same_ip_network_acl, |
350 |
goto out; |
ccs_merge_ip_network_acl); |
|
if (count == 1) |
|
|
max_port = min_port; |
|
|
return ccs_update_network_entry(operation, record_type, group, |
|
|
(u32 *) min_address, |
|
|
(u32 *) max_address, |
|
|
min_port, max_port, domain, condition, |
|
|
is_delete); |
|
351 |
out: |
out: |
352 |
return -EINVAL; |
if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP) |
353 |
|
ccs_put_group(e.address.group); |
354 |
|
else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) { |
355 |
|
ccs_put_ipv6_address(e.address.ipv6.min); |
356 |
|
ccs_put_ipv6_address(e.address.ipv6.max); |
357 |
|
} |
358 |
|
ccs_put_number_union(&e.port); |
359 |
|
return error; |
360 |
} |
} |
361 |
|
|
362 |
/** |
#ifndef CONFIG_NET |
|
* ccs_check_network_listen_acl - Check permission for listen() operation. |
|
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static inline int ccs_check_network_listen_acl(const bool is_ipv6, |
|
|
const u8 *address, |
|
|
const u16 port) |
|
|
{ |
|
|
return ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN, |
|
|
(const u32 *) address, ntohs(port)); |
|
|
} |
|
363 |
|
|
364 |
/** |
void __init ccs_network_init(void) |
|
* ccs_check_network_connect_acl - Check permission for connect() operation. |
|
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @sock_type: Type of socket. (TCP or UDP or RAW) |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static inline int ccs_check_network_connect_acl(const bool is_ipv6, |
|
|
const int sock_type, |
|
|
const u8 *address, |
|
|
const u16 port) |
|
365 |
{ |
{ |
|
u8 operation; |
|
|
switch (sock_type) { |
|
|
case SOCK_STREAM: |
|
|
operation = NETWORK_ACL_TCP_CONNECT; |
|
|
break; |
|
|
case SOCK_DGRAM: |
|
|
operation = NETWORK_ACL_UDP_CONNECT; |
|
|
break; |
|
|
default: |
|
|
operation = NETWORK_ACL_RAW_CONNECT; |
|
|
} |
|
|
return ccs_check_network_entry(is_ipv6, operation, |
|
|
(const u32 *) address, ntohs(port)); |
|
366 |
} |
} |
367 |
|
|
368 |
/** |
#else |
369 |
* ccs_check_network_bind_acl - Check permission for bind() operation. |
|
370 |
* |
static bool ccs_check_address(const struct sockaddr *addr, |
371 |
* @is_ipv6: True if @address is an IPv6 address. |
const unsigned int addr_len, |
372 |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
struct ccs_addr_info *address) |
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int ccs_check_network_bind_acl(const bool is_ipv6, const int sock_type, |
|
|
const u8 *address, const u16 port) |
|
373 |
{ |
{ |
374 |
u8 operation; |
switch (addr->sa_family) { |
375 |
switch (sock_type) { |
case AF_INET6: |
376 |
case SOCK_STREAM: |
if (addr_len < SIN6_LEN_RFC2133) |
377 |
operation = NETWORK_ACL_TCP_BIND; |
goto skip; |
378 |
|
address->is_ipv6 = true; |
379 |
|
address->address = (u32 *) |
380 |
|
((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr; |
381 |
|
address->port = ((struct sockaddr_in6 *) addr)->sin6_port; |
382 |
break; |
break; |
383 |
case SOCK_DGRAM: |
case AF_INET: |
384 |
operation = NETWORK_ACL_UDP_BIND; |
if (addr_len < sizeof(struct sockaddr_in)) |
385 |
|
goto skip; |
386 |
|
address->is_ipv6 = false; |
387 |
|
address->address = (u32 *) |
388 |
|
&((struct sockaddr_in *) addr)->sin_addr; |
389 |
|
address->port = ((struct sockaddr_in *) addr)->sin_port; |
390 |
break; |
break; |
391 |
default: |
default: |
392 |
operation = NETWORK_ACL_RAW_BIND; |
goto skip; |
393 |
} |
} |
394 |
return ccs_check_network_entry(is_ipv6, operation, |
return true; |
395 |
(const u32 *) address, ntohs(port)); |
skip: |
396 |
} |
return false; |
|
|
|
|
/** |
|
|
* ccs_check_network_accept_acl - Check permission for accept() operation. |
|
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static inline int ccs_check_network_accept_acl(const bool is_ipv6, |
|
|
const u8 *address, |
|
|
const u16 port) |
|
|
{ |
|
|
int retval; |
|
|
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
|
|
retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, |
|
|
(const u32 *) address, ntohs(port)); |
|
|
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
|
|
return retval; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_check_network_sendmsg_acl - Check permission for sendmsg() operation. |
|
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @sock_type: Type of socket. (UDP or RAW) |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static inline int ccs_check_network_sendmsg_acl(const bool is_ipv6, |
|
|
const int sock_type, |
|
|
const u8 *address, |
|
|
const u16 port) |
|
|
{ |
|
|
u8 operation; |
|
|
if (sock_type == SOCK_DGRAM) |
|
|
operation = NETWORK_ACL_UDP_CONNECT; |
|
|
else |
|
|
operation = NETWORK_ACL_RAW_CONNECT; |
|
|
return ccs_check_network_entry(is_ipv6, operation, |
|
|
(const u32 *) address, ntohs(port)); |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_check_network_recvmsg_acl - Check permission for recvmsg() operation. |
|
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @sock_type: Type of socket. (UDP or RAW) |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static inline int ccs_check_network_recvmsg_acl(const bool is_ipv6, |
|
|
const int sock_type, |
|
|
const u8 *address, |
|
|
const u16 port) |
|
|
{ |
|
|
int retval; |
|
|
const u8 operation |
|
|
= (sock_type == SOCK_DGRAM) ? |
|
|
NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
|
|
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
|
|
retval = ccs_check_network_entry(is_ipv6, operation, |
|
|
(const u32 *) address, ntohs(port)); |
|
|
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
|
|
return retval; |
|
397 |
} |
} |
398 |
|
|
|
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
|
|
|
|
399 |
/* Check permission for creating a socket. */ |
/* Check permission for creating a socket. */ |
400 |
int ccs_socket_create_permission(int family, int type, int protocol) |
static int __ccs_socket_create_permission(int family, int type, int protocol) |
401 |
{ |
{ |
|
int error = 0; |
|
402 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
403 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
404 |
return 0; |
return 0; |
405 |
if (family == PF_PACKET && !ccs_capable(TOMOYO_USE_PACKET_SOCKET)) |
if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET)) |
406 |
return -EPERM; |
return -EPERM; |
407 |
if (family == PF_ROUTE && !ccs_capable(TOMOYO_USE_ROUTE_SOCKET)) |
if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET)) |
408 |
return -EPERM; |
return -EPERM; |
409 |
if (family != PF_INET && family != PF_INET6) |
return 0; |
|
return 0; |
|
|
switch (type) { |
|
|
case SOCK_STREAM: |
|
|
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CREATE)) |
|
|
error = -EPERM; |
|
|
break; |
|
|
case SOCK_DGRAM: |
|
|
if (!ccs_capable(TOMOYO_USE_INET_DGRAM_SOCKET)) |
|
|
error = -EPERM; |
|
|
break; |
|
|
case SOCK_RAW: |
|
|
if (!ccs_capable(TOMOYO_USE_INET_RAW_SOCKET)) |
|
|
error = -EPERM; |
|
|
break; |
|
|
} |
|
|
return error; |
|
410 |
} |
} |
411 |
|
|
412 |
/* Check permission for listening a TCP socket. */ |
/* Check permission for listening a TCP socket. */ |
413 |
int ccs_socket_listen_permission(struct socket *sock) |
static int __ccs_socket_listen_permission(struct socket *sock) |
414 |
{ |
{ |
415 |
|
struct sockaddr_storage addr; |
416 |
int error = 0; |
int error = 0; |
|
char addr[MAX_SOCK_ADDR]; |
|
417 |
int addr_len; |
int addr_len; |
418 |
|
struct ccs_addr_info address; |
419 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
420 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
421 |
return 0; |
return 0; |
428 |
default: |
default: |
429 |
return 0; |
return 0; |
430 |
} |
} |
431 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_LISTEN)) |
if (sock->ops->getname(sock, (struct sockaddr *) &addr, &addr_len, 0)) |
|
return -EPERM; |
|
|
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
|
432 |
return -EPERM; |
return -EPERM; |
433 |
switch (((struct sockaddr *) addr)->sa_family) { |
address.protocol = CCS_NETWORK_TCP_PROTOCOL; |
434 |
struct sockaddr_in6 *addr6; |
address.operation = CCS_NETWORK_LISTEN; |
435 |
struct sockaddr_in *addr4; |
if (ccs_check_address((struct sockaddr *) &addr, addr_len, &address)) |
436 |
case AF_INET6: |
error = ccs_network_entry(&address); |
437 |
addr6 = (struct sockaddr_in6 *) addr; |
return error; |
|
error = ccs_check_network_listen_acl(true, |
|
|
addr6->sin6_addr.s6_addr, |
|
|
addr6->sin6_port); |
|
|
break; |
|
|
case AF_INET: |
|
|
addr4 = (struct sockaddr_in *) addr; |
|
|
error = ccs_check_network_listen_acl(false, |
|
|
(u8 *) &addr4->sin_addr, |
|
|
addr4->sin_port); |
|
|
break; |
|
|
} |
|
|
return error; |
|
438 |
} |
} |
439 |
|
|
440 |
/* 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. */ |
441 |
int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_connect_permission(struct socket *sock, |
442 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
443 |
{ |
{ |
444 |
int error = 0; |
int error = 0; |
445 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
446 |
|
struct ccs_addr_info address; |
447 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
448 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
449 |
return 0; |
return 0; |
450 |
switch (type) { |
switch (type) { |
451 |
case SOCK_STREAM: |
case SOCK_STREAM: |
452 |
|
address.protocol = CCS_NETWORK_TCP_PROTOCOL; |
453 |
|
break; |
454 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
455 |
|
address.protocol = CCS_NETWORK_UDP_PROTOCOL; |
456 |
|
break; |
457 |
case SOCK_RAW: |
case SOCK_RAW: |
458 |
|
address.protocol = CCS_NETWORK_RAW_PROTOCOL; |
459 |
break; |
break; |
460 |
default: |
default: |
461 |
return 0; |
return 0; |
462 |
} |
} |
463 |
switch (addr->sa_family) { |
address.operation = CCS_NETWORK_CONNECT; |
464 |
struct sockaddr_in6 *addr6; |
if (!ccs_check_address(addr, addr_len, &address)) |
465 |
struct sockaddr_in *addr4; |
goto skip; |
466 |
u16 port; |
if (type == SOCK_RAW) |
467 |
case AF_INET6: |
address.port = htons(sock->sk->sk_protocol); |
468 |
if (addr_len < SIN6_LEN_RFC2133) |
error = ccs_network_entry(&address); |
469 |
break; |
skip: |
|
addr6 = (struct sockaddr_in6 *) addr; |
|
|
if (type != SOCK_RAW) |
|
|
port = addr6->sin6_port; |
|
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_connect_acl(true, type, |
|
|
addr6->sin6_addr.s6_addr, |
|
|
port); |
|
|
break; |
|
|
case AF_INET: |
|
|
if (addr_len < sizeof(struct sockaddr_in)) |
|
|
break; |
|
|
addr4 = (struct sockaddr_in *) addr; |
|
|
if (type != SOCK_RAW) |
|
|
port = addr4->sin_port; |
|
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_connect_acl(false, type, |
|
|
(u8 *) &addr4->sin_addr, |
|
|
port); |
|
|
break; |
|
|
} |
|
|
if (type != SOCK_STREAM) |
|
|
return error; |
|
|
switch (sock->sk->sk_family) { |
|
|
case PF_INET: |
|
|
case PF_INET6: |
|
|
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CONNECT)) |
|
|
error = -EPERM; |
|
|
break; |
|
|
} |
|
470 |
return error; |
return error; |
471 |
} |
} |
472 |
|
|
473 |
/* 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. */ |
474 |
int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_bind_permission(struct socket *sock, |
475 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
476 |
{ |
{ |
477 |
int error = 0; |
int error = 0; |
478 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
479 |
|
struct ccs_addr_info address; |
480 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
481 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
482 |
return 0; |
return 0; |
483 |
switch (type) { |
switch (type) { |
484 |
case SOCK_STREAM: |
case SOCK_STREAM: |
485 |
|
address.protocol = CCS_NETWORK_TCP_PROTOCOL; |
486 |
|
break; |
487 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
488 |
|
address.protocol = CCS_NETWORK_UDP_PROTOCOL; |
489 |
|
break; |
490 |
case SOCK_RAW: |
case SOCK_RAW: |
491 |
|
address.protocol = CCS_NETWORK_RAW_PROTOCOL; |
492 |
break; |
break; |
493 |
default: |
default: |
494 |
return 0; |
return 0; |
495 |
} |
} |
496 |
switch (addr->sa_family) { |
address.operation = CCS_NETWORK_BIND; |
497 |
struct sockaddr_in6 *addr6; |
if (!ccs_check_address(addr, addr_len, &address)) |
498 |
struct sockaddr_in *addr4; |
goto skip; |
499 |
u16 port; |
if (type == SOCK_RAW) |
500 |
case AF_INET6: |
address.port = htons(sock->sk->sk_protocol); |
501 |
if (addr_len < SIN6_LEN_RFC2133) |
error = ccs_network_entry(&address); |
502 |
break; |
skip: |
|
addr6 = (struct sockaddr_in6 *) addr; |
|
|
if (type != SOCK_RAW) |
|
|
port = addr6->sin6_port; |
|
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_bind_acl(true, type, |
|
|
addr6->sin6_addr.s6_addr, |
|
|
port); |
|
|
break; |
|
|
case AF_INET: |
|
|
if (addr_len < sizeof(struct sockaddr_in)) |
|
|
break; |
|
|
addr4 = (struct sockaddr_in *) addr; |
|
|
if (type != SOCK_RAW) |
|
|
port = addr4->sin_port; |
|
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_bind_acl(false, type, |
|
|
(u8 *) &addr4->sin_addr, |
|
|
port); |
|
|
break; |
|
|
} |
|
503 |
return error; |
return error; |
504 |
} |
} |
505 |
|
|
506 |
/* |
/* Check permission for accepting a TCP socket. */ |
507 |
* Check permission for accepting a TCP socket. |
static int __ccs_socket_post_accept_permission(struct socket *sock, |
508 |
* |
struct socket *newsock) |
|
* Currently, the LSM hook for this purpose is not provided. |
|
|
*/ |
|
|
int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr) |
|
509 |
{ |
{ |
510 |
|
struct sockaddr_storage addr; |
511 |
|
struct task_struct * const task = current; |
512 |
int error = 0; |
int error = 0; |
513 |
int addr_len; |
int addr_len; |
514 |
|
struct ccs_addr_info address; |
515 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
516 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
517 |
return 0; |
return 0; |
518 |
switch (sock->sk->sk_family) { |
switch (newsock->sk->sk_family) { |
519 |
case PF_INET: |
case PF_INET: |
520 |
case PF_INET6: |
case PF_INET6: |
521 |
break; |
break; |
522 |
default: |
default: |
523 |
return 0; |
return 0; |
524 |
} |
} |
525 |
error = sock->ops->getname(sock, addr, &addr_len, 2); |
error = newsock->ops->getname(newsock, (struct sockaddr *) &addr, |
526 |
|
&addr_len, 2); |
527 |
if (error) |
if (error) |
528 |
return error; |
return error; |
529 |
switch (addr->sa_family) { |
if (!ccs_check_address((struct sockaddr *) &addr, addr_len, |
530 |
struct sockaddr_in6 *addr6; |
&address)) |
531 |
struct sockaddr_in *addr4; |
goto skip; |
532 |
case AF_INET6: |
address.protocol = CCS_NETWORK_TCP_PROTOCOL; |
533 |
addr6 = (struct sockaddr_in6 *) addr; |
address.operation = CCS_NETWORK_ACCEPT; |
534 |
error = ccs_check_network_accept_acl(true, |
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
535 |
addr6->sin6_addr.s6_addr, |
error = ccs_network_entry(&address); |
536 |
addr6->sin6_port); |
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
537 |
break; |
skip: |
|
case AF_INET: |
|
|
addr4 = (struct sockaddr_in *) addr; |
|
|
error = ccs_check_network_accept_acl(false, |
|
|
(u8 *) &addr4->sin_addr, |
|
|
addr4->sin_port); |
|
|
break; |
|
|
} |
|
538 |
return error; |
return error; |
539 |
} |
} |
540 |
|
|
541 |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
542 |
int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_sendmsg_permission(struct socket *sock, |
543 |
int addr_len) |
struct msghdr *msg, int size) |
544 |
{ |
{ |
545 |
int error = 0; |
int error = 0; |
546 |
const int type = sock->type; |
const int type = sock->type; |
547 |
|
struct ccs_addr_info address; |
548 |
|
if (!msg->msg_name) |
549 |
|
return 0; |
550 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
551 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
552 |
return 0; |
return 0; |
553 |
if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW)) |
switch (type) { |
554 |
return 0; |
case SOCK_DGRAM: |
555 |
switch (addr->sa_family) { |
address.protocol = CCS_NETWORK_UDP_PROTOCOL; |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
|
u16 port; |
|
|
case AF_INET6: |
|
|
if (addr_len < SIN6_LEN_RFC2133) |
|
|
break; |
|
|
addr6 = (struct sockaddr_in6 *) addr; |
|
|
if (type == SOCK_DGRAM) |
|
|
port = addr6->sin6_port; |
|
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_sendmsg_acl(true, type, |
|
|
addr6->sin6_addr.s6_addr, |
|
|
port); |
|
556 |
break; |
break; |
557 |
case AF_INET: |
case SOCK_RAW: |
558 |
if (addr_len < sizeof(struct sockaddr_in)) |
address.protocol = CCS_NETWORK_RAW_PROTOCOL; |
|
break; |
|
|
addr4 = (struct sockaddr_in *) addr; |
|
|
if (type == SOCK_DGRAM) |
|
|
port = addr4->sin_port; |
|
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_sendmsg_acl(false, type, |
|
|
(u8 *) &addr4->sin_addr, |
|
|
port); |
|
559 |
break; |
break; |
560 |
|
default: |
561 |
|
return 0; |
562 |
} |
} |
563 |
|
address.operation = CCS_NETWORK_SEND; |
564 |
|
if (!ccs_check_address((struct sockaddr *) msg->msg_name, |
565 |
|
msg->msg_namelen, &address)) |
566 |
|
goto skip; |
567 |
|
if (type == SOCK_RAW) |
568 |
|
address.port = htons(sock->sk->sk_protocol); |
569 |
|
error = ccs_network_entry(&address); |
570 |
|
skip: |
571 |
return error; |
return error; |
572 |
} |
} |
573 |
|
|
574 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
575 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
576 |
|
#if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2 |
577 |
|
|
578 |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
579 |
{ |
{ |
592 |
|
|
593 |
#endif |
#endif |
594 |
#endif |
#endif |
595 |
|
#endif |
596 |
|
|
597 |
/* |
/* Check permission for receiving a datagram via a UDP or RAW socket. */ |
598 |
* Check permission for receiving a datagram via a UDP or RAW socket. |
static int __ccs_socket_post_recvmsg_permission(struct sock *sk, |
599 |
* |
struct sk_buff *skb) |
|
* Currently, the LSM hook for this purpose is not provided. |
|
|
*/ |
|
|
int ccs_socket_recv_datagram_permission(struct sock *sk, struct sk_buff *skb, |
|
|
const unsigned int flags) |
|
600 |
{ |
{ |
601 |
|
struct task_struct * const task = current; |
602 |
int error = 0; |
int error = 0; |
603 |
const unsigned int type = sk->sk_type; |
const unsigned int type = sk->sk_type; |
604 |
/* Nothing to do if I didn't receive a datagram. */ |
struct ccs_addr_info address; |
605 |
if (!skb) |
union { |
606 |
return 0; |
struct in6_addr sin6; |
607 |
/* Nothing to do if I can't sleep. */ |
struct in_addr sin4; |
608 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
} ip_address; |
609 |
if (in_interrupt()) |
switch (type) { |
610 |
return 0; |
case SOCK_DGRAM: |
611 |
#else |
address.protocol = CCS_NETWORK_UDP_PROTOCOL; |
612 |
if (in_atomic()) |
break; |
613 |
|
case SOCK_RAW: |
614 |
|
address.protocol = CCS_NETWORK_RAW_PROTOCOL; |
615 |
|
break; |
616 |
|
default: |
617 |
return 0; |
return 0; |
618 |
#endif |
} |
619 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
620 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
621 |
return 0; |
return 0; |
|
if (type != SOCK_DGRAM && type != SOCK_RAW) |
|
|
return 0; |
|
|
|
|
622 |
switch (sk->sk_family) { |
switch (sk->sk_family) { |
|
struct in6_addr sin6; |
|
|
struct in_addr sin4; |
|
|
u16 port; |
|
623 |
case PF_INET6: |
case PF_INET6: |
624 |
if (type == SOCK_DGRAM) { /* UDP IPv6 */ |
address.is_ipv6 = true; |
625 |
if (skb->protocol == htons(ETH_P_IP)) { |
if (type == SOCK_DGRAM && skb->protocol == htons(ETH_P_IP)) |
626 |
ipv6_addr_set(&sin6, 0, 0, htonl(0xffff), |
ipv6_addr_set(&ip_address.sin6, 0, 0, htonl(0xffff), |
627 |
ip_hdr(skb)->saddr); |
ip_hdr(skb)->saddr); |
628 |
} else { |
else |
629 |
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
ipv6_addr_copy(&ip_address.sin6, &ipv6_hdr(skb)->saddr); |
|
} |
|
|
port = udp_hdr(skb)->source; |
|
|
} else { /* RAW IPv6 */ |
|
|
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
|
|
port = htons(sk->sk_protocol); |
|
|
} |
|
|
error = ccs_check_network_recvmsg_acl(true, type, |
|
|
(u8 *) &sin6, port); |
|
630 |
break; |
break; |
631 |
case PF_INET: |
case PF_INET: |
632 |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
address.is_ipv6 = false; |
633 |
sin4.s_addr = ip_hdr(skb)->saddr; |
ip_address.sin4.s_addr = ip_hdr(skb)->saddr; |
|
port = udp_hdr(skb)->source; |
|
|
} else { /* RAW IPv4 */ |
|
|
sin4.s_addr = ip_hdr(skb)->saddr; |
|
|
port = htons(sk->sk_protocol); |
|
|
} |
|
|
error = ccs_check_network_recvmsg_acl(false, type, |
|
|
(u8 *) &sin4, port); |
|
634 |
break; |
break; |
635 |
|
default: |
636 |
|
goto skip; |
637 |
} |
} |
638 |
if (!error) |
address.address = (u32 *) &ip_address; |
639 |
return 0; |
if (type == SOCK_DGRAM) |
640 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
address.port = udp_hdr(skb)->source; |
641 |
lock_sock(sk); |
else |
642 |
#endif |
address.port = htons(sk->sk_protocol); |
643 |
/* |
address.operation = CCS_NETWORK_RECV; |
644 |
* Remove from queue if MSG_PEEK is used so that |
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
645 |
* the head message from unwanted source in receive queue will not |
error = ccs_network_entry(&address); |
646 |
* prevent the caller from picking up next message from wanted source |
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
647 |
* when the caller is using MSG_PEEK flag for picking up. |
skip: |
648 |
*/ |
return error; |
|
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); |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
|
|
release_sock(sk); |
|
|
#endif |
|
|
/* Hope less harmful than -EPERM. */ |
|
|
return -EAGAIN; |
|
649 |
} |
} |
650 |
|
|
651 |
|
void __init ccs_network_init(void) |
652 |
|
{ |
653 |
|
ccsecurity_ops.socket_create_permission = |
654 |
|
__ccs_socket_create_permission; |
655 |
|
ccsecurity_ops.socket_listen_permission = |
656 |
|
__ccs_socket_listen_permission; |
657 |
|
ccsecurity_ops.socket_connect_permission = |
658 |
|
__ccs_socket_connect_permission; |
659 |
|
ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission; |
660 |
|
ccsecurity_ops.socket_post_accept_permission = |
661 |
|
__ccs_socket_post_accept_permission; |
662 |
|
ccsecurity_ops.socket_sendmsg_permission = |
663 |
|
__ccs_socket_sendmsg_permission; |
664 |
|
ccsecurity_ops.socket_post_recvmsg_permission = |
665 |
|
__ccs_socket_post_recvmsg_permission; |
666 |
|
} |
667 |
|
|
668 |
|
#endif |