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/06/04 |
|
* |
|
|
* Version: 1.6.8-pre 2009/05/08 |
|
7 |
* |
* |
8 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
9 |
* See README.ccs for ChangeLog. |
* See README.ccs for ChangeLog. |
10 |
* |
* |
11 |
*/ |
*/ |
12 |
|
|
|
#include <linux/ccs_common.h> |
|
|
#include <linux/tomoyo.h> |
|
|
#include <linux/tomoyo_socket.h> |
|
|
#include <linux/realpath.h> |
|
13 |
#include <linux/net.h> |
#include <linux/net.h> |
14 |
#include <linux/inet.h> |
#include <linux/inet.h> |
15 |
#include <linux/in.h> |
#include <linux/in.h> |
17 |
#include <net/ip.h> |
#include <net/ip.h> |
18 |
#include <net/ipv6.h> |
#include <net/ipv6.h> |
19 |
#include <net/udp.h> |
#include <net/udp.h> |
20 |
|
#include "internal.h" |
|
/* Index numbers for Network Controls. */ |
|
|
enum ccs_network_acl_index { |
|
|
NETWORK_ACL_UDP_BIND, |
|
|
NETWORK_ACL_UDP_CONNECT, |
|
|
NETWORK_ACL_TCP_BIND, |
|
|
NETWORK_ACL_TCP_LISTEN, |
|
|
NETWORK_ACL_TCP_CONNECT, |
|
|
NETWORK_ACL_TCP_ACCEPT, |
|
|
NETWORK_ACL_RAW_BIND, |
|
|
NETWORK_ACL_RAW_CONNECT |
|
|
}; |
|
21 |
|
|
22 |
/** |
/** |
23 |
* ccs_audit_network_log - Audit network log. |
* ccs_audit_network_log - Audit network log. |
24 |
* |
* |
25 |
* @r: Pointer to "struct ccs_request_info". |
* @r: Pointer to "struct ccs_request_info". |
|
* @operation: The name of operation. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @port: Port number. |
|
|
* @is_granted: True if this is a granted log. |
|
26 |
* |
* |
27 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
28 |
*/ |
*/ |
29 |
static int ccs_audit_network_log(struct ccs_request_info *r, |
static int ccs_audit_network_log(struct ccs_request_info *r) |
|
const char *operation, const char *address, |
|
|
const u16 port, const bool is_granted) |
|
|
{ |
|
|
return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK |
|
|
"%s %s %u\n", operation, address, port); |
|
|
} |
|
|
|
|
|
/* The list for "struct ccs_address_group_entry". */ |
|
|
LIST_HEAD(ccs_address_group_list); |
|
|
|
|
|
/** |
|
|
* ccs_get_address_group - Allocate memory for "struct ccs_address_group_entry". |
|
|
* |
|
|
* @group_name: The name of address group. |
|
|
* |
|
|
* Returns pointer to "struct ccs_address_group_entry" on success, |
|
|
* NULL otherwise. |
|
|
*/ |
|
|
static struct ccs_address_group_entry *ccs_get_address_group(const char * |
|
|
group_name) |
|
30 |
{ |
{ |
31 |
struct ccs_address_group_entry *entry = NULL; |
char buf[128]; |
32 |
struct ccs_address_group_entry *group; |
const char *operation = ccs_net2keyword(r->param.network.operation); |
33 |
const struct ccs_path_info *saved_group_name; |
const u32 *address = r->param.network.address; |
34 |
int error = -ENOMEM; |
const u16 port = r->param.network.port; |
35 |
if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) || |
if (r->param.network.is_ipv6) |
36 |
!group_name[0]) |
ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *) |
37 |
return NULL; |
address, (const struct in6_addr *) address); |
38 |
saved_group_name = ccs_get_name(group_name); |
else |
39 |
if (!saved_group_name) |
ccs_print_ipv4(buf, sizeof(buf), r->param.network.ip, |
40 |
return NULL; |
r->param.network.ip); |
41 |
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
ccs_write_log(r, CCS_KEYWORD_ALLOW_NETWORK "%s %s %u\n", operation, |
42 |
/***** WRITER SECTION START *****/ |
buf, port); |
43 |
down_write(&ccs_policy_lock); |
if (r->granted) |
44 |
list_for_each_entry(group, &ccs_address_group_list, list) { |
return 0; |
45 |
if (saved_group_name != group->group_name) |
ccs_warn_log(r, "%s %s %u", operation, buf, port); |
46 |
continue; |
return ccs_supervisor(r, CCS_KEYWORD_ALLOW_NETWORK "%s %s %u\n", |
47 |
atomic_inc(&group->users); |
operation, buf, port); |
|
error = 0; |
|
|
break; |
|
|
} |
|
|
if (error && ccs_memory_ok(entry)) { |
|
|
INIT_LIST_HEAD(&entry->address_group_member_list); |
|
|
entry->group_name = saved_group_name; |
|
|
saved_group_name = NULL; |
|
|
atomic_set(&entry->users, 1); |
|
|
list_add_tail(&entry->list, &ccs_address_group_list); |
|
|
group = entry; |
|
|
entry = NULL; |
|
|
error = 0; |
|
|
} |
|
|
up_write(&ccs_policy_lock); |
|
|
/***** WRITER SECTION END *****/ |
|
|
ccs_put_name(saved_group_name); |
|
|
kfree(entry); |
|
|
return !error ? group : NULL; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_update_address_group_entry - Update "struct ccs_address_group_entry" list. |
|
|
* |
|
|
* @group_name: The name of address group. |
|
|
* @is_ipv6: True if @min_address and @max_address are IPv6 addresses. |
|
|
* @min_address: Start of IPv4 or IPv6 address range. |
|
|
* @max_address: End of IPv4 or IPv6 address range. |
|
|
* @is_delete: True if it is a delete request. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int ccs_update_address_group_entry(const char *group_name, |
|
|
const bool is_ipv6, |
|
|
const u16 *min_address, |
|
|
const u16 *max_address, |
|
|
const bool is_delete) |
|
|
{ |
|
|
struct ccs_address_group_entry *group; |
|
|
struct ccs_address_group_member *entry = NULL; |
|
|
struct ccs_address_group_member *member; |
|
|
const struct in6_addr *saved_min_address = NULL; |
|
|
const struct in6_addr *saved_max_address = NULL; |
|
|
int error = is_delete ? -ENOENT : -ENOMEM; |
|
|
const u32 min_ipv4_address = ntohl(*(u32 *) min_address); |
|
|
const u32 max_ipv4_address = ntohl(*(u32 *) max_address); |
|
|
group = ccs_get_address_group(group_name); |
|
|
if (!group) |
|
|
return -ENOMEM; |
|
|
if (is_ipv6) { |
|
|
saved_min_address |
|
|
= ccs_get_ipv6_address((struct in6_addr *) |
|
|
min_address); |
|
|
saved_max_address |
|
|
= ccs_get_ipv6_address((struct in6_addr *) |
|
|
max_address); |
|
|
if (!saved_min_address || !saved_max_address) |
|
|
goto out; |
|
|
} |
|
|
if (!is_delete) |
|
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
|
|
/***** WRITER SECTION START *****/ |
|
|
down_write(&ccs_policy_lock); |
|
|
list_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 != min_ipv4_address || |
|
|
member->max.ipv4 != max_ipv4_address) |
|
|
continue; |
|
|
} |
|
|
member->is_deleted = is_delete; |
|
|
error = 0; |
|
|
break; |
|
|
} |
|
|
if (!is_delete && error && ccs_memory_ok(entry)) { |
|
|
entry->is_ipv6 = is_ipv6; |
|
|
if (is_ipv6) { |
|
|
entry->min.ipv6 = saved_min_address; |
|
|
saved_min_address = NULL; |
|
|
entry->max.ipv6 = saved_max_address; |
|
|
saved_max_address = NULL; |
|
|
} else { |
|
|
entry->min.ipv4 = min_ipv4_address; |
|
|
entry->max.ipv4 = max_ipv4_address; |
|
|
} |
|
|
list_add_tail(&entry->list, &group->address_group_member_list); |
|
|
entry = NULL; |
|
|
atomic_inc(&group->users); |
|
|
error = 0; |
|
|
} |
|
|
up_write(&ccs_policy_lock); |
|
|
/***** WRITER SECTION END *****/ |
|
|
out: |
|
|
ccs_put_ipv6_address(saved_min_address); |
|
|
ccs_put_ipv6_address(saved_max_address); |
|
|
ccs_put_address_group(group); |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
|
|
return error; |
|
48 |
} |
} |
49 |
|
|
50 |
/** |
/** |
54 |
* @min: Pointer to store min address. |
* @min: Pointer to store min address. |
55 |
* @max: Pointer to store max address. |
* @max: Pointer to store max address. |
56 |
* |
* |
57 |
* Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise. |
* Returns CCS_IP_ADDRESS_TYPE_IPv6 if @address is an IPv6, |
58 |
|
* CCS_IP_ADDRESS_TYPE_IPv4 if @address is an IPv4, |
59 |
|
* CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP otherwise. |
60 |
*/ |
*/ |
61 |
static int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
62 |
{ |
{ |
63 |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
64 |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
74 |
min[i] = htons(min[i]); |
min[i] = htons(min[i]); |
75 |
max[i] = htons(max[i]); |
max[i] = htons(max[i]); |
76 |
} |
} |
77 |
return 2; |
return CCS_IP_ADDRESS_TYPE_IPv6; |
78 |
} |
} |
79 |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
80 |
&min[0], &min[1], &min[2], &min[3], |
&min[0], &min[1], &min[2], &min[3], |
84 |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
85 |
memmove(min, &ip, sizeof(ip)); |
memmove(min, &ip, sizeof(ip)); |
86 |
if (count == 8) |
if (count == 8) |
87 |
ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16) |
ip = htonl((((u8) max[0]) << 24) |
88 |
|
+ (((u8) max[1]) << 16) |
89 |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
90 |
memmove(max, &ip, sizeof(ip)); |
memmove(max, &ip, sizeof(ip)); |
91 |
return 1; |
return CCS_IP_ADDRESS_TYPE_IPv4; |
|
} |
|
|
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_address_matches_group - Check whether the given address matches members of the given address group. |
|
|
* |
|
|
* @is_ipv6: True if @address is an IPv6 address. |
|
|
* @address: An IPv4 or IPv6 address. |
|
|
* @group: Pointer to "struct ccs_address_group_entry". |
|
|
* |
|
|
* Returns true if @address matches addresses in @group group, false otherwise. |
|
|
* |
|
|
* Caller holds ccs_policy_lockfor reading. |
|
|
*/ |
|
|
static bool ccs_address_matches_group(const bool is_ipv6, const u32 *address, |
|
|
const struct ccs_address_group_entry * |
|
|
group) |
|
|
{ |
|
|
struct ccs_address_group_member *member; |
|
|
const u32 ip = ntohl(*address); |
|
|
bool matched = false; |
|
|
list_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) { |
|
|
matched = true; |
|
|
break; |
|
|
} |
|
|
} else { |
|
|
if (!is_ipv6 && |
|
|
member->min.ipv4 <= ip && ip <= member->max.ipv4) { |
|
|
matched = true; |
|
|
break; |
|
|
} |
|
|
} |
|
92 |
} |
} |
93 |
return matched; |
return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
94 |
} |
} |
95 |
|
|
96 |
/** |
/** |
97 |
* ccs_read_address_group_policy - Read "struct ccs_address_group_entry" list. |
* ccs_print_ipv4 - Print an IPv4 address. |
98 |
* |
* |
99 |
* @head: Pointer to "struct ccs_io_buffer". |
* @buffer: Buffer to write to. |
100 |
|
* @buffer_len: Size of @buffer. |
101 |
|
* @min_ip: Min address in host byte order. |
102 |
|
* @max_ip: Max address in host byte order. |
103 |
* |
* |
104 |
* Returns true on success, false otherwise. |
* Returns nothing. |
105 |
*/ |
*/ |
106 |
bool ccs_read_address_group_policy(struct ccs_io_buffer *head) |
void ccs_print_ipv4(char *buffer, const int buffer_len, |
107 |
|
const u32 min_ip, const u32 max_ip) |
108 |
{ |
{ |
109 |
struct list_head *gpos; |
memset(buffer, 0, buffer_len); |
110 |
struct list_head *mpos; |
snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u", |
111 |
bool done = true; |
HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-', |
112 |
/***** READER SECTION START *****/ |
HIPQUAD(max_ip)); |
|
down_read(&ccs_policy_lock); |
|
|
list_for_each_cookie(gpos, head->read_var1.u.list, |
|
|
&ccs_address_group_list) { |
|
|
struct ccs_address_group_entry *group; |
|
|
group = list_entry(gpos, struct ccs_address_group_entry, list); |
|
|
list_for_each_cookie(mpos, head->read_var2.u.list, |
|
|
&group->address_group_member_list) { |
|
|
char buf[128]; |
|
|
struct ccs_address_group_member *member; |
|
|
member = list_entry(mpos, |
|
|
struct ccs_address_group_member, |
|
|
list); |
|
|
if (member->is_deleted) |
|
|
continue; |
|
|
if (member->is_ipv6) { |
|
|
const struct in6_addr *min_address |
|
|
= member->min.ipv6; |
|
|
const struct in6_addr *max_address |
|
|
= member->max.ipv6; |
|
|
ccs_print_ipv6(buf, sizeof(buf), min_address); |
|
|
if (min_address != max_address) { |
|
|
int len; |
|
|
char *cp = buf + strlen(buf); |
|
|
*cp++ = '-'; |
|
|
len = strlen(buf); |
|
|
ccs_print_ipv6(cp, sizeof(buf) - len, |
|
|
max_address); |
|
|
} |
|
|
} else { |
|
|
const u32 min_address = member->min.ipv4; |
|
|
const u32 max_address = member->max.ipv4; |
|
|
memset(buf, 0, sizeof(buf)); |
|
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", |
|
|
HIPQUAD(min_address)); |
|
|
if (min_address != max_address) { |
|
|
const int len = strlen(buf); |
|
|
snprintf(buf + len, |
|
|
sizeof(buf) - 1 - len, |
|
|
"-%u.%u.%u.%u", |
|
|
HIPQUAD(max_address)); |
|
|
} |
|
|
} |
|
|
done = ccs_io_printf(head, KEYWORD_ADDRESS_GROUP |
|
|
"%s %s\n", group->group_name->name, |
|
|
buf); |
|
|
if (!done) |
|
|
break; |
|
|
} |
|
|
if (!done) |
|
|
break; |
|
|
} |
|
|
up_read(&ccs_policy_lock); |
|
|
/***** READER SECTION END *****/ |
|
|
return done; |
|
113 |
} |
} |
114 |
|
|
115 |
#if !defined(NIP6) |
#if !defined(NIP6) |
116 |
#define NIP6(addr) \ |
#define NIP6(addr) \ |
117 |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
118 |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
119 |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
120 |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
121 |
#endif |
#endif |
122 |
|
|
123 |
/** |
/** |
125 |
* |
* |
126 |
* @buffer: Buffer to write to. |
* @buffer: Buffer to write to. |
127 |
* @buffer_len: Size of @buffer. |
* @buffer_len: Size of @buffer. |
128 |
* @ip: Pointer to "struct in6_addr". |
* @min_ip: Pointer to "struct in6_addr". |
129 |
|
* @max_ip: Pointer to "struct in6_addr". |
130 |
* |
* |
131 |
* Returns nothing. |
* Returns nothing. |
132 |
*/ |
*/ |
133 |
void ccs_print_ipv6(char *buffer, const int buffer_len, |
void ccs_print_ipv6(char *buffer, const int buffer_len, |
134 |
const struct in6_addr *ip) |
const struct in6_addr *min_ip, |
135 |
|
const struct in6_addr *max_ip) |
136 |
{ |
{ |
137 |
memset(buffer, 0, buffer_len); |
memset(buffer, 0, buffer_len); |
138 |
snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip)); |
snprintf(buffer, buffer_len - 1, |
139 |
|
"%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x", |
140 |
|
NIP6(*min_ip), min_ip == max_ip ? '\0' : '-', |
141 |
|
NIP6(*max_ip)); |
142 |
} |
} |
143 |
|
|
144 |
/** |
/** |
152 |
{ |
{ |
153 |
const char *keyword = "unknown"; |
const char *keyword = "unknown"; |
154 |
switch (operation) { |
switch (operation) { |
155 |
case NETWORK_ACL_UDP_BIND: |
case CCS_NETWORK_UDP_BIND: |
156 |
keyword = "UDP bind"; |
keyword = "UDP bind"; |
157 |
break; |
break; |
158 |
case NETWORK_ACL_UDP_CONNECT: |
case CCS_NETWORK_UDP_CONNECT: |
159 |
keyword = "UDP connect"; |
keyword = "UDP connect"; |
160 |
break; |
break; |
161 |
case NETWORK_ACL_TCP_BIND: |
case CCS_NETWORK_TCP_BIND: |
162 |
keyword = "TCP bind"; |
keyword = "TCP bind"; |
163 |
break; |
break; |
164 |
case NETWORK_ACL_TCP_LISTEN: |
case CCS_NETWORK_TCP_LISTEN: |
165 |
keyword = "TCP listen"; |
keyword = "TCP listen"; |
166 |
break; |
break; |
167 |
case NETWORK_ACL_TCP_CONNECT: |
case CCS_NETWORK_TCP_CONNECT: |
168 |
keyword = "TCP connect"; |
keyword = "TCP connect"; |
169 |
break; |
break; |
170 |
case NETWORK_ACL_TCP_ACCEPT: |
case CCS_NETWORK_TCP_ACCEPT: |
171 |
keyword = "TCP accept"; |
keyword = "TCP accept"; |
172 |
break; |
break; |
173 |
case NETWORK_ACL_RAW_BIND: |
case CCS_NETWORK_RAW_BIND: |
174 |
keyword = "RAW bind"; |
keyword = "RAW bind"; |
175 |
break; |
break; |
176 |
case NETWORK_ACL_RAW_CONNECT: |
case CCS_NETWORK_RAW_CONNECT: |
177 |
keyword = "RAW connect"; |
keyword = "RAW connect"; |
178 |
break; |
break; |
179 |
} |
} |
180 |
return keyword; |
return keyword; |
181 |
} |
} |
182 |
|
|
183 |
/** |
static bool ccs_check_network_acl(const struct ccs_request_info *r, |
184 |
* ccs_update_network_entry - Update "struct ccs_ip_network_acl_record" list. |
const struct ccs_acl_info *ptr) |
|
* |
|
|
* @operation: Type of operation. |
|
|
* @record_type: Type of address. |
|
|
* @group: Name of group. May be NULL. |
|
|
* @min_address: Start of IPv4 or IPv6 address range. |
|
|
* @max_address: End of IPv4 or IPv6 address range. |
|
|
* @min_port: Start of port number range. |
|
|
* @max_port: End of port number range. |
|
|
* @domain: Pointer to "struct ccs_domain_info". |
|
|
* @condition: Pointer to "struct ccs_condition_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 char *group_name, |
|
|
const u32 *min_address, |
|
|
const u32 *max_address, |
|
|
const u16 min_port, const u16 max_port, |
|
|
struct ccs_domain_info *domain, |
|
|
struct ccs_condition_list *condition, |
|
|
const bool is_delete) |
|
185 |
{ |
{ |
186 |
struct ccs_ip_network_acl_record *entry = NULL; |
const struct ccs_ip_network_acl *acl = |
187 |
struct ccs_acl_info *ptr; |
container_of(ptr, typeof(*acl), head); |
188 |
int error = is_delete ? -ENOENT : -ENOMEM; |
bool ret; |
189 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
if (!(acl->perm & (1 << r->param.network.operation)) || |
190 |
const u32 min_ip = ntohl(*min_address); |
!ccs_compare_number_union(r->param.network.port, &acl->port)) |
191 |
const u32 max_ip = ntohl(*max_address); |
return false; |
192 |
const struct in6_addr *saved_min_address = NULL; |
switch (acl->address_type) { |
193 |
const struct in6_addr *saved_max_address = NULL; |
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
194 |
struct ccs_address_group_entry *group = NULL; |
ret = ccs_address_matches_group(r->param.network.is_ipv6, |
195 |
if (!domain) |
r->param.network.address, |
196 |
return -EINVAL; |
acl->address.group); |
197 |
if (group_name) { |
break; |
198 |
group = ccs_get_address_group(group_name); |
case CCS_IP_ADDRESS_TYPE_IPv4: |
199 |
if (!group) |
ret = !r->param.network.is_ipv6 && |
200 |
return -ENOMEM; |
acl->address.ipv4.min <= r->param.network.ip && |
201 |
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
r->param.network.ip <= acl->address.ipv4.max; |
|
saved_min_address = ccs_get_ipv6_address((struct in6_addr *) |
|
|
min_address); |
|
|
saved_max_address = ccs_get_ipv6_address((struct in6_addr *) |
|
|
max_address); |
|
|
if (!saved_min_address || !saved_max_address) |
|
|
goto out; |
|
|
} |
|
|
if (is_delete) |
|
|
goto delete; |
|
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
|
|
/***** WRITER SECTION START *****/ |
|
|
down_write(&ccs_policy_lock); |
|
|
list_for_each_entry(ptr, &domain->acl_info_list, list) { |
|
|
struct ccs_ip_network_acl_record *acl; |
|
|
if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
if (ptr->cond != condition) |
|
|
continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (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); |
|
202 |
break; |
break; |
203 |
} |
default: |
204 |
if (error && ccs_memory_ok(entry)) { |
ret = r->param.network.is_ipv6 && |
205 |
entry->head.type = TYPE_IP_NETWORK_ACL; |
memcmp(acl->address.ipv6.min, r->param.network.address, |
206 |
entry->head.cond = condition; |
16) <= 0 && |
207 |
entry->operation_type = operation; |
memcmp(r->param.network.address, acl->address.ipv6.max, |
208 |
entry->record_type = record_type; |
16) <= 0; |
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
entry->u.group = group; |
|
|
group = NULL; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
entry->u.ipv4.min = min_ip; |
|
|
entry->u.ipv4.max = max_ip; |
|
|
} else { |
|
|
entry->u.ipv6.min = saved_min_address; |
|
|
saved_min_address = NULL; |
|
|
entry->u.ipv6.max = saved_max_address; |
|
|
saved_max_address = NULL; |
|
|
} |
|
|
entry->min_port = min_port; |
|
|
entry->max_port = max_port; |
|
|
error = ccs_add_domain_acl(domain, &entry->head); |
|
|
entry = NULL; |
|
|
} |
|
|
up_write(&ccs_policy_lock); |
|
|
/***** WRITER SECTION END *****/ |
|
|
goto out; |
|
|
delete: |
|
|
/***** WRITER SECTION START *****/ |
|
|
down_write(&ccs_policy_lock); |
|
|
list_for_each_entry(ptr, &domain->acl_info_list, list) { |
|
|
struct ccs_ip_network_acl_record *acl; |
|
|
if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL) |
|
|
continue; |
|
|
if (ptr->cond != condition) |
|
|
continue; |
|
|
acl = container_of(ptr, struct ccs_ip_network_acl_record, head); |
|
|
if (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); |
|
209 |
break; |
break; |
210 |
} |
} |
211 |
up_write(&ccs_policy_lock); |
return ret; |
|
/***** WRITER SECTION END *****/ |
|
|
out: |
|
|
ccs_put_ipv6_address(saved_min_address); |
|
|
ccs_put_ipv6_address(saved_max_address); |
|
|
ccs_put_address_group(group); |
|
|
kfree(entry); |
|
|
return error; |
|
212 |
} |
} |
213 |
|
|
214 |
/** |
/** |
215 |
* ccs_check_network_entry - Check permission for network operation. |
* ccs_network_entry - Check permission for network operation. |
216 |
* |
* |
217 |
* @is_ipv6: True if @address is an IPv6 address. |
* @is_ipv6: True if @address is an IPv6 address. |
218 |
* @operation: Type of operation. |
* @operation: Type of operation. |
219 |
* @address: An IPv4 or IPv6 address. |
* @address: An IPv4 or IPv6 address in network byte order. |
220 |
* @port: Port number. |
* @port: Port number in network byte order. |
221 |
* |
* |
222 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
223 |
*/ |
*/ |
224 |
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, |
225 |
const u32 *address, const u16 port) |
const u32 *address, const u16 port) |
226 |
{ |
{ |
227 |
|
const int idx = ccs_read_lock(); |
228 |
struct ccs_request_info r; |
struct ccs_request_info r; |
229 |
struct ccs_acl_info *ptr; |
int error = 0; |
230 |
const char *keyword = ccs_net2keyword(operation); |
if (ccs_init_request_info(&r, CCS_MAC_NETWORK_UDP_BIND + operation) |
231 |
bool is_enforce; |
!= CCS_CONFIG_DISABLED) { |
232 |
/* using host byte order to allow u32 comparison than memcmp().*/ |
r.param_type = CCS_TYPE_IP_NETWORK_ACL; |
233 |
const u32 ip = ntohl(*address); |
r.param.network.operation = operation; |
234 |
bool found = false; |
r.param.network.is_ipv6 = is_ipv6; |
235 |
int error = -EPERM; |
r.param.network.address = address; |
236 |
char buf[64]; |
r.param.network.port = ntohs(port); |
237 |
if (!ccs_can_sleep()) |
/* use host byte order to allow u32 comparison than memcmp().*/ |
238 |
return 0; |
r.param.network.ip = ntohl(*address); |
239 |
ccs_init_request_info(&r, NULL, CCS_MAC_FOR_NETWORK); |
do { |
240 |
is_enforce = (r.mode == 3); |
ccs_check_acl(&r, ccs_check_network_acl); |
241 |
if (!r.mode) { |
error = ccs_audit_network_log(&r); |
242 |
error = 0; |
} while (error == CCS_RETRY_REQUEST); |
|
goto done; |
|
|
} |
|
|
retry: |
|
|
/***** READER SECTION START *****/ |
|
|
down_read(&ccs_policy_lock); |
|
|
list_for_each_entry(ptr, &r.cookie.u.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.condition_cookie.u.cond = ptr->cond; |
|
|
found = true; |
|
|
break; |
|
243 |
} |
} |
244 |
up_read(&ccs_policy_lock); |
ccs_read_unlock(idx); |
|
/***** READER SECTION END *****/ |
|
|
memset(buf, 0, sizeof(buf)); |
|
|
if (is_ipv6) |
|
|
ccs_print_ipv6(buf, sizeof(buf), |
|
|
(const struct in6_addr *) address); |
|
|
else |
|
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
|
|
ccs_audit_network_log(&r, keyword, buf, port, found); |
|
|
if (found) { |
|
|
error = 0; |
|
|
goto done; |
|
|
} |
|
|
if (ccs_verbose_mode(r.cookie.u.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.cookie.u.domain)); |
|
|
if (is_enforce) { |
|
|
int err = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK |
|
|
"%s %s %u\n", keyword, buf, |
|
|
port); |
|
|
if (err == 1) |
|
|
goto retry; |
|
|
error = err; |
|
|
goto done; |
|
|
} |
|
|
if (r.mode == 1 && ccs_domain_quota_ok(r.cookie.u.domain)) |
|
|
ccs_update_network_entry(operation, is_ipv6 ? |
|
|
IP_RECORD_TYPE_IPv6 : |
|
|
IP_RECORD_TYPE_IPv4, |
|
|
NULL, address, address, port, port, |
|
|
r.cookie.u.domain, ccs_handler_cond(), |
|
|
false); |
|
|
error = 0; |
|
|
done: |
|
|
ccs_exit_request_info(&r); |
|
245 |
return error; |
return error; |
246 |
} |
} |
247 |
|
|
248 |
|
static bool ccs_same_ip_network_acl(const struct ccs_acl_info *a, |
249 |
|
const struct ccs_acl_info *b) |
250 |
|
{ |
251 |
|
const struct ccs_ip_network_acl *p1 = container_of(a, typeof(*p1), |
252 |
|
head); |
253 |
|
const struct ccs_ip_network_acl *p2 = container_of(b, typeof(*p2), |
254 |
|
head); |
255 |
|
return ccs_same_acl_head(&p1->head, &p2->head) |
256 |
|
&& p1->address_type == p2->address_type && |
257 |
|
p1->address.ipv4.min == p2->address.ipv4.min && |
258 |
|
p1->address.ipv6.min == p2->address.ipv6.min && |
259 |
|
p1->address.ipv4.max == p2->address.ipv4.max && |
260 |
|
p1->address.ipv6.max == p2->address.ipv6.max && |
261 |
|
p1->address.group == p2->address.group && |
262 |
|
ccs_same_number_union(&p1->port, &p2->port); |
263 |
|
} |
264 |
|
|
265 |
|
static bool ccs_merge_ip_network_acl(struct ccs_acl_info *a, |
266 |
|
struct ccs_acl_info *b, |
267 |
|
const bool is_delete) |
268 |
|
{ |
269 |
|
u8 * const a_perm = &container_of(a, struct ccs_ip_network_acl, head) |
270 |
|
->perm; |
271 |
|
u8 perm = *a_perm; |
272 |
|
const u8 b_perm = container_of(b, struct ccs_ip_network_acl, head) |
273 |
|
->perm; |
274 |
|
if (is_delete) |
275 |
|
perm &= ~b_perm; |
276 |
|
else |
277 |
|
perm |= b_perm; |
278 |
|
*a_perm = perm; |
279 |
|
return !perm; |
280 |
|
} |
281 |
|
|
282 |
/** |
/** |
283 |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list. |
* ccs_write_network - Write "struct ccs_ip_network_acl" list. |
284 |
* |
* |
285 |
* @data: String to parse. |
* @data: String to parse. |
286 |
* @domain: Pointer to "struct ccs_domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
287 |
* @condition: Pointer to "struct ccs_condition_list". May be NULL. |
* @condition: Pointer to "struct ccs_condition". Maybe NULL. |
288 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
289 |
* |
* |
290 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
291 |
*/ |
*/ |
292 |
int ccs_write_network_policy(char *data, struct ccs_domain_info *domain, |
int ccs_write_network(char *data, struct ccs_domain_info *domain, |
293 |
struct ccs_condition_list *condition, |
struct ccs_condition *condition, const bool is_delete) |
|
const bool is_delete) |
|
294 |
{ |
{ |
295 |
u8 sock_type; |
struct ccs_ip_network_acl e = { |
296 |
u8 operation; |
.head.type = CCS_TYPE_IP_NETWORK_ACL, |
297 |
u8 record_type; |
.head.cond = condition, |
298 |
|
}; |
299 |
u16 min_address[8]; |
u16 min_address[8]; |
300 |
u16 max_address[8]; |
u16 max_address[8]; |
301 |
const char *group_name = NULL; |
int error = is_delete ? -ENOENT : -ENOMEM; |
302 |
u16 min_port; |
u8 sock_type; |
303 |
u16 max_port; |
u8 operation; |
304 |
u8 count; |
char *w[4]; |
305 |
char *cp1 = strchr(data, ' '); |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
306 |
char *cp2; |
return -EINVAL; |
307 |
if (!cp1) |
if (!strcmp(w[0], "TCP")) |
|
goto out; |
|
|
cp1++; |
|
|
if (!strncmp(data, "TCP ", 4)) |
|
308 |
sock_type = SOCK_STREAM; |
sock_type = SOCK_STREAM; |
309 |
else if (!strncmp(data, "UDP ", 4)) |
else if (!strcmp(w[0], "UDP")) |
310 |
sock_type = SOCK_DGRAM; |
sock_type = SOCK_DGRAM; |
311 |
else if (!strncmp(data, "RAW ", 4)) |
else if (!strcmp(w[0], "RAW")) |
312 |
sock_type = SOCK_RAW; |
sock_type = SOCK_RAW; |
313 |
else |
else |
314 |
goto out; |
return -EINVAL; |
315 |
cp2 = strchr(cp1, ' '); |
if (!strcmp(w[1], "bind")) |
|
if (!cp2) |
|
|
goto out; |
|
|
cp2++; |
|
|
if (!strncmp(cp1, "bind ", 5)) |
|
316 |
switch (sock_type) { |
switch (sock_type) { |
317 |
case SOCK_STREAM: |
case SOCK_STREAM: |
318 |
operation = NETWORK_ACL_TCP_BIND; |
operation = CCS_NETWORK_TCP_BIND; |
319 |
break; |
break; |
320 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
321 |
operation = NETWORK_ACL_UDP_BIND; |
operation = CCS_NETWORK_UDP_BIND; |
322 |
break; |
break; |
323 |
default: |
default: |
324 |
operation = NETWORK_ACL_RAW_BIND; |
operation = CCS_NETWORK_RAW_BIND; |
325 |
|
break; |
326 |
} |
} |
327 |
else if (!strncmp(cp1, "connect ", 8)) |
else if (!strcmp(w[1], "connect")) |
328 |
switch (sock_type) { |
switch (sock_type) { |
329 |
case SOCK_STREAM: |
case SOCK_STREAM: |
330 |
operation = NETWORK_ACL_TCP_CONNECT; |
operation = CCS_NETWORK_TCP_CONNECT; |
331 |
break; |
break; |
332 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
333 |
operation = NETWORK_ACL_UDP_CONNECT; |
operation = CCS_NETWORK_UDP_CONNECT; |
334 |
break; |
break; |
335 |
default: |
default: |
336 |
operation = NETWORK_ACL_RAW_CONNECT; |
operation = CCS_NETWORK_RAW_CONNECT; |
337 |
|
break; |
338 |
} |
} |
339 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen")) |
340 |
operation = NETWORK_ACL_TCP_LISTEN; |
operation = CCS_NETWORK_TCP_LISTEN; |
341 |
else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7)) |
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept")) |
342 |
operation = NETWORK_ACL_TCP_ACCEPT; |
operation = CCS_NETWORK_TCP_ACCEPT; |
343 |
else |
else |
344 |
goto out; |
return -EINVAL; |
345 |
cp1 = strchr(cp2, ' '); |
e.perm = 1 << operation; |
346 |
if (!cp1) |
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
347 |
goto out; |
case CCS_IP_ADDRESS_TYPE_IPv6: |
348 |
*cp1++ = '\0'; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv6; |
349 |
switch (ccs_parse_ip_address(cp2, min_address, max_address)) { |
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
350 |
case 2: |
min_address); |
351 |
record_type = IP_RECORD_TYPE_IPv6; |
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
352 |
|
max_address); |
353 |
|
if (!e.address.ipv6.min || !e.address.ipv6.max) |
354 |
|
goto out; |
355 |
break; |
break; |
356 |
case 1: |
case CCS_IP_ADDRESS_TYPE_IPv4: |
357 |
record_type = IP_RECORD_TYPE_IPv4; |
e.address_type = CCS_IP_ADDRESS_TYPE_IPv4; |
358 |
|
/* use host byte order to allow u32 comparison.*/ |
359 |
|
e.address.ipv4.min = ntohl(*(u32 *) min_address); |
360 |
|
e.address.ipv4.max = ntohl(*(u32 *) max_address); |
361 |
break; |
break; |
362 |
default: |
default: |
363 |
if (*cp2 != '@') |
if (w[2][0] != '@') |
364 |
goto out; |
return -EINVAL; |
365 |
group_name = cp2 + 1; |
e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
366 |
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP); |
367 |
|
if (!e.address.group) |
368 |
|
return -ENOMEM; |
369 |
break; |
break; |
370 |
} |
} |
371 |
if (strchr(cp1, ' ')) |
if (!ccs_parse_number_union(w[3], &e.port)) |
|
goto out; |
|
|
count = sscanf(cp1, "%hu-%hu", &min_port, &max_port); |
|
|
if (count != 1 && count != 2) |
|
372 |
goto out; |
goto out; |
373 |
if (count == 1) |
error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain, |
374 |
max_port = min_port; |
ccs_same_ip_network_acl, |
375 |
return ccs_update_network_entry(operation, record_type, group_name, |
ccs_merge_ip_network_acl); |
|
(u32 *) min_address, |
|
|
(u32 *) max_address, |
|
|
min_port, max_port, domain, condition, |
|
|
is_delete); |
|
376 |
out: |
out: |
377 |
return -EINVAL; |
if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP) |
378 |
} |
ccs_put_group(e.address.group); |
379 |
|
else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) { |
380 |
/** |
ccs_put_ipv6_address(e.address.ipv6.min); |
381 |
* 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; |
|
|
} |
|
|
return ccs_check_network_entry(is_ipv6, operation, |
|
|
(const u32 *) address, ntohs(port)); |
|
|
} |
|
|
|
|
|
/** |
|
|
* 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; |
|
382 |
} |
} |
383 |
return ccs_check_network_entry(is_ipv6, operation, |
ccs_put_number_union(&e.port); |
384 |
(const u32 *) address, ntohs(port)); |
return error; |
385 |
} |
} |
386 |
|
|
387 |
/** |
#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->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
|
|
retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, |
|
|
(const u32 *) address, ntohs(port)); |
|
|
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
|
|
return retval; |
|
|
} |
|
388 |
|
|
389 |
/** |
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) |
|
390 |
{ |
{ |
|
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)); |
|
391 |
} |
} |
392 |
|
|
393 |
/** |
#else |
|
* 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->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
|
|
retval = ccs_check_network_entry(is_ipv6, operation, |
|
|
(const u32 *) address, ntohs(port)); |
|
|
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
|
|
return retval; |
|
|
} |
|
394 |
|
|
395 |
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
396 |
|
|
397 |
/* Check permission for creating a socket. */ |
/* Check permission for creating a socket. */ |
398 |
int ccs_socket_create_permission(int family, int type, int protocol) |
static int __ccs_socket_create_permission(int family, int type, int protocol) |
399 |
{ |
{ |
400 |
int error = 0; |
int error = 0; |
401 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
425 |
} |
} |
426 |
|
|
427 |
/* Check permission for listening a TCP socket. */ |
/* Check permission for listening a TCP socket. */ |
428 |
int ccs_socket_listen_permission(struct socket *sock) |
static int __ccs_socket_listen_permission(struct socket *sock) |
429 |
{ |
{ |
430 |
int error = 0; |
int error = 0; |
431 |
char addr[MAX_SOCK_ADDR]; |
char addr[MAX_SOCK_ADDR]; |
432 |
int addr_len; |
int addr_len; |
433 |
|
u32 *address; |
434 |
|
u16 port; |
435 |
|
bool is_ipv6; |
436 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
437 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
438 |
return 0; |
return 0; |
450 |
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
451 |
return -EPERM; |
return -EPERM; |
452 |
switch (((struct sockaddr *) addr)->sa_family) { |
switch (((struct sockaddr *) addr)->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
453 |
case AF_INET6: |
case AF_INET6: |
454 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
455 |
error = ccs_check_network_listen_acl(true, |
address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr |
456 |
addr6->sin6_addr.s6_addr, |
.s6_addr; |
457 |
addr6->sin6_port); |
port = ((struct sockaddr_in6 *) addr)->sin6_port; |
458 |
break; |
break; |
459 |
case AF_INET: |
case AF_INET: |
460 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
461 |
error = ccs_check_network_listen_acl(false, |
address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr; |
462 |
(u8 *) &addr4->sin_addr, |
port = ((struct sockaddr_in *) addr)->sin_port; |
|
addr4->sin_port); |
|
463 |
break; |
break; |
464 |
|
default: |
465 |
|
goto skip; |
466 |
} |
} |
467 |
|
error = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN, address, |
468 |
|
port); |
469 |
|
skip: |
470 |
return error; |
return error; |
471 |
} |
} |
472 |
|
|
473 |
/* 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. */ |
474 |
int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_connect_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 |
|
u32 *address; |
480 |
|
u16 port; |
481 |
|
u8 operation; |
482 |
|
bool is_ipv6; |
483 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
484 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
485 |
return 0; |
return 0; |
486 |
switch (type) { |
switch (type) { |
487 |
case SOCK_STREAM: |
case SOCK_STREAM: |
488 |
|
operation = CCS_NETWORK_TCP_CONNECT; |
489 |
|
break; |
490 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
491 |
|
operation = CCS_NETWORK_UDP_CONNECT; |
492 |
|
break; |
493 |
case SOCK_RAW: |
case SOCK_RAW: |
494 |
|
operation = CCS_NETWORK_RAW_CONNECT; |
495 |
break; |
break; |
496 |
default: |
default: |
497 |
return 0; |
return 0; |
498 |
} |
} |
499 |
switch (addr->sa_family) { |
switch (addr->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
|
u16 port; |
|
500 |
case AF_INET6: |
case AF_INET6: |
501 |
if (addr_len < SIN6_LEN_RFC2133) |
if (addr_len < SIN6_LEN_RFC2133) |
502 |
break; |
goto skip; |
503 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
504 |
if (type != SOCK_RAW) |
address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr |
505 |
port = addr6->sin6_port; |
.s6_addr; |
506 |
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); |
|
507 |
break; |
break; |
508 |
case AF_INET: |
case AF_INET: |
509 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
510 |
break; |
goto skip; |
511 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
512 |
if (type != SOCK_RAW) |
address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr; |
513 |
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); |
|
514 |
break; |
break; |
515 |
|
default: |
516 |
|
goto skip; |
517 |
} |
} |
518 |
|
if (type == SOCK_RAW) |
519 |
|
port = htons(sock->sk->sk_protocol); |
520 |
|
error = ccs_network_entry(is_ipv6, operation, address, port); |
521 |
|
skip: |
522 |
if (type != SOCK_STREAM) |
if (type != SOCK_STREAM) |
523 |
return error; |
return error; |
524 |
switch (sock->sk->sk_family) { |
switch (sock->sk->sk_family) { |
532 |
} |
} |
533 |
|
|
534 |
/* 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. */ |
535 |
int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_bind_permission(struct socket *sock, |
536 |
int addr_len) |
struct sockaddr *addr, int addr_len) |
537 |
{ |
{ |
538 |
int error = 0; |
int error = 0; |
539 |
const unsigned int type = sock->type; |
const unsigned int type = sock->type; |
540 |
|
const u32 *address; |
541 |
|
u16 port; |
542 |
|
u8 operation; |
543 |
|
bool is_ipv6; |
544 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
545 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
546 |
return 0; |
return 0; |
547 |
switch (type) { |
switch (type) { |
548 |
case SOCK_STREAM: |
case SOCK_STREAM: |
549 |
|
operation = CCS_NETWORK_TCP_BIND; |
550 |
|
break; |
551 |
case SOCK_DGRAM: |
case SOCK_DGRAM: |
552 |
|
operation = CCS_NETWORK_UDP_BIND; |
553 |
|
break; |
554 |
case SOCK_RAW: |
case SOCK_RAW: |
555 |
|
operation = CCS_NETWORK_RAW_BIND; |
556 |
break; |
break; |
557 |
default: |
default: |
558 |
return 0; |
return 0; |
559 |
} |
} |
560 |
switch (addr->sa_family) { |
switch (addr->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
|
u16 port; |
|
561 |
case AF_INET6: |
case AF_INET6: |
562 |
if (addr_len < SIN6_LEN_RFC2133) |
if (addr_len < SIN6_LEN_RFC2133) |
563 |
break; |
goto skip; |
564 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
565 |
if (type != SOCK_RAW) |
address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr |
566 |
port = addr6->sin6_port; |
.s6_addr; |
567 |
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); |
|
568 |
break; |
break; |
569 |
case AF_INET: |
case AF_INET: |
570 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
571 |
break; |
goto skip; |
572 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
573 |
if (type != SOCK_RAW) |
address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr; |
574 |
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); |
|
575 |
break; |
break; |
576 |
|
default: |
577 |
|
goto skip; |
578 |
} |
} |
579 |
|
if (type == SOCK_RAW) |
580 |
|
port = htons(sock->sk->sk_protocol); |
581 |
|
error = ccs_network_entry(is_ipv6, operation, address, port); |
582 |
|
skip: |
583 |
return error; |
return error; |
584 |
} |
} |
585 |
|
|
588 |
* |
* |
589 |
* Currently, the LSM hook for this purpose is not provided. |
* Currently, the LSM hook for this purpose is not provided. |
590 |
*/ |
*/ |
591 |
int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr) |
static int __ccs_socket_accept_permission(struct socket *sock, |
592 |
|
struct sockaddr *addr) |
593 |
{ |
{ |
594 |
|
struct task_struct * const task = current; |
595 |
int error = 0; |
int error = 0; |
596 |
int addr_len; |
int addr_len; |
597 |
|
u32 *address; |
598 |
|
u16 port; |
599 |
|
bool is_ipv6; |
600 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
601 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
602 |
return 0; |
return 0; |
611 |
if (error) |
if (error) |
612 |
return error; |
return error; |
613 |
switch (addr->sa_family) { |
switch (addr->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
614 |
case AF_INET6: |
case AF_INET6: |
615 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
616 |
error = ccs_check_network_accept_acl(true, |
address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr |
617 |
addr6->sin6_addr.s6_addr, |
.s6_addr; |
618 |
addr6->sin6_port); |
port = ((struct sockaddr_in6 *) addr)->sin6_port; |
619 |
break; |
break; |
620 |
case AF_INET: |
case AF_INET: |
621 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
622 |
error = ccs_check_network_accept_acl(false, |
address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr; |
623 |
(u8 *) &addr4->sin_addr, |
port = ((struct sockaddr_in *) addr)->sin_port; |
|
addr4->sin_port); |
|
624 |
break; |
break; |
625 |
|
default: |
626 |
|
goto skip; |
627 |
} |
} |
628 |
|
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
629 |
|
error = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT, address, |
630 |
|
port); |
631 |
|
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
632 |
|
skip: |
633 |
return error; |
return error; |
634 |
} |
} |
635 |
|
|
636 |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
637 |
int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr, |
static int __ccs_socket_sendmsg_permission(struct socket *sock, |
638 |
int addr_len) |
struct msghdr *msg, int size) |
639 |
{ |
{ |
640 |
|
struct sockaddr *addr = (struct sockaddr *) msg->msg_name; |
641 |
|
const int addr_len = msg->msg_namelen; |
642 |
int error = 0; |
int error = 0; |
643 |
const int type = sock->type; |
const int type = sock->type; |
644 |
|
u32 *address; |
645 |
|
u16 port; |
646 |
|
bool is_ipv6; |
647 |
|
u8 operation; |
648 |
|
if (!addr) |
649 |
|
return 0; |
650 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
651 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
652 |
return 0; |
return 0; |
653 |
if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW)) |
switch (type) { |
654 |
|
case SOCK_DGRAM: |
655 |
|
operation = CCS_NETWORK_UDP_CONNECT; |
656 |
|
break; |
657 |
|
case SOCK_RAW: |
658 |
|
operation = CCS_NETWORK_RAW_CONNECT; |
659 |
|
break; |
660 |
|
default: |
661 |
return 0; |
return 0; |
662 |
|
} |
663 |
switch (addr->sa_family) { |
switch (addr->sa_family) { |
|
struct sockaddr_in6 *addr6; |
|
|
struct sockaddr_in *addr4; |
|
|
u16 port; |
|
664 |
case AF_INET6: |
case AF_INET6: |
665 |
if (addr_len < SIN6_LEN_RFC2133) |
if (addr_len < SIN6_LEN_RFC2133) |
666 |
break; |
goto skip; |
667 |
addr6 = (struct sockaddr_in6 *) addr; |
is_ipv6 = true; |
668 |
if (type == SOCK_DGRAM) |
address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr |
669 |
port = addr6->sin6_port; |
.s6_addr; |
670 |
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); |
|
671 |
break; |
break; |
672 |
case AF_INET: |
case AF_INET: |
673 |
if (addr_len < sizeof(struct sockaddr_in)) |
if (addr_len < sizeof(struct sockaddr_in)) |
674 |
break; |
goto skip; |
675 |
addr4 = (struct sockaddr_in *) addr; |
is_ipv6 = false; |
676 |
if (type == SOCK_DGRAM) |
address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr; |
677 |
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); |
|
678 |
break; |
break; |
679 |
|
default: |
680 |
|
goto skip; |
681 |
} |
} |
682 |
|
if (type == SOCK_RAW) |
683 |
|
port = htons(sock->sk->sk_protocol); |
684 |
|
error = ccs_network_entry(is_ipv6, operation, address, port); |
685 |
|
skip: |
686 |
return error; |
return error; |
687 |
} |
} |
688 |
|
|
689 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
690 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
691 |
|
#if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2 |
692 |
|
|
693 |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
694 |
{ |
{ |
707 |
|
|
708 |
#endif |
#endif |
709 |
#endif |
#endif |
710 |
|
#endif |
711 |
|
|
712 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) |
713 |
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
752 |
* |
* |
753 |
* Currently, the LSM hook for this purpose is not provided. |
* Currently, the LSM hook for this purpose is not provided. |
754 |
*/ |
*/ |
755 |
int ccs_socket_recvmsg_permission(struct sock *sk, struct sk_buff *skb, |
static int __ccs_socket_recvmsg_permission(struct sock *sk, |
756 |
const unsigned int flags) |
struct sk_buff *skb, |
757 |
|
const unsigned int flags) |
758 |
{ |
{ |
759 |
|
struct task_struct * const task = current; |
760 |
int error = 0; |
int error = 0; |
761 |
const unsigned int type = sk->sk_type; |
const unsigned int type = sk->sk_type; |
762 |
if (type != SOCK_DGRAM && type != SOCK_RAW) |
u16 port; |
763 |
|
bool is_ipv6; |
764 |
|
u8 operation; |
765 |
|
union { |
766 |
|
struct in6_addr sin6; |
767 |
|
struct in_addr sin4; |
768 |
|
} address; |
769 |
|
switch (type) { |
770 |
|
case SOCK_DGRAM: |
771 |
|
operation = CCS_NETWORK_UDP_CONNECT; |
772 |
|
break; |
773 |
|
case SOCK_RAW: |
774 |
|
operation = CCS_NETWORK_RAW_CONNECT; |
775 |
|
break; |
776 |
|
default: |
777 |
return 0; |
return 0; |
778 |
|
} |
779 |
/* Nothing to do if I am a kernel service. */ |
/* Nothing to do if I am a kernel service. */ |
780 |
if (segment_eq(get_fs(), KERNEL_DS)) |
if (segment_eq(get_fs(), KERNEL_DS)) |
781 |
return 0; |
return 0; |
|
|
|
782 |
switch (sk->sk_family) { |
switch (sk->sk_family) { |
|
struct in6_addr sin6; |
|
|
struct in_addr sin4; |
|
|
u16 port; |
|
783 |
case PF_INET6: |
case PF_INET6: |
784 |
if (type == SOCK_DGRAM) { /* UDP IPv6 */ |
is_ipv6 = true; |
785 |
if (skb->protocol == htons(ETH_P_IP)) { |
if (type == SOCK_DGRAM && skb->protocol == htons(ETH_P_IP)) |
786 |
ipv6_addr_set(&sin6, 0, 0, htonl(0xffff), |
ipv6_addr_set(&address.sin6, 0, 0, htonl(0xffff), |
787 |
ip_hdr(skb)->saddr); |
ip_hdr(skb)->saddr); |
788 |
} else { |
else |
789 |
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); |
|
790 |
break; |
break; |
791 |
case PF_INET: |
case PF_INET: |
792 |
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
is_ipv6 = false; |
793 |
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); |
|
794 |
break; |
break; |
795 |
|
default: |
796 |
|
goto skip; |
797 |
} |
} |
798 |
|
if (type == SOCK_DGRAM) |
799 |
|
port = udp_hdr(skb)->source; |
800 |
|
else |
801 |
|
port = htons(sk->sk_protocol); |
802 |
|
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
803 |
|
error = ccs_network_entry(is_ipv6, operation, (u32 *) &address, port); |
804 |
|
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
805 |
|
skip: |
806 |
if (!error) |
if (!error) |
807 |
return 0; |
return 0; |
808 |
/* |
/* |
811 |
* prevent the caller from picking up next message from wanted source |
* prevent the caller from picking up next message from wanted source |
812 |
* when the caller is using MSG_PEEK flag for picking up. |
* when the caller is using MSG_PEEK flag for picking up. |
813 |
*/ |
*/ |
814 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
{ |
815 |
if (type == SOCK_DGRAM) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35) |
816 |
lock_sock(sk); |
bool slow = false; |
817 |
|
if (type == SOCK_DGRAM) |
818 |
|
slow = lock_sock_fast(sk); |
819 |
|
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
820 |
|
if (type == SOCK_DGRAM) |
821 |
|
lock_sock(sk); |
822 |
#endif |
#endif |
823 |
skb_kill_datagram(sk, skb, flags); |
skb_kill_datagram(sk, skb, flags); |
824 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35) |
825 |
if (type == SOCK_DGRAM) |
if (type == SOCK_DGRAM) |
826 |
release_sock(sk); |
unlock_sock_fast(sk, slow); |
827 |
|
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
828 |
|
if (type == SOCK_DGRAM) |
829 |
|
release_sock(sk); |
830 |
#endif |
#endif |
831 |
|
} |
832 |
/* Hope less harmful than -EPERM. */ |
/* Hope less harmful than -EPERM. */ |
833 |
return -ENOMEM; |
return -ENOMEM; |
834 |
} |
} |
835 |
EXPORT_SYMBOL(ccs_socket_recvmsg_permission); |
|
836 |
|
void __init ccs_network_init(void) |
837 |
|
{ |
838 |
|
ccsecurity_ops.socket_create_permission = |
839 |
|
__ccs_socket_create_permission; |
840 |
|
ccsecurity_ops.socket_listen_permission = |
841 |
|
__ccs_socket_listen_permission; |
842 |
|
ccsecurity_ops.socket_connect_permission = |
843 |
|
__ccs_socket_connect_permission; |
844 |
|
ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission; |
845 |
|
ccsecurity_ops.socket_accept_permission = |
846 |
|
__ccs_socket_accept_permission; |
847 |
|
ccsecurity_ops.socket_sendmsg_permission = |
848 |
|
__ccs_socket_sendmsg_permission; |
849 |
|
ccsecurity_ops.socket_recvmsg_permission = |
850 |
|
__ccs_socket_recvmsg_permission; |
851 |
|
} |
852 |
|
|
853 |
|
#endif |