Blame view

security/tomoyo/condition.c 27.1 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
2
3
4
5
6
7
8
9
10
11
12
13
14
  /*
   * security/tomoyo/condition.c
   *
   * Copyright (C) 2005-2011  NTT DATA CORPORATION
   */
  
  #include "common.h"
  #include <linux/slab.h>
  
  /* List of "struct tomoyo_condition". */
  LIST_HEAD(tomoyo_condition_list);
  
  /**
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   * tomoyo_argv - Check argv[] in "struct linux_binbrm".
   *
   * @index:   Index number of @arg_ptr.
   * @arg_ptr: Contents of argv[@index].
   * @argc:    Length of @argv.
   * @argv:    Pointer to "struct tomoyo_argv".
   * @checked: Set to true if @argv[@index] was found.
   *
   * Returns true on success, false otherwise.
   */
  static bool tomoyo_argv(const unsigned int index, const char *arg_ptr,
  			const int argc, const struct tomoyo_argv *argv,
  			u8 *checked)
  {
  	int i;
  	struct tomoyo_path_info arg;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
31

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
32
33
34
  	arg.name = arg_ptr;
  	for (i = 0; i < argc; argv++, checked++, i++) {
  		bool result;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
35

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  		if (index != argv->index)
  			continue;
  		*checked = 1;
  		tomoyo_fill_path_info(&arg);
  		result = tomoyo_path_matches_pattern(&arg, argv->value);
  		if (argv->is_not)
  			result = !result;
  		if (!result)
  			return false;
  	}
  	return true;
  }
  
  /**
   * tomoyo_envp - Check envp[] in "struct linux_binbrm".
   *
   * @env_name:  The name of environment variable.
   * @env_value: The value of environment variable.
   * @envc:      Length of @envp.
   * @envp:      Pointer to "struct tomoyo_envp".
   * @checked:   Set to true if @envp[@env_name] was found.
   *
   * Returns true on success, false otherwise.
   */
  static bool tomoyo_envp(const char *env_name, const char *env_value,
  			const int envc, const struct tomoyo_envp *envp,
  			u8 *checked)
  {
  	int i;
  	struct tomoyo_path_info name;
  	struct tomoyo_path_info value;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
67

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
68
69
70
71
72
73
  	name.name = env_name;
  	tomoyo_fill_path_info(&name);
  	value.name = env_value;
  	tomoyo_fill_path_info(&value);
  	for (i = 0; i < envc; envp++, checked++, i++) {
  		bool result;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
74

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  		if (!tomoyo_path_matches_pattern(&name, envp->name))
  			continue;
  		*checked = 1;
  		if (envp->value) {
  			result = tomoyo_path_matches_pattern(&value,
  							     envp->value);
  			if (envp->is_not)
  				result = !result;
  		} else {
  			result = true;
  			if (!envp->is_not)
  				result = !result;
  		}
  		if (!result)
  			return false;
  	}
  	return true;
  }
  
  /**
   * tomoyo_scan_bprm - Scan "struct linux_binprm".
   *
   * @ee:   Pointer to "struct tomoyo_execve".
   * @argc: Length of @argc.
   * @argv: Pointer to "struct tomoyo_argv".
   * @envc: Length of @envp.
   * @envp: Poiner to "struct tomoyo_envp".
   *
   * Returns true on success, false otherwise.
   */
  static bool tomoyo_scan_bprm(struct tomoyo_execve *ee,
  			     const u16 argc, const struct tomoyo_argv *argv,
  			     const u16 envc, const struct tomoyo_envp *envp)
  {
  	struct linux_binprm *bprm = ee->bprm;
  	struct tomoyo_page_dump *dump = &ee->dump;
  	char *arg_ptr = ee->tmp;
  	int arg_len = 0;
  	unsigned long pos = bprm->p;
  	int offset = pos % PAGE_SIZE;
  	int argv_count = bprm->argc;
  	int envp_count = bprm->envc;
  	bool result = true;
  	u8 local_checked[32];
  	u8 *checked;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
120

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  	if (argc + envc <= sizeof(local_checked)) {
  		checked = local_checked;
  		memset(local_checked, 0, sizeof(local_checked));
  	} else {
  		checked = kzalloc(argc + envc, GFP_NOFS);
  		if (!checked)
  			return false;
  	}
  	while (argv_count || envp_count) {
  		if (!tomoyo_dump_page(bprm, pos, dump)) {
  			result = false;
  			goto out;
  		}
  		pos += PAGE_SIZE - offset;
  		while (offset < PAGE_SIZE) {
  			/* Read. */
  			const char *kaddr = dump->data;
  			const unsigned char c = kaddr[offset++];
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
139

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  			if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) {
  				if (c == '\\') {
  					arg_ptr[arg_len++] = '\\';
  					arg_ptr[arg_len++] = '\\';
  				} else if (c > ' ' && c < 127) {
  					arg_ptr[arg_len++] = c;
  				} else {
  					arg_ptr[arg_len++] = '\\';
  					arg_ptr[arg_len++] = (c >> 6) + '0';
  					arg_ptr[arg_len++] =
  						((c >> 3) & 7) + '0';
  					arg_ptr[arg_len++] = (c & 7) + '0';
  				}
  			} else {
  				arg_ptr[arg_len] = '\0';
  			}
  			if (c)
  				continue;
  			/* Check. */
  			if (argv_count) {
  				if (!tomoyo_argv(bprm->argc - argv_count,
  						 arg_ptr, argc, argv,
  						 checked)) {
  					result = false;
  					break;
  				}
  				argv_count--;
  			} else if (envp_count) {
  				char *cp = strchr(arg_ptr, '=');
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
169

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  				if (cp) {
  					*cp = '\0';
  					if (!tomoyo_envp(arg_ptr, cp + 1,
  							 envc, envp,
  							 checked + argc)) {
  						result = false;
  						break;
  					}
  				}
  				envp_count--;
  			} else {
  				break;
  			}
  			arg_len = 0;
  		}
  		offset = 0;
  		if (!result)
  			break;
  	}
  out:
  	if (result) {
  		int i;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
192

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  		/* Check not-yet-checked entries. */
  		for (i = 0; i < argc; i++) {
  			if (checked[i])
  				continue;
  			/*
  			 * Return true only if all unchecked indexes in
  			 * bprm->argv[] are not matched.
  			 */
  			if (argv[i].is_not)
  				continue;
  			result = false;
  			break;
  		}
  		for (i = 0; i < envc; envp++, i++) {
  			if (checked[argc + i])
  				continue;
  			/*
  			 * Return true only if all unchecked environ variables
  			 * in bprm->envp[] are either undefined or not matched.
  			 */
  			if ((!envp->value && !envp->is_not) ||
  			    (envp->value && envp->is_not))
  				continue;
  			result = false;
  			break;
  		}
  	}
  	if (checked != local_checked)
  		kfree(checked);
  	return result;
  }
  
  /**
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
   * tomoyo_scan_exec_realpath - Check "exec.realpath" parameter of "struct tomoyo_condition".
   *
   * @file:  Pointer to "struct file".
   * @ptr:   Pointer to "struct tomoyo_name_union".
   * @match: True if "exec.realpath=", false if "exec.realpath!=".
   *
   * Returns true on success, false otherwise.
   */
  static bool tomoyo_scan_exec_realpath(struct file *file,
  				      const struct tomoyo_name_union *ptr,
  				      const bool match)
  {
  	bool result;
  	struct tomoyo_path_info exe;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
240

2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  	if (!file)
  		return false;
  	exe.name = tomoyo_realpath_from_path(&file->f_path);
  	if (!exe.name)
  		return false;
  	tomoyo_fill_path_info(&exe);
  	result = tomoyo_compare_name_union(&exe, ptr);
  	kfree(exe.name);
  	return result == match;
  }
  
  /**
   * tomoyo_get_dqword - tomoyo_get_name() for a quoted string.
   *
   * @start: String to save.
   *
   * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
   */
  static const struct tomoyo_path_info *tomoyo_get_dqword(char *start)
  {
  	char *cp = start + strlen(start) - 1;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
262

2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  	if (cp == start || *start++ != '"' || *cp != '"')
  		return NULL;
  	*cp = '\0';
  	if (*start && !tomoyo_correct_word(start))
  		return NULL;
  	return tomoyo_get_name(start);
  }
  
  /**
   * tomoyo_parse_name_union_quoted - Parse a quoted word.
   *
   * @param: Pointer to "struct tomoyo_acl_param".
   * @ptr:   Pointer to "struct tomoyo_name_union".
   *
   * Returns true on success, false otherwise.
   */
  static bool tomoyo_parse_name_union_quoted(struct tomoyo_acl_param *param,
  					   struct tomoyo_name_union *ptr)
  {
  	char *filename = param->data;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
283

2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
284
285
286
287
288
289
290
  	if (*filename == '@')
  		return tomoyo_parse_name_union(param, ptr);
  	ptr->filename = tomoyo_get_dqword(filename);
  	return ptr->filename != NULL;
  }
  
  /**
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
   * tomoyo_parse_argv - Parse an argv[] condition part.
   *
   * @left:  Lefthand value.
   * @right: Righthand value.
   * @argv:  Pointer to "struct tomoyo_argv".
   *
   * Returns true on success, false otherwise.
   */
  static bool tomoyo_parse_argv(char *left, char *right,
  			      struct tomoyo_argv *argv)
  {
  	if (tomoyo_parse_ulong(&argv->index, &left) !=
  	    TOMOYO_VALUE_TYPE_DECIMAL || *left++ != ']' || *left)
  		return false;
  	argv->value = tomoyo_get_dqword(right);
  	return argv->value != NULL;
  }
  
  /**
   * tomoyo_parse_envp - Parse an envp[] condition part.
   *
   * @left:  Lefthand value.
   * @right: Righthand value.
   * @envp:  Pointer to "struct tomoyo_envp".
   *
   * Returns true on success, false otherwise.
   */
  static bool tomoyo_parse_envp(char *left, char *right,
  			      struct tomoyo_envp *envp)
  {
  	const struct tomoyo_path_info *name;
  	const struct tomoyo_path_info *value;
  	char *cp = left + strlen(left) - 1;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
324

5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  	if (*cp-- != ']' || *cp != '"')
  		goto out;
  	*cp = '\0';
  	if (!tomoyo_correct_word(left))
  		goto out;
  	name = tomoyo_get_name(left);
  	if (!name)
  		goto out;
  	if (!strcmp(right, "NULL")) {
  		value = NULL;
  	} else {
  		value = tomoyo_get_dqword(right);
  		if (!value) {
  			tomoyo_put_name(name);
  			goto out;
  		}
  	}
  	envp->name = name;
  	envp->value = value;
  	return true;
  out:
  	return false;
  }
  
  /**
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
350
351
352
353
354
355
356
357
358
359
360
361
   * tomoyo_same_condition - Check for duplicated "struct tomoyo_condition" entry.
   *
   * @a: Pointer to "struct tomoyo_condition".
   * @b: Pointer to "struct tomoyo_condition".
   *
   * Returns true if @a == @b, false otherwise.
   */
  static inline bool tomoyo_same_condition(const struct tomoyo_condition *a,
  					 const struct tomoyo_condition *b)
  {
  	return a->size == b->size && a->condc == b->condc &&
  		a->numbers_count == b->numbers_count &&
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
362
  		a->names_count == b->names_count &&
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
363
  		a->argc == b->argc && a->envc == b->envc &&
6bce98edc   Tetsuo Handa   TOMOYO: Allow spe...
364
  		a->grant_log == b->grant_log && a->transit == b->transit &&
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  		!memcmp(a + 1, b + 1, a->size - sizeof(*a));
  }
  
  /**
   * tomoyo_condition_type - Get condition type.
   *
   * @word: Keyword string.
   *
   * Returns one of values in "enum tomoyo_conditions_index" on success,
   * TOMOYO_MAX_CONDITION_KEYWORD otherwise.
   */
  static u8 tomoyo_condition_type(const char *word)
  {
  	u8 i;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
379

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  	for (i = 0; i < TOMOYO_MAX_CONDITION_KEYWORD; i++) {
  		if (!strcmp(word, tomoyo_condition_keyword[i]))
  			break;
  	}
  	return i;
  }
  
  /* Define this to enable debug mode. */
  /* #define DEBUG_CONDITION */
  
  #ifdef DEBUG_CONDITION
  #define dprintk printk
  #else
  #define dprintk(...) do { } while (0)
  #endif
  
  /**
   * tomoyo_commit_condition - Commit "struct tomoyo_condition".
   *
   * @entry: Pointer to "struct tomoyo_condition".
   *
   * Returns pointer to "struct tomoyo_condition" on success, NULL otherwise.
   *
   * This function merges duplicated entries. This function returns NULL if
   * @entry is not duplicated but memory quota for policy has exceeded.
   */
  static struct tomoyo_condition *tomoyo_commit_condition
  (struct tomoyo_condition *entry)
  {
  	struct tomoyo_condition *ptr;
  	bool found = false;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
411

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
412
413
414
415
416
417
418
  	if (mutex_lock_interruptible(&tomoyo_policy_lock)) {
  		dprintk(KERN_WARNING "%u: %s failed
  ", __LINE__, __func__);
  		ptr = NULL;
  		found = true;
  		goto out;
  	}
f9732ea14   Tetsuo Handa   TOMOYO: Simplify ...
419
420
421
  	list_for_each_entry(ptr, &tomoyo_condition_list, head.list) {
  		if (!tomoyo_same_condition(ptr, entry) ||
  		    atomic_read(&ptr->head.users) == TOMOYO_GC_IN_PROGRESS)
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
422
423
424
425
426
427
428
429
430
  			continue;
  		/* Same entry found. Share this entry. */
  		atomic_inc(&ptr->head.users);
  		found = true;
  		break;
  	}
  	if (!found) {
  		if (tomoyo_memory_ok(entry)) {
  			atomic_set(&entry->head.users, 1);
f9732ea14   Tetsuo Handa   TOMOYO: Simplify ...
431
  			list_add(&entry->head.list, &tomoyo_condition_list);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
  		} else {
  			found = true;
  			ptr = NULL;
  		}
  	}
  	mutex_unlock(&tomoyo_policy_lock);
  out:
  	if (found) {
  		tomoyo_del_condition(&entry->head.list);
  		kfree(entry);
  		entry = ptr;
  	}
  	return entry;
  }
  
  /**
6bce98edc   Tetsuo Handa   TOMOYO: Allow spe...
448
449
450
451
452
453
454
455
456
457
458
459
   * tomoyo_get_transit_preference - Parse domain transition preference for execve().
   *
   * @param: Pointer to "struct tomoyo_acl_param".
   * @e:     Pointer to "struct tomoyo_condition".
   *
   * Returns the condition string part.
   */
  static char *tomoyo_get_transit_preference(struct tomoyo_acl_param *param,
  					   struct tomoyo_condition *e)
  {
  	char * const pos = param->data;
  	bool flag;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
460

6bce98edc   Tetsuo Handa   TOMOYO: Allow spe...
461
462
463
464
465
466
  	if (*pos == '<') {
  		e->transit = tomoyo_get_domainname(param);
  		goto done;
  	}
  	{
  		char *cp = strchr(pos, ' ');
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
467

6bce98edc   Tetsuo Handa   TOMOYO: Allow spe...
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
  		if (cp)
  			*cp = '\0';
  		flag = tomoyo_correct_path(pos) || !strcmp(pos, "keep") ||
  			!strcmp(pos, "initialize") || !strcmp(pos, "reset") ||
  			!strcmp(pos, "child") || !strcmp(pos, "parent");
  		if (cp)
  			*cp = ' ';
  	}
  	if (!flag)
  		return pos;
  	e->transit = tomoyo_get_name(tomoyo_read_token(param));
  done:
  	if (e->transit)
  		return param->data;
  	/*
  	 * Return a bad read-only condition string that will let
  	 * tomoyo_get_condition() return NULL.
  	 */
  	return "/";
  }
  
  /**
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
490
491
492
493
494
495
496
497
498
499
500
   * tomoyo_get_condition - Parse condition part.
   *
   * @param: Pointer to "struct tomoyo_acl_param".
   *
   * Returns pointer to "struct tomoyo_condition" on success, NULL otherwise.
   */
  struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param)
  {
  	struct tomoyo_condition *entry = NULL;
  	struct tomoyo_condition_element *condp = NULL;
  	struct tomoyo_number_union *numbers_p = NULL;
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
501
  	struct tomoyo_name_union *names_p = NULL;
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
502
503
  	struct tomoyo_argv *argv = NULL;
  	struct tomoyo_envp *envp = NULL;
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
504
  	struct tomoyo_condition e = { };
6bce98edc   Tetsuo Handa   TOMOYO: Allow spe...
505
506
  	char * const start_of_string =
  		tomoyo_get_transit_preference(param, &e);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
507
508
  	char * const end_of_string = start_of_string + strlen(start_of_string);
  	char *pos;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
509

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
510
511
512
513
514
515
516
517
518
  rerun:
  	pos = start_of_string;
  	while (1) {
  		u8 left = -1;
  		u8 right = -1;
  		char *left_word = pos;
  		char *cp;
  		char *right_word;
  		bool is_not;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
519

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
  		if (!*left_word)
  			break;
  		/*
  		 * Since left-hand condition does not allow use of "path_group"
  		 * or "number_group" and environment variable's names do not
  		 * accept '=', it is guaranteed that the original line consists
  		 * of one or more repetition of $left$operator$right blocks
  		 * where "$left is free from '=' and ' '" and "$operator is
  		 * either '=' or '!='" and "$right is free from ' '".
  		 * Therefore, we can reconstruct the original line at the end
  		 * of dry run even if we overwrite $operator with '\0'.
  		 */
  		cp = strchr(pos, ' ');
  		if (cp) {
  			*cp = '\0'; /* Will restore later. */
  			pos = cp + 1;
  		} else {
  			pos = "";
  		}
  		right_word = strchr(left_word, '=');
  		if (!right_word || right_word == left_word)
  			goto out;
  		is_not = *(right_word - 1) == '!';
  		if (is_not)
  			*(right_word++ - 1) = '\0'; /* Will restore later. */
  		else if (*(right_word + 1) != '=')
  			*right_word++ = '\0'; /* Will restore later. */
  		else
  			goto out;
  		dprintk(KERN_WARNING "%u: <%s>%s=<%s>
  ", __LINE__, left_word,
  			is_not ? "!" : "", right_word);
1f067a682   Tetsuo Handa   TOMOYO: Allow con...
552
553
554
555
556
557
558
559
560
561
562
563
564
565
  		if (!strcmp(left_word, "grant_log")) {
  			if (entry) {
  				if (is_not ||
  				    entry->grant_log != TOMOYO_GRANTLOG_AUTO)
  					goto out;
  				else if (!strcmp(right_word, "yes"))
  					entry->grant_log = TOMOYO_GRANTLOG_YES;
  				else if (!strcmp(right_word, "no"))
  					entry->grant_log = TOMOYO_GRANTLOG_NO;
  				else
  					goto out;
  			}
  			continue;
  		}
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  		if (!strncmp(left_word, "exec.argv[", 10)) {
  			if (!argv) {
  				e.argc++;
  				e.condc++;
  			} else {
  				e.argc--;
  				e.condc--;
  				left = TOMOYO_ARGV_ENTRY;
  				argv->is_not = is_not;
  				if (!tomoyo_parse_argv(left_word + 10,
  						       right_word, argv++))
  					goto out;
  			}
  			goto store_value;
  		}
  		if (!strncmp(left_word, "exec.envp[\"", 11)) {
  			if (!envp) {
  				e.envc++;
  				e.condc++;
  			} else {
  				e.envc--;
  				e.condc--;
  				left = TOMOYO_ENVP_ENTRY;
  				envp->is_not = is_not;
  				if (!tomoyo_parse_envp(left_word + 11,
  						       right_word, envp++))
  					goto out;
  			}
  			goto store_value;
  		}
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
  		left = tomoyo_condition_type(left_word);
  		dprintk(KERN_WARNING "%u: <%s> left=%u
  ", __LINE__, left_word,
  			left);
  		if (left == TOMOYO_MAX_CONDITION_KEYWORD) {
  			if (!numbers_p) {
  				e.numbers_count++;
  			} else {
  				e.numbers_count--;
  				left = TOMOYO_NUMBER_UNION;
  				param->data = left_word;
  				if (*left_word == '@' ||
  				    !tomoyo_parse_number_union(param,
  							       numbers_p++))
  					goto out;
  			}
  		}
  		if (!condp)
  			e.condc++;
  		else
  			e.condc--;
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  		if (left == TOMOYO_EXEC_REALPATH ||
  		    left == TOMOYO_SYMLINK_TARGET) {
  			if (!names_p) {
  				e.names_count++;
  			} else {
  				e.names_count--;
  				right = TOMOYO_NAME_UNION;
  				param->data = right_word;
  				if (!tomoyo_parse_name_union_quoted(param,
  								    names_p++))
  					goto out;
  			}
  			goto store_value;
  		}
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
631
632
633
634
635
636
637
638
639
640
641
642
643
  		right = tomoyo_condition_type(right_word);
  		if (right == TOMOYO_MAX_CONDITION_KEYWORD) {
  			if (!numbers_p) {
  				e.numbers_count++;
  			} else {
  				e.numbers_count--;
  				right = TOMOYO_NUMBER_UNION;
  				param->data = right_word;
  				if (!tomoyo_parse_number_union(param,
  							       numbers_p++))
  					goto out;
  			}
  		}
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
644
  store_value:
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
645
  		if (!condp) {
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
646
647
648
  			dprintk(KERN_WARNING "%u: dry_run left=%u right=%u match=%u
  ",
  				__LINE__, left, right, !is_not);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
649
650
651
652
653
654
655
656
657
658
659
  			continue;
  		}
  		condp->left = left;
  		condp->right = right;
  		condp->equals = !is_not;
  		dprintk(KERN_WARNING "%u: left=%u right=%u match=%u
  ",
  			__LINE__, condp->left, condp->right,
  			condp->equals);
  		condp++;
  	}
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
660
661
662
663
  	dprintk(KERN_INFO "%u: cond=%u numbers=%u names=%u ac=%u ec=%u
  ",
  		__LINE__, e.condc, e.numbers_count, e.names_count, e.argc,
  		e.envc);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
664
  	if (entry) {
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
665
666
  		BUG_ON(e.names_count | e.numbers_count | e.argc | e.envc |
  		       e.condc);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
667
668
669
670
  		return tomoyo_commit_condition(entry);
  	}
  	e.size = sizeof(*entry)
  		+ e.condc * sizeof(struct tomoyo_condition_element)
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
671
  		+ e.numbers_count * sizeof(struct tomoyo_number_union)
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
672
673
674
  		+ e.names_count * sizeof(struct tomoyo_name_union)
  		+ e.argc * sizeof(struct tomoyo_argv)
  		+ e.envc * sizeof(struct tomoyo_envp);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
675
676
  	entry = kzalloc(e.size, GFP_NOFS);
  	if (!entry)
6bce98edc   Tetsuo Handa   TOMOYO: Allow spe...
677
  		goto out2;
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
678
  	*entry = e;
6bce98edc   Tetsuo Handa   TOMOYO: Allow spe...
679
  	e.transit = NULL;
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
680
681
  	condp = (struct tomoyo_condition_element *) (entry + 1);
  	numbers_p = (struct tomoyo_number_union *) (condp + e.condc);
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
682
  	names_p = (struct tomoyo_name_union *) (numbers_p + e.numbers_count);
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
683
684
  	argv = (struct tomoyo_argv *) (names_p + e.names_count);
  	envp = (struct tomoyo_envp *) (argv + e.argc);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
685
686
  	{
  		bool flag = false;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
687

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
  		for (pos = start_of_string; pos < end_of_string; pos++) {
  			if (*pos)
  				continue;
  			if (flag) /* Restore " ". */
  				*pos = ' ';
  			else if (*(pos + 1) == '=') /* Restore "!=". */
  				*pos = '!';
  			else /* Restore "=". */
  				*pos = '=';
  			flag = !flag;
  		}
  	}
  	goto rerun;
  out:
  	dprintk(KERN_WARNING "%u: %s failed
  ", __LINE__, __func__);
  	if (entry) {
  		tomoyo_del_condition(&entry->head.list);
  		kfree(entry);
  	}
6bce98edc   Tetsuo Handa   TOMOYO: Allow spe...
708
709
  out2:
  	tomoyo_put_name(e.transit);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
710
711
712
713
  	return NULL;
  }
  
  /**
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
714
715
716
717
718
719
720
721
722
723
724
725
726
   * tomoyo_get_attributes - Revalidate "struct inode".
   *
   * @obj: Pointer to "struct tomoyo_obj_info".
   *
   * Returns nothing.
   */
  void tomoyo_get_attributes(struct tomoyo_obj_info *obj)
  {
  	u8 i;
  	struct dentry *dentry = NULL;
  
  	for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
  		struct inode *inode;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
727

8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  		switch (i) {
  		case TOMOYO_PATH1:
  			dentry = obj->path1.dentry;
  			if (!dentry)
  				continue;
  			break;
  		case TOMOYO_PATH2:
  			dentry = obj->path2.dentry;
  			if (!dentry)
  				continue;
  			break;
  		default:
  			if (!dentry)
  				continue;
  			dentry = dget_parent(dentry);
  			break;
  		}
c6f493d63   David Howells   VFS: security/: d...
745
  		inode = d_backing_inode(dentry);
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
746
747
  		if (inode) {
  			struct tomoyo_mini_stat *stat = &obj->stat[i];
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
748

8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
749
750
751
752
753
754
755
756
  			stat->uid  = inode->i_uid;
  			stat->gid  = inode->i_gid;
  			stat->ino  = inode->i_ino;
  			stat->mode = inode->i_mode;
  			stat->dev  = inode->i_sb->s_dev;
  			stat->rdev = inode->i_rdev;
  			obj->stat_valid[i] = true;
  		}
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
757
  		if (i & 1) /* TOMOYO_PATH1_PARENT or TOMOYO_PATH2_PARENT */
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
758
759
760
761
762
  			dput(dentry);
  	}
  }
  
  /**
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
   * tomoyo_condition - Check condition part.
   *
   * @r:    Pointer to "struct tomoyo_request_info".
   * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
   *
   * Returns true on success, false otherwise.
   *
   * Caller holds tomoyo_read_lock().
   */
  bool tomoyo_condition(struct tomoyo_request_info *r,
  		      const struct tomoyo_condition *cond)
  {
  	u32 i;
  	unsigned long min_v[2] = { 0, 0 };
  	unsigned long max_v[2] = { 0, 0 };
  	const struct tomoyo_condition_element *condp;
  	const struct tomoyo_number_union *numbers_p;
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
780
  	const struct tomoyo_name_union *names_p;
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
781
782
  	const struct tomoyo_argv *argv;
  	const struct tomoyo_envp *envp;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
783
  	struct tomoyo_obj_info *obj;
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
784
  	u16 condc;
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
785
786
787
  	u16 argc;
  	u16 envc;
  	struct linux_binprm *bprm = NULL;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
788

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
789
790
791
  	if (!cond)
  		return true;
  	condc = cond->condc;
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
792
793
  	argc = cond->argc;
  	envc = cond->envc;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
794
  	obj = r->obj;
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
795
796
797
798
  	if (r->ee)
  		bprm = r->ee->bprm;
  	if (!bprm && (argc || envc))
  		return false;
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
799
800
  	condp = (struct tomoyo_condition_element *) (cond + 1);
  	numbers_p = (const struct tomoyo_number_union *) (condp + condc);
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
801
802
  	names_p = (const struct tomoyo_name_union *)
  		(numbers_p + cond->numbers_count);
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
803
804
  	argv = (const struct tomoyo_argv *) (names_p + cond->names_count);
  	envp = (const struct tomoyo_envp *) (argv + argc);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
805
806
807
808
  	for (i = 0; i < condc; i++) {
  		const bool match = condp->equals;
  		const u8 left = condp->left;
  		const u8 right = condp->right;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
809
  		bool is_bitop[2] = { false, false };
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
810
  		u8 j;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
811

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
812
  		condp++;
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
813
814
815
  		/* Check argv[] and envp[] later. */
  		if (left == TOMOYO_ARGV_ENTRY || left == TOMOYO_ENVP_ENTRY)
  			continue;
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
816
817
818
  		/* Check string expressions. */
  		if (right == TOMOYO_NAME_UNION) {
  			const struct tomoyo_name_union *ptr = names_p++;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
819
820
821
  			struct tomoyo_path_info *symlink;
  			struct tomoyo_execve *ee;
  			struct file *file;
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
822
  			switch (left) {
2ca9bf453   Tetsuo Handa   TOMOYO: Allow usi...
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
  			case TOMOYO_SYMLINK_TARGET:
  				symlink = obj ? obj->symlink_target : NULL;
  				if (!symlink ||
  				    !tomoyo_compare_name_union(symlink, ptr)
  				    == match)
  					goto out;
  				break;
  			case TOMOYO_EXEC_REALPATH:
  				ee = r->ee;
  				file = ee ? ee->bprm->file : NULL;
  				if (!tomoyo_scan_exec_realpath(file, ptr,
  							       match))
  					goto out;
  				break;
  			}
  			continue;
  		}
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
840
841
842
843
  		/* Check numeric or bit-op expressions. */
  		for (j = 0; j < 2; j++) {
  			const u8 index = j ? right : left;
  			unsigned long value = 0;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
844

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
845
846
  			switch (index) {
  			case TOMOYO_TASK_UID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
847
  				value = from_kuid(&init_user_ns, current_uid());
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
848
849
  				break;
  			case TOMOYO_TASK_EUID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
850
  				value = from_kuid(&init_user_ns, current_euid());
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
851
852
  				break;
  			case TOMOYO_TASK_SUID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
853
  				value = from_kuid(&init_user_ns, current_suid());
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
854
855
  				break;
  			case TOMOYO_TASK_FSUID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
856
  				value = from_kuid(&init_user_ns, current_fsuid());
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
857
858
  				break;
  			case TOMOYO_TASK_GID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
859
  				value = from_kgid(&init_user_ns, current_gid());
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
860
861
  				break;
  			case TOMOYO_TASK_EGID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
862
  				value = from_kgid(&init_user_ns, current_egid());
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
863
864
  				break;
  			case TOMOYO_TASK_SGID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
865
  				value = from_kgid(&init_user_ns, current_sgid());
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
866
867
  				break;
  			case TOMOYO_TASK_FSGID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
868
  				value = from_kgid(&init_user_ns, current_fsgid());
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
869
870
871
872
873
874
875
  				break;
  			case TOMOYO_TASK_PID:
  				value = tomoyo_sys_getpid();
  				break;
  			case TOMOYO_TASK_PPID:
  				value = tomoyo_sys_getppid();
  				break;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
  			case TOMOYO_TYPE_IS_SOCKET:
  				value = S_IFSOCK;
  				break;
  			case TOMOYO_TYPE_IS_SYMLINK:
  				value = S_IFLNK;
  				break;
  			case TOMOYO_TYPE_IS_FILE:
  				value = S_IFREG;
  				break;
  			case TOMOYO_TYPE_IS_BLOCK_DEV:
  				value = S_IFBLK;
  				break;
  			case TOMOYO_TYPE_IS_DIRECTORY:
  				value = S_IFDIR;
  				break;
  			case TOMOYO_TYPE_IS_CHAR_DEV:
  				value = S_IFCHR;
  				break;
  			case TOMOYO_TYPE_IS_FIFO:
  				value = S_IFIFO;
  				break;
  			case TOMOYO_MODE_SETUID:
  				value = S_ISUID;
  				break;
  			case TOMOYO_MODE_SETGID:
  				value = S_ISGID;
  				break;
  			case TOMOYO_MODE_STICKY:
  				value = S_ISVTX;
  				break;
  			case TOMOYO_MODE_OWNER_READ:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
907
  				value = 0400;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
908
909
  				break;
  			case TOMOYO_MODE_OWNER_WRITE:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
910
  				value = 0200;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
911
912
  				break;
  			case TOMOYO_MODE_OWNER_EXECUTE:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
913
  				value = 0100;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
914
915
  				break;
  			case TOMOYO_MODE_GROUP_READ:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
916
  				value = 0040;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
917
918
  				break;
  			case TOMOYO_MODE_GROUP_WRITE:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
919
  				value = 0020;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
920
921
  				break;
  			case TOMOYO_MODE_GROUP_EXECUTE:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
922
  				value = 0010;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
923
924
  				break;
  			case TOMOYO_MODE_OTHERS_READ:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
925
  				value = 0004;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
926
927
  				break;
  			case TOMOYO_MODE_OTHERS_WRITE:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
928
  				value = 0002;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
929
930
  				break;
  			case TOMOYO_MODE_OTHERS_EXECUTE:
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
931
  				value = 0001;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
932
  				break;
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
933
934
935
936
937
938
939
940
941
942
  			case TOMOYO_EXEC_ARGC:
  				if (!bprm)
  					goto out;
  				value = bprm->argc;
  				break;
  			case TOMOYO_EXEC_ENVC:
  				if (!bprm)
  					goto out;
  				value = bprm->envc;
  				break;
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
943
944
945
946
  			case TOMOYO_NUMBER_UNION:
  				/* Fetch values later. */
  				break;
  			default:
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
947
948
949
950
951
952
953
954
955
  				if (!obj)
  					goto out;
  				if (!obj->validate_done) {
  					tomoyo_get_attributes(obj);
  					obj->validate_done = true;
  				}
  				{
  					u8 stat_index;
  					struct tomoyo_mini_stat *stat;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
956

8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  					switch (index) {
  					case TOMOYO_PATH1_UID:
  					case TOMOYO_PATH1_GID:
  					case TOMOYO_PATH1_INO:
  					case TOMOYO_PATH1_MAJOR:
  					case TOMOYO_PATH1_MINOR:
  					case TOMOYO_PATH1_TYPE:
  					case TOMOYO_PATH1_DEV_MAJOR:
  					case TOMOYO_PATH1_DEV_MINOR:
  					case TOMOYO_PATH1_PERM:
  						stat_index = TOMOYO_PATH1;
  						break;
  					case TOMOYO_PATH2_UID:
  					case TOMOYO_PATH2_GID:
  					case TOMOYO_PATH2_INO:
  					case TOMOYO_PATH2_MAJOR:
  					case TOMOYO_PATH2_MINOR:
  					case TOMOYO_PATH2_TYPE:
  					case TOMOYO_PATH2_DEV_MAJOR:
  					case TOMOYO_PATH2_DEV_MINOR:
  					case TOMOYO_PATH2_PERM:
  						stat_index = TOMOYO_PATH2;
  						break;
  					case TOMOYO_PATH1_PARENT_UID:
  					case TOMOYO_PATH1_PARENT_GID:
  					case TOMOYO_PATH1_PARENT_INO:
  					case TOMOYO_PATH1_PARENT_PERM:
  						stat_index =
  							TOMOYO_PATH1_PARENT;
  						break;
  					case TOMOYO_PATH2_PARENT_UID:
  					case TOMOYO_PATH2_PARENT_GID:
  					case TOMOYO_PATH2_PARENT_INO:
  					case TOMOYO_PATH2_PARENT_PERM:
  						stat_index =
  							TOMOYO_PATH2_PARENT;
  						break;
  					default:
  						goto out;
  					}
  					if (!obj->stat_valid[stat_index])
  						goto out;
  					stat = &obj->stat[stat_index];
  					switch (index) {
  					case TOMOYO_PATH1_UID:
  					case TOMOYO_PATH2_UID:
  					case TOMOYO_PATH1_PARENT_UID:
  					case TOMOYO_PATH2_PARENT_UID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
1005
  						value = from_kuid(&init_user_ns, stat->uid);
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
1006
1007
1008
1009
1010
  						break;
  					case TOMOYO_PATH1_GID:
  					case TOMOYO_PATH2_GID:
  					case TOMOYO_PATH1_PARENT_GID:
  					case TOMOYO_PATH2_PARENT_GID:
609fcd1b3   Eric W. Biederman   userns: Convert t...
1011
  						value = from_kgid(&init_user_ns, stat->gid);
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
  						break;
  					case TOMOYO_PATH1_INO:
  					case TOMOYO_PATH2_INO:
  					case TOMOYO_PATH1_PARENT_INO:
  					case TOMOYO_PATH2_PARENT_INO:
  						value = stat->ino;
  						break;
  					case TOMOYO_PATH1_MAJOR:
  					case TOMOYO_PATH2_MAJOR:
  						value = MAJOR(stat->dev);
  						break;
  					case TOMOYO_PATH1_MINOR:
  					case TOMOYO_PATH2_MINOR:
  						value = MINOR(stat->dev);
  						break;
  					case TOMOYO_PATH1_TYPE:
  					case TOMOYO_PATH2_TYPE:
  						value = stat->mode & S_IFMT;
  						break;
  					case TOMOYO_PATH1_DEV_MAJOR:
  					case TOMOYO_PATH2_DEV_MAJOR:
  						value = MAJOR(stat->rdev);
  						break;
  					case TOMOYO_PATH1_DEV_MINOR:
  					case TOMOYO_PATH2_DEV_MINOR:
  						value = MINOR(stat->rdev);
  						break;
  					case TOMOYO_PATH1_PERM:
  					case TOMOYO_PATH2_PERM:
  					case TOMOYO_PATH1_PARENT_PERM:
  					case TOMOYO_PATH2_PARENT_PERM:
  						value = stat->mode & S_IALLUGO;
  						break;
  					}
  				}
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
1047
1048
1049
1050
  				break;
  			}
  			max_v[j] = value;
  			min_v[j] = value;
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
  			switch (index) {
  			case TOMOYO_MODE_SETUID:
  			case TOMOYO_MODE_SETGID:
  			case TOMOYO_MODE_STICKY:
  			case TOMOYO_MODE_OWNER_READ:
  			case TOMOYO_MODE_OWNER_WRITE:
  			case TOMOYO_MODE_OWNER_EXECUTE:
  			case TOMOYO_MODE_GROUP_READ:
  			case TOMOYO_MODE_GROUP_WRITE:
  			case TOMOYO_MODE_GROUP_EXECUTE:
  			case TOMOYO_MODE_OTHERS_READ:
  			case TOMOYO_MODE_OTHERS_WRITE:
  			case TOMOYO_MODE_OTHERS_EXECUTE:
  				is_bitop[j] = true;
  			}
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
1066
1067
1068
1069
  		}
  		if (left == TOMOYO_NUMBER_UNION) {
  			/* Fetch values now. */
  			const struct tomoyo_number_union *ptr = numbers_p++;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
1070

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
1071
1072
1073
1074
1075
1076
  			min_v[0] = ptr->values[0];
  			max_v[0] = ptr->values[1];
  		}
  		if (right == TOMOYO_NUMBER_UNION) {
  			/* Fetch values now. */
  			const struct tomoyo_number_union *ptr = numbers_p++;
cdcf6723a   Tetsuo Handa   tomoyo: Coding st...
1077

2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
  			if (ptr->group) {
  				if (tomoyo_number_matches_group(min_v[0],
  								max_v[0],
  								ptr->group)
  				    == match)
  					continue;
  			} else {
  				if ((min_v[0] <= ptr->values[1] &&
  				     max_v[0] >= ptr->values[0]) == match)
  					continue;
  			}
  			goto out;
  		}
8761afd49   Tetsuo Handa   TOMOYO: Allow usi...
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
  		/*
  		 * Bit operation is valid only when counterpart value
  		 * represents permission.
  		 */
  		if (is_bitop[0] && is_bitop[1]) {
  			goto out;
  		} else if (is_bitop[0]) {
  			switch (right) {
  			case TOMOYO_PATH1_PERM:
  			case TOMOYO_PATH1_PARENT_PERM:
  			case TOMOYO_PATH2_PERM:
  			case TOMOYO_PATH2_PARENT_PERM:
  				if (!(max_v[0] & max_v[1]) == !match)
  					continue;
  			}
  			goto out;
  		} else if (is_bitop[1]) {
  			switch (left) {
  			case TOMOYO_PATH1_PERM:
  			case TOMOYO_PATH1_PARENT_PERM:
  			case TOMOYO_PATH2_PERM:
  			case TOMOYO_PATH2_PARENT_PERM:
  				if (!(max_v[0] & max_v[1]) == !match)
  					continue;
  			}
  			goto out;
  		}
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
1118
1119
1120
1121
1122
1123
  		/* Normal value range comparison. */
  		if ((min_v[0] <= max_v[1] && max_v[0] >= min_v[1]) == match)
  			continue;
  out:
  		return false;
  	}
5b636857f   Tetsuo Handa   TOMOYO: Allow usi...
1124
1125
1126
  	/* Check argv[] and envp[] now. */
  	if (r->ee && (argc || envc))
  		return tomoyo_scan_bprm(r->ee, argc, argv, envc, envp);
2066a3612   Tetsuo Handa   TOMOYO: Allow usi...
1127
1128
  	return true;
  }