blob: 9027ac1534afca2d8c58db1ee7075742f4bbaa4e [file] [log] [blame]
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09001/*
2 * security/tomoyo/domain.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09005 */
6
7#include "common.h"
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +09008#include <linux/binfmts.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +090010
11/* Variables definitions.*/
12
13/* The initial domain. */
14struct tomoyo_domain_info tomoyo_kernel_domain;
15
Tetsuo Handa237ab452010-06-12 20:46:22 +090016/**
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090017 * tomoyo_update_policy - Update an entry for exception policy.
18 *
19 * @new_entry: Pointer to "struct tomoyo_acl_info".
20 * @size: Size of @new_entry in bytes.
Tetsuo Handaa238cf52011-06-26 23:17:10 +090021 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090022 * @check_duplicate: Callback function to find duplicated entry.
23 *
24 * Returns 0 on success, negative value otherwise.
25 *
26 * Caller holds tomoyo_read_lock().
27 */
28int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +090029 struct tomoyo_acl_param *param,
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090030 bool (*check_duplicate) (const struct tomoyo_acl_head
31 *,
32 const struct tomoyo_acl_head
33 *))
34{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090035 int error = param->is_delete ? -ENOENT : -ENOMEM;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090036 struct tomoyo_acl_head *entry;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090037 struct list_head *list = param->list;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090038
39 if (mutex_lock_interruptible(&tomoyo_policy_lock))
40 return -ENOMEM;
41 list_for_each_entry_rcu(entry, list, list) {
Tetsuo Handaf9732ea2011-09-25 17:50:23 +090042 if (entry->is_deleted == TOMOYO_GC_IN_PROGRESS)
43 continue;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090044 if (!check_duplicate(entry, new_entry))
45 continue;
Tetsuo Handaa238cf52011-06-26 23:17:10 +090046 entry->is_deleted = param->is_delete;
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090047 error = 0;
48 break;
49 }
Tetsuo Handaa238cf52011-06-26 23:17:10 +090050 if (error && !param->is_delete) {
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +090051 entry = tomoyo_commit_ok(new_entry, size);
52 if (entry) {
53 list_add_tail_rcu(&entry->list, list);
54 error = 0;
55 }
56 }
57 mutex_unlock(&tomoyo_policy_lock);
58 return error;
59}
60
61/**
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090062 * tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
63 *
64 * @a: Pointer to "struct tomoyo_acl_info".
65 * @b: Pointer to "struct tomoyo_acl_info".
66 *
67 * Returns true if @a == @b, false otherwise.
68 */
69static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
70 const struct tomoyo_acl_info *b)
71{
Tetsuo Handa2066a362011-07-08 13:21:37 +090072 return a->type == b->type && a->cond == b->cond;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090073}
74
75/**
Tetsuo Handa237ab452010-06-12 20:46:22 +090076 * tomoyo_update_domain - Update an entry for domain policy.
77 *
78 * @new_entry: Pointer to "struct tomoyo_acl_info".
79 * @size: Size of @new_entry in bytes.
Tetsuo Handaa238cf52011-06-26 23:17:10 +090080 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa237ab452010-06-12 20:46:22 +090081 * @check_duplicate: Callback function to find duplicated entry.
82 * @merge_duplicate: Callback function to merge duplicated entry.
83 *
84 * Returns 0 on success, negative value otherwise.
85 *
86 * Caller holds tomoyo_read_lock().
87 */
88int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
Tetsuo Handaa238cf52011-06-26 23:17:10 +090089 struct tomoyo_acl_param *param,
Tetsuo Handa237ab452010-06-12 20:46:22 +090090 bool (*check_duplicate) (const struct tomoyo_acl_info
91 *,
92 const struct tomoyo_acl_info
93 *),
94 bool (*merge_duplicate) (struct tomoyo_acl_info *,
95 struct tomoyo_acl_info *,
96 const bool))
97{
Tetsuo Handaa238cf52011-06-26 23:17:10 +090098 const bool is_delete = param->is_delete;
Tetsuo Handa237ab452010-06-12 20:46:22 +090099 int error = is_delete ? -ENOENT : -ENOMEM;
100 struct tomoyo_acl_info *entry;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900101 struct list_head * const list = param->list;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900102
Tetsuo Handa2066a362011-07-08 13:21:37 +0900103 if (param->data[0]) {
104 new_entry->cond = tomoyo_get_condition(param);
105 if (!new_entry->cond)
106 return -EINVAL;
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900107 /*
108 * Domain transition preference is allowed for only
109 * "file execute" entries.
110 */
111 if (new_entry->cond->transit &&
112 !(new_entry->type == TOMOYO_TYPE_PATH_ACL &&
113 container_of(new_entry, struct tomoyo_path_acl, head)
114 ->perm == 1 << TOMOYO_TYPE_EXECUTE))
115 goto out;
Tetsuo Handa2066a362011-07-08 13:21:37 +0900116 }
Tetsuo Handa237ab452010-06-12 20:46:22 +0900117 if (mutex_lock_interruptible(&tomoyo_policy_lock))
Tetsuo Handa2066a362011-07-08 13:21:37 +0900118 goto out;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900119 list_for_each_entry_rcu(entry, list, list) {
Tetsuo Handaf9732ea2011-09-25 17:50:23 +0900120 if (entry->is_deleted == TOMOYO_GC_IN_PROGRESS)
121 continue;
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900122 if (!tomoyo_same_acl_head(entry, new_entry) ||
123 !check_duplicate(entry, new_entry))
Tetsuo Handa237ab452010-06-12 20:46:22 +0900124 continue;
125 if (merge_duplicate)
126 entry->is_deleted = merge_duplicate(entry, new_entry,
127 is_delete);
128 else
129 entry->is_deleted = is_delete;
130 error = 0;
131 break;
132 }
133 if (error && !is_delete) {
134 entry = tomoyo_commit_ok(new_entry, size);
135 if (entry) {
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900136 list_add_tail_rcu(&entry->list, list);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900137 error = 0;
138 }
139 }
140 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handa2066a362011-07-08 13:21:37 +0900141out:
142 tomoyo_put_condition(new_entry->cond);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900143 return error;
144}
145
Tetsuo Handa32997142011-06-26 23:19:28 +0900146/**
147 * tomoyo_check_acl - Do permission check.
148 *
149 * @r: Pointer to "struct tomoyo_request_info".
150 * @check_entry: Callback function to check type specific parameters.
151 *
152 * Returns 0 on success, negative value otherwise.
153 *
154 * Caller holds tomoyo_read_lock().
155 */
Tetsuo Handa99a85252010-06-16 16:22:51 +0900156void tomoyo_check_acl(struct tomoyo_request_info *r,
Tetsuo Handa484ca792010-07-29 14:29:55 +0900157 bool (*check_entry) (struct tomoyo_request_info *,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900158 const struct tomoyo_acl_info *))
159{
160 const struct tomoyo_domain_info *domain = r->domain;
161 struct tomoyo_acl_info *ptr;
Tetsuo Handa32997142011-06-26 23:19:28 +0900162 bool retried = false;
163 const struct list_head *list = &domain->acl_info_list;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900164
Tetsuo Handa32997142011-06-26 23:19:28 +0900165retry:
166 list_for_each_entry_rcu(ptr, list, list) {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900167 if (ptr->is_deleted || ptr->type != r->param_type)
168 continue;
Tetsuo Handa2066a362011-07-08 13:21:37 +0900169 if (!check_entry(r, ptr))
170 continue;
171 if (!tomoyo_condition(r, ptr->cond))
172 continue;
Tetsuo Handa1f067a62011-09-10 15:24:56 +0900173 r->matched_acl = ptr;
Tetsuo Handa2066a362011-07-08 13:21:37 +0900174 r->granted = true;
175 return;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900176 }
Tetsuo Handa32997142011-06-26 23:19:28 +0900177 if (!retried) {
178 retried = true;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900179 list = &domain->ns->acl_group[domain->group];
Tetsuo Handa32997142011-06-26 23:19:28 +0900180 goto retry;
181 }
Tetsuo Handa99a85252010-06-16 16:22:51 +0900182 r->granted = false;
183}
184
Tetsuo Handaa230f9e2010-06-17 16:53:24 +0900185/* The list for "struct tomoyo_domain_info". */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900186LIST_HEAD(tomoyo_domain_list);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900187
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900188/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900189 * tomoyo_last_word - Get last component of a domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900190 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900191 * @name: Domainname to check.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900192 *
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900193 * Returns the last word of @domainname.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900194 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900195static const char *tomoyo_last_word(const char *name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900196{
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900197 const char *cp = strrchr(name, ' ');
198 if (cp)
199 return cp + 1;
200 return name;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900201}
202
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900203/**
204 * tomoyo_same_transition_control - Check for duplicated "struct tomoyo_transition_control" entry.
205 *
206 * @a: Pointer to "struct tomoyo_acl_head".
207 * @b: Pointer to "struct tomoyo_acl_head".
208 *
209 * Returns true if @a == @b, false otherwise.
210 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900211static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a,
212 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900213{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900214 const struct tomoyo_transition_control *p1 = container_of(a,
215 typeof(*p1),
216 head);
217 const struct tomoyo_transition_control *p2 = container_of(b,
218 typeof(*p2),
219 head);
220 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900221 && p1->domainname == p2->domainname
222 && p1->program == p2->program;
223}
224
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900225/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900226 * tomoyo_write_transition_control - Write "struct tomoyo_transition_control" list.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900227 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900228 * @param: Pointer to "struct tomoyo_acl_param".
229 * @type: Type of this entry.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900230 *
231 * Returns 0 on success, negative value otherwise.
232 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900233int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
234 const u8 type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900235{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900236 struct tomoyo_transition_control e = { .type = type };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900237 int error = param->is_delete ? -ENOENT : -ENOMEM;
238 char *program = param->data;
239 char *domainname = strstr(program, " from ");
240 if (domainname) {
241 *domainname = '\0';
242 domainname += 6;
243 } else if (type == TOMOYO_TRANSITION_CONTROL_NO_KEEP ||
244 type == TOMOYO_TRANSITION_CONTROL_KEEP) {
245 domainname = program;
246 program = NULL;
247 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900248 if (program && strcmp(program, "any")) {
Tetsuo Handa75093152010-06-16 16:23:55 +0900249 if (!tomoyo_correct_path(program))
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900250 return -EINVAL;
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900251 e.program = tomoyo_get_name(program);
252 if (!e.program)
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900253 goto out;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900254 }
Tetsuo Handa0d2171d2011-06-26 23:17:46 +0900255 if (domainname && strcmp(domainname, "any")) {
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900256 if (!tomoyo_correct_domain(domainname)) {
257 if (!tomoyo_correct_path(domainname))
258 goto out;
259 e.is_last_name = true;
260 }
261 e.domainname = tomoyo_get_name(domainname);
262 if (!e.domainname)
263 goto out;
264 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900265 param->list = &param->ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900266 error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900267 tomoyo_same_transition_control);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900268out:
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900269 tomoyo_put_name(e.domainname);
270 tomoyo_put_name(e.program);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900271 return error;
272}
273
274/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900275 * tomoyo_scan_transition - Try to find specific domain transition type.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900276 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900277 * @list: Pointer to "struct list_head".
278 * @domainname: The name of current domain.
279 * @program: The name of requested program.
280 * @last_name: The last component of @domainname.
281 * @type: One of values in "enum tomoyo_transition_type".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900282 *
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900283 * Returns true if found one, false otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900284 *
285 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900286 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900287static inline bool tomoyo_scan_transition
288(const struct list_head *list, const struct tomoyo_path_info *domainname,
289 const struct tomoyo_path_info *program, const char *last_name,
290 const enum tomoyo_transition_type type)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900291{
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900292 const struct tomoyo_transition_control *ptr;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900293 list_for_each_entry_rcu(ptr, list, head.list) {
294 if (ptr->head.is_deleted || ptr->type != type)
295 continue;
296 if (ptr->domainname) {
297 if (!ptr->is_last_name) {
298 if (ptr->domainname != domainname)
299 continue;
300 } else {
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900301 /*
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900302 * Use direct strcmp() since this is
303 * unlikely used.
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900304 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900305 if (strcmp(ptr->domainname->name, last_name))
306 continue;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900307 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900308 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900309 if (ptr->program && tomoyo_pathcmp(ptr->program, program))
310 continue;
311 return true;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900312 }
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900313 return false;
314}
315
316/**
317 * tomoyo_transition_type - Get domain transition type.
318 *
319 * @ns: Pointer to "struct tomoyo_policy_namespace".
320 * @domainname: The name of current domain.
321 * @program: The name of requested program.
322 *
323 * Returns TOMOYO_TRANSITION_CONTROL_TRANSIT if executing @program causes
324 * domain transition across namespaces, TOMOYO_TRANSITION_CONTROL_INITIALIZE if
325 * executing @program reinitializes domain transition within that namespace,
326 * TOMOYO_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
327 * others otherwise.
328 *
329 * Caller holds tomoyo_read_lock().
330 */
331static enum tomoyo_transition_type tomoyo_transition_type
332(const struct tomoyo_policy_namespace *ns,
333 const struct tomoyo_path_info *domainname,
334 const struct tomoyo_path_info *program)
335{
336 const char *last_name = tomoyo_last_word(domainname->name);
337 enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET;
338 while (type < TOMOYO_MAX_TRANSITION_TYPE) {
339 const struct list_head * const list =
340 &ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
341 if (!tomoyo_scan_transition(list, domainname, program,
342 last_name, type)) {
343 type++;
344 continue;
345 }
346 if (type != TOMOYO_TRANSITION_CONTROL_NO_RESET &&
347 type != TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE)
348 break;
349 /*
350 * Do not check for reset_domain if no_reset_domain matched.
351 * Do not check for initialize_domain if no_initialize_domain
352 * matched.
353 */
354 type++;
355 type++;
356 }
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900357 return type;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900358}
359
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900360/**
361 * tomoyo_same_aggregator - Check for duplicated "struct tomoyo_aggregator" entry.
362 *
363 * @a: Pointer to "struct tomoyo_acl_head".
364 * @b: Pointer to "struct tomoyo_acl_head".
365 *
366 * Returns true if @a == @b, false otherwise.
367 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900368static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a,
369 const struct tomoyo_acl_head *b)
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900370{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900371 const struct tomoyo_aggregator *p1 = container_of(a, typeof(*p1),
372 head);
373 const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
374 head);
Tetsuo Handa36f5e1f2010-06-15 09:23:26 +0900375 return p1->original_name == p2->original_name &&
376 p1->aggregated_name == p2->aggregated_name;
377}
378
Tetsuo Handa10843072010-06-03 20:38:03 +0900379/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900380 * tomoyo_write_aggregator - Write "struct tomoyo_aggregator" list.
Tetsuo Handa10843072010-06-03 20:38:03 +0900381 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900382 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handa10843072010-06-03 20:38:03 +0900383 *
384 * Returns 0 on success, negative value otherwise.
385 *
386 * Caller holds tomoyo_read_lock().
387 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900388int tomoyo_write_aggregator(struct tomoyo_acl_param *param)
Tetsuo Handa10843072010-06-03 20:38:03 +0900389{
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900390 struct tomoyo_aggregator e = { };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900391 int error = param->is_delete ? -ENOENT : -ENOMEM;
392 const char *original_name = tomoyo_read_token(param);
393 const char *aggregated_name = tomoyo_read_token(param);
394 if (!tomoyo_correct_word(original_name) ||
Tetsuo Handa75093152010-06-16 16:23:55 +0900395 !tomoyo_correct_path(aggregated_name))
Tetsuo Handa10843072010-06-03 20:38:03 +0900396 return -EINVAL;
397 e.original_name = tomoyo_get_name(original_name);
398 e.aggregated_name = tomoyo_get_name(aggregated_name);
399 if (!e.original_name || !e.aggregated_name ||
400 e.aggregated_name->is_patterned) /* No patterns allowed. */
401 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900402 param->list = &param->ns->policy_list[TOMOYO_ID_AGGREGATOR];
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900403 error = tomoyo_update_policy(&e.head, sizeof(e), param,
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900404 tomoyo_same_aggregator);
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900405out:
Tetsuo Handa10843072010-06-03 20:38:03 +0900406 tomoyo_put_name(e.original_name);
407 tomoyo_put_name(e.aggregated_name);
408 return error;
409}
410
411/**
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900412 * tomoyo_find_namespace - Find specified namespace.
413 *
414 * @name: Name of namespace to find.
415 * @len: Length of @name.
416 *
417 * Returns pointer to "struct tomoyo_policy_namespace" if found,
418 * NULL otherwise.
419 *
420 * Caller holds tomoyo_read_lock().
421 */
422static struct tomoyo_policy_namespace *tomoyo_find_namespace
423(const char *name, const unsigned int len)
424{
425 struct tomoyo_policy_namespace *ns;
426 list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
427 if (strncmp(name, ns->name, len) ||
428 (name[len] && name[len] != ' '))
429 continue;
430 return ns;
431 }
432 return NULL;
433}
434
435/**
436 * tomoyo_assign_namespace - Create a new namespace.
437 *
438 * @domainname: Name of namespace to create.
439 *
440 * Returns pointer to "struct tomoyo_policy_namespace" on success,
441 * NULL otherwise.
442 *
443 * Caller holds tomoyo_read_lock().
444 */
445struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname)
446{
447 struct tomoyo_policy_namespace *ptr;
448 struct tomoyo_policy_namespace *entry;
449 const char *cp = domainname;
450 unsigned int len = 0;
451 while (*cp && *cp++ != ' ')
452 len++;
453 ptr = tomoyo_find_namespace(domainname, len);
454 if (ptr)
455 return ptr;
456 if (len >= TOMOYO_EXEC_TMPSIZE - 10 || !tomoyo_domain_def(domainname))
457 return NULL;
458 entry = kzalloc(sizeof(*entry) + len + 1, GFP_NOFS);
459 if (!entry)
460 return NULL;
461 if (mutex_lock_interruptible(&tomoyo_policy_lock))
462 goto out;
463 ptr = tomoyo_find_namespace(domainname, len);
464 if (!ptr && tomoyo_memory_ok(entry)) {
465 char *name = (char *) (entry + 1);
466 ptr = entry;
467 memmove(name, domainname, len);
468 name[len] = '\0';
469 entry->name = name;
470 tomoyo_init_policy_namespace(entry);
471 entry = NULL;
472 }
473 mutex_unlock(&tomoyo_policy_lock);
474out:
475 kfree(entry);
476 return ptr;
477}
478
479/**
480 * tomoyo_namespace_jump - Check for namespace jump.
481 *
482 * @domainname: Name of domain.
483 *
484 * Returns true if namespace differs, false otherwise.
485 */
486static bool tomoyo_namespace_jump(const char *domainname)
487{
488 const char *namespace = tomoyo_current_namespace()->name;
489 const int len = strlen(namespace);
490 return strncmp(domainname, namespace, len) ||
491 (domainname[len] && domainname[len] != ' ');
492}
493
494/**
495 * tomoyo_assign_domain - Create a domain or a namespace.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900496 *
497 * @domainname: The name of domain.
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900498 * @transit: True if transit to domain found or created.
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900499 *
500 * Returns pointer to "struct tomoyo_domain_info" on success, NULL otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900501 *
502 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900503 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900504struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900505 const bool transit)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900506{
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900507 struct tomoyo_domain_info e = { };
508 struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname);
509 bool created = false;
510 if (entry) {
511 if (transit) {
512 /*
513 * Since namespace is created at runtime, profiles may
514 * not be created by the moment the process transits to
515 * that domain. Do not perform domain transition if
516 * profile for that domain is not yet created.
517 */
Tetsuo Handae00fb3f2011-09-27 11:48:53 +0900518 if (tomoyo_policy_loaded &&
519 !entry->ns->profile_ptr[entry->profile])
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900520 return NULL;
521 }
522 return entry;
523 }
524 /* Requested domain does not exist. */
525 /* Don't create requested domain if domainname is invalid. */
526 if (strlen(domainname) >= TOMOYO_EXEC_TMPSIZE - 10 ||
527 !tomoyo_correct_domain(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900528 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900529 /*
530 * Since definition of profiles and acl_groups may differ across
531 * namespaces, do not inherit "use_profile" and "use_group" settings
532 * by automatically creating requested domain upon domain transition.
533 */
534 if (transit && tomoyo_namespace_jump(domainname))
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900535 return NULL;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900536 e.ns = tomoyo_assign_namespace(domainname);
537 if (!e.ns)
538 return NULL;
539 /*
540 * "use_profile" and "use_group" settings for automatically created
541 * domains are inherited from current domain. These are 0 for manually
542 * created domains.
543 */
544 if (transit) {
545 const struct tomoyo_domain_info *domain = tomoyo_domain();
546 e.profile = domain->profile;
547 e.group = domain->group;
548 }
549 e.domainname = tomoyo_get_name(domainname);
550 if (!e.domainname)
551 return NULL;
Tetsuo Handa29282382010-05-06 00:18:15 +0900552 if (mutex_lock_interruptible(&tomoyo_policy_lock))
553 goto out;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900554 entry = tomoyo_find_domain(domainname);
555 if (!entry) {
556 entry = tomoyo_commit_ok(&e, sizeof(e));
557 if (entry) {
558 INIT_LIST_HEAD(&entry->acl_info_list);
559 list_add_tail_rcu(&entry->list, &tomoyo_domain_list);
560 created = true;
561 }
Tetsuo Handaca0b7df2010-02-07 20:23:59 +0900562 }
Tetsuo Handaf737d952010-01-03 21:16:32 +0900563 mutex_unlock(&tomoyo_policy_lock);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900564out:
565 tomoyo_put_name(e.domainname);
566 if (entry && transit) {
567 if (created) {
568 struct tomoyo_request_info r;
569 tomoyo_init_request_info(&r, entry,
570 TOMOYO_MAC_FILE_EXECUTE);
571 r.granted = false;
572 tomoyo_write_log(&r, "use_profile %u\n",
573 entry->profile);
574 tomoyo_write_log(&r, "use_group %u\n", entry->group);
Tetsuo Handa778c4a42011-09-25 17:49:09 +0900575 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900576 }
577 }
578 return entry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900579}
580
581/**
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900582 * tomoyo_environ - Check permission for environment variable names.
583 *
584 * @ee: Pointer to "struct tomoyo_execve".
585 *
586 * Returns 0 on success, negative value otherwise.
587 */
588static int tomoyo_environ(struct tomoyo_execve *ee)
589{
590 struct tomoyo_request_info *r = &ee->r;
591 struct linux_binprm *bprm = ee->bprm;
592 /* env_page.data is allocated by tomoyo_dump_page(). */
593 struct tomoyo_page_dump env_page = { };
594 char *arg_ptr; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
595 int arg_len = 0;
596 unsigned long pos = bprm->p;
597 int offset = pos % PAGE_SIZE;
598 int argv_count = bprm->argc;
599 int envp_count = bprm->envc;
600 int error = -ENOMEM;
601
602 ee->r.type = TOMOYO_MAC_ENVIRON;
603 ee->r.profile = r->domain->profile;
604 ee->r.mode = tomoyo_get_mode(r->domain->ns, ee->r.profile,
605 TOMOYO_MAC_ENVIRON);
606 if (!r->mode || !envp_count)
607 return 0;
608 arg_ptr = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
609 if (!arg_ptr)
610 goto out;
611 while (error == -ENOMEM) {
612 if (!tomoyo_dump_page(bprm, pos, &env_page))
613 goto out;
614 pos += PAGE_SIZE - offset;
615 /* Read. */
616 while (argv_count && offset < PAGE_SIZE) {
617 if (!env_page.data[offset++])
618 argv_count--;
619 }
620 if (argv_count) {
621 offset = 0;
622 continue;
623 }
624 while (offset < PAGE_SIZE) {
625 const unsigned char c = env_page.data[offset++];
626
627 if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) {
628 if (c == '=') {
629 arg_ptr[arg_len++] = '\0';
630 } else if (c == '\\') {
631 arg_ptr[arg_len++] = '\\';
632 arg_ptr[arg_len++] = '\\';
633 } else if (c > ' ' && c < 127) {
634 arg_ptr[arg_len++] = c;
635 } else {
636 arg_ptr[arg_len++] = '\\';
637 arg_ptr[arg_len++] = (c >> 6) + '0';
638 arg_ptr[arg_len++]
639 = ((c >> 3) & 7) + '0';
640 arg_ptr[arg_len++] = (c & 7) + '0';
641 }
642 } else {
643 arg_ptr[arg_len] = '\0';
644 }
645 if (c)
646 continue;
647 if (tomoyo_env_perm(r, arg_ptr)) {
648 error = -EPERM;
649 break;
650 }
651 if (!--envp_count) {
652 error = 0;
653 break;
654 }
655 arg_len = 0;
656 }
657 offset = 0;
658 }
659out:
660 if (r->mode != TOMOYO_CONFIG_ENFORCING)
661 error = 0;
662 kfree(env_page.data);
663 kfree(arg_ptr);
664 return error;
665}
666
667/**
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900668 * tomoyo_find_next_domain - Find a domain.
669 *
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900670 * @bprm: Pointer to "struct linux_binprm".
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900671 *
672 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900673 *
674 * Caller holds tomoyo_read_lock().
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900675 */
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900676int tomoyo_find_next_domain(struct linux_binprm *bprm)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900677{
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900678 struct tomoyo_domain_info *old_domain = tomoyo_domain();
679 struct tomoyo_domain_info *domain = NULL;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900680 const char *original_name = bprm->filename;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900681 int retval = -ENOMEM;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900682 bool reject_on_transition_failure = false;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900683 const struct tomoyo_path_info *candidate;
684 struct tomoyo_path_info exename;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900685 struct tomoyo_execve *ee = kzalloc(sizeof(*ee), GFP_NOFS);
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900686
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900687 if (!ee)
688 return -ENOMEM;
689 ee->tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
690 if (!ee->tmp) {
691 kfree(ee);
692 return -ENOMEM;
693 }
694 /* ee->dump->data is allocated by tomoyo_dump_page(). */
695 tomoyo_init_request_info(&ee->r, NULL, TOMOYO_MAC_FILE_EXECUTE);
696 ee->r.ee = ee;
697 ee->bprm = bprm;
698 ee->r.obj = &ee->obj;
699 ee->obj.path1 = bprm->file->f_path;
Tetsuo Handa0617c7f2010-06-21 09:58:53 +0900700 /* Get symlink's pathname of program. */
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900701 retval = -ENOENT;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900702 exename.name = tomoyo_realpath_nofollow(original_name);
703 if (!exename.name)
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900704 goto out;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900705 tomoyo_fill_path_info(&exename);
706retry:
Tetsuo Handa10843072010-06-03 20:38:03 +0900707 /* Check 'aggregator' directive. */
708 {
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900709 struct tomoyo_aggregator *ptr;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900710 struct list_head *list =
711 &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR];
712 /* Check 'aggregator' directive. */
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900713 candidate = &exename;
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900714 list_for_each_entry_rcu(ptr, list, head.list) {
Tetsuo Handa82e0f002010-06-15 09:22:42 +0900715 if (ptr->head.is_deleted ||
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900716 !tomoyo_path_matches_pattern(&exename,
Tetsuo Handa10843072010-06-03 20:38:03 +0900717 ptr->original_name))
718 continue;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900719 candidate = ptr->aggregated_name;
Tetsuo Handa10843072010-06-03 20:38:03 +0900720 break;
721 }
722 }
723
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900724 /* Check execute permission. */
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900725 retval = tomoyo_execute_permission(&ee->r, candidate);
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900726 if (retval == TOMOYO_RETRY_REQUEST)
727 goto retry;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900728 if (retval < 0)
729 goto out;
Tetsuo Handa484ca792010-07-29 14:29:55 +0900730 /*
731 * To be able to specify domainnames with wildcards, use the
732 * pathname specified in the policy (which may contain
733 * wildcard) rather than the pathname passed to execve()
734 * (which never contains wildcard).
735 */
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900736 if (ee->r.param.path.matched_path)
737 candidate = ee->r.param.path.matched_path;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900738
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900739 /*
740 * Check for domain transition preference if "file execute" matched.
741 * If preference is given, make do_execve() fail if domain transition
742 * has failed, for domain transition preference should be used with
743 * destination domain defined.
744 */
745 if (ee->transition) {
746 const char *domainname = ee->transition->name;
747 reject_on_transition_failure = true;
748 if (!strcmp(domainname, "keep"))
749 goto force_keep_domain;
750 if (!strcmp(domainname, "child"))
751 goto force_child_domain;
752 if (!strcmp(domainname, "reset"))
753 goto force_reset_domain;
754 if (!strcmp(domainname, "initialize"))
755 goto force_initialize_domain;
756 if (!strcmp(domainname, "parent")) {
757 char *cp;
758 strncpy(ee->tmp, old_domain->domainname->name,
759 TOMOYO_EXEC_TMPSIZE - 1);
760 cp = strrchr(ee->tmp, ' ');
761 if (cp)
762 *cp = '\0';
763 } else if (*domainname == '<')
764 strncpy(ee->tmp, domainname, TOMOYO_EXEC_TMPSIZE - 1);
765 else
766 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
767 old_domain->domainname->name, domainname);
768 goto force_jump_domain;
769 }
770 /*
771 * No domain transition preference specified.
772 * Calculate domain to transit to.
773 */
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900774 switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname,
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900775 candidate)) {
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900776 case TOMOYO_TRANSITION_CONTROL_RESET:
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900777force_reset_domain:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900778 /* Transit to the root of specified namespace. */
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900779 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>",
780 candidate->name);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900781 /*
782 * Make do_execve() fail if domain transition across namespaces
783 * has failed.
784 */
785 reject_on_transition_failure = true;
786 break;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900787 case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900788force_initialize_domain:
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900789 /* Transit to the child of current namespace's root. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900790 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900791 old_domain->ns->name, candidate->name);
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900792 break;
793 case TOMOYO_TRANSITION_CONTROL_KEEP:
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900794force_keep_domain:
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900795 /* Keep current domain. */
796 domain = old_domain;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900797 break;
798 default:
799 if (old_domain == &tomoyo_kernel_domain &&
800 !tomoyo_policy_loaded) {
801 /*
802 * Needn't to transit from kernel domain before
803 * starting /sbin/init. But transit from kernel domain
804 * if executing initializers because they might start
805 * before /sbin/init.
806 */
807 domain = old_domain;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900808 break;
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900809 }
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900810force_child_domain:
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900811 /* Normal domain transition. */
812 snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
813 old_domain->domainname->name, candidate->name);
Tetsuo Handa5448ec42010-06-21 11:14:39 +0900814 break;
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900815 }
Tetsuo Handa6bce98e2011-09-16 22:54:25 +0900816force_jump_domain:
Tetsuo Handa7c759642011-06-26 23:15:31 +0900817 if (!domain)
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900818 domain = tomoyo_assign_domain(ee->tmp, true);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900819 if (domain)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900820 retval = 0;
821 else if (reject_on_transition_failure) {
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900822 printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n",
823 ee->tmp);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900824 retval = -ENOMEM;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900825 } else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING)
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900826 retval = -ENOMEM;
827 else {
828 retval = 0;
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900829 if (!old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED]) {
830 old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED] = true;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900831 ee->r.granted = false;
832 tomoyo_write_log(&ee->r, "%s", tomoyo_dif
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900833 [TOMOYO_DIF_TRANSITION_FAILED]);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900834 printk(KERN_WARNING
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900835 "ERROR: Domain '%s' not defined.\n", ee->tmp);
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900836 }
837 }
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900838 out:
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900839 if (!domain)
840 domain = old_domain;
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900841 /* Update reference count on "struct tomoyo_domain_info". */
842 atomic_inc(&domain->users);
Tetsuo Handa56f8c9bc2009-06-19 14:13:27 +0900843 bprm->cred->security = domain;
Tetsuo Handaa8f76402011-09-10 15:27:12 +0900844 kfree(exename.name);
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900845 if (!retval) {
846 ee->r.domain = domain;
847 retval = tomoyo_environ(ee);
848 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900849 kfree(ee->tmp);
850 kfree(ee->dump.data);
851 kfree(ee);
Kentaro Takeda26a2a1c2009-02-05 17:18:15 +0900852 return retval;
853}
Tetsuo Handa5b636852011-07-08 13:24:54 +0900854
855/**
856 * tomoyo_dump_page - Dump a page to buffer.
857 *
858 * @bprm: Pointer to "struct linux_binprm".
859 * @pos: Location to dump.
860 * @dump: Poiner to "struct tomoyo_page_dump".
861 *
862 * Returns true on success, false otherwise.
863 */
864bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
865 struct tomoyo_page_dump *dump)
866{
867 struct page *page;
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900868
869 /* dump->data is released by tomoyo_find_next_domain(). */
Tetsuo Handa5b636852011-07-08 13:24:54 +0900870 if (!dump->data) {
871 dump->data = kzalloc(PAGE_SIZE, GFP_NOFS);
872 if (!dump->data)
873 return false;
874 }
875 /* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
876#ifdef CONFIG_MMU
877 if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
878 return false;
879#else
880 page = bprm->page[pos / PAGE_SIZE];
881#endif
882 if (page != dump->page) {
883 const unsigned int offset = pos % PAGE_SIZE;
884 /*
885 * Maybe kmap()/kunmap() should be used here.
886 * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
887 * So do I.
888 */
889 char *kaddr = kmap_atomic(page, KM_USER0);
Tetsuo Handad58e0da2011-09-10 15:22:48 +0900890
Tetsuo Handa5b636852011-07-08 13:24:54 +0900891 dump->page = page;
892 memcpy(dump->data + offset, kaddr + offset,
893 PAGE_SIZE - offset);
894 kunmap_atomic(kaddr, KM_USER0);
895 }
896 /* Same with put_arg_page(page) in fs/exec.c */
897#ifdef CONFIG_MMU
898 put_page(page);
899#endif
900 return true;
901}