1 |
/* |
/* |
2 |
* fs/tomoyo_domain.c |
* security/ccsecurity/domain.c |
3 |
* |
* |
4 |
* Implementation of the Domain-Based Mandatory Access Control. |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
5 |
* |
* |
6 |
* Copyright (C) 2005-2007 NTT DATA CORPORATION |
* Version: 1.7.0-pre 2009/08/08 |
|
* |
|
|
* Version: 1.5.0-pre 2007/08/06 |
|
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 |
*/ |
*/ |
|
/***** TOMOYO Linux start. *****/ |
|
12 |
|
|
13 |
#include <linux/ccs_common.h> |
#include <linux/slab.h> |
|
#include <linux/tomoyo.h> |
|
|
#include <linux/realpath.h> |
|
14 |
#include <linux/highmem.h> |
#include <linux/highmem.h> |
15 |
#include <linux/binfmts.h> |
#include <linux/version.h> |
16 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
17 |
|
#include <linux/namei.h> |
18 |
|
#include <linux/mount.h> |
19 |
|
#endif |
20 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30) |
21 |
|
#include <linux/fs_struct.h> |
22 |
|
#endif |
23 |
|
#include "internal.h" |
24 |
|
|
25 |
|
/* For compatibility with older kernels. */ |
26 |
#ifndef for_each_process |
#ifndef for_each_process |
27 |
#define for_each_process for_each_task |
#define for_each_process for_each_task |
28 |
#endif |
#endif |
29 |
|
|
30 |
/************************* VARIABLES *************************/ |
/* Variables definitions.*/ |
31 |
|
|
32 |
/* The initial domain. */ |
/* The initial domain. */ |
33 |
struct domain_info KERNEL_DOMAIN = { NULL, NULL, NULL, 0, 0, 0 }; |
struct ccs_domain_info ccs_kernel_domain; |
|
|
|
|
/* /sbin/init started? */ |
|
|
extern int sbin_init_started; |
|
|
|
|
|
#ifdef CONFIG_TOMOYO |
|
|
|
|
|
/* Lock for appending domain's ACL. */ |
|
|
DECLARE_MUTEX(domain_acl_lock); |
|
|
|
|
|
/************************* UTILITY FUNCTIONS *************************/ |
|
|
|
|
|
/***** The structure for program files to force domain reconstruction. *****/ |
|
|
|
|
|
struct domain_initializer_entry { |
|
|
struct domain_initializer_entry *next; |
|
|
const struct path_info *domainname; /* This may be NULL */ |
|
|
const struct path_info *program; |
|
|
u8 is_deleted; |
|
|
u8 is_not; |
|
|
u8 is_last_name; |
|
|
u8 is_oldstyle; |
|
|
}; |
|
|
|
|
|
/***** The structure for domains to not to transit domains. *****/ |
|
|
|
|
|
struct domain_keeper_entry { |
|
|
struct domain_keeper_entry *next; |
|
|
const struct path_info *domainname; |
|
|
const struct path_info *program; /* This may be NULL */ |
|
|
u8 is_deleted; |
|
|
u8 is_not; |
|
|
u8 is_last_name; |
|
|
}; |
|
34 |
|
|
35 |
/***** The structure for program files that should be aggregated. *****/ |
/* The list for "struct ccs_domain_info". */ |
36 |
|
LIST_HEAD(ccs_domain_list); |
37 |
|
|
38 |
struct aggregator_entry { |
/** |
39 |
struct aggregator_entry *next; |
* ccs_get_last_name - Get last component of a domainname. |
40 |
const struct path_info *original_name; |
* |
41 |
const struct path_info *aggregated_name; |
* @domain: Pointer to "struct ccs_domain_info". |
42 |
int is_deleted; |
* |
43 |
}; |
* Returns the last component of the domainname. |
44 |
|
*/ |
45 |
/***** The structure for program files that should be aliased. *****/ |
const char *ccs_get_last_name(const struct ccs_domain_info *domain) |
|
|
|
|
struct alias_entry { |
|
|
struct alias_entry *next; |
|
|
const struct path_info *original_name; |
|
|
const struct path_info *aliased_name; |
|
|
int is_deleted; |
|
|
}; |
|
|
|
|
|
/************************* VARIABLES *************************/ |
|
|
|
|
|
/* Domain creation lock. */ |
|
|
static DECLARE_MUTEX(new_domain_assign_lock); |
|
|
|
|
|
/************************* UTILITY FUNCTIONS *************************/ |
|
|
|
|
|
int IsDomainDef(const unsigned char *buffer) |
|
|
{ |
|
|
/* while (*buffer && (*buffer <= ' ' || *buffer >= 127)) buffer++; */ |
|
|
return strncmp(buffer, ROOT_NAME, ROOT_NAME_LEN) == 0; |
|
|
} |
|
|
|
|
|
const char *GetLastName(const struct domain_info *domain) |
|
46 |
{ |
{ |
47 |
const char *cp0 = domain->domainname->name, *cp1; |
const char *cp0 = domain->domainname->name; |
48 |
if ((cp1 = strrchr(cp0, ' ')) != NULL) return cp1 + 1; |
const char *cp1 = strrchr(cp0, ' '); |
49 |
|
if (cp1) |
50 |
|
return cp1 + 1; |
51 |
return cp0; |
return cp0; |
52 |
} |
} |
53 |
|
|
54 |
int ReadSelfDomain(struct io_buffer *head) |
/** |
55 |
{ |
* ccs_audit_execute_handler_log - Audit execute_handler log. |
56 |
if (!head->read_eof) { |
* |
57 |
io_printf(head, "%s", current->domain_info->domainname->name); |
* @ee: Pointer to "struct ccs_execve_entry". |
58 |
head->read_eof = 1; |
* @is_default: True if it is "execute_handler" log. |
59 |
} |
* |
60 |
return 0; |
* Returns 0 on success, negative value otherwise. |
61 |
} |
*/ |
62 |
|
static int ccs_audit_execute_handler_log(struct ccs_execve_entry *ee, |
63 |
int AddDomainACL(struct acl_info *ptr, struct domain_info *domain, struct acl_info *new_ptr) |
const bool is_default) |
64 |
{ |
{ |
65 |
mb(); /* Instead of using spinlock. */ |
struct ccs_request_info *r = &ee->r; |
66 |
if (!ptr) domain->first_acl_ptr = (struct acl_info *) new_ptr; |
const char *handler = ee->handler->name; |
67 |
else ptr->next = (struct acl_info *) new_ptr; |
r->mode = ccs_flags(r->domain, CCS_MAC_EXECUTE); |
68 |
UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY); |
return ccs_write_audit_log(true, r, "%s %s\n", |
69 |
return 0; |
is_default ? CCS_KEYWORD_EXECUTE_HANDLER : |
70 |
|
CCS_KEYWORD_DENIED_EXECUTE_HANDLER, handler); |
71 |
} |
} |
72 |
|
|
73 |
int DelDomainACL(struct acl_info *ptr) |
/** |
74 |
|
* ccs_audit_domain_creation_log - Audit domain creation log. |
75 |
|
* |
76 |
|
* @domain: Pointer to "struct ccs_domain_info". |
77 |
|
* |
78 |
|
* Returns 0 on success, negative value otherwise. |
79 |
|
*/ |
80 |
|
static int ccs_audit_domain_creation_log(struct ccs_domain_info *domain) |
81 |
{ |
{ |
82 |
ptr->is_deleted = 1; |
int error; |
83 |
UpdateCounter(CCS_UPDATES_COUNTER_DOMAIN_POLICY); |
struct ccs_request_info r; |
84 |
return 0; |
ccs_init_request_info(&r, domain, CCS_MAC_EXECUTE); |
85 |
} |
error = ccs_write_audit_log(false, &r, "use_profile %u\n", r.profile); |
86 |
|
return error; |
|
int TooManyDomainACL(struct domain_info * const domain) { |
|
|
unsigned int count = 0; |
|
|
struct acl_info *ptr; |
|
|
for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) { |
|
|
if (!ptr->is_deleted) count++; |
|
|
} |
|
|
/* If there are so many entries, don't append if learning mode. */ |
|
|
if (count < CheckCCSFlags(CCS_TOMOYO_MAX_ACCEPT_ENTRY)) return 0; |
|
|
if (!domain->quota_warned) { |
|
|
printk("TOMOYO-WARNING: Domain '%s' has so many ACLs to hold. Stopped learning mode.\n", domain->domainname->name); |
|
|
domain->quota_warned = 1; |
|
|
} |
|
|
return 1; |
|
87 |
} |
} |
88 |
|
|
89 |
|
/* The list for "struct ccs_domain_initializer_entry". */ |
90 |
|
LIST_HEAD(ccs_domain_initializer_list); |
91 |
|
|
92 |
/************************* DOMAIN INITIALIZER HANDLER *************************/ |
/** |
93 |
|
* ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer_entry" list. |
94 |
static struct domain_initializer_entry *domain_initializer_list = NULL; |
* |
95 |
|
* @domainname: The name of domain. May be NULL. |
96 |
static int AddDomainInitializerEntry(const char *domainname, const char *program, const int is_not, const int is_delete, const int is_oldstyle) |
* @program: The name of program. |
97 |
{ |
* @is_not: True if it is "no_initialize_domain" entry. |
98 |
struct domain_initializer_entry *new_entry, *ptr; |
* @is_delete: True if it is a delete request. |
99 |
static DECLARE_MUTEX(lock); |
* |
100 |
const struct path_info *saved_program, *saved_domainname = NULL; |
* Returns 0 on success, negative value otherwise. |
101 |
int error = -ENOMEM; |
*/ |
102 |
int is_last_name = 0; |
static int ccs_update_domain_initializer_entry(const char *domainname, |
103 |
if (!IsCorrectPath(program, 1, -1, -1, __FUNCTION__)) return -EINVAL; /* No patterns allowed. */ |
const char *program, |
104 |
|
const bool is_not, |
105 |
|
const bool is_delete) |
106 |
|
{ |
107 |
|
struct ccs_domain_initializer_entry *entry = NULL; |
108 |
|
struct ccs_domain_initializer_entry *ptr; |
109 |
|
struct ccs_domain_initializer_entry e = { .is_not = is_not }; |
110 |
|
int error = is_delete ? -ENOENT : -ENOMEM; |
111 |
|
if (!ccs_is_correct_path(program, 1, -1, -1)) |
112 |
|
return -EINVAL; /* No patterns allowed. */ |
113 |
if (domainname) { |
if (domainname) { |
114 |
if (!IsDomainDef(domainname) && IsCorrectPath(domainname, 1, -1, -1, __FUNCTION__)) { |
if (!ccs_is_domain_def(domainname) && |
115 |
is_last_name = 1; |
ccs_is_correct_path(domainname, 1, -1, -1)) |
116 |
} else if (!IsCorrectDomain(domainname, __FUNCTION__)) { |
e.is_last_name = true; |
117 |
|
else if (!ccs_is_correct_domain(domainname)) |
118 |
return -EINVAL; |
return -EINVAL; |
119 |
} |
e.domainname = ccs_get_name(domainname); |
120 |
if ((saved_domainname = SaveName(domainname)) == NULL) return -ENOMEM; |
if (!e.domainname) |
|
} |
|
|
if ((saved_program = SaveName(program)) == NULL) return -ENOMEM; |
|
|
down(&lock); |
|
|
for (ptr = domain_initializer_list; ptr; ptr = ptr->next) { |
|
|
if (ptr->is_not == is_not && ptr->is_oldstyle == is_oldstyle && ptr->domainname == saved_domainname && ptr->program == saved_program) { |
|
|
ptr->is_deleted = is_delete; |
|
|
error = 0; |
|
121 |
goto out; |
goto out; |
|
} |
|
122 |
} |
} |
123 |
if (is_delete) { |
e.program = ccs_get_name(program); |
124 |
error = -ENOENT; |
if (!e.program) |
125 |
goto out; |
goto out; |
126 |
|
if (!is_delete) |
127 |
|
entry = kmalloc(sizeof(e), GFP_KERNEL); |
128 |
|
mutex_lock(&ccs_policy_lock); |
129 |
|
list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) { |
130 |
|
if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not), |
131 |
|
sizeof(e))) |
132 |
|
continue; |
133 |
|
ptr->is_deleted = is_delete; |
134 |
|
error = 0; |
135 |
|
break; |
136 |
} |
} |
137 |
if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out; |
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
138 |
new_entry->domainname = saved_domainname; |
list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list); |
139 |
new_entry->program = saved_program; |
entry = NULL; |
140 |
new_entry->is_not = is_not; |
error = 0; |
|
new_entry->is_last_name = is_last_name; |
|
|
new_entry->is_oldstyle = is_oldstyle; |
|
|
mb(); /* Instead of using spinlock. */ |
|
|
if ((ptr = domain_initializer_list) != NULL) { |
|
|
while (ptr->next) ptr = ptr->next; ptr->next = new_entry; |
|
|
} else { |
|
|
domain_initializer_list = new_entry; |
|
141 |
} |
} |
142 |
error = 0; |
mutex_unlock(&ccs_policy_lock); |
143 |
out: |
out: |
144 |
up(&lock); |
ccs_put_name(e.domainname); |
145 |
|
ccs_put_name(e.program); |
146 |
|
kfree(entry); |
147 |
return error; |
return error; |
148 |
} |
} |
149 |
|
|
150 |
int ReadDomainInitializerPolicy(struct io_buffer *head) |
/** |
151 |
|
* ccs_read_domain_initializer_policy - Read "struct ccs_domain_initializer_entry" list. |
152 |
|
* |
153 |
|
* @head: Pointer to "struct ccs_io_buffer". |
154 |
|
* |
155 |
|
* Returns true on success, false otherwise. |
156 |
|
* |
157 |
|
* Caller holds ccs_read_lock(). |
158 |
|
*/ |
159 |
|
bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head) |
160 |
{ |
{ |
161 |
struct domain_initializer_entry *ptr = head->read_var2; |
struct list_head *pos; |
162 |
if (!ptr) ptr = domain_initializer_list; |
bool done = true; |
163 |
while (ptr) { |
ccs_assert_read_lock(); |
164 |
head->read_var2 = ptr; |
list_for_each_cookie(pos, head->read_var2, |
165 |
if (!ptr->is_deleted) { |
&ccs_domain_initializer_list) { |
166 |
if (ptr->domainname) { |
const char *no; |
167 |
if (io_printf(head, "%s%s%s from %s\n", ptr->is_not ? "no_" : "", ptr->is_oldstyle ? KEYWORD_INITIALIZER : KEYWORD_INITIALIZE_DOMAIN, ptr->program->name, ptr->domainname->name)) break; |
const char *from = ""; |
168 |
} else { |
const char *domain = ""; |
169 |
if (io_printf(head, "%s%s%s\n", ptr->is_not ? "no_" : "", ptr->is_oldstyle ? KEYWORD_INITIALIZER : KEYWORD_INITIALIZE_DOMAIN, ptr->program->name)) break; |
struct ccs_domain_initializer_entry *ptr; |
170 |
} |
ptr = list_entry(pos, struct ccs_domain_initializer_entry, |
171 |
|
list); |
172 |
|
if (ptr->is_deleted) |
173 |
|
continue; |
174 |
|
no = ptr->is_not ? "no_" : ""; |
175 |
|
if (ptr->domainname) { |
176 |
|
from = " from "; |
177 |
|
domain = ptr->domainname->name; |
178 |
} |
} |
179 |
ptr = ptr->next; |
done = ccs_io_printf(head, |
180 |
|
"%s" CCS_KEYWORD_INITIALIZE_DOMAIN "%s%s%s\n", |
181 |
|
no, ptr->program->name, from, domain); |
182 |
|
if (!done) |
183 |
|
break; |
184 |
} |
} |
185 |
return ptr ? -ENOMEM : 0; |
return done; |
186 |
} |
} |
187 |
|
|
188 |
int AddDomainInitializerPolicy(char *data, const int is_not, const int is_delete, const int is_oldstyle) |
/** |
189 |
|
* ccs_write_domain_initializer_policy - Write "struct ccs_domain_initializer_entry" list. |
190 |
|
* |
191 |
|
* @data: String to parse. |
192 |
|
* @is_not: True if it is "no_initialize_domain" entry. |
193 |
|
* @is_delete: True if it is a delete request. |
194 |
|
* |
195 |
|
* Returns 0 on success, negative value otherwise. |
196 |
|
*/ |
197 |
|
int ccs_write_domain_initializer_policy(char *data, const bool is_not, |
198 |
|
const bool is_delete) |
199 |
{ |
{ |
200 |
char *cp = strstr(data, " from "); |
char *cp = strstr(data, " from "); |
201 |
if (cp) { |
if (cp) { |
202 |
*cp = '\0'; |
*cp = '\0'; |
203 |
return AddDomainInitializerEntry(cp + 6, data, is_not, is_delete, is_oldstyle); |
return ccs_update_domain_initializer_entry(cp + 6, data, |
204 |
} else { |
is_not, is_delete); |
|
return AddDomainInitializerEntry(NULL, data, is_not, is_delete, is_oldstyle); |
|
205 |
} |
} |
206 |
|
return ccs_update_domain_initializer_entry(NULL, data, is_not, |
207 |
|
is_delete); |
208 |
} |
} |
209 |
|
|
210 |
static int IsDomainInitializer(const struct path_info *domainname, const struct path_info *program, const struct path_info *last_name) |
/** |
211 |
{ |
* ccs_is_domain_initializer - Check whether the given program causes domainname reinitialization. |
212 |
struct domain_initializer_entry *ptr; |
* |
213 |
int flag = 0; |
* @domainname: The name of domain. |
214 |
for (ptr = domain_initializer_list; ptr; ptr = ptr->next) { |
* @program: The name of program. |
215 |
if (ptr->is_deleted ) continue; |
* @last_name: The last component of @domainname. |
216 |
|
* |
217 |
|
* Returns true if executing @program reinitializes domain transition, |
218 |
|
* false otherwise. |
219 |
|
* |
220 |
|
* Caller holds ccs_read_lock(). |
221 |
|
*/ |
222 |
|
static bool ccs_is_domain_initializer(const struct ccs_path_info *domainname, |
223 |
|
const struct ccs_path_info *program, |
224 |
|
const struct ccs_path_info *last_name) |
225 |
|
{ |
226 |
|
struct ccs_domain_initializer_entry *ptr; |
227 |
|
bool flag = false; |
228 |
|
ccs_assert_read_lock(); |
229 |
|
list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) { |
230 |
|
if (ptr->is_deleted) |
231 |
|
continue; |
232 |
if (ptr->domainname) { |
if (ptr->domainname) { |
233 |
if (!ptr->is_last_name) { |
if (!ptr->is_last_name) { |
234 |
if (ptr->domainname != domainname) continue; |
if (ptr->domainname != domainname) |
235 |
|
continue; |
236 |
} else { |
} else { |
237 |
if (pathcmp(ptr->domainname, last_name)) continue; |
if (ccs_pathcmp(ptr->domainname, last_name)) |
238 |
|
continue; |
239 |
} |
} |
240 |
} |
} |
241 |
if (pathcmp(ptr->program, program)) continue; |
if (ccs_pathcmp(ptr->program, program)) |
242 |
if (ptr->is_not) return 0; |
continue; |
243 |
flag = 1; |
if (ptr->is_not) { |
244 |
|
flag = false; |
245 |
|
break; |
246 |
|
} |
247 |
|
flag = true; |
248 |
} |
} |
249 |
return flag; |
return flag; |
250 |
} |
} |
251 |
|
|
252 |
/************************* DOMAIN KEEPER HANDLER *************************/ |
/* The list for "struct ccs_domain_keeper_entry". */ |
253 |
|
LIST_HEAD(ccs_domain_keeper_list); |
254 |
|
|
255 |
static struct domain_keeper_entry *domain_keeper_list = NULL; |
/** |
256 |
|
* ccs_update_domain_keeper_entry - Update "struct ccs_domain_keeper_entry" list. |
257 |
static int AddDomainKeeperEntry(const char *domainname, const char *program, const int is_not, const int is_delete) |
* |
258 |
{ |
* @domainname: The name of domain. |
259 |
struct domain_keeper_entry *new_entry, *ptr; |
* @program: The name of program. May be NULL. |
260 |
const struct path_info *saved_domainname, *saved_program = NULL; |
* @is_not: True if it is "no_keep_domain" entry. |
261 |
static DECLARE_MUTEX(lock); |
* @is_delete: True if it is a delete request. |
262 |
int error = -ENOMEM; |
* |
263 |
int is_last_name = 0; |
* Returns 0 on success, negative value otherwise. |
264 |
if (!IsDomainDef(domainname) && IsCorrectPath(domainname, 1, -1, -1, __FUNCTION__)) { |
*/ |
265 |
is_last_name = 1; |
static int ccs_update_domain_keeper_entry(const char *domainname, |
266 |
} else if (!IsCorrectDomain(domainname, __FUNCTION__)) { |
const char *program, |
267 |
|
const bool is_not, |
268 |
|
const bool is_delete) |
269 |
|
{ |
270 |
|
struct ccs_domain_keeper_entry *entry = NULL; |
271 |
|
struct ccs_domain_keeper_entry *ptr; |
272 |
|
struct ccs_domain_keeper_entry e = { .is_not = is_not }; |
273 |
|
int error = is_delete ? -ENOENT : -ENOMEM; |
274 |
|
if (!ccs_is_domain_def(domainname) && |
275 |
|
ccs_is_correct_path(domainname, 1, -1, -1)) |
276 |
|
e.is_last_name = true; |
277 |
|
else if (!ccs_is_correct_domain(domainname)) |
278 |
return -EINVAL; |
return -EINVAL; |
|
} |
|
279 |
if (program) { |
if (program) { |
280 |
if (!IsCorrectPath(program, 1, -1, -1, __FUNCTION__)) return -EINVAL; |
if (!ccs_is_correct_path(program, 1, -1, -1)) |
281 |
if ((saved_program = SaveName(program)) == NULL) return -ENOMEM; |
return -EINVAL; |
282 |
} |
e.program = ccs_get_name(program); |
283 |
if ((saved_domainname = SaveName(domainname)) == NULL) return -ENOMEM; |
if (!e.program) |
|
down(&lock); |
|
|
for (ptr = domain_keeper_list; ptr; ptr = ptr->next) { |
|
|
if (ptr->is_not == is_not && ptr->domainname == saved_domainname && ptr->program == saved_program) { |
|
|
ptr->is_deleted = is_delete; |
|
|
error = 0; |
|
284 |
goto out; |
goto out; |
|
} |
|
285 |
} |
} |
286 |
if (is_delete) { |
e.domainname = ccs_get_name(domainname); |
287 |
error = -ENOENT; |
if (!e.domainname) |
288 |
goto out; |
goto out; |
289 |
|
if (!is_delete) |
290 |
|
entry = kmalloc(sizeof(e), GFP_KERNEL); |
291 |
|
mutex_lock(&ccs_policy_lock); |
292 |
|
list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) { |
293 |
|
if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not), |
294 |
|
sizeof(e))) |
295 |
|
continue; |
296 |
|
ptr->is_deleted = is_delete; |
297 |
|
error = 0; |
298 |
|
break; |
299 |
} |
} |
300 |
if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out; |
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
301 |
new_entry->domainname = saved_domainname; |
list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list); |
302 |
new_entry->program = saved_program; |
entry = NULL; |
303 |
new_entry->is_not = is_not; |
error = 0; |
|
new_entry->is_last_name = is_last_name; |
|
|
mb(); /* Instead of using spinlock. */ |
|
|
if ((ptr = domain_keeper_list) != NULL) { |
|
|
while (ptr->next) ptr = ptr->next; ptr->next = new_entry; |
|
|
} else { |
|
|
domain_keeper_list = new_entry; |
|
304 |
} |
} |
305 |
error = 0; |
mutex_unlock(&ccs_policy_lock); |
306 |
out: |
out: |
307 |
up(&lock); |
ccs_put_name(e.domainname); |
308 |
|
ccs_put_name(e.program); |
309 |
|
kfree(entry); |
310 |
return error; |
return error; |
311 |
} |
} |
312 |
|
|
313 |
int AddDomainKeeperPolicy(char *data, const int is_not, const int is_delete) |
/** |
314 |
|
* ccs_write_domain_keeper_policy - Write "struct ccs_domain_keeper_entry" list. |
315 |
|
* |
316 |
|
* @data: String to parse. |
317 |
|
* @is_not: True if it is "no_keep_domain" entry. |
318 |
|
* @is_delete: True if it is a delete request. |
319 |
|
* |
320 |
|
*/ |
321 |
|
int ccs_write_domain_keeper_policy(char *data, const bool is_not, |
322 |
|
const bool is_delete) |
323 |
{ |
{ |
324 |
char *cp = strstr(data, " from "); |
char *cp = strstr(data, " from "); |
325 |
if (cp) { |
if (cp) { |
326 |
*cp = '\0'; |
*cp = '\0'; |
327 |
return AddDomainKeeperEntry(cp + 6, data, is_not, is_delete); |
return ccs_update_domain_keeper_entry(cp + 6, data, |
328 |
} else { |
is_not, is_delete); |
|
return AddDomainKeeperEntry(data, NULL, is_not, is_delete); |
|
329 |
} |
} |
330 |
|
return ccs_update_domain_keeper_entry(data, NULL, is_not, is_delete); |
331 |
} |
} |
332 |
|
|
333 |
int ReadDomainKeeperPolicy(struct io_buffer *head) |
/** |
334 |
|
* ccs_read_domain_keeper_policy - Read "struct ccs_domain_keeper_entry" list. |
335 |
|
* |
336 |
|
* @head: Pointer to "struct ccs_io_buffer". |
337 |
|
* |
338 |
|
* Returns true on success, false otherwise. |
339 |
|
* |
340 |
|
* Caller holds ccs_read_lock(). |
341 |
|
*/ |
342 |
|
bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head) |
343 |
{ |
{ |
344 |
struct domain_keeper_entry *ptr = head->read_var2; |
struct list_head *pos; |
345 |
if (!ptr) ptr = domain_keeper_list; |
bool done = true; |
346 |
while (ptr) { |
ccs_assert_read_lock(); |
347 |
head->read_var2 = ptr; |
list_for_each_cookie(pos, head->read_var2, |
348 |
if (!ptr->is_deleted) { |
&ccs_domain_keeper_list) { |
349 |
if (ptr->program) { |
struct ccs_domain_keeper_entry *ptr; |
350 |
if (io_printf(head, "%s" KEYWORD_KEEP_DOMAIN "%s from %s\n", ptr->is_not ? "no_" : "", ptr->program->name, ptr->domainname->name)) break; |
const char *no; |
351 |
} else { |
const char *from = ""; |
352 |
if (io_printf(head, "%s" KEYWORD_KEEP_DOMAIN "%s\n", ptr->is_not ? "no_" : "", ptr->domainname->name)) break; |
const char *program = ""; |
353 |
} |
ptr = list_entry(pos, struct ccs_domain_keeper_entry, list); |
354 |
|
if (ptr->is_deleted) |
355 |
|
continue; |
356 |
|
no = ptr->is_not ? "no_" : ""; |
357 |
|
if (ptr->program) { |
358 |
|
from = " from "; |
359 |
|
program = ptr->program->name; |
360 |
} |
} |
361 |
ptr = ptr->next; |
done = ccs_io_printf(head, "%s" CCS_KEYWORD_KEEP_DOMAIN |
362 |
|
"%s%s%s\n", no, program, from, |
363 |
|
ptr->domainname->name); |
364 |
|
if (!done) |
365 |
|
break; |
366 |
} |
} |
367 |
return ptr ? -ENOMEM : 0; |
return done; |
368 |
} |
} |
369 |
|
|
370 |
static int IsDomainKeeper(const struct path_info *domainname, const struct path_info *program, const struct path_info *last_name) |
/** |
371 |
{ |
* ccs_is_domain_keeper - Check whether the given program causes domain transition suppression. |
372 |
struct domain_keeper_entry *ptr; |
* |
373 |
int flag = 0; |
* @domainname: The name of domain. |
374 |
for (ptr = domain_keeper_list; ptr; ptr = ptr->next) { |
* @program: The name of program. |
375 |
if (ptr->is_deleted) continue; |
* @last_name: The last component of @domainname. |
376 |
|
* |
377 |
|
* Returns true if executing @program supresses domain transition, |
378 |
|
* false otherwise. |
379 |
|
* |
380 |
|
* Caller holds ccs_read_lock(). |
381 |
|
*/ |
382 |
|
static bool ccs_is_domain_keeper(const struct ccs_path_info *domainname, |
383 |
|
const struct ccs_path_info *program, |
384 |
|
const struct ccs_path_info *last_name) |
385 |
|
{ |
386 |
|
struct ccs_domain_keeper_entry *ptr; |
387 |
|
bool flag = false; |
388 |
|
ccs_assert_read_lock(); |
389 |
|
list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) { |
390 |
|
if (ptr->is_deleted) |
391 |
|
continue; |
392 |
if (!ptr->is_last_name) { |
if (!ptr->is_last_name) { |
393 |
if (ptr->domainname != domainname) continue; |
if (ptr->domainname != domainname) |
394 |
|
continue; |
395 |
} else { |
} else { |
396 |
if (pathcmp(ptr->domainname, last_name)) continue; |
if (ccs_pathcmp(ptr->domainname, last_name)) |
397 |
|
continue; |
398 |
|
} |
399 |
|
if (ptr->program && ccs_pathcmp(ptr->program, program)) |
400 |
|
continue; |
401 |
|
if (ptr->is_not) { |
402 |
|
flag = false; |
403 |
|
break; |
404 |
} |
} |
405 |
if (ptr->program && pathcmp(ptr->program, program)) continue; |
flag = true; |
|
if (ptr->is_not) return 0; |
|
|
flag = 1; |
|
406 |
} |
} |
407 |
return flag; |
return flag; |
408 |
} |
} |
409 |
|
|
410 |
/************************* SYMBOLIC LINKED PROGRAM HANDLER *************************/ |
/* The list for "struct ccs_aggregator_entry". */ |
411 |
|
LIST_HEAD(ccs_aggregator_list); |
|
static struct alias_entry *alias_list = NULL; |
|
412 |
|
|
413 |
static int AddAliasEntry(const char *original_name, const char *aliased_name, const int is_delete) |
/** |
414 |
{ |
* ccs_update_aggregator_entry - Update "struct ccs_aggregator_entry" list. |
415 |
struct alias_entry *new_entry, *ptr; |
* |
416 |
static DECLARE_MUTEX(lock); |
* @original_name: The original program's name. |
417 |
const struct path_info *saved_original_name, *saved_aliased_name; |
* @aggregated_name: The aggregated program's name. |
418 |
int error = -ENOMEM; |
* @is_delete: True if it is a delete request. |
419 |
if (!IsCorrectPath(original_name, 1, -1, -1, __FUNCTION__) || !IsCorrectPath(aliased_name, 1, -1, -1, __FUNCTION__)) return -EINVAL; /* No patterns allowed. */ |
* |
420 |
if ((saved_original_name = SaveName(original_name)) == NULL || (saved_aliased_name = SaveName(aliased_name)) == NULL) return -ENOMEM; |
* Returns 0 on success, negative value otherwise. |
421 |
down(&lock); |
*/ |
422 |
for (ptr = alias_list; ptr; ptr = ptr->next) { |
static int ccs_update_aggregator_entry(const char *original_name, |
423 |
if (ptr->original_name == saved_original_name && ptr->aliased_name == saved_aliased_name) { |
const char *aggregated_name, |
424 |
ptr->is_deleted = is_delete; |
const bool is_delete) |
425 |
error = 0; |
{ |
426 |
goto out; |
struct ccs_aggregator_entry *entry = NULL; |
427 |
} |
struct ccs_aggregator_entry *ptr; |
428 |
} |
struct ccs_aggregator_entry e = { }; |
429 |
if (is_delete) { |
int error = is_delete ? -ENOENT : -ENOMEM; |
430 |
error = -ENOENT; |
if (!ccs_is_correct_path(original_name, 1, 0, -1) || |
431 |
|
!ccs_is_correct_path(aggregated_name, 1, -1, -1)) |
432 |
|
return -EINVAL; |
433 |
|
e.original_name = ccs_get_name(original_name); |
434 |
|
e.aggregated_name = ccs_get_name(aggregated_name); |
435 |
|
if (!e.original_name || !e.aggregated_name) |
436 |
goto out; |
goto out; |
437 |
|
if (!is_delete) |
438 |
|
entry = kmalloc(sizeof(e), GFP_KERNEL); |
439 |
|
mutex_lock(&ccs_policy_lock); |
440 |
|
list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) { |
441 |
|
if (ccs_memcmp(ptr, &e, offsetof(typeof(e), original_name), |
442 |
|
sizeof(e))) |
443 |
|
continue; |
444 |
|
ptr->is_deleted = is_delete; |
445 |
|
error = 0; |
446 |
|
break; |
447 |
} |
} |
448 |
if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out; |
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
449 |
new_entry->original_name = saved_original_name; |
list_add_tail_rcu(&entry->list, &ccs_aggregator_list); |
450 |
new_entry->aliased_name = saved_aliased_name; |
entry = NULL; |
451 |
mb(); /* Instead of using spinlock. */ |
error = 0; |
|
if ((ptr = alias_list) != NULL) { |
|
|
while (ptr->next) ptr = ptr->next; ptr->next = new_entry; |
|
|
} else { |
|
|
alias_list = new_entry; |
|
452 |
} |
} |
453 |
error = 0; |
mutex_unlock(&ccs_policy_lock); |
454 |
out: |
out: |
455 |
up(&lock); |
ccs_put_name(e.original_name); |
456 |
|
ccs_put_name(e.aggregated_name); |
457 |
|
kfree(entry); |
458 |
return error; |
return error; |
459 |
} |
} |
460 |
|
|
461 |
int ReadAliasPolicy(struct io_buffer *head) |
/** |
462 |
|
* ccs_read_aggregator_policy - Read "struct ccs_aggregator_entry" list. |
463 |
|
* |
464 |
|
* @head: Pointer to "struct ccs_io_buffer". |
465 |
|
* |
466 |
|
* Returns true on success, false otherwise. |
467 |
|
* |
468 |
|
* Caller holds ccs_read_lock(). |
469 |
|
*/ |
470 |
|
bool ccs_read_aggregator_policy(struct ccs_io_buffer *head) |
471 |
{ |
{ |
472 |
struct alias_entry *ptr = head->read_var2; |
struct list_head *pos; |
473 |
if (!ptr) ptr = alias_list; |
bool done = true; |
474 |
while (ptr) { |
ccs_assert_read_lock(); |
475 |
head->read_var2 = ptr; |
list_for_each_cookie(pos, head->read_var2, &ccs_aggregator_list) { |
476 |
if (!ptr->is_deleted && io_printf(head, KEYWORD_ALIAS "%s %s\n", ptr->original_name->name, ptr->aliased_name->name)) break; |
struct ccs_aggregator_entry *ptr; |
477 |
ptr = ptr->next; |
ptr = list_entry(pos, struct ccs_aggregator_entry, list); |
478 |
|
if (ptr->is_deleted) |
479 |
|
continue; |
480 |
|
done = ccs_io_printf(head, CCS_KEYWORD_AGGREGATOR "%s %s\n", |
481 |
|
ptr->original_name->name, |
482 |
|
ptr->aggregated_name->name); |
483 |
|
if (!done) |
484 |
|
break; |
485 |
} |
} |
486 |
return ptr ? -ENOMEM : 0; |
return done; |
487 |
} |
} |
488 |
|
|
489 |
int AddAliasPolicy(char *data, const int is_delete) |
/** |
490 |
|
* ccs_write_aggregator_policy - Write "struct ccs_aggregator_entry" list. |
491 |
|
* |
492 |
|
* @data: String to parse. |
493 |
|
* @is_delete: True if it is a delete request. |
494 |
|
* |
495 |
|
* Returns 0 on success, negative value otherwise. |
496 |
|
*/ |
497 |
|
int ccs_write_aggregator_policy(char *data, const bool is_delete) |
498 |
{ |
{ |
499 |
char *cp = strchr(data, ' '); |
char *w[2]; |
500 |
if (!cp) return -EINVAL; |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0]) |
501 |
*cp++ = '\0'; |
return -EINVAL; |
502 |
return AddAliasEntry(data, cp, is_delete); |
return ccs_update_aggregator_entry(w[0], w[1], is_delete); |
503 |
} |
} |
504 |
|
|
505 |
/************************* DOMAIN AGGREGATOR HANDLER *************************/ |
/* Domain create/delete handler. */ |
506 |
|
|
507 |
static struct aggregator_entry *aggregator_list = NULL; |
/** |
508 |
|
* ccs_delete_domain - Delete a domain. |
509 |
static int AddAggregatorEntry(const char *original_name, const char *aggregated_name, const int is_delete) |
* |
510 |
|
* @domainname: The name of domain. |
511 |
|
* |
512 |
|
* Returns 0. |
513 |
|
*/ |
514 |
|
int ccs_delete_domain(char *domainname) |
515 |
{ |
{ |
516 |
struct aggregator_entry *new_entry, *ptr; |
struct ccs_domain_info *domain; |
517 |
static DECLARE_MUTEX(lock); |
struct ccs_path_info name; |
518 |
const struct path_info *saved_original_name, *saved_aggregated_name; |
name.name = domainname; |
519 |
int error = -ENOMEM; |
ccs_fill_path_info(&name); |
520 |
if (!IsCorrectPath(original_name, 1, 0, -1, __FUNCTION__) || !IsCorrectPath(aggregated_name, 1, -1, -1, __FUNCTION__)) return -EINVAL; |
mutex_lock(&ccs_policy_lock); |
521 |
if ((saved_original_name = SaveName(original_name)) == NULL || (saved_aggregated_name = SaveName(aggregated_name)) == NULL) return -ENOMEM; |
/* Is there an active domain? */ |
522 |
down(&lock); |
list_for_each_entry_rcu(domain, &ccs_domain_list, list) { |
523 |
for (ptr = aggregator_list; ptr; ptr = ptr->next) { |
/* Never delete ccs_kernel_domain */ |
524 |
if (ptr->original_name == saved_original_name && ptr->aggregated_name == saved_aggregated_name) { |
if (domain == &ccs_kernel_domain) |
525 |
ptr->is_deleted = is_delete; |
continue; |
526 |
error = 0; |
if (domain->is_deleted || |
527 |
goto out; |
ccs_pathcmp(domain->domainname, &name)) |
528 |
} |
continue; |
529 |
} |
domain->is_deleted = true; |
530 |
if (is_delete) { |
break; |
|
error = -ENOENT; |
|
|
goto out; |
|
|
} |
|
|
if ((new_entry = alloc_element(sizeof(*new_entry))) == NULL) goto out; |
|
|
new_entry->original_name = saved_original_name; |
|
|
new_entry->aggregated_name = saved_aggregated_name; |
|
|
mb(); /* Instead of using spinlock. */ |
|
|
if ((ptr = aggregator_list) != NULL) { |
|
|
while (ptr->next) ptr = ptr->next; ptr->next = new_entry; |
|
|
} else { |
|
|
aggregator_list = new_entry; |
|
531 |
} |
} |
532 |
error = 0; |
mutex_unlock(&ccs_policy_lock); |
533 |
out: |
return 0; |
|
up(&lock); |
|
|
return error; |
|
534 |
} |
} |
535 |
|
|
536 |
int ReadAggregatorPolicy(struct io_buffer *head) |
/** |
537 |
|
* ccs_find_or_assign_new_domain - Create a domain. |
538 |
|
* |
539 |
|
* @domainname: The name of domain. |
540 |
|
* @profile: Profile number to assign if the domain was newly created. |
541 |
|
* |
542 |
|
* Returns pointer to "struct ccs_domain_info" on success, NULL otherwise. |
543 |
|
*/ |
544 |
|
struct ccs_domain_info *ccs_find_or_assign_new_domain(const char *domainname, |
545 |
|
const u8 profile) |
546 |
{ |
{ |
547 |
struct aggregator_entry *ptr = head->read_var2; |
struct ccs_domain_info *entry; |
548 |
if (!ptr) ptr = aggregator_list; |
struct ccs_domain_info *domain; |
549 |
while (ptr) { |
const struct ccs_path_info *saved_domainname; |
550 |
head->read_var2 = ptr; |
bool found = false; |
551 |
if (!ptr->is_deleted && io_printf(head, KEYWORD_AGGREGATOR "%s %s\n", ptr->original_name->name, ptr->aggregated_name->name)) break; |
|
552 |
ptr = ptr->next; |
if (!ccs_is_correct_domain(domainname)) |
553 |
|
return NULL; |
554 |
|
saved_domainname = ccs_get_name(domainname); |
555 |
|
if (!saved_domainname) |
556 |
|
return NULL; |
557 |
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
558 |
|
mutex_lock(&ccs_policy_lock); |
559 |
|
list_for_each_entry_rcu(domain, &ccs_domain_list, list) { |
560 |
|
if (domain->is_deleted || |
561 |
|
ccs_pathcmp(saved_domainname, domain->domainname)) |
562 |
|
continue; |
563 |
|
found = true; |
564 |
|
break; |
565 |
} |
} |
566 |
return ptr ? -ENOMEM : 0; |
if (!found && ccs_memory_ok(entry, sizeof(*entry))) { |
567 |
|
INIT_LIST_HEAD(&entry->acl_info_list); |
568 |
|
entry->domainname = saved_domainname; |
569 |
|
saved_domainname = NULL; |
570 |
|
entry->profile = profile; |
571 |
|
list_add_tail_rcu(&entry->list, &ccs_domain_list); |
572 |
|
domain = entry; |
573 |
|
entry = NULL; |
574 |
|
found = true; |
575 |
|
} |
576 |
|
mutex_unlock(&ccs_policy_lock); |
577 |
|
ccs_put_name(saved_domainname); |
578 |
|
kfree(entry); |
579 |
|
return found ? domain : NULL; |
580 |
} |
} |
581 |
|
|
582 |
int AddAggregatorPolicy(char *data, const int is_delete) |
/** |
583 |
|
* ccs_find_next_domain - Find a domain. |
584 |
|
* |
585 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
586 |
|
* |
587 |
|
* Returns 0 on success, negative value otherwise. |
588 |
|
* |
589 |
|
* Caller holds ccs_read_lock(). |
590 |
|
*/ |
591 |
|
static int ccs_find_next_domain(struct ccs_execve_entry *ee) |
592 |
{ |
{ |
593 |
char *cp = strchr(data, ' '); |
struct ccs_request_info *r = &ee->r; |
594 |
if (!cp) return -EINVAL; |
const struct ccs_path_info *handler = ee->handler; |
595 |
*cp++ = '\0'; |
struct ccs_domain_info *domain = NULL; |
596 |
return AddAggregatorEntry(data, cp, is_delete); |
const char *old_domain_name = r->domain->domainname->name; |
597 |
} |
struct linux_binprm *bprm = ee->bprm; |
598 |
|
const u8 mode = r->mode; |
599 |
|
const bool is_enforce = (mode == 3); |
600 |
|
const u32 ccs_flags = current->ccs_flags; |
601 |
|
struct ccs_path_info rn = { }; /* real name */ |
602 |
|
struct ccs_path_info ln; /* last name */ |
603 |
|
int retval; |
604 |
|
bool need_kfree = false; |
605 |
|
ccs_assert_read_lock(); |
606 |
|
retry: |
607 |
|
current->ccs_flags = ccs_flags; |
608 |
|
r->cond = NULL; |
609 |
|
/* Get symlink's pathname of program. */ |
610 |
|
retval = ccs_symlink_path(bprm->filename, &rn); |
611 |
|
if (retval < 0) |
612 |
|
goto out; |
613 |
|
need_kfree = true; |
614 |
|
|
615 |
/************************* DOMAIN DELETION HANDLER *************************/ |
ln.name = ccs_get_last_name(r->domain); |
616 |
|
ccs_fill_path_info(&ln); |
617 |
|
|
618 |
/* #define DEBUG_DOMAIN_UNDELETE */ |
if (handler) { |
619 |
|
if (ccs_pathcmp(&rn, handler)) { |
620 |
|
/* Failed to verify execute handler. */ |
621 |
|
static u8 counter = 20; |
622 |
|
if (counter) { |
623 |
|
counter--; |
624 |
|
printk(KERN_WARNING "Failed to verify: %s\n", |
625 |
|
handler->name); |
626 |
|
} |
627 |
|
goto out; |
628 |
|
} |
629 |
|
goto calculate_domain; |
630 |
|
} |
631 |
|
|
632 |
int DeleteDomain(char *domainname0) |
/* Check 'aggregator' directive. */ |
633 |
{ |
{ |
634 |
struct domain_info *domain; |
struct ccs_aggregator_entry *ptr; |
635 |
struct path_info domainname; |
/* Is this program allowed to be aggregated? */ |
636 |
domainname.name = domainname0; |
list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) { |
637 |
fill_path_info(&domainname); |
if (ptr->is_deleted || |
638 |
down(&new_domain_assign_lock); |
!ccs_path_matches_pattern(&rn, ptr->original_name)) |
639 |
#ifdef DEBUG_DOMAIN_UNDELETE |
continue; |
640 |
printk("DeleteDomain %s\n", domainname0); |
kfree(rn.name); |
641 |
for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) { |
need_kfree = false; |
642 |
if (pathcmp(domain->domainname, &domainname)) continue; |
rn = *ptr->aggregated_name; |
643 |
printk("List: %p %u\n", domain, domain->is_deleted); |
break; |
644 |
|
} |
645 |
} |
} |
646 |
#endif |
|
647 |
/* Is there an active domain? */ |
/* Check execute permission. */ |
648 |
for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) { /* Never delete KERNEL_DOMAIN */ |
r->mode = mode; |
649 |
if (domain->is_deleted || pathcmp(domain->domainname, &domainname)) continue; |
retval = ccs_exec_perm(r, &rn); |
650 |
break; |
if (retval == 1) |
651 |
|
goto retry; |
652 |
|
if (retval < 0) |
653 |
|
goto out; |
654 |
|
|
655 |
|
calculate_domain: |
656 |
|
if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) { |
657 |
|
/* Transit to the child of ccs_kernel_domain domain. */ |
658 |
|
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, ROOT_NAME " " "%s", |
659 |
|
rn.name); |
660 |
|
} else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) { |
661 |
|
/* |
662 |
|
* Needn't to transit from kernel domain before starting |
663 |
|
* /sbin/init. But transit from kernel domain if executing |
664 |
|
* initializers because they might start before /sbin/init. |
665 |
|
*/ |
666 |
|
domain = r->domain; |
667 |
|
} else if (ccs_is_domain_keeper(r->domain->domainname, &rn, &ln)) { |
668 |
|
/* Keep current domain. */ |
669 |
|
domain = r->domain; |
670 |
|
} else { |
671 |
|
/* Normal domain transition. */ |
672 |
|
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s", |
673 |
|
old_domain_name, rn.name); |
674 |
} |
} |
675 |
if (domain) { |
if (domain || strlen(ee->tmp) >= CCS_EXEC_TMPSIZE - 10) |
676 |
struct domain_info *domain2; |
goto done; |
677 |
/* Mark already deleted domains as non undeletable. */ |
domain = ccs_find_domain(ee->tmp); |
678 |
for (domain2 = KERNEL_DOMAIN.next; domain2; domain2 = domain2->next) { |
if (domain) |
679 |
if (!domain2->is_deleted || pathcmp(domain2->domainname, &domainname)) continue; |
goto done; |
680 |
#ifdef DEBUG_DOMAIN_UNDELETE |
if (is_enforce) { |
681 |
if (domain2->is_deleted != 255) printk("Marked %p as non undeletable\n", domain2); |
int error = ccs_supervisor(r, "# wants to create domain\n" |
682 |
#endif |
"%s\n", ee->tmp); |
683 |
domain2->is_deleted = 255; |
if (error == 1) |
684 |
} |
goto retry; |
685 |
/* Delete and mark active domain as undeletable. */ |
if (error < 0) |
686 |
domain->is_deleted = 1; |
goto done; |
687 |
#ifdef DEBUG_DOMAIN_UNDELETE |
} |
688 |
printk("Marked %p as undeletable\n", domain); |
domain = ccs_find_or_assign_new_domain(ee->tmp, r->profile); |
689 |
#endif |
if (domain) |
690 |
|
ccs_audit_domain_creation_log(r->domain); |
691 |
|
done: |
692 |
|
if (!domain) { |
693 |
|
printk(KERN_WARNING "ERROR: Domain '%s' not defined.\n", |
694 |
|
ee->tmp); |
695 |
|
if (is_enforce) |
696 |
|
retval = -EPERM; |
697 |
|
else { |
698 |
|
retval = 0; |
699 |
|
r->domain->domain_transition_failed = true; |
700 |
|
} |
701 |
|
} else { |
702 |
|
retval = 0; |
703 |
} |
} |
704 |
up(&new_domain_assign_lock); |
out: |
705 |
return 0; |
if (domain) |
706 |
|
r->domain = domain; |
707 |
|
if (need_kfree) |
708 |
|
kfree(rn.name); |
709 |
|
return retval; |
710 |
} |
} |
711 |
|
|
712 |
struct domain_info *UndeleteDomain(const char *domainname0) |
/** |
713 |
|
* ccs_environ - Check permission for environment variable names. |
714 |
|
* |
715 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
716 |
|
* |
717 |
|
* Returns 0 on success, negative value otherwise. |
718 |
|
*/ |
719 |
|
static int ccs_environ(struct ccs_execve_entry *ee) |
720 |
{ |
{ |
721 |
struct domain_info *domain, *candidate_domain = NULL; |
struct ccs_request_info *r = &ee->r; |
722 |
struct path_info domainname; |
struct linux_binprm *bprm = ee->bprm; |
723 |
domainname.name = domainname0; |
char *arg_ptr = ee->tmp; |
724 |
fill_path_info(&domainname); |
int arg_len = 0; |
725 |
down(&new_domain_assign_lock); |
unsigned long pos = bprm->p; |
726 |
#ifdef DEBUG_DOMAIN_UNDELETE |
int offset = pos % PAGE_SIZE; |
727 |
printk("UndeleteDomain %s\n", domainname0); |
int argv_count = bprm->argc; |
728 |
for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) { |
int envp_count = bprm->envc; |
729 |
if (pathcmp(domain->domainname, &domainname)) continue; |
/* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */ |
730 |
printk("List: %p %u\n", domain, domain->is_deleted); |
int error = -ENOMEM; |
731 |
} |
if (!r->mode || !envp_count) |
732 |
#endif |
return 0; |
733 |
for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) { |
while (error == -ENOMEM) { |
734 |
if (pathcmp(&domainname, domain->domainname)) continue; |
if (!ccs_dump_page(bprm, pos, &ee->dump)) |
735 |
if (!domain->is_deleted) { |
goto out; |
736 |
/* This domain is active. I can't undelete. */ |
pos += PAGE_SIZE - offset; |
737 |
candidate_domain = NULL; |
/* Read. */ |
738 |
#ifdef DEBUG_DOMAIN_UNDELETE |
while (argv_count && offset < PAGE_SIZE) { |
739 |
printk("%p is active. I can't undelete.\n", domain); |
const char *kaddr = ee->dump.data; |
740 |
#endif |
if (!kaddr[offset++]) |
741 |
break; |
argv_count--; |
742 |
} |
} |
743 |
/* Is this domain undeletable? */ |
if (argv_count) { |
744 |
if (domain->is_deleted == 1) candidate_domain = domain; |
offset = 0; |
745 |
} |
continue; |
746 |
if (candidate_domain) { |
} |
747 |
candidate_domain->is_deleted = 0; |
while (offset < PAGE_SIZE) { |
748 |
#ifdef DEBUG_DOMAIN_UNDELETE |
const char *kaddr = ee->dump.data; |
749 |
printk("%p was undeleted.\n", candidate_domain); |
const unsigned char c = kaddr[offset++]; |
750 |
#endif |
if (c && arg_len < CCS_EXEC_TMPSIZE - 10) { |
751 |
} |
if (c == '=') { |
752 |
up(&new_domain_assign_lock); |
arg_ptr[arg_len++] = '\0'; |
753 |
return candidate_domain; |
} else if (c == '\\') { |
754 |
} |
arg_ptr[arg_len++] = '\\'; |
755 |
|
arg_ptr[arg_len++] = '\\'; |
756 |
/************************* DOMAIN TRANSITION HANDLER *************************/ |
} else if (c > ' ' && c < 127) { |
757 |
|
arg_ptr[arg_len++] = c; |
758 |
struct domain_info *FindDomain(const char *domainname0) |
} else { |
759 |
{ |
arg_ptr[arg_len++] = '\\'; |
760 |
struct domain_info *domain; |
arg_ptr[arg_len++] = (c >> 6) + '0'; |
761 |
static int first = 1; |
arg_ptr[arg_len++] |
762 |
struct path_info domainname; |
= ((c >> 3) & 7) + '0'; |
763 |
domainname.name = domainname0; |
arg_ptr[arg_len++] = (c & 7) + '0'; |
764 |
fill_path_info(&domainname); |
} |
765 |
if (first) { |
} else { |
766 |
KERNEL_DOMAIN.domainname = SaveName(ROOT_NAME); |
arg_ptr[arg_len] = '\0'; |
767 |
first = 0; |
} |
768 |
} |
if (c) |
769 |
for (domain = &KERNEL_DOMAIN; domain; domain = domain->next) { |
continue; |
770 |
if (!domain->is_deleted && !pathcmp(&domainname, domain->domainname)) return domain; |
if (ccs_env_perm(r, arg_ptr)) { |
771 |
} |
error = -EPERM; |
772 |
return NULL; |
break; |
773 |
} |
} |
774 |
|
if (!--envp_count) { |
775 |
struct domain_info *FindOrAssignNewDomain(const char *domainname, const u8 profile) |
error = 0; |
776 |
{ |
break; |
777 |
struct domain_info *domain = NULL; |
} |
778 |
const struct path_info *saved_domainname; |
arg_len = 0; |
779 |
down(&new_domain_assign_lock); |
} |
780 |
if ((domain = FindDomain(domainname)) != NULL) goto out; |
offset = 0; |
|
if (!IsCorrectDomain(domainname, __FUNCTION__)) goto out; |
|
|
if ((saved_domainname = SaveName(domainname)) == NULL) goto out; |
|
|
/* Can I reuse memory of deleted domain? */ |
|
|
for (domain = KERNEL_DOMAIN.next; domain; domain = domain->next) { |
|
|
struct task_struct *p; |
|
|
struct acl_info *ptr; |
|
|
int flag; |
|
|
if (!domain->is_deleted || domain->domainname != saved_domainname) continue; |
|
|
flag = 0; |
|
|
/***** CRITICAL SECTION START *****/ |
|
|
read_lock(&tasklist_lock); |
|
|
for_each_process(p) { |
|
|
if (p->domain_info == domain) { flag = 1; break; } |
|
|
} |
|
|
read_unlock(&tasklist_lock); |
|
|
/***** CRITICAL SECTION END *****/ |
|
|
if (flag) continue; |
|
|
#ifdef DEBUG_DOMAIN_UNDELETE |
|
|
printk("Reusing %p %s\n", domain, domain->domainname->name); |
|
|
#endif |
|
|
for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) ptr->is_deleted = 1; |
|
|
domain->profile = profile; |
|
|
domain->quota_warned = 0; |
|
|
mb(); /* Instead of using spinlock. */ |
|
|
domain->is_deleted = 0; |
|
|
goto out; |
|
781 |
} |
} |
782 |
/* No memory reusable. Create using new memory. */ |
out: |
783 |
if ((domain = alloc_element(sizeof(*domain))) != NULL) { |
if (r->mode != 3) |
784 |
struct domain_info *ptr = &KERNEL_DOMAIN; |
error = 0; |
785 |
domain->domainname = saved_domainname; |
return error; |
|
domain->profile = profile; |
|
|
mb(); /* Instead of using spinlock. */ |
|
|
while (ptr->next) ptr = ptr->next; ptr->next = domain; |
|
|
} |
|
|
out: ; |
|
|
up(&new_domain_assign_lock); |
|
|
return domain; |
|
786 |
} |
} |
787 |
|
|
788 |
static int Escape(char *dest, const char *src, int dest_len) |
/** |
789 |
|
* ccs_unescape - Unescape escaped string. |
790 |
|
* |
791 |
|
* @dest: String to unescape. |
792 |
|
* |
793 |
|
* Returns nothing. |
794 |
|
*/ |
795 |
|
static void ccs_unescape(unsigned char *dest) |
796 |
{ |
{ |
797 |
while (*src) { |
unsigned char *src = dest; |
798 |
const unsigned char c = * (const unsigned char *) src; |
unsigned char c; |
799 |
|
unsigned char d; |
800 |
|
unsigned char e; |
801 |
|
while (1) { |
802 |
|
c = *src++; |
803 |
|
if (!c) |
804 |
|
break; |
805 |
|
if (c != '\\') { |
806 |
|
*dest++ = c; |
807 |
|
continue; |
808 |
|
} |
809 |
|
c = *src++; |
810 |
if (c == '\\') { |
if (c == '\\') { |
|
dest_len -= 2; |
|
|
if (dest_len <= 0) goto out; |
|
|
*dest++ = '\\'; |
|
|
*dest++ = '\\'; |
|
|
} else if (c > ' ' && c < 127) { |
|
|
if (--dest_len <= 0) goto out; |
|
811 |
*dest++ = c; |
*dest++ = c; |
812 |
} else { |
continue; |
|
dest_len -= 4; |
|
|
if (dest_len <= 0) goto out; |
|
|
*dest++ = '\\'; |
|
|
*dest++ = (c >> 6) + '0'; |
|
|
*dest++ = ((c >> 3) & 7) + '0'; |
|
|
*dest++ = (c & 7) + '0'; |
|
813 |
} |
} |
814 |
src++; |
if (c < '0' || c > '3') |
815 |
|
break; |
816 |
|
d = *src++; |
817 |
|
if (d < '0' || d > '7') |
818 |
|
break; |
819 |
|
e = *src++; |
820 |
|
if (e < '0' || e > '7') |
821 |
|
break; |
822 |
|
*dest++ = ((c - '0') << 6) + ((d - '0') << 3) + (e - '0'); |
823 |
} |
} |
|
if (--dest_len <= 0) goto out; |
|
824 |
*dest = '\0'; |
*dest = '\0'; |
|
return 0; |
|
|
out: |
|
|
return -ENOMEM; |
|
825 |
} |
} |
826 |
|
|
827 |
static char *get_argv0(struct linux_binprm *bprm) |
/** |
828 |
|
* ccs_root_depth - Get number of directories to strip. |
829 |
|
* |
830 |
|
* @dentry: Pointer to "struct dentry". |
831 |
|
* @vfsmnt: Pointer to "struct vfsmount". |
832 |
|
* |
833 |
|
* Returns number of directories to strip. |
834 |
|
*/ |
835 |
|
static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt) |
836 |
{ |
{ |
837 |
char *arg_ptr = ccs_alloc(PAGE_SIZE); /* Initial buffer. */ |
int depth = 0; |
838 |
int arg_len = 0; |
ccs_realpath_lock(); |
839 |
unsigned long pos = bprm->p; |
for (;;) { |
840 |
int i = pos / PAGE_SIZE, offset = pos % PAGE_SIZE; |
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
841 |
if (!bprm->argc || !arg_ptr) goto out; |
/* Global root? */ |
842 |
while (1) { |
if (vfsmnt->mnt_parent == vfsmnt) |
843 |
struct page *page; |
break; |
844 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU) |
dentry = vfsmnt->mnt_mountpoint; |
845 |
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) goto out; |
vfsmnt = vfsmnt->mnt_parent; |
846 |
#else |
continue; |
847 |
page = bprm->page[i]; |
} |
848 |
|
dentry = dentry->d_parent; |
849 |
|
depth++; |
850 |
|
} |
851 |
|
ccs_realpath_unlock(); |
852 |
|
return depth; |
853 |
|
} |
854 |
|
|
855 |
|
/** |
856 |
|
* ccs_get_root_depth - return the depth of root directory. |
857 |
|
* |
858 |
|
* Returns number of directories to strip. |
859 |
|
*/ |
860 |
|
static int ccs_get_root_depth(void) |
861 |
|
{ |
862 |
|
int depth; |
863 |
|
struct dentry *dentry; |
864 |
|
struct vfsmount *vfsmnt; |
865 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
866 |
|
struct path root; |
867 |
#endif |
#endif |
868 |
{ /* Map and copy to kernel buffer and unmap. */ |
read_lock(¤t->fs->lock); |
869 |
const char *kaddr = kmap(page); |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
870 |
if (!kaddr) { /* Mapping failed. */ |
root = current->fs->root; |
871 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU) |
path_get(¤t->fs->root); |
872 |
put_page(page); |
dentry = root.dentry; |
873 |
|
vfsmnt = root.mnt; |
874 |
|
#else |
875 |
|
dentry = dget(current->fs->root); |
876 |
|
vfsmnt = mntget(current->fs->rootmnt); |
877 |
#endif |
#endif |
878 |
goto out; |
read_unlock(¤t->fs->lock); |
879 |
} |
depth = ccs_root_depth(dentry, vfsmnt); |
880 |
memmove(arg_ptr + arg_len, kaddr + offset, PAGE_SIZE - offset); |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
881 |
kunmap(page); |
path_put(&root); |
882 |
} |
#else |
883 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) && defined(CONFIG_MMU) |
dput(dentry); |
884 |
put_page(page); |
mntput(vfsmnt); |
|
pos += PAGE_SIZE - offset; |
|
885 |
#endif |
#endif |
886 |
arg_len += PAGE_SIZE - offset; |
return depth; |
887 |
if (memchr(arg_ptr, '\0', arg_len)) break; |
} |
888 |
{ /* Initial buffer was too small for argv[0]. Retry after expanding buffer. */ |
|
889 |
char *tmp_arg_ptr = ccs_alloc(arg_len + PAGE_SIZE); |
static LIST_HEAD(ccs_execve_list); |
890 |
if (!tmp_arg_ptr) goto out; |
static DEFINE_SPINLOCK(ccs_execve_list_lock); |
891 |
memmove(tmp_arg_ptr, arg_ptr, arg_len); |
|
892 |
ccs_free(arg_ptr); |
/** |
893 |
arg_ptr = tmp_arg_ptr; |
* ccs_allocate_execve_entry - Allocate memory for execve(). |
894 |
} |
* |
895 |
i++; |
* Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise. |
896 |
offset = 0; |
*/ |
897 |
|
static struct ccs_execve_entry *ccs_allocate_execve_entry(void) |
898 |
|
{ |
899 |
|
struct ccs_execve_entry *ee = kzalloc(sizeof(*ee), GFP_KERNEL); |
900 |
|
if (!ee) |
901 |
|
return NULL; |
902 |
|
ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL); |
903 |
|
if (!ee->tmp) { |
904 |
|
kfree(ee); |
905 |
|
return NULL; |
906 |
|
} |
907 |
|
ee->reader_idx = ccs_read_lock(); |
908 |
|
/* ee->dump->data is allocated by ccs_dump_page(). */ |
909 |
|
ee->task = current; |
910 |
|
spin_lock(&ccs_execve_list_lock); |
911 |
|
list_add(&ee->list, &ccs_execve_list); |
912 |
|
spin_unlock(&ccs_execve_list_lock); |
913 |
|
return ee; |
914 |
|
} |
915 |
|
|
916 |
|
/** |
917 |
|
* ccs_find_execve_entry - Find ccs_execve_entry of current process. |
918 |
|
* |
919 |
|
* Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise. |
920 |
|
*/ |
921 |
|
static struct ccs_execve_entry *ccs_find_execve_entry(void) |
922 |
|
{ |
923 |
|
struct task_struct *task = current; |
924 |
|
struct ccs_execve_entry *ee = NULL; |
925 |
|
struct ccs_execve_entry *p; |
926 |
|
spin_lock(&ccs_execve_list_lock); |
927 |
|
list_for_each_entry(p, &ccs_execve_list, list) { |
928 |
|
if (p->task != task) |
929 |
|
continue; |
930 |
|
ee = p; |
931 |
|
break; |
932 |
} |
} |
933 |
return arg_ptr; |
spin_unlock(&ccs_execve_list_lock); |
934 |
out: /* Release initial buffer. */ |
return ee; |
935 |
ccs_free(arg_ptr); |
} |
936 |
return NULL; |
|
937 |
} |
/** |
938 |
|
* ccs_free_execve_entry - Free memory for execve(). |
939 |
static int FindNextDomain(struct linux_binprm *bprm, struct domain_info **next_domain) |
* |
940 |
{ |
* @ee: Pointer to "struct ccs_execve_entry". |
941 |
/* This function assumes that the size of buffer returned by realpath() = CCS_MAX_PATHNAME_LEN. */ |
*/ |
942 |
struct domain_info *old_domain = current->domain_info, *domain = NULL; |
static void ccs_free_execve_entry(struct ccs_execve_entry *ee) |
943 |
const char *old_domain_name = old_domain->domainname->name; |
{ |
944 |
const char *original_name = bprm->filename; |
if (!ee) |
945 |
struct file *filp = bprm->file; |
return; |
946 |
char *new_domain_name = NULL; |
spin_lock(&ccs_execve_list_lock); |
947 |
char *real_program_name = NULL, *symlink_program_name = NULL; |
list_del(&ee->list); |
948 |
const int is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_FILE); |
spin_unlock(&ccs_execve_list_lock); |
949 |
|
kfree(ee->handler_path); |
950 |
|
kfree(ee->tmp); |
951 |
|
kfree(ee->dump.data); |
952 |
|
ccs_read_unlock(ee->reader_idx); |
953 |
|
kfree(ee); |
954 |
|
} |
955 |
|
|
956 |
|
/** |
957 |
|
* ccs_try_alt_exec - Try to start execute handler. |
958 |
|
* |
959 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
960 |
|
* |
961 |
|
* Returns 0 on success, negative value otherwise. |
962 |
|
*/ |
963 |
|
static int ccs_try_alt_exec(struct ccs_execve_entry *ee) |
964 |
|
{ |
965 |
|
/* |
966 |
|
* Contents of modified bprm. |
967 |
|
* The envp[] in original bprm is moved to argv[] so that |
968 |
|
* the alternatively executed program won't be affected by |
969 |
|
* some dangerous environment variables like LD_PRELOAD. |
970 |
|
* |
971 |
|
* modified bprm->argc |
972 |
|
* = original bprm->argc + original bprm->envc + 7 |
973 |
|
* modified bprm->envc |
974 |
|
* = 0 |
975 |
|
* |
976 |
|
* modified bprm->argv[0] |
977 |
|
* = the program's name specified by execute_handler |
978 |
|
* modified bprm->argv[1] |
979 |
|
* = ccs_current_domain()->domainname->name |
980 |
|
* modified bprm->argv[2] |
981 |
|
* = the current process's name |
982 |
|
* modified bprm->argv[3] |
983 |
|
* = the current process's information (e.g. uid/gid). |
984 |
|
* modified bprm->argv[4] |
985 |
|
* = original bprm->filename |
986 |
|
* modified bprm->argv[5] |
987 |
|
* = original bprm->argc in string expression |
988 |
|
* modified bprm->argv[6] |
989 |
|
* = original bprm->envc in string expression |
990 |
|
* modified bprm->argv[7] |
991 |
|
* = original bprm->argv[0] |
992 |
|
* ... |
993 |
|
* modified bprm->argv[bprm->argc + 6] |
994 |
|
* = original bprm->argv[bprm->argc - 1] |
995 |
|
* modified bprm->argv[bprm->argc + 7] |
996 |
|
* = original bprm->envp[0] |
997 |
|
* ... |
998 |
|
* modified bprm->argv[bprm->envc + bprm->argc + 6] |
999 |
|
* = original bprm->envp[bprm->envc - 1] |
1000 |
|
*/ |
1001 |
|
struct linux_binprm *bprm = ee->bprm; |
1002 |
|
struct file *filp; |
1003 |
int retval; |
int retval; |
1004 |
struct path_info r, s, l; |
const int original_argc = bprm->argc; |
1005 |
|
const int original_envc = bprm->envc; |
1006 |
|
struct task_struct *task = current; |
1007 |
|
|
1008 |
|
/* Close the requested program's dentry. */ |
1009 |
|
allow_write_access(bprm->file); |
1010 |
|
fput(bprm->file); |
1011 |
|
bprm->file = NULL; |
1012 |
|
|
1013 |
|
/* Invalidate page dump cache. */ |
1014 |
|
ee->dump.page = NULL; |
1015 |
|
|
1016 |
|
/* Move envp[] to argv[] */ |
1017 |
|
bprm->argc += bprm->envc; |
1018 |
|
bprm->envc = 0; |
1019 |
|
|
1020 |
|
/* Set argv[6] */ |
1021 |
{ |
{ |
1022 |
/* |
char *cp = ee->tmp; |
1023 |
* Built-in initializers. This is needed because policies are not loaded until starting /sbin/init . |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc); |
1024 |
*/ |
retval = copy_strings_kernel(1, &cp, bprm); |
1025 |
static int first = 1; |
if (retval < 0) |
1026 |
if (first) { |
goto out; |
1027 |
AddDomainInitializerEntry(NULL, "/sbin/hotplug", 0, 0, 0); |
bprm->argc++; |
|
AddDomainInitializerEntry(NULL, "/sbin/modprobe", 0, 0, 0); |
|
|
first = 0; |
|
|
} |
|
1028 |
} |
} |
1029 |
|
|
1030 |
/* Get realpath of program. */ |
/* Set argv[5] */ |
1031 |
retval = -ENOENT; /* I hope realpath() won't fail with -ENOMEM. */ |
{ |
1032 |
if ((real_program_name = realpath(original_name)) == NULL) goto out; |
char *cp = ee->tmp; |
1033 |
/* Get realpath of symbolic link. */ |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc); |
1034 |
if ((symlink_program_name = realpath_nofollow(original_name)) == NULL) goto out; |
retval = copy_strings_kernel(1, &cp, bprm); |
1035 |
|
if (retval < 0) |
1036 |
r.name = real_program_name; |
goto out; |
1037 |
fill_path_info(&r); |
bprm->argc++; |
|
s.name = symlink_program_name; |
|
|
fill_path_info(&s); |
|
|
if ((l.name = strrchr(old_domain_name, ' ')) != NULL) l.name++; |
|
|
else l.name = old_domain_name; |
|
|
fill_path_info(&l); |
|
|
|
|
|
/* Check 'alias' directive. */ |
|
|
if (pathcmp(&r, &s)) { |
|
|
struct alias_entry *ptr; |
|
|
/* Is this program allowed to be called via symbolic links? */ |
|
|
for (ptr = alias_list; ptr; ptr = ptr->next) { |
|
|
if (ptr->is_deleted || pathcmp(&r, ptr->original_name) || pathcmp(&s, ptr->aliased_name)) continue; |
|
|
memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN); |
|
|
strncpy(real_program_name, ptr->aliased_name->name, CCS_MAX_PATHNAME_LEN - 1); |
|
|
fill_path_info(&r); |
|
|
break; |
|
|
} |
|
1038 |
} |
} |
1039 |
|
|
1040 |
/* Compare basename of real_program_name and argv[0] */ |
/* Set argv[4] */ |
1041 |
if (bprm->argc > 0 && CheckCCSFlags(CCS_TOMOYO_MAC_FOR_ARGV0)) { |
{ |
1042 |
char *org_argv0 = get_argv0(bprm); |
retval = copy_strings_kernel(1, &bprm->filename, bprm); |
1043 |
retval = -ENOMEM; |
if (retval < 0) |
1044 |
if (org_argv0) { |
goto out; |
1045 |
const int len = strlen(org_argv0); |
bprm->argc++; |
|
char *printable_argv0 = ccs_alloc(len * 4 + 8); |
|
|
if (printable_argv0 && Escape(printable_argv0, org_argv0, len * 4 + 8) == 0) { |
|
|
const char *base_argv0, *base_filename; |
|
|
if ((base_argv0 = strrchr(printable_argv0, '/')) == NULL) base_argv0 = printable_argv0; else base_argv0++; |
|
|
if ((base_filename = strrchr(real_program_name, '/')) == NULL) base_filename = real_program_name; else base_filename++; |
|
|
if (strcmp(base_argv0, base_filename)) retval = CheckArgv0Perm(&r, base_argv0); |
|
|
else retval = 0; |
|
|
} |
|
|
ccs_free(printable_argv0); |
|
|
ccs_free(org_argv0); |
|
|
} |
|
|
if (retval) goto out; |
|
1046 |
} |
} |
1047 |
|
|
1048 |
/* Check 'aggregator' directive. */ |
/* Set argv[3] */ |
1049 |
{ |
{ |
1050 |
struct aggregator_entry *ptr; |
char *cp = ee->tmp; |
1051 |
/* Is this program allowed to be aggregated? */ |
const u32 ccs_flags = task->ccs_flags; |
1052 |
for (ptr = aggregator_list; ptr; ptr = ptr->next) { |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, |
1053 |
if (ptr->is_deleted || !PathMatchesToPattern(&r, ptr->original_name)) continue; |
"pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d " |
1054 |
memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN); |
"sgid=%d fsuid=%d fsgid=%d state[0]=%u " |
1055 |
strncpy(real_program_name, ptr->aggregated_name->name, CCS_MAX_PATHNAME_LEN - 1); |
"state[1]=%u state[2]=%u", |
1056 |
fill_path_info(&r); |
(pid_t) sys_getpid(), current_uid(), current_gid(), |
1057 |
break; |
current_euid(), current_egid(), current_suid(), |
1058 |
} |
current_sgid(), current_fsuid(), current_fsgid(), |
1059 |
|
(u8) (ccs_flags >> 24), (u8) (ccs_flags >> 16), |
1060 |
|
(u8) (ccs_flags >> 8)); |
1061 |
|
retval = copy_strings_kernel(1, &cp, bprm); |
1062 |
|
if (retval < 0) |
1063 |
|
goto out; |
1064 |
|
bprm->argc++; |
1065 |
} |
} |
1066 |
|
|
1067 |
/* Check execute permission. */ |
/* Set argv[2] */ |
1068 |
if ((retval = CheckExecPerm(&r, filp)) < 0) goto out; |
{ |
1069 |
|
char *exe = (char *) ccs_get_exe(); |
1070 |
|
if (exe) { |
1071 |
|
retval = copy_strings_kernel(1, &exe, bprm); |
1072 |
|
kfree(exe); |
1073 |
|
} else { |
1074 |
|
exe = ee->tmp; |
1075 |
|
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>"); |
1076 |
|
retval = copy_strings_kernel(1, &exe, bprm); |
1077 |
|
} |
1078 |
|
if (retval < 0) |
1079 |
|
goto out; |
1080 |
|
bprm->argc++; |
1081 |
|
} |
1082 |
|
|
1083 |
/* Allocate memory for calcurating domain name. */ |
/* Set argv[1] */ |
1084 |
retval = -ENOMEM; |
{ |
1085 |
if ((new_domain_name = ccs_alloc(CCS_MAX_PATHNAME_LEN + 16)) == NULL) goto out; |
char *cp = ee->tmp; |
1086 |
|
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s", |
1087 |
if (IsDomainInitializer(old_domain->domainname, &r, &l)) { |
ccs_current_domain()->domainname->name); |
1088 |
/* Transit to the child of KERNEL_DOMAIN domain. */ |
retval = copy_strings_kernel(1, &cp, bprm); |
1089 |
snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, ROOT_NAME " " "%s", real_program_name); |
if (retval < 0) |
1090 |
} else if (old_domain == &KERNEL_DOMAIN && !sbin_init_started) { |
goto out; |
1091 |
/* |
bprm->argc++; |
|
* Needn't to transit from kernel domain before starting /sbin/init . |
|
|
* But transit from kernel domain if executing initializers, for they might start before /sbin/init . |
|
|
*/ |
|
|
domain = old_domain; |
|
|
} else if (IsDomainKeeper(old_domain->domainname, &r, &l)) { |
|
|
/* Keep current domain. */ |
|
|
domain = old_domain; |
|
|
} else { |
|
|
/* Normal domain transition. */ |
|
|
snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, "%s %s", old_domain_name, real_program_name); |
|
1092 |
} |
} |
1093 |
if (!domain && strlen(new_domain_name) < CCS_MAX_PATHNAME_LEN) { |
|
1094 |
if (is_enforce) { |
/* Set argv[0] */ |
1095 |
domain = FindDomain(new_domain_name); |
{ |
1096 |
if (!domain) if (CheckSupervisor("#Need to create domain\n%s\n", new_domain_name) == 0) domain = FindOrAssignNewDomain(new_domain_name, current->domain_info->profile); |
int depth = ccs_get_root_depth(); |
1097 |
} else { |
int len = ee->handler->total_len + 1; |
1098 |
domain = FindOrAssignNewDomain(new_domain_name, current->domain_info->profile); |
char *cp = kmalloc(len, GFP_KERNEL); |
1099 |
|
if (!cp) { |
1100 |
|
retval = ENOMEM; |
1101 |
|
goto out; |
1102 |
} |
} |
1103 |
|
ee->handler_path = cp; |
1104 |
|
memmove(cp, ee->handler->name, len); |
1105 |
|
ccs_unescape(cp); |
1106 |
|
retval = -ENOENT; |
1107 |
|
if (!*cp || *cp != '/') |
1108 |
|
goto out; |
1109 |
|
/* Adjust root directory for open_exec(). */ |
1110 |
|
while (depth) { |
1111 |
|
cp = strchr(cp + 1, '/'); |
1112 |
|
if (!cp) |
1113 |
|
goto out; |
1114 |
|
depth--; |
1115 |
|
} |
1116 |
|
memmove(ee->handler_path, cp, strlen(cp) + 1); |
1117 |
|
cp = ee->handler_path; |
1118 |
|
retval = copy_strings_kernel(1, &cp, bprm); |
1119 |
|
if (retval < 0) |
1120 |
|
goto out; |
1121 |
|
bprm->argc++; |
1122 |
} |
} |
1123 |
if (!domain) { |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) |
1124 |
printk("TOMOYO-ERROR: Domain '%s' not defined.\n", new_domain_name); |
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24) |
1125 |
if (is_enforce) retval = -EPERM; |
bprm->argv_len = bprm->exec - bprm->p; |
1126 |
} else { |
#endif |
1127 |
retval = 0; |
#endif |
1128 |
|
|
1129 |
|
/* OK, now restart the process with execute handler program's dentry. */ |
1130 |
|
filp = open_exec(ee->handler_path); |
1131 |
|
if (IS_ERR(filp)) { |
1132 |
|
retval = PTR_ERR(filp); |
1133 |
|
goto out; |
1134 |
} |
} |
1135 |
out: ; |
bprm->file = filp; |
1136 |
ccs_free(new_domain_name); |
bprm->filename = ee->handler_path; |
1137 |
ccs_free(real_program_name); |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
1138 |
ccs_free(symlink_program_name); |
bprm->interp = bprm->filename; |
1139 |
*next_domain = domain ? domain : old_domain; |
#endif |
1140 |
|
retval = prepare_binprm(bprm); |
1141 |
|
if (retval < 0) |
1142 |
|
goto out; |
1143 |
|
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
1144 |
|
retval = ccs_find_next_domain(ee); |
1145 |
|
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
1146 |
|
out: |
1147 |
return retval; |
return retval; |
1148 |
} |
} |
1149 |
|
|
1150 |
#endif |
/** |
1151 |
|
* ccs_find_execute_handler - Find an execute handler. |
1152 |
|
* |
1153 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
1154 |
|
* @type: Type of execute handler. |
1155 |
|
* |
1156 |
|
* Returns true if found, false otherwise. |
1157 |
|
* |
1158 |
|
* Caller holds ccs_read_lock(). |
1159 |
|
*/ |
1160 |
|
static bool ccs_find_execute_handler(struct ccs_execve_entry *ee, |
1161 |
|
const u8 type) |
1162 |
|
{ |
1163 |
|
struct task_struct *task = current; |
1164 |
|
const struct ccs_domain_info *domain = ccs_current_domain(); |
1165 |
|
struct ccs_acl_info *ptr; |
1166 |
|
bool found = false; |
1167 |
|
ccs_assert_read_lock(); |
1168 |
|
/* |
1169 |
|
* Don't use execute handler if the current process is |
1170 |
|
* marked as execute handler to avoid infinite execute handler loop. |
1171 |
|
*/ |
1172 |
|
if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) |
1173 |
|
return false; |
1174 |
|
list_for_each_entry(ptr, &domain->acl_info_list, list) { |
1175 |
|
struct ccs_execute_handler_record *acl; |
1176 |
|
if (ptr->type != type) |
1177 |
|
continue; |
1178 |
|
acl = container_of(ptr, struct ccs_execute_handler_record, |
1179 |
|
head); |
1180 |
|
ee->handler = acl->handler; |
1181 |
|
found = true; |
1182 |
|
break; |
1183 |
|
} |
1184 |
|
return found; |
1185 |
|
} |
1186 |
|
|
1187 |
int search_binary_handler_with_transition(struct linux_binprm *bprm, struct pt_regs *regs) |
/** |
1188 |
|
* ccs_dump_page - Dump a page to buffer. |
1189 |
|
* |
1190 |
|
* @bprm: Pointer to "struct linux_binprm". |
1191 |
|
* @pos: Location to dump. |
1192 |
|
* @dump: Poiner to "struct ccs_page_dump". |
1193 |
|
* |
1194 |
|
* Returns true on success, false otherwise. |
1195 |
|
*/ |
1196 |
|
bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos, |
1197 |
|
struct ccs_page_dump *dump) |
1198 |
{ |
{ |
1199 |
struct domain_info *next_domain = NULL, *prev_domain = current->domain_info; |
struct page *page; |
1200 |
int retval; |
/* dump->data is released by ccs_free_execve_entry(). */ |
1201 |
#if defined(CONFIG_SAKURA) || defined(CONFIG_TOMOYO) |
if (!dump->data) { |
1202 |
extern void CCS_LoadPolicy(const char *filename); |
dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL); |
1203 |
CCS_LoadPolicy(bprm->filename); |
if (!dump->data) |
1204 |
#endif |
return false; |
1205 |
#if defined(CONFIG_TOMOYO) |
} |
1206 |
retval = FindNextDomain(bprm, &next_domain); |
/* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */ |
1207 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
1208 |
|
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) |
1209 |
|
return false; |
1210 |
|
#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR == 3 && defined(CONFIG_MMU) |
1211 |
|
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) |
1212 |
|
return false; |
1213 |
#else |
#else |
1214 |
retval = 0; next_domain = prev_domain; |
page = bprm->page[pos / PAGE_SIZE]; |
1215 |
#endif |
#endif |
1216 |
if (retval == 0) { |
if (page != dump->page) { |
1217 |
current->tomoyo_flags |= TOMOYO_CHECK_READ_FOR_OPEN_EXEC; |
const unsigned int offset = pos % PAGE_SIZE; |
1218 |
current->domain_info = next_domain; |
/* |
1219 |
retval = search_binary_handler(bprm, regs); |
* Maybe kmap()/kunmap() should be used here. |
1220 |
if (retval < 0) current->domain_info = prev_domain; |
* But remove_arg_zero() uses kmap_atomic()/kunmap_atomic(). |
1221 |
current->tomoyo_flags &= ~TOMOYO_CHECK_READ_FOR_OPEN_EXEC; |
* So do I. |
1222 |
|
*/ |
1223 |
|
char *kaddr = kmap_atomic(page, KM_USER0); |
1224 |
|
dump->page = page; |
1225 |
|
memcpy(dump->data + offset, kaddr + offset, PAGE_SIZE - offset); |
1226 |
|
kunmap_atomic(kaddr, KM_USER0); |
1227 |
|
} |
1228 |
|
/* Same with put_arg_page(page) in fs/exec.c */ |
1229 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
1230 |
|
put_page(page); |
1231 |
|
#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR == 3 && defined(CONFIG_MMU) |
1232 |
|
put_page(page); |
1233 |
|
#endif |
1234 |
|
return true; |
1235 |
|
} |
1236 |
|
|
1237 |
|
/** |
1238 |
|
* ccs_fetch_next_domain - Fetch next_domain from the list. |
1239 |
|
* |
1240 |
|
* Returns pointer to "struct ccs_domain_info" which will be used if execve() |
1241 |
|
* succeeds. This function does not return NULL. |
1242 |
|
*/ |
1243 |
|
struct ccs_domain_info *ccs_fetch_next_domain(void) |
1244 |
|
{ |
1245 |
|
struct ccs_execve_entry *ee = ccs_find_execve_entry(); |
1246 |
|
struct ccs_domain_info *next_domain = NULL; |
1247 |
|
if (ee) |
1248 |
|
next_domain = ee->r.domain; |
1249 |
|
if (!next_domain) |
1250 |
|
next_domain = ccs_current_domain(); |
1251 |
|
return next_domain; |
1252 |
|
} |
1253 |
|
|
1254 |
|
/** |
1255 |
|
* ccs_start_execve - Prepare for execve() operation. |
1256 |
|
* |
1257 |
|
* @bprm: Pointer to "struct linux_binprm". |
1258 |
|
* |
1259 |
|
* Returns 0 on success, negative value otherwise. |
1260 |
|
*/ |
1261 |
|
int ccs_start_execve(struct linux_binprm *bprm) |
1262 |
|
{ |
1263 |
|
int retval; |
1264 |
|
struct task_struct *task = current; |
1265 |
|
struct ccs_execve_entry *ee = ccs_allocate_execve_entry(); |
1266 |
|
if (!ccs_policy_loaded) |
1267 |
|
ccs_load_policy(bprm->filename); |
1268 |
|
if (!ee) |
1269 |
|
return -ENOMEM; |
1270 |
|
ccs_init_request_info(&ee->r, NULL, CCS_MAC_EXECUTE); |
1271 |
|
ee->r.ee = ee; |
1272 |
|
ee->bprm = bprm; |
1273 |
|
ee->r.obj = &ee->obj; |
1274 |
|
ee->obj.path1.dentry = bprm->file->f_dentry; |
1275 |
|
ee->obj.path1.mnt = bprm->file->f_vfsmnt; |
1276 |
|
/* Clear manager flag. */ |
1277 |
|
task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER; |
1278 |
|
if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER)) { |
1279 |
|
retval = ccs_try_alt_exec(ee); |
1280 |
|
if (!retval) |
1281 |
|
ccs_audit_execute_handler_log(ee, true); |
1282 |
|
goto ok; |
1283 |
|
} |
1284 |
|
retval = ccs_find_next_domain(ee); |
1285 |
|
if (retval != -EPERM) |
1286 |
|
goto ok; |
1287 |
|
if (ccs_find_execute_handler(ee, CCS_TYPE_DENIED_EXECUTE_HANDLER)) { |
1288 |
|
retval = ccs_try_alt_exec(ee); |
1289 |
|
if (!retval) |
1290 |
|
ccs_audit_execute_handler_log(ee, false); |
1291 |
} |
} |
1292 |
|
ok: |
1293 |
|
if (retval < 0) |
1294 |
|
goto out; |
1295 |
|
ee->r.mode = ccs_flags(ee->r.domain, CCS_MAC_ENVIRON); |
1296 |
|
retval = ccs_environ(ee); |
1297 |
|
if (retval < 0) |
1298 |
|
goto out; |
1299 |
|
task->ccs_flags |= CCS_CHECK_READ_FOR_OPEN_EXEC; |
1300 |
|
retval = 0; |
1301 |
|
out: |
1302 |
|
if (retval) |
1303 |
|
ccs_finish_execve(retval); |
1304 |
return retval; |
return retval; |
1305 |
} |
} |
1306 |
|
|
1307 |
/***** TOMOYO Linux end. *****/ |
/** |
1308 |
|
* ccs_finish_execve - Clean up execve() operation. |
1309 |
|
* |
1310 |
|
* @retval: Return code of an execve() operation. |
1311 |
|
* |
1312 |
|
* Caller holds ccs_read_lock(). |
1313 |
|
*/ |
1314 |
|
void ccs_finish_execve(int retval) |
1315 |
|
{ |
1316 |
|
struct task_struct *task = current; |
1317 |
|
struct ccs_execve_entry *ee = ccs_find_execve_entry(); |
1318 |
|
ccs_assert_read_lock(); |
1319 |
|
task->ccs_flags &= ~CCS_CHECK_READ_FOR_OPEN_EXEC; |
1320 |
|
if (!ee) |
1321 |
|
return; |
1322 |
|
if (retval < 0) |
1323 |
|
goto out; |
1324 |
|
/* Proceed to next domain if execution suceeded. */ |
1325 |
|
task->ccs_domain_info = ee->r.domain; |
1326 |
|
/* Mark the current process as execute handler. */ |
1327 |
|
if (ee->handler) |
1328 |
|
task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER; |
1329 |
|
/* Mark the current process as normal process. */ |
1330 |
|
else |
1331 |
|
task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER; |
1332 |
|
out: |
1333 |
|
ccs_free_execve_entry(ee); |
1334 |
|
} |