1 |
kumaneko |
111 |
/* |
2 |
|
|
* fs/tomoyo_capability.c |
3 |
|
|
* |
4 |
|
|
* Implementation of the Domain-Based Mandatory Access Control. |
5 |
|
|
* |
6 |
kumaneko |
849 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
kumaneko |
111 |
* |
8 |
kumaneko |
1052 |
* Version: 1.6.0-pre 2008/03/24 |
9 |
kumaneko |
111 |
* |
10 |
|
|
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
11 |
|
|
* See README.ccs for ChangeLog. |
12 |
|
|
* |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
#include <linux/ccs_common.h> |
16 |
|
|
#include <linux/tomoyo.h> |
17 |
|
|
#include <linux/realpath.h> |
18 |
|
|
|
19 |
kumaneko |
1052 |
/** |
20 |
|
|
* cap_operation2name - Convert capability operation to capability message. |
21 |
|
|
* |
22 |
|
|
* @operation: Type of operation. |
23 |
|
|
* |
24 |
|
|
* Returns the name of capability. |
25 |
|
|
*/ |
26 |
kumaneko |
851 |
static const char *cap_operation2name(const u8 operation) |
27 |
kumaneko |
111 |
{ |
28 |
kumaneko |
1015 |
static const char *capability_name[TOMOYO_MAX_CAPABILITY_INDEX] = { |
29 |
kumaneko |
1052 |
[TOMOYO_INET_STREAM_SOCKET_CREATE] = |
30 |
|
|
"socket(PF_INET, SOCK_STREAM)", |
31 |
|
|
[TOMOYO_INET_STREAM_SOCKET_LISTEN] = |
32 |
|
|
"listen(PF_INET, SOCK_STREAM)", |
33 |
|
|
[TOMOYO_INET_STREAM_SOCKET_CONNECT] = |
34 |
|
|
"connect(PF_INET, SOCK_STREAM)", |
35 |
|
|
[TOMOYO_USE_INET_DGRAM_SOCKET] = |
36 |
|
|
"socket(PF_INET, SOCK_DGRAM)", |
37 |
|
|
[TOMOYO_USE_INET_RAW_SOCKET] = |
38 |
|
|
"socket(PF_INET, SOCK_RAW)", |
39 |
kumaneko |
1015 |
[TOMOYO_USE_ROUTE_SOCKET] = "socket(PF_ROUTE)", |
40 |
|
|
[TOMOYO_USE_PACKET_SOCKET] = "socket(PF_PACKET)", |
41 |
|
|
[TOMOYO_SYS_MOUNT] = "sys_mount()", |
42 |
|
|
[TOMOYO_SYS_UMOUNT] = "sys_umount()", |
43 |
|
|
[TOMOYO_SYS_REBOOT] = "sys_reboot()", |
44 |
|
|
[TOMOYO_SYS_CHROOT] = "sys_chroot()", |
45 |
|
|
[TOMOYO_SYS_KILL] = "sys_kill()", |
46 |
|
|
[TOMOYO_SYS_VHANGUP] = "sys_vhangup()", |
47 |
|
|
[TOMOYO_SYS_SETTIME] = "sys_settimeofday()", |
48 |
|
|
[TOMOYO_SYS_NICE] = "sys_nice()", |
49 |
|
|
[TOMOYO_SYS_SETHOSTNAME] = "sys_sethostname()", |
50 |
|
|
[TOMOYO_USE_KERNEL_MODULE] = "kernel_module", |
51 |
|
|
[TOMOYO_CREATE_FIFO] = "mknod(FIFO)", |
52 |
|
|
[TOMOYO_CREATE_BLOCK_DEV] = "mknod(BDEV)", |
53 |
|
|
[TOMOYO_CREATE_CHAR_DEV] = "mknod(CDEV)", |
54 |
|
|
[TOMOYO_CREATE_UNIX_SOCKET] = "mknod(SOCKET)", |
55 |
|
|
[TOMOYO_SYS_LINK] = "sys_link()", |
56 |
|
|
[TOMOYO_SYS_SYMLINK] = "sys_symlink()", |
57 |
|
|
[TOMOYO_SYS_RENAME] = "sys_rename()", |
58 |
|
|
[TOMOYO_SYS_UNLINK] = "sys_unlink()", |
59 |
|
|
[TOMOYO_SYS_CHMOD] = "sys_chmod()", |
60 |
|
|
[TOMOYO_SYS_CHOWN] = "sys_chown()", |
61 |
|
|
[TOMOYO_SYS_IOCTL] = "sys_ioctl()", |
62 |
|
|
[TOMOYO_SYS_KEXEC_LOAD] = "sys_kexec_load()", |
63 |
|
|
[TOMOYO_SYS_PIVOT_ROOT] = "sys_pivot_root()", |
64 |
|
|
[TOMOYO_SYS_PTRACE] = "sys_ptrace()", |
65 |
|
|
}; |
66 |
kumaneko |
1052 |
if (operation < TOMOYO_MAX_CAPABILITY_INDEX) |
67 |
|
|
return capability_name[operation]; |
68 |
|
|
return NULL; |
69 |
kumaneko |
111 |
} |
70 |
|
|
|
71 |
kumaneko |
1052 |
/** |
72 |
|
|
* audit_capability_log - Audit capability log. |
73 |
|
|
* |
74 |
|
|
* @operation: Type of operation. |
75 |
|
|
* @is_granted: True if this is a granted log. |
76 |
|
|
* @profile: Profile number. |
77 |
|
|
* @mode: Access control mode. |
78 |
|
|
* |
79 |
|
|
* Returns 0 on success, negative value otherwise. |
80 |
|
|
*/ |
81 |
|
|
static int audit_capability_log(const u8 operation, const bool is_granted, |
82 |
|
|
const u8 profile, const u8 mode) |
83 |
kumaneko |
111 |
{ |
84 |
|
|
char *buf; |
85 |
|
|
int len = 64; |
86 |
kumaneko |
1052 |
int len2; |
87 |
|
|
if (ccs_can_save_audit_log(is_granted) < 0) |
88 |
|
|
return -ENOMEM; |
89 |
|
|
buf = ccs_init_audit_log(&len, profile, mode, NULL); |
90 |
|
|
if (!buf) |
91 |
|
|
return -ENOMEM; |
92 |
|
|
len2 = strlen(buf); |
93 |
|
|
snprintf(buf + len2, len - len2 - 1, KEYWORD_ALLOW_CAPABILITY "%s\n", |
94 |
|
|
ccs_cap2keyword(operation)); |
95 |
|
|
return ccs_write_audit_log(buf, is_granted); |
96 |
kumaneko |
111 |
} |
97 |
|
|
|
98 |
kumaneko |
1052 |
/** |
99 |
|
|
* update_capability_acl - Update "struct capability_acl_record" list. |
100 |
|
|
* |
101 |
|
|
* @operation: Type of operation. |
102 |
|
|
* @domain: Pointer to "struct domain_info". |
103 |
|
|
* @condition: Pointer to "struct condition_list". May be NULL. |
104 |
|
|
* @is_delete: True if it is a delete request. |
105 |
|
|
* |
106 |
|
|
* Returns 0 on success, negative value otherwise. |
107 |
|
|
*/ |
108 |
|
|
static int update_capability_acl(const u8 operation, struct domain_info *domain, |
109 |
|
|
const struct condition_list *condition, |
110 |
|
|
const bool is_delete) |
111 |
kumaneko |
111 |
{ |
112 |
|
|
struct acl_info *ptr; |
113 |
kumaneko |
708 |
struct capability_acl_record *acl; |
114 |
kumaneko |
111 |
int error = -ENOMEM; |
115 |
kumaneko |
1052 |
if (!domain) |
116 |
|
|
return -EINVAL; |
117 |
kumaneko |
652 |
mutex_lock(&domain_acl_lock); |
118 |
kumaneko |
1052 |
if (is_delete) |
119 |
|
|
goto delete; |
120 |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
121 |
|
|
if ((ptr->type & ~(ACL_DELETED | ACL_WITH_CONDITION)) |
122 |
|
|
!= TYPE_CAPABILITY_ACL) |
123 |
|
|
continue; |
124 |
|
|
if (ccs_get_condition_part(ptr) != condition) |
125 |
|
|
continue; |
126 |
|
|
acl = container_of(ptr, struct capability_acl_record, head); |
127 |
|
|
if (acl->operation != operation) |
128 |
|
|
continue; |
129 |
|
|
error = ccs_add_domain_acl(NULL, ptr); |
130 |
|
|
goto out; |
131 |
kumaneko |
111 |
} |
132 |
kumaneko |
1052 |
/* Not found. Append it to the tail. */ |
133 |
|
|
acl = ccs_alloc_acl_element(TYPE_CAPABILITY_ACL, condition); |
134 |
|
|
if (!acl) |
135 |
|
|
goto out; |
136 |
|
|
acl->operation = operation; |
137 |
|
|
error = ccs_add_domain_acl(domain, &acl->head); |
138 |
|
|
goto out; |
139 |
|
|
delete: |
140 |
|
|
error = -ENOENT; |
141 |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
142 |
|
|
if ((ptr->type & ~ACL_WITH_CONDITION) != TYPE_CAPABILITY_ACL) |
143 |
|
|
continue; |
144 |
|
|
if (ccs_get_condition_part(ptr) != condition) |
145 |
|
|
continue; |
146 |
|
|
acl = container_of(ptr, struct capability_acl_record, head); |
147 |
|
|
if (acl->operation != operation) |
148 |
|
|
continue; |
149 |
|
|
error = ccs_del_domain_acl(ptr); |
150 |
|
|
break; |
151 |
|
|
} |
152 |
|
|
out: |
153 |
kumaneko |
652 |
mutex_unlock(&domain_acl_lock); |
154 |
kumaneko |
111 |
return error; |
155 |
|
|
} |
156 |
|
|
|
157 |
kumaneko |
1052 |
/** |
158 |
|
|
* ccs_capable - Check permission for capability. |
159 |
|
|
* |
160 |
|
|
* @operation: Type of operation. |
161 |
|
|
* |
162 |
kumaneko |
1054 |
* Returns true on success, false otherwise. |
163 |
kumaneko |
1052 |
*/ |
164 |
kumaneko |
1054 |
bool ccs_capable(const u8 operation) |
165 |
kumaneko |
111 |
{ |
166 |
|
|
struct domain_info * const domain = current->domain_info; |
167 |
|
|
struct acl_info *ptr; |
168 |
kumaneko |
815 |
const u8 profile = current->domain_info->profile; |
169 |
kumaneko |
1052 |
const u8 mode = ccs_check_capability_flags(operation); |
170 |
kumaneko |
815 |
const bool is_enforce = (mode == 3); |
171 |
kumaneko |
1016 |
bool found = false; |
172 |
kumaneko |
1052 |
if (!mode) |
173 |
kumaneko |
1054 |
return true; |
174 |
kumaneko |
722 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
175 |
kumaneko |
708 |
struct capability_acl_record *acl; |
176 |
kumaneko |
1052 |
if ((ptr->type & ~ACL_WITH_CONDITION) != TYPE_CAPABILITY_ACL) |
177 |
|
|
continue; |
178 |
kumaneko |
912 |
acl = container_of(ptr, struct capability_acl_record, head); |
179 |
kumaneko |
1052 |
if (acl->operation != operation || |
180 |
|
|
!ccs_check_condition(ptr, NULL)) |
181 |
|
|
continue; |
182 |
|
|
ccs_update_condition(ptr); |
183 |
kumaneko |
1016 |
found = true; |
184 |
kumaneko |
856 |
break; |
185 |
kumaneko |
111 |
} |
186 |
kumaneko |
1052 |
audit_capability_log(operation, found, profile, mode); |
187 |
|
|
if (found) |
188 |
kumaneko |
1054 |
return true; |
189 |
kumaneko |
1052 |
if (ccs_verbose_mode()) |
190 |
|
|
printk(KERN_WARNING "TOMOYO-%s: %s denied for %s\n", |
191 |
|
|
ccs_get_msg(is_enforce), cap_operation2name(operation), |
192 |
|
|
ccs_get_last_name(domain)); |
193 |
|
|
if (is_enforce) |
194 |
kumaneko |
1054 |
return !ccs_check_supervisor("%s\n" |
195 |
kumaneko |
1052 |
KEYWORD_ALLOW_CAPABILITY "%s\n", |
196 |
|
|
domain->domainname->name, |
197 |
|
|
ccs_cap2keyword(operation)); |
198 |
|
|
else if (mode == 1 && ccs_check_domain_quota(domain)) |
199 |
|
|
update_capability_acl(operation, domain, NULL, false); |
200 |
kumaneko |
1054 |
return true; |
201 |
kumaneko |
111 |
} |
202 |
kumaneko |
1052 |
/* I need to export this for net/unix/af_unix.c */ |
203 |
|
|
EXPORT_SYMBOL(ccs_capable); |
204 |
kumaneko |
111 |
|
205 |
kumaneko |
1052 |
/** |
206 |
|
|
* ccs_write_capability_policy - Write "struct capability_acl_record" list. |
207 |
|
|
* |
208 |
|
|
* @data: String to parse. |
209 |
|
|
* @domain: Pointer to "struct domain_info". |
210 |
|
|
* @condition: Pointer to "struct condition_list". May be NULL. |
211 |
|
|
* @is_delete: True if it is a delete request. |
212 |
|
|
* |
213 |
|
|
* Returns 0 on success, negative value otherwise. |
214 |
|
|
*/ |
215 |
|
|
int ccs_write_capability_policy(char *data, struct domain_info *domain, |
216 |
|
|
const struct condition_list *condition, |
217 |
|
|
const bool is_delete) |
218 |
kumaneko |
111 |
{ |
219 |
kumaneko |
851 |
u8 capability; |
220 |
kumaneko |
1052 |
for (capability = 0; capability < TOMOYO_MAX_CAPABILITY_INDEX; |
221 |
|
|
capability++) { |
222 |
|
|
if (strcmp(data, ccs_cap2keyword(capability))) |
223 |
|
|
continue; |
224 |
|
|
return update_capability_acl(capability, domain, condition, |
225 |
|
|
is_delete); |
226 |
kumaneko |
111 |
} |
227 |
|
|
return -EINVAL; |
228 |
|
|
} |