1 |
/* |
/* |
2 |
* fs/tomoyo_network.c |
* security/ccsecurity/network.c |
3 |
* |
* |
4 |
* Implementation of the Domain-Based Mandatory Access Control. |
* Copyright (C) 2005-2010 NTT DATA CORPORATION |
5 |
* |
* |
6 |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
* Version: 1.7.2+ 2010/07/21 |
|
* |
|
|
* 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. */ |
const char *ccs_net_keyword[CCS_MAX_NETWORK_OPERATION] = { |
23 |
enum ccs_network_acl_index { |
[CCS_NETWORK_UDP_BIND] = "UDP bind", |
24 |
NETWORK_ACL_UDP_BIND, |
[CCS_NETWORK_UDP_CONNECT] = "UDP connect", |
25 |
NETWORK_ACL_UDP_CONNECT, |
[CCS_NETWORK_TCP_BIND] = "TCP bind", |
26 |
NETWORK_ACL_TCP_BIND, |
[CCS_NETWORK_TCP_LISTEN] = "TCP listen", |
27 |
NETWORK_ACL_TCP_LISTEN, |
[CCS_NETWORK_TCP_CONNECT] = "TCP connect", |
28 |
NETWORK_ACL_TCP_CONNECT, |
[CCS_NETWORK_TCP_ACCEPT] = "TCP accept", |
29 |
NETWORK_ACL_TCP_ACCEPT, |
[CCS_NETWORK_RAW_BIND] = "RAW bind", |
30 |
NETWORK_ACL_RAW_BIND, |
[CCS_NETWORK_RAW_CONNECT] = "RAW connect" |
|
NETWORK_ACL_RAW_CONNECT |
|
31 |
}; |
}; |
32 |
|
|
33 |
/** |
/** |
34 |
* ccs_audit_network_log - Audit network log. |
* ccs_audit_network_log - Audit network log. |
35 |
* |
* |
36 |
* @r: Pointer to "struct ccs_request_info". |
* @r: Pointer to "struct ccs_request_info". |
|
* @operation: The name of operation. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* @is_granted: True if this is a granted log. |
|
37 |
* |
* |
38 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
39 |
*/ |
*/ |
40 |
static int ccs_audit_network_log(struct ccs_request_info *r, |
static int ccs_audit_network_log(struct ccs_request_info *r) |
|
const char *operation, const char *address, |
|
|
const u16 port, const bool is_granted) |
|
|
{ |
|
|
return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK |
|
|
"%s %s %u\n", operation, address, port); |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_save_ipv6_address - Keep the given IPv6 address on the RAM. |
|
|
* |
|
|
* @addr: Pointer to "struct in6_addr". |
|
|
* |
|
|
* Returns pointer to "struct in6_addr" on success, NULL otherwise. |
|
|
* |
|
|
* The RAM is shared, so NEVER try to modify or kfree() the returned address. |
|
|
*/ |
|
|
static const struct in6_addr *ccs_save_ipv6_address(const struct in6_addr *addr) |
|
41 |
{ |
{ |
42 |
static const u8 ccs_block_size = 16; |
char buf[128]; |
43 |
struct ccs_addr_list { |
const char *operation = ccs_net_keyword[r->param.network.operation]; |
44 |
/* Workaround for gcc 4.3's bug. */ |
const u32 *address = r->param.network.address; |
45 |
struct in6_addr addr[16]; /* = ccs_block_size */ |
const u16 port = r->param.network.port; |
46 |
struct list1_head list; |
if (r->param.network.is_ipv6) |
47 |
u32 in_use_count; |
ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *) |
48 |
}; |
address, (const struct in6_addr *) address); |
49 |
static LIST1_HEAD(ccs_address_list); |
else |
50 |
struct ccs_addr_list *ptr; |
ccs_print_ipv4(buf, sizeof(buf), r->param.network.ip, |
51 |
static DEFINE_MUTEX(lock); |
r->param.network.ip); |
52 |
u8 i = ccs_block_size; |
ccs_write_log(r, "network %s %s %u\n", operation, buf, port); |
53 |
if (!addr) |
if (r->granted) |
54 |
return NULL; |
return 0; |
55 |
mutex_lock(&lock); |
ccs_warn_log(r, "network %s %s %u", operation, buf, port); |
56 |
list1_for_each_entry(ptr, &ccs_address_list, list) { |
return ccs_supervisor(r, "network %s %s %u\n", operation, buf, port); |
|
for (i = 0; i < ptr->in_use_count; i++) { |
|
|
if (!memcmp(&ptr->addr[i], addr, sizeof(*addr))) |
|
|
goto ok; |
|
|
} |
|
|
if (i < ccs_block_size) |
|
|
break; |
|
|
} |
|
|
if (i == ccs_block_size) { |
|
|
ptr = ccs_alloc_element(sizeof(*ptr)); |
|
|
if (!ptr) |
|
|
goto ok; |
|
|
list1_add_tail_mb(&ptr->list, &ccs_address_list); |
|
|
i = 0; |
|
|
} |
|
|
ptr->addr[ptr->in_use_count++] = *addr; |
|
|
ok: |
|
|
mutex_unlock(&lock); |
|
|
return ptr ? &ptr->addr[i] : NULL; |
|
|
} |
|
|
|
|
|
/* The list for "struct ccs_address_group_entry". */ |
|
|
static LIST1_HEAD(ccs_address_group_list); |
|
|
|
|
|
/** |
|
|
* ccs_update_address_group_entry - Update "struct ccs_address_group_entry" list. |
|
|
* |
|
|
* @group_name: The name of address group. |
|
|
* @is_ipv6: True if @min_address and @max_address are IPv6 addresses. |
|
|
* @min_address: Start of IPv4 or IPv6 address range. |
|
|
* @max_address: End of IPv4 or IPv6 address range. |
|
|
* @is_delete: True if it is a delete request. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int ccs_update_address_group_entry(const char *group_name, |
|
|
const bool is_ipv6, |
|
|
const u16 *min_address, |
|
|
const u16 *max_address, |
|
|
const bool is_delete) |
|
|
{ |
|
|
static DEFINE_MUTEX(lock); |
|
|
struct ccs_address_group_entry *new_group; |
|
|
struct ccs_address_group_entry *group; |
|
|
struct ccs_address_group_member *new_member; |
|
|
struct ccs_address_group_member *member; |
|
|
const struct ccs_path_info *saved_group_name; |
|
|
const struct in6_addr *saved_min_address = NULL; |
|
|
const struct in6_addr *saved_max_address = NULL; |
|
|
int error = -ENOMEM; |
|
|
bool found = false; |
|
|
if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) || |
|
|
!group_name[0]) |
|
|
return -EINVAL; |
|
|
saved_group_name = ccs_save_name(group_name); |
|
|
if (!saved_group_name) |
|
|
return -ENOMEM; |
|
|
if (!is_ipv6) |
|
|
goto not_ipv6; |
|
|
saved_min_address |
|
|
= ccs_save_ipv6_address((struct in6_addr *) min_address); |
|
|
saved_max_address |
|
|
= ccs_save_ipv6_address((struct in6_addr *) max_address); |
|
|
if (!saved_min_address || !saved_max_address) |
|
|
return -ENOMEM; |
|
|
not_ipv6: |
|
|
mutex_lock(&lock); |
|
|
list1_for_each_entry(group, &ccs_address_group_list, list) { |
|
|
if (saved_group_name != group->group_name) |
|
|
continue; |
|
|
list1_for_each_entry(member, |
|
|
&group->address_group_member_list, |
|
|
list) { |
|
|
if (member->is_ipv6 != is_ipv6) |
|
|
continue; |
|
|
if (is_ipv6) { |
|
|
if (member->min.ipv6 != saved_min_address || |
|
|
member->max.ipv6 != saved_max_address) |
|
|
continue; |
|
|
} else { |
|
|
if (member->min.ipv4 != *(u32 *) min_address || |
|
|
member->max.ipv4 != *(u32 *) max_address) |
|
|
continue; |
|
|
} |
|
|
member->is_deleted = is_delete; |
|
|
error = 0; |
|
|
goto out; |
|
|
} |
|
|
found = true; |
|
|
break; |
|
|
} |
|
|
if (is_delete) { |
|
|
error = -ENOENT; |
|
|
goto out; |
|
|
} |
|
|
if (!found) { |
|
|
new_group = ccs_alloc_element(sizeof(*new_group)); |
|
|
if (!new_group) |
|
|
goto out; |
|
|
INIT_LIST1_HEAD(&new_group->address_group_member_list); |
|
|
new_group->group_name = saved_group_name; |
|
|
list1_add_tail_mb(&new_group->list, &ccs_address_group_list); |
|
|
group = new_group; |
|
|
} |
|
|
new_member = ccs_alloc_element(sizeof(*new_member)); |
|
|
if (!new_member) |
|
|
goto out; |
|
|
new_member->is_ipv6 = is_ipv6; |
|
|
if (is_ipv6) { |
|
|
new_member->min.ipv6 = saved_min_address; |
|
|
new_member->max.ipv6 = saved_max_address; |
|
|
} else { |
|
|
new_member->min.ipv4 = *(u32 *) min_address; |
|
|
new_member->max.ipv4 = *(u32 *) max_address; |
|
|
} |
|
|
list1_add_tail_mb(&new_member->list, |
|
|
&group->address_group_member_list); |
|
|
error = 0; |
|
|
out: |
|
|
mutex_unlock(&lock); |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
|
|
return error; |
|
57 |
} |
} |
58 |
|
|
59 |
/** |
/** |
63 |
* @min: Pointer to store min address. |
* @min: Pointer to store min address. |
64 |
* @max: Pointer to store max address. |
* @max: Pointer to store max address. |
65 |
* |
* |
66 |
* 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, |
67 |
|
* CCS_IP_ADDRESS_TYPE_IPv4 if @address is an IPv4, |
68 |
|
* CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP otherwise. |
69 |
*/ |
*/ |
70 |
static int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
71 |
{ |
{ |
72 |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
73 |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
83 |
min[i] = htons(min[i]); |
min[i] = htons(min[i]); |
84 |
max[i] = htons(max[i]); |
max[i] = htons(max[i]); |
85 |
} |
} |
86 |
return 2; |
return CCS_IP_ADDRESS_TYPE_IPv6; |
87 |
} |
} |
88 |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
89 |
&min[0], &min[1], &min[2], &min[3], |
&min[0], &min[1], &min[2], &min[3], |
93 |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
94 |
memmove(min, &ip, sizeof(ip)); |
memmove(min, &ip, sizeof(ip)); |
95 |
if (count == 8) |
if (count == 8) |
96 |
ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16) |
ip = htonl((((u8) max[0]) << 24) |
97 |
|
+ (((u8) max[1]) << 16) |
98 |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
99 |
memmove(max, &ip, sizeof(ip)); |
memmove(max, &ip, sizeof(ip)); |
100 |
return 1; |
return CCS_IP_ADDRESS_TYPE_IPv4; |
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_write_address_group_policy - Write "struct ccs_address_group_entry" list. |
|
|
* |
|
|
* @data: String to parse. |
|
|
* @is_delete: True if it is a delete request. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
int ccs_write_address_group_policy(char *data, const bool is_delete) |
|
|
{ |
|
|
bool is_ipv6; |
|
|
u16 min_address[8]; |
|
|
u16 max_address[8]; |
|
|
char *cp = strchr(data, ' '); |
|
|
if (!cp) |
|
|
return -EINVAL; |
|
|
*cp++ = '\0'; |
|
|
switch (ccs_parse_ip_address(cp, min_address, max_address)) { |
|
|
case 2: |
|
|
is_ipv6 = true; |
|
|
break; |
|
|
case 1: |
|
|
is_ipv6 = false; |
|
|
break; |
|
|
default: |
|
|
return -EINVAL; |
|
|
} |
|
|
return ccs_update_address_group_entry(data, is_ipv6, min_address, |
|
|
max_address, is_delete); |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_find_or_assign_new_address_group - Create address group. |
|
|
* |
|
|
* @group_name: The name of address group. |
|
|
* |
|
|
* Returns pointer to "struct ccs_address_group_entry" on success, |
|
|
* NULL otherwise. |
|
|
*/ |
|
|
static struct ccs_address_group_entry * |
|
|
ccs_find_or_assign_new_address_group(const char *group_name) |
|
|
{ |
|
|
u8 i; |
|
|
struct ccs_address_group_entry *group; |
|
|
for (i = 0; i <= 1; i++) { |
|
|
list1_for_each_entry(group, &ccs_address_group_list, list) { |
|
|
if (!strcmp(group_name, group->group_name->name)) |
|
|
return group; |
|
|
} |
|
|
if (!i) { |
|
|
const u16 dummy[2] = { 0, 0 }; |
|
|
ccs_update_address_group_entry(group_name, false, |
|
|
dummy, dummy, false); |
|
|
ccs_update_address_group_entry(group_name, false, |
|
|
dummy, dummy, true); |
|
|
} |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_address_matches_group - Check whether the given address matches members of the given address group. |
|
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @group: Pointer to "struct ccs_address_group_entry". |
|
|
* |
|
|
* Returns true if @address matches addresses in @group group, false otherwise. |
|
|
*/ |
|
|
static bool ccs_address_matches_group(const bool is_ipv6, const u32 *address, |
|
|
const struct ccs_address_group_entry * |
|
|
group) |
|
|
{ |
|
|
struct ccs_address_group_member *member; |
|
|
const u32 ip = ntohl(*address); |
|
|
list1_for_each_entry(member, &group->address_group_member_list, |
|
|
list) { |
|
|
if (member->is_deleted) |
|
|
continue; |
|
|
if (member->is_ipv6) { |
|
|
if (is_ipv6 && |
|
|
memcmp(member->min.ipv6, address, 16) <= 0 && |
|
|
memcmp(address, member->max.ipv6, 16) <= 0) |
|
|
return true; |
|
|
} else { |
|
|
if (!is_ipv6 && |
|
|
member->min.ipv4 <= ip && ip <= member->max.ipv4) |
|
|
return true; |
|
|
} |
|
101 |
} |
} |
102 |
return false; |
return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
103 |
} |
} |
104 |
|
|
105 |
/** |
/** |
106 |
* ccs_read_address_group_policy - Read "struct ccs_address_group_entry" list. |
* ccs_print_ipv4 - Print an IPv4 address. |
107 |
* |
* |
108 |
* @head: Pointer to "struct ccs_io_buffer". |
* @buffer: Buffer to write to. |
109 |
|
* @buffer_len: Size of @buffer. |
110 |
|
* @min_ip: Min address in host byte order. |
111 |
|
* @max_ip: Max address in host byte order. |
112 |
* |
* |
113 |
* Returns true on success, false otherwise. |
* Returns nothing. |
114 |
*/ |
*/ |
115 |
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
void ccs_print_ipv4(char *buffer, const int buffer_len, |
116 |
|
const u32 min_ip, const u32 max_ip) |
117 |
{ |
{ |
118 |
struct list1_head *gpos; |
memset(buffer, 0, buffer_len); |
119 |
struct list1_head *mpos; |
snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u", |
120 |
list1_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) { |
HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-', |
121 |
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; |
|
122 |
} |
} |
123 |
|
|
124 |
#if !defined(NIP6) |
#if !defined(NIP6) |
125 |
#define NIP6(addr) \ |
#define NIP6(addr) \ |
126 |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
127 |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
128 |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
129 |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
130 |
#endif |
#endif |
131 |
|
|
132 |
/** |
/** |
134 |
* |
* |
135 |
* @buffer: Buffer to write to. |
* @buffer: Buffer to write to. |
136 |
* @buffer_len: Size of @buffer. |
* @buffer_len: Size of @buffer. |
137 |
* @ip: Pointer to "struct in6_addr". |
* @min_ip: Pointer to "struct in6_addr". |
138 |
|
* @max_ip: Pointer to "struct in6_addr". |
139 |
* |
* |
140 |
* Returns nothing. |
* Returns nothing. |
141 |
*/ |
*/ |
142 |
void ccs_print_ipv6(char *buffer, const int buffer_len, |
void ccs_print_ipv6(char *buffer, const int buffer_len, |
143 |
const struct in6_addr *ip) |
const struct in6_addr *min_ip, |
144 |
|
const struct in6_addr *max_ip) |
145 |
{ |
{ |
146 |
memset(buffer, 0, buffer_len); |
memset(buffer, 0, buffer_len); |
147 |
snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip)); |
snprintf(buffer, buffer_len - 1, |
148 |
} |
"%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x", |
149 |
|
NIP6(*min_ip), min_ip == max_ip ? '\0' : '-', |
150 |
/** |
NIP6(*max_ip)); |
151 |
* ccs_net2keyword - Convert network operation index to network operation name. |
} |
152 |
* |
|
153 |
* @operation: Type of operation. |
static bool ccs_check_network_acl(const struct ccs_request_info *r, |
154 |
* |
const struct ccs_acl_info *ptr) |
155 |
* Returns the name of operation. |
{ |
156 |
*/ |
const struct ccs_ip_network_acl *acl = |
157 |
const char *ccs_net2keyword(const u8 operation) |
container_of(ptr, typeof(*acl), head); |
158 |
{ |
bool ret; |
159 |
const char *keyword = "unknown"; |
if (!(acl->perm & (1 << r->param.network.operation)) || |
160 |
switch (operation) { |
!ccs_compare_number_union(r->param.network.port, &acl->port)) |
161 |
case NETWORK_ACL_UDP_BIND: |
return false; |
162 |
keyword = "UDP bind"; |
switch (acl->address_type) { |
163 |
break; |
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
164 |
case NETWORK_ACL_UDP_CONNECT: |
ret = ccs_address_matches_group(r->param.network.is_ipv6, |
165 |
keyword = "UDP connect"; |
r->param.network.address, |
166 |
break; |
acl->address.group); |
167 |
case NETWORK_ACL_TCP_BIND: |
break; |
168 |
keyword = "TCP bind"; |
case CCS_IP_ADDRESS_TYPE_IPv4: |
169 |
break; |
ret = !r->param.network.is_ipv6 && |
170 |
case NETWORK_ACL_TCP_LISTEN: |
acl->address.ipv4.min <= r->param.network.ip && |
171 |
keyword = "TCP listen"; |
r->param.network.ip <= acl->address.ipv4.max; |
172 |
break; |
break; |
173 |
case NETWORK_ACL_TCP_CONNECT: |
default: |
174 |
keyword = "TCP connect"; |
ret = r->param.network.is_ipv6 && |
175 |
break; |
memcmp(acl->address.ipv6.min, r->param.network.address, |
176 |
case NETWORK_ACL_TCP_ACCEPT: |
16) <= 0 && |
177 |
keyword = "TCP accept"; |
memcmp(r->param.network.address, acl->address.ipv6.max, |
178 |
break; |
16) <= 0; |
|
case NETWORK_ACL_RAW_BIND: |
|
|
keyword = "RAW bind"; |
|
|
break; |
|
|
case NETWORK_ACL_RAW_CONNECT: |
|
|
keyword = "RAW connect"; |
|
|
break; |
|
|
} |
|
|
return keyword; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_update_network_entry - Update "struct ccs_ip_network_acl_record" list. |
|
|
* |
|
|
* @operation: Type of operation. |
|
|
* @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. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int ccs_update_network_entry(const u8 operation, const u8 record_type, |
|
|
const struct ccs_address_group_entry *group, |
|
|
const u32 *min_address, |
|
|
const u32 *max_address, |
|
|
const u16 min_port, const u16 max_port, |
|
|
struct domain_info *domain, |
|
|
const struct ccs_condition_list *condition, |
|
|
const bool is_delete) |
|
|
{ |
|
|
static DEFINE_MUTEX(lock); |
|
|
struct ccs_acl_info *ptr; |
|
|
struct ccs_ip_network_acl_record *acl; |
|
|
int error = -ENOMEM; |
|
|
/* using host byte order to allow u32 comparison than memcmp().*/ |
|
|
const u32 min_ip = ntohl(*min_address); |
|
|
const u32 max_ip = ntohl(*max_address); |
|
|
const struct in6_addr *saved_min_address = NULL; |
|
|
const struct in6_addr *saved_max_address = NULL; |
|
|
if (!domain) |
|
|
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); |
|
179 |
break; |
break; |
180 |
} |
} |
181 |
out: |
return ret; |
|
mutex_unlock(&lock); |
|
|
return error; |
|
182 |
} |
} |
183 |
|
|
184 |
/** |
/** |
185 |
* ccs_check_network_entry - Check permission for network operation. |
* ccs_network_entry - Check permission for network operation. |
186 |
* |
* |
187 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
188 |
* @operation: Type of operation. |
* @operation: Type of operation. |
189 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address in network byte order. |
190 |
* @port: Port number. |
* @port: Port number in network byte order. |
191 |
* |
* |
192 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
193 |
*/ |
*/ |
194 |
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, |
195 |
const u32 *address, const u16 port) |
const u32 *address, const u16 port) |
196 |
{ |
{ |
197 |
|
const int idx = ccs_read_lock(); |
198 |
struct ccs_request_info r; |
struct ccs_request_info r; |
199 |
struct ccs_acl_info *ptr; |
int error = 0; |
200 |
const char *keyword = ccs_net2keyword(operation); |
if (ccs_init_request_info(&r, CCS_MAC_NETWORK_UDP_BIND + operation) |
201 |
bool is_enforce; |
!= CCS_CONFIG_DISABLED) { |
202 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
r.param_type = CCS_TYPE_IP_NETWORK_ACL; |
203 |
const u32 ip = ntohl(*address); |
r.param.network.operation = operation; |
204 |
bool found = false; |
r.param.network.is_ipv6 = is_ipv6; |
205 |
char buf[64]; |
r.param.network.address = address; |
206 |
if (!ccs_can_sleep()) |
r.param.network.port = ntohs(port); |
207 |
return 0; |
/* use host byte order to allow u32 comparison than memcmp().*/ |
208 |
ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_NETWORK); |
r.param.network.ip = ntohl(*address); |
209 |
is_enforce = (r.mode == 3); |
do { |
210 |
if (!r.mode) |
ccs_check_acl(&r, ccs_check_network_acl); |
211 |
return 0; |
error = ccs_audit_network_log(&r); |
212 |
retry: |
} while (error == CCS_RETRY_REQUEST); |
|
list1_for_each_entry(ptr, &r.domain->acl_info_list, list) { |
|
|
struct ccs_ip_network_acl_record *acl; |
|
|
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (acl->operation_type != operation || port < acl->min_port || |
|
|
acl->max_port < port || !ccs_check_condition(&r, ptr)) |
|
|
continue; |
|
|
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
if (!ccs_address_matches_group(is_ipv6, address, |
|
|
acl->u.group)) |
|
|
continue; |
|
|
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
|
|
if (is_ipv6 || |
|
|
ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) |
|
|
continue; |
|
|
} else { |
|
|
if (!is_ipv6 || |
|
|
memcmp(acl->u.ipv6.min, address, 16) > 0 || |
|
|
memcmp(address, acl->u.ipv6.max, 16) > 0) |
|
|
continue; |
|
|
} |
|
|
r.cond = ccs_get_condition_part(ptr); |
|
|
found = true; |
|
|
break; |
|
213 |
} |
} |
214 |
memset(buf, 0, sizeof(buf)); |
ccs_read_unlock(idx); |
215 |
if (is_ipv6) |
return error; |
216 |
ccs_print_ipv6(buf, sizeof(buf), |
} |
217 |
(const struct in6_addr *) address); |
|
218 |
|
static bool ccs_same_ip_network_acl(const struct ccs_acl_info *a, |
219 |
|
const struct ccs_acl_info *b) |
220 |
|
{ |
221 |
|
const struct ccs_ip_network_acl *p1 = container_of(a, typeof(*p1), |
222 |
|
head); |
223 |
|
const struct ccs_ip_network_acl *p2 = container_of(b, typeof(*p2), |
224 |
|
head); |
225 |
|
return ccs_same_acl_head(&p1->head, &p2->head) |
226 |
|
&& p1->address_type == p2->address_type && |
227 |
|
p1->address.ipv4.min == p2->address.ipv4.min && |
228 |
|
p1->address.ipv6.min == p2->address.ipv6.min && |
229 |
|
p1->address.ipv4.max == p2->address.ipv4.max && |
230 |
|
p1->address.ipv6.max == p2->address.ipv6.max && |
231 |
|
p1->address.group == p2->address.group && |
232 |
|
ccs_same_number_union(&p1->port, &p2->port); |
233 |
|
} |
234 |
|
|
235 |
|
static bool ccs_merge_ip_network_acl(struct ccs_acl_info *a, |
236 |
|
struct ccs_acl_info *b, |
237 |
|
const bool is_delete) |
238 |
|
{ |
239 |
|
u8 * const a_perm = &container_of(a, struct ccs_ip_network_acl, head) |
240 |
|
->perm; |
241 |
|
u8 perm = *a_perm; |
242 |
|
const u8 b_perm = container_of(b, struct ccs_ip_network_acl, head) |
243 |
|
->perm; |
244 |
|
if (is_delete) |
245 |
|
perm &= ~b_perm; |
246 |
else |
else |
247 |
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
perm |= b_perm; |
248 |
ccs_audit_network_log(&r, keyword, buf, port, found); |
*a_perm = perm; |
249 |
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; |
|
250 |
} |
} |
251 |
|
|
252 |
/** |
/** |
253 |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list. |
* ccs_write_network - Write "struct ccs_ip_network_acl" list. |
254 |
* |
* |
255 |
* @data: String to parse. |
* @data: String to parse. |
256 |
* @domain: Pointer to "struct domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
257 |
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
* @condition: Pointer to "struct ccs_condition". Maybe NULL. |
258 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
259 |
* |
* |
260 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
261 |
*/ |
*/ |
262 |
int ccs_write_network_policy(char *data, struct domain_info *domain, |
int ccs_write_network(char *data, struct ccs_domain_info *domain, |
263 |
const struct ccs_condition_list *condition, |
struct ccs_condition *condition, const bool is_delete) |
|
const bool is_delete) |
|
264 |
{ |
{ |
265 |
u8 sock_type; |
struct ccs_ip_network_acl e = { |
266 |
u8 operation; |
.head.type = CCS_TYPE_IP_NETWORK_ACL, |
267 |
u8 record_type; |
.head.cond = condition, |
268 |
|
}; |
269 |
u16 min_address[8]; |
u16 min_address[8]; |
270 |
u16 max_address[8]; |
u16 max_address[8]; |
271 |
struct ccs_address_group_entry *group = NULL; |
int error = is_delete ? -ENOENT : -ENOMEM; |
272 |
u16 min_port; |
u8 sock_type; |
273 |
u16 max_port; |
u8 operation; |
274 |
u8 count; |
char *w[4]; |
275 |
char *cp1 = strchr(data, ' '); |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
276 |
char *cp2; |
return -EINVAL; |
277 |
if (!cp1) |
if (!strcmp(w[0], "TCP")) |
|
goto out; |
|
|
cp1++; |
|
|
if (!strncmp(data, "TCP ", 4)) |
|
278 |
sock_type = SOCK_STREAM; |
sock_type = SOCK_STREAM; |
279 |
else if (!strncmp(data, "UDP ", 4)) |
else if (!strcmp(w[0], "UDP")) |
280 |
sock_type = SOCK_DGRAM; |
sock_type = SOCK_DGRAM; |
281 |
else if (!strncmp(data, "RAW ", 4)) |
else if (!strcmp(w[0], "RAW")) |
282 |
sock_type = SOCK_RAW; |
sock_type = SOCK_RAW; |
283 |
else |
else |
284 |
goto out; |
return -EINVAL; |
285 |
cp2 = strchr(cp1, ' '); |
if (!strcmp(w[1], "bind")) |
|
if (!cp2) |
|
|
goto out; |
|
|
cp2++; |
|
|
if (!strncmp(cp1, "bind ", 5)) |
|
286 |
switch (sock_type) { |
switch (sock_type) { |
287 |
case SOCK_STREAM: |
case SOCK_STREAM: |
288 |
operation = NETWORK_ACL_TCP_BIND; |
operation = CCS_NETWORK_TCP_BIND; |
289 |
break; |
break; |
290 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
291 |
operation = NETWORK_ACL_UDP_BIND; |
operation = CCS_NETWORK_UDP_BIND; |
292 |
break; |
break; |
293 |
default: |
default: |
294 |
operation = NETWORK_ACL_RAW_BIND; |
operation = CCS_NETWORK_RAW_BIND; |
295 |
|
break; |
296 |
} |
} |
297 |
else if (!strncmp(cp1, "connect ", 8)) |
else if (!strcmp(w[1], "connect")) |
298 |
switch (sock_type) { |
switch (sock_type) { |
299 |
case SOCK_STREAM: |
case SOCK_STREAM: |
300 |
operation = NETWORK_ACL_TCP_CONNECT; |
operation = CCS_NETWORK_TCP_CONNECT; |
301 |
break; |
break; |
302 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
303 |
operation = NETWORK_ACL_UDP_CONNECT; |
operation = CCS_NETWORK_UDP_CONNECT; |
304 |
break; |
break; |
305 |
default: |
default: |
306 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = CCS_NETWORK_RAW_CONNECT; |
307 |
|
break; |
308 |
} |
} |
309 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen")) |
310 |
operation = NETWORK_ACL_TCP_LISTEN; |
operation = CCS_NETWORK_TCP_LISTEN; |
311 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept")) |
312 |
operation = NETWORK_ACL_TCP_ACCEPT; |
operation = CCS_NETWORK_TCP_ACCEPT; |
313 |
else |
else |
314 |
goto out; |
return -EINVAL; |
315 |
cp1 = strchr(cp2, ' '); |
e.perm = 1 << operation; |
316 |
if (!cp1) |
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
317 |
goto out; |
case CCS_IP_ADDRESS_TYPE_IPv6: |
318 |
*cp1++ = '\0'; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv6; |
319 |
switch (ccs_parse_ip_address(cp2, min_address, max_address)) { |
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
320 |
case 2: |
min_address); |
321 |
record_type = IP_RECORD_TYPE_IPv6; |
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
322 |
|
max_address); |
323 |
|
if (!e.address.ipv6.min || !e.address.ipv6.max) |
324 |
|
goto out; |
325 |
break; |
break; |
326 |
case 1: |
case CCS_IP_ADDRESS_TYPE_IPv4: |
327 |
record_type = IP_RECORD_TYPE_IPv4; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv4; |
328 |
|
/* use host byte order to allow u32 comparison.*/ |
329 |
|
e.address.ipv4.min = ntohl(*(u32 *) min_address); |
330 |
|
e.address.ipv4.max = ntohl(*(u32 *) max_address); |
331 |
break; |
break; |
332 |
default: |
default: |
333 |
if (*cp2 != '@') |
if (w[2][0] != '@') |
334 |
goto out; |
return -EINVAL; |
335 |
group = ccs_find_or_assign_new_address_group(cp2 + 1); |
e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
336 |
if (!group) |
e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP); |
337 |
|
if (!e.address.group) |
338 |
return -ENOMEM; |
return -ENOMEM; |
|
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
|
339 |
break; |
break; |
340 |
} |
} |
341 |
if (strchr(cp1, ' ')) |
if (!ccs_parse_number_union(w[3], &e.port)) |
342 |
goto out; |
goto out; |
343 |
count = sscanf(cp1, "%hu-%hu", &min_port, &max_port); |
error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain, |
344 |
if (count != 1 && count != 2) |
ccs_same_ip_network_acl, |
345 |
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); |
|
346 |
out: |
out: |
347 |
return -EINVAL; |
if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP) |
348 |
} |
ccs_put_group(e.address.group); |
349 |
|
else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) { |
350 |
/** |
ccs_put_ipv6_address(e.address.ipv6.min); |
351 |
* ccs_check_network_listen_acl - Check permission for listen() operation. |
ccs_put_ipv6_address(e.address.ipv6.max); |
|
* |
|
|
* @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)); |
|
|
} |
|
|
|
|
|
/** |
|
|
* 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) |
|
|
{ |
|
|
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; |
|
352 |
} |
} |
353 |
return ccs_check_network_entry(is_ipv6, operation, |
ccs_put_number_union(&e.port); |
354 |
(const u32 *) address, ntohs(port)); |
return error; |
|
} |
|
|
|
|
|
/** |
|
|
* ccs_check_network_bind_acl - Check permission for bind() 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 int ccs_check_network_bind_acl(const bool is_ipv6, const int sock_type, |
|
|
const u8 *address, const u16 port) |
|
|
{ |
|
|
u8 operation; |
|
|
switch (sock_type) { |
|
|
case SOCK_STREAM: |
|
|
operation = NETWORK_ACL_TCP_BIND; |
|
|
break; |
|
|
case SOCK_DGRAM: |
|
|
operation = NETWORK_ACL_UDP_BIND; |
|
|
break; |
|
|
default: |
|
|
operation = NETWORK_ACL_RAW_BIND; |
|
|
} |
|
|
return ccs_check_network_entry(is_ipv6, operation, |
|
|
(const u32 *) address, ntohs(port)); |
|
355 |
} |
} |
356 |
|
|
357 |
/** |
#ifndef CONFIG_NET |
|
* 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; |
|
|
} |
|
358 |
|
|
359 |
/** |
void __init ccs_network_init(void) |
|
* 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) |
|
360 |
{ |
{ |
|
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; |
|
361 |
} |
} |
362 |
|
|
363 |
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
#else |
364 |
|
|
365 |
/* Check permission for creating a socket. */ |
/* Check permission for creating a socket. */ |
366 |
int ccs_socket_create_permission(int family, int type, int protocol) |
static int __ccs_socket_create_permission(int family, int type, int protocol) |
367 |
{ |
{ |
368 |
int error = 0; |
int error = 0; |
369 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
370 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
371 |
return 0; |
return 0; |
372 |
if (family == PF_PACKET && !ccs_capable(TOMOYO_USE_PACKET_SOCKET)) |
if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET)) |
373 |
return -EPERM; |
return -EPERM; |
374 |
if (family == PF_ROUTE && !ccs_capable(TOMOYO_USE_ROUTE_SOCKET)) |
if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET)) |
375 |
return -EPERM; |
return -EPERM; |
376 |
if (family != PF_INET && family != PF_INET6) |
if (family != PF_INET && family != PF_INET6) |
377 |
return 0; |
return 0; |
378 |
switch (type) { |
switch (type) { |
379 |
case SOCK_STREAM: |
case SOCK_STREAM: |
380 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CREATE)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_CREATE)) |
381 |
error = -EPERM; |
error = -EPERM; |
382 |
break; |
break; |
383 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
384 |
if (!ccs_capable(TOMOYO_USE_INET_DGRAM_SOCKET)) |
if (!ccs_capable(CCS_USE_INET_DGRAM_SOCKET)) |
385 |
error = -EPERM; |
error = -EPERM; |
386 |
break; |
break; |
387 |
case SOCK_RAW: |
case SOCK_RAW: |
388 |
if (!ccs_capable(TOMOYO_USE_INET_RAW_SOCKET)) |
if (!ccs_capable(CCS_USE_INET_RAW_SOCKET)) |
389 |
error = -EPERM; |
error = -EPERM; |
390 |
break; |
break; |
391 |
} |
} |
393 |
} |
} |
394 |
|
|
395 |
/* Check permission for listening a TCP socket. */ |
/* Check permission for listening a TCP socket. */ |
396 |
int ccs_socket_listen_permission(struct socket *sock) |
static int __ccs_socket_listen_permission(struct socket *sock) |
397 |
{ |
{ |
398 |
|
struct sockaddr_storage addr; |
399 |
int error = 0; |
int error = 0; |
|
char addr[MAX_SOCK_ADDR]; |
|
400 |
int addr_len; |
int addr_len; |
401 |
|
u32 *address; |
402 |
|
u16 port; |
403 |
|
bool is_ipv6; |
404 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
405 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
406 |
return 0; |
return 0; |
413 |
default: |
default: |
414 |
return 0; |
return 0; |
415 |
} |
} |
416 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_LISTEN)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN)) |
417 |
return -EPERM; |
return -EPERM; |
418 |
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
if (sock->ops->getname(sock, (struct sockaddr *) &addr, &addr_len, 0)) |
419 |
return -EPERM; |
return -EPERM; |
420 |
switch (((struct sockaddr *) addr)->sa_family) { |
switch (((struct sockaddr *) &addr)->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
421 |
case AF_INET6: |
case AF_INET6: |
422 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
423 |
error = ccs_check_network_listen_acl(true, |
address = (u32 *) ((struct sockaddr_in6 *) &addr)->sin6_addr |
424 |
addr6->sin6_addr.s6_addr, |
.s6_addr; |
425 |
addr6->sin6_port); |
port = ((struct sockaddr_in6 *) &addr)->sin6_port; |
426 |
break; |
break; |
427 |
case AF_INET: |
case AF_INET: |
428 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
429 |
error = ccs_check_network_listen_acl(false, |
address = (u32 *) &((struct sockaddr_in *) &addr)->sin_addr; |
430 |
(u8 *) &addr4->sin_addr, |
port = ((struct sockaddr_in *) &addr)->sin_port; |
|
addr4->sin_port); |
|
431 |
break; |
break; |
432 |
|
default: |
433 |
|
goto skip; |
434 |
} |
} |
435 |
|
error = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN, address, |
436 |
|
port); |
437 |
|
skip: |
438 |
return error; |
return error; |
439 |
} |
} |
440 |
|
|
441 |
/* 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. */ |
442 |
int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_connect_permission(struct socket *sock, |
443 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
444 |
{ |
{ |
445 |
int error = 0; |
int error = 0; |
446 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
447 |
|
u32 *address; |
448 |
|
u16 port; |
449 |
|
u8 operation; |
450 |
|
bool is_ipv6; |
451 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
452 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
453 |
return 0; |
return 0; |
454 |
switch (type) { |
switch (type) { |
455 |
case SOCK_STREAM: |
case SOCK_STREAM: |
456 |
|
operation = CCS_NETWORK_TCP_CONNECT; |
457 |
|
break; |
458 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
459 |
|
operation = CCS_NETWORK_UDP_CONNECT; |
460 |
|
break; |
461 |
case SOCK_RAW: |
case SOCK_RAW: |
462 |
|
operation = CCS_NETWORK_RAW_CONNECT; |
463 |
break; |
break; |
464 |
default: |
default: |
465 |
return 0; |
return 0; |
466 |
} |
} |
467 |
switch (addr->sa_family) { |
switch (addr->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
|
u16 port; |
|
468 |
case AF_INET6: |
case AF_INET6: |
469 |
if (addr_len < SIN6_LEN_RFC2133) |
if (addr_len < SIN6_LEN_RFC2133) |
470 |
break; |
goto skip; |
471 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
472 |
if (type != SOCK_RAW) |
address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr |
473 |
port = addr6->sin6_port; |
.s6_addr; |
474 |
else |
port = ((struct sockaddr_in6 *) addr)->sin6_port; |
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_connect_acl(true, type, |
|
|
addr6->sin6_addr.s6_addr, |
|
|
port); |
|
475 |
break; |
break; |
476 |
case AF_INET: |
case AF_INET: |
477 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
478 |
break; |
goto skip; |
479 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
480 |
if (type != SOCK_RAW) |
address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr; |
481 |
port = addr4->sin_port; |
port = ((struct sockaddr_in *) addr)->sin_port; |
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_connect_acl(false, type, |
|
|
(u8 *) &addr4->sin_addr, |
|
|
port); |
|
482 |
break; |
break; |
483 |
|
default: |
484 |
|
goto skip; |
485 |
} |
} |
486 |
|
if (type == SOCK_RAW) |
487 |
|
port = htons(sock->sk->sk_protocol); |
488 |
|
error = ccs_network_entry(is_ipv6, operation, address, port); |
489 |
|
skip: |
490 |
if (type != SOCK_STREAM) |
if (type != SOCK_STREAM) |
491 |
return error; |
return error; |
492 |
switch (sock->sk->sk_family) { |
switch (sock->sk->sk_family) { |
493 |
case PF_INET: |
case PF_INET: |
494 |
case PF_INET6: |
case PF_INET6: |
495 |
if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CONNECT)) |
if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT)) |
496 |
error = -EPERM; |
error = -EPERM; |
497 |
break; |
break; |
498 |
} |
} |
500 |
} |
} |
501 |
|
|
502 |
/* 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. */ |
503 |
int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_bind_permission(struct socket *sock, |
504 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
505 |
{ |
{ |
506 |
int error = 0; |
int error = 0; |
507 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
508 |
|
const u32 *address; |
509 |
|
u16 port; |
510 |
|
u8 operation; |
511 |
|
bool is_ipv6; |
512 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
513 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
514 |
return 0; |
return 0; |
515 |
switch (type) { |
switch (type) { |
516 |
case SOCK_STREAM: |
case SOCK_STREAM: |
517 |
|
operation = CCS_NETWORK_TCP_BIND; |
518 |
|
break; |
519 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
520 |
|
operation = CCS_NETWORK_UDP_BIND; |
521 |
|
break; |
522 |
case SOCK_RAW: |
case SOCK_RAW: |
523 |
|
operation = CCS_NETWORK_RAW_BIND; |
524 |
break; |
break; |
525 |
default: |
default: |
526 |
return 0; |
return 0; |
527 |
} |
} |
528 |
switch (addr->sa_family) { |
switch (addr->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
|
u16 port; |
|
529 |
case AF_INET6: |
case AF_INET6: |
530 |
if (addr_len < SIN6_LEN_RFC2133) |
if (addr_len < SIN6_LEN_RFC2133) |
531 |
break; |
goto skip; |
532 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
533 |
if (type != SOCK_RAW) |
address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr |
534 |
port = addr6->sin6_port; |
.s6_addr; |
535 |
else |
port = ((struct sockaddr_in6 *) addr)->sin6_port; |
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_bind_acl(true, type, |
|
|
addr6->sin6_addr.s6_addr, |
|
|
port); |
|
536 |
break; |
break; |
537 |
case AF_INET: |
case AF_INET: |
538 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
539 |
break; |
goto skip; |
540 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
541 |
if (type != SOCK_RAW) |
address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr; |
542 |
port = addr4->sin_port; |
port = ((struct sockaddr_in *) addr)->sin_port; |
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_bind_acl(false, type, |
|
|
(u8 *) &addr4->sin_addr, |
|
|
port); |
|
543 |
break; |
break; |
544 |
|
default: |
545 |
|
goto skip; |
546 |
} |
} |
547 |
|
if (type == SOCK_RAW) |
548 |
|
port = htons(sock->sk->sk_protocol); |
549 |
|
error = ccs_network_entry(is_ipv6, operation, address, port); |
550 |
|
skip: |
551 |
return error; |
return error; |
552 |
} |
} |
553 |
|
|
554 |
/* |
/* Check permission for accepting a TCP socket. */ |
555 |
* Check permission for accepting a TCP socket. |
static int __ccs_socket_post_accept_permission(struct socket *sock, |
556 |
* |
struct socket *newsock) |
|
* Currently, the LSM hook for this purpose is not provided. |
|
|
*/ |
|
|
int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr) |
|
557 |
{ |
{ |
558 |
|
struct sockaddr_storage addr; |
559 |
|
struct task_struct * const task = current; |
560 |
int error = 0; |
int error = 0; |
561 |
int addr_len; |
int addr_len; |
562 |
|
u32 *address; |
563 |
|
u16 port; |
564 |
|
bool is_ipv6; |
565 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
566 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
567 |
return 0; |
return 0; |
568 |
switch (sock->sk->sk_family) { |
switch (newsock->sk->sk_family) { |
569 |
case PF_INET: |
case PF_INET: |
570 |
case PF_INET6: |
case PF_INET6: |
571 |
break; |
break; |
572 |
default: |
default: |
573 |
return 0; |
return 0; |
574 |
} |
} |
575 |
error = sock->ops->getname(sock, addr, &addr_len, 2); |
error = newsock->ops->getname(newsock, (struct sockaddr *) &addr, |
576 |
|
&addr_len, 2); |
577 |
if (error) |
if (error) |
578 |
return error; |
return error; |
579 |
switch (addr->sa_family) { |
switch (((struct sockaddr *) &addr)->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
580 |
case AF_INET6: |
case AF_INET6: |
581 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
582 |
error = ccs_check_network_accept_acl(true, |
address = (u32 *) ((struct sockaddr_in6 *) &addr)->sin6_addr |
583 |
addr6->sin6_addr.s6_addr, |
.s6_addr; |
584 |
addr6->sin6_port); |
port = ((struct sockaddr_in6 *) &addr)->sin6_port; |
585 |
break; |
break; |
586 |
case AF_INET: |
case AF_INET: |
587 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
588 |
error = ccs_check_network_accept_acl(false, |
address = (u32 *) &((struct sockaddr_in *) &addr)->sin_addr; |
589 |
(u8 *) &addr4->sin_addr, |
port = ((struct sockaddr_in *) &addr)->sin_port; |
|
addr4->sin_port); |
|
590 |
break; |
break; |
591 |
|
default: |
592 |
|
goto skip; |
593 |
} |
} |
594 |
|
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
595 |
|
error = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT, address, |
596 |
|
port); |
597 |
|
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
598 |
|
skip: |
599 |
return error; |
return error; |
600 |
} |
} |
601 |
|
|
602 |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
603 |
int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_sendmsg_permission(struct socket *sock, |
604 |
int addr_len) |
struct msghdr *msg, int size) |
605 |
{ |
{ |
606 |
|
struct sockaddr *addr = (struct sockaddr *) msg->msg_name; |
607 |
|
const int addr_len = msg->msg_namelen; |
608 |
int error = 0; |
int error = 0; |
609 |
const int type = sock->type; |
const int type = sock->type; |
610 |
|
u32 *address; |
611 |
|
u16 port; |
612 |
|
bool is_ipv6; |
613 |
|
u8 operation; |
614 |
|
if (!addr) |
615 |
|
return 0; |
616 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
617 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
618 |
return 0; |
return 0; |
619 |
if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW)) |
switch (type) { |
620 |
|
case SOCK_DGRAM: |
621 |
|
operation = CCS_NETWORK_UDP_CONNECT; |
622 |
|
break; |
623 |
|
case SOCK_RAW: |
624 |
|
operation = CCS_NETWORK_RAW_CONNECT; |
625 |
|
break; |
626 |
|
default: |
627 |
return 0; |
return 0; |
628 |
|
} |
629 |
switch (addr->sa_family) { |
switch (addr->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
|
u16 port; |
|
630 |
case AF_INET6: |
case AF_INET6: |
631 |
if (addr_len < SIN6_LEN_RFC2133) |
if (addr_len < SIN6_LEN_RFC2133) |
632 |
break; |
goto skip; |
633 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
634 |
if (type == SOCK_DGRAM) |
address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr |
635 |
port = addr6->sin6_port; |
.s6_addr; |
636 |
else |
port = ((struct sockaddr_in6 *) addr)->sin6_port; |
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_sendmsg_acl(true, type, |
|
|
addr6->sin6_addr.s6_addr, |
|
|
port); |
|
637 |
break; |
break; |
638 |
case AF_INET: |
case AF_INET: |
639 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
640 |
break; |
goto skip; |
641 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
642 |
if (type == SOCK_DGRAM) |
address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr; |
643 |
port = addr4->sin_port; |
port = ((struct sockaddr_in *) addr)->sin_port; |
|
else |
|
|
port = htons(sock->sk->sk_protocol); |
|
|
error = ccs_check_network_sendmsg_acl(false, type, |
|
|
(u8 *) &addr4->sin_addr, |
|
|
port); |
|
644 |
break; |
break; |
645 |
|
default: |
646 |
|
goto skip; |
647 |
} |
} |
648 |
|
if (type == SOCK_RAW) |
649 |
|
port = htons(sock->sk->sk_protocol); |
650 |
|
error = ccs_network_entry(is_ipv6, operation, address, port); |
651 |
|
skip: |
652 |
return error; |
return error; |
653 |
} |
} |
654 |
|
|
655 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
656 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
657 |
|
#if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2 |
658 |
|
|
659 |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
660 |
{ |
{ |
673 |
|
|
674 |
#endif |
#endif |
675 |
#endif |
#endif |
676 |
|
#endif |
677 |
|
|
678 |
/* |
/* Check permission for receiving a datagram via a UDP or RAW socket. */ |
679 |
* Check permission for receiving a datagram via a UDP or RAW socket. |
static int __ccs_socket_post_recvmsg_permission(struct sock *sk, |
680 |
* |
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) |
|
681 |
{ |
{ |
682 |
|
struct task_struct * const task = current; |
683 |
int error = 0; |
int error = 0; |
684 |
const unsigned int type = sk->sk_type; |
const unsigned int type = sk->sk_type; |
685 |
/* Nothing to do if I didn't receive a datagram. */ |
u16 port; |
686 |
if (!skb) |
bool is_ipv6; |
687 |
return 0; |
u8 operation; |
688 |
/* Nothing to do if I can't sleep. */ |
union { |
689 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
struct in6_addr sin6; |
690 |
if (in_interrupt()) |
struct in_addr sin4; |
691 |
return 0; |
} address; |
692 |
#else |
switch (type) { |
693 |
if (in_atomic()) |
case SOCK_DGRAM: |
694 |
|
operation = CCS_NETWORK_UDP_CONNECT; |
695 |
|
break; |
696 |
|
case SOCK_RAW: |
697 |
|
operation = CCS_NETWORK_RAW_CONNECT; |
698 |
|
break; |
699 |
|
default: |
700 |
return 0; |
return 0; |
701 |
#endif |
} |
702 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
703 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
704 |
return 0; |
return 0; |
|
if (type != SOCK_DGRAM && type != SOCK_RAW) |
|
|
return 0; |
|
|
|
|
705 |
switch (sk->sk_family) { |
switch (sk->sk_family) { |
|
struct in6_addr sin6; |
|
|
struct in_addr sin4; |
|
|
u16 port; |
|
706 |
case PF_INET6: |
case PF_INET6: |
707 |
if (type == SOCK_DGRAM) { /* UDP IPv6 */ |
is_ipv6 = true; |
708 |
if (skb->protocol == htons(ETH_P_IP)) { |
if (type == SOCK_DGRAM && skb->protocol == htons(ETH_P_IP)) |
709 |
ipv6_addr_set(&sin6, 0, 0, htonl(0xffff), |
ipv6_addr_set(&address.sin6, 0, 0, htonl(0xffff), |
710 |
ip_hdr(skb)->saddr); |
ip_hdr(skb)->saddr); |
711 |
} else { |
else |
712 |
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
ipv6_addr_copy(&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); |
|
713 |
break; |
break; |
714 |
case PF_INET: |
case PF_INET: |
715 |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
is_ipv6 = false; |
716 |
sin4.s_addr = ip_hdr(skb)->saddr; |
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); |
|
717 |
break; |
break; |
718 |
|
default: |
719 |
|
goto skip; |
720 |
} |
} |
721 |
if (!error) |
if (type == SOCK_DGRAM) |
722 |
return 0; |
port = udp_hdr(skb)->source; |
723 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
else |
724 |
lock_sock(sk); |
port = htons(sk->sk_protocol); |
725 |
#endif |
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
726 |
/* |
error = ccs_network_entry(is_ipv6, operation, (u32 *) &address, port); |
727 |
* Remove from queue if MSG_PEEK is used so that |
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
728 |
* the head message from unwanted source in receive queue will not |
skip: |
729 |
* prevent the caller from picking up next message from wanted source |
return error; |
|
* when the caller is using MSG_PEEK flag for picking up. |
|
|
*/ |
|
|
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; |
|
730 |
} |
} |
731 |
|
|
732 |
|
void __init ccs_network_init(void) |
733 |
|
{ |
734 |
|
ccsecurity_ops.socket_create_permission = |
735 |
|
__ccs_socket_create_permission; |
736 |
|
ccsecurity_ops.socket_listen_permission = |
737 |
|
__ccs_socket_listen_permission; |
738 |
|
ccsecurity_ops.socket_connect_permission = |
739 |
|
__ccs_socket_connect_permission; |
740 |
|
ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission; |
741 |
|
ccsecurity_ops.socket_post_accept_permission = |
742 |
|
__ccs_socket_post_accept_permission; |
743 |
|
ccsecurity_ops.socket_sendmsg_permission = |
744 |
|
__ccs_socket_sendmsg_permission; |
745 |
|
ccsecurity_ops.socket_post_recvmsg_permission = |
746 |
|
__ccs_socket_post_recvmsg_permission; |
747 |
|
} |
748 |
|
|
749 |
|
#endif |