1 |
kumaneko |
111 |
/* |
2 |
|
|
* fs/tomoyo_exec.c |
3 |
|
|
* |
4 |
|
|
* Implementation of the Domain-Based Mandatory Access Control. |
5 |
|
|
* |
6 |
kumaneko |
851 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
kumaneko |
111 |
* |
8 |
kumaneko |
1052 |
* Version: 1.6.0-pre 2008/03/24 |
9 |
kumaneko |
111 |
* |
10 |
|
|
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
11 |
|
|
* See README.ccs for ChangeLog. |
12 |
|
|
* |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
#include <linux/ccs_common.h> |
16 |
|
|
#include <linux/tomoyo.h> |
17 |
|
|
#include <linux/realpath.h> |
18 |
|
|
|
19 |
kumaneko |
1052 |
/** |
20 |
|
|
* audit_argv0_log - Audit argv[0] log. |
21 |
|
|
* |
22 |
|
|
* @filename: The fullpath of program. |
23 |
|
|
* @argv0: The basename of argv[0]. |
24 |
|
|
* @is_granted: True if this is a granted log. |
25 |
|
|
* @profile: Profile number. |
26 |
|
|
* @mode: Access control mode. |
27 |
|
|
* |
28 |
|
|
* Returns 0 on success, negative value otherwise. |
29 |
|
|
*/ |
30 |
|
|
static int audit_argv0_log(const struct path_info *filename, const char *argv0, |
31 |
|
|
const bool is_granted, const u8 profile, |
32 |
|
|
const u8 mode) |
33 |
kumaneko |
111 |
{ |
34 |
|
|
char *buf; |
35 |
kumaneko |
1052 |
int len, len2; |
36 |
|
|
if (ccs_can_save_audit_log(is_granted) < 0) |
37 |
|
|
return -ENOMEM; |
38 |
kumaneko |
111 |
len = filename->total_len + strlen(argv0) + 8; |
39 |
kumaneko |
1052 |
buf = ccs_init_audit_log(&len, profile, mode, NULL); |
40 |
|
|
if (!buf) |
41 |
|
|
return -ENOMEM; |
42 |
|
|
len2 = strlen(buf); |
43 |
|
|
snprintf(buf + len2, len - len2 - 1, |
44 |
|
|
KEYWORD_ALLOW_ARGV0 "%s %s\n", filename->name, argv0); |
45 |
|
|
return ccs_write_audit_log(buf, is_granted); |
46 |
kumaneko |
111 |
} |
47 |
|
|
|
48 |
kumaneko |
1052 |
/** |
49 |
|
|
* update_argv0_entry - Update "struct argv0_acl_record" list. |
50 |
|
|
* |
51 |
|
|
* @filename: The fullpath of the program. |
52 |
|
|
* @argv0: The basename of argv[0]. |
53 |
|
|
* @domain: Pointer to "struct domain_info". |
54 |
|
|
* @condition: Pointer to "struct condition_list". May be NULL. |
55 |
|
|
* @is_delete: True if it is a delete request. |
56 |
|
|
* |
57 |
|
|
* Returns 0 on success, negative value otherwise. |
58 |
|
|
*/ |
59 |
|
|
static int update_argv0_entry(const char *filename, const char *argv0, |
60 |
|
|
struct domain_info *domain, |
61 |
|
|
const struct condition_list *condition, |
62 |
|
|
const bool is_delete) |
63 |
kumaneko |
111 |
{ |
64 |
|
|
struct acl_info *ptr; |
65 |
kumaneko |
708 |
struct argv0_acl_record *acl; |
66 |
kumaneko |
111 |
const struct path_info *saved_filename, *saved_argv0; |
67 |
|
|
int error = -ENOMEM; |
68 |
kumaneko |
1052 |
if (!ccs_is_correct_path(filename, 1, 0, -1, __func__) || |
69 |
|
|
!ccs_is_correct_path(argv0, -1, 0, -1, __func__) || |
70 |
|
|
strchr(argv0, '/')) |
71 |
|
|
return -EINVAL; |
72 |
|
|
saved_filename = ccs_save_name(filename); |
73 |
|
|
saved_argv0 = ccs_save_name(argv0); |
74 |
|
|
if (!saved_filename || !saved_argv0) |
75 |
|
|
return -ENOMEM; |
76 |
kumaneko |
652 |
mutex_lock(&domain_acl_lock); |
77 |
kumaneko |
1052 |
if (is_delete) |
78 |
|
|
goto delete; |
79 |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
80 |
|
|
if ((ptr->type & ~(ACL_DELETED | ACL_WITH_CONDITION)) |
81 |
|
|
!= TYPE_ARGV0_ACL) |
82 |
|
|
continue; |
83 |
|
|
if (ccs_get_condition_part(ptr) != condition) |
84 |
|
|
continue; |
85 |
|
|
acl = container_of(ptr, struct argv0_acl_record, head); |
86 |
|
|
if (acl->filename != saved_filename || |
87 |
|
|
acl->argv0 != saved_argv0) |
88 |
|
|
continue; |
89 |
|
|
error = ccs_add_domain_acl(NULL, ptr); |
90 |
|
|
goto out; |
91 |
kumaneko |
111 |
} |
92 |
kumaneko |
1052 |
/* Not found. Append it to the tail. */ |
93 |
|
|
acl = ccs_alloc_acl_element(TYPE_ARGV0_ACL, condition); |
94 |
|
|
if (!acl) |
95 |
|
|
goto out; |
96 |
|
|
acl->filename = saved_filename; |
97 |
|
|
acl->argv0 = saved_argv0; |
98 |
|
|
error = ccs_add_domain_acl(domain, &acl->head); |
99 |
|
|
goto out; |
100 |
|
|
delete: |
101 |
|
|
error = -ENOENT; |
102 |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
103 |
|
|
if ((ptr->type & ~ACL_WITH_CONDITION) != TYPE_ARGV0_ACL) |
104 |
|
|
continue; |
105 |
|
|
if (ccs_get_condition_part(ptr) != condition) |
106 |
|
|
continue; |
107 |
|
|
acl = container_of(ptr, struct argv0_acl_record, head); |
108 |
|
|
if (acl->filename != saved_filename || |
109 |
|
|
acl->argv0 != saved_argv0) |
110 |
|
|
continue; |
111 |
|
|
error = ccs_del_domain_acl(ptr); |
112 |
|
|
break; |
113 |
|
|
} |
114 |
|
|
out: |
115 |
kumaneko |
652 |
mutex_unlock(&domain_acl_lock); |
116 |
kumaneko |
111 |
return error; |
117 |
|
|
} |
118 |
|
|
|
119 |
kumaneko |
1052 |
/** |
120 |
|
|
* check_argv0_acl - Check permission for argv[0]. |
121 |
|
|
* |
122 |
|
|
* @filename: The fullpath of the program. |
123 |
|
|
* @argv0: The basename of argv[0]. |
124 |
|
|
* |
125 |
|
|
* Returns 0 on success, -EPERM otherwise. |
126 |
|
|
*/ |
127 |
|
|
static int check_argv0_acl(const struct path_info *filename, const char *argv0) |
128 |
kumaneko |
111 |
{ |
129 |
|
|
const struct domain_info *domain = current->domain_info; |
130 |
|
|
int error = -EPERM; |
131 |
|
|
struct acl_info *ptr; |
132 |
kumaneko |
1052 |
struct path_info argv_0; |
133 |
|
|
argv_0.name = argv0; |
134 |
|
|
ccs_fill_path_info(&argv_0); |
135 |
kumaneko |
722 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
136 |
kumaneko |
708 |
struct argv0_acl_record *acl; |
137 |
kumaneko |
1052 |
if ((ptr->type & ~ACL_WITH_CONDITION) != TYPE_ARGV0_ACL) |
138 |
|
|
continue; |
139 |
kumaneko |
912 |
acl = container_of(ptr, struct argv0_acl_record, head); |
140 |
kumaneko |
1052 |
if (!ccs_check_condition(ptr, NULL) || |
141 |
|
|
!ccs_path_matches_pattern(filename, acl->filename) || |
142 |
|
|
!ccs_path_matches_pattern(&argv_0, acl->argv0)) |
143 |
|
|
continue; |
144 |
|
|
ccs_update_condition(ptr); |
145 |
kumaneko |
856 |
error = 0; |
146 |
|
|
break; |
147 |
kumaneko |
111 |
} |
148 |
|
|
return error; |
149 |
|
|
} |
150 |
|
|
|
151 |
kumaneko |
1052 |
/** |
152 |
|
|
* ccs_check_argv0_perm - Check permission for argv[0]. |
153 |
|
|
* |
154 |
|
|
* @filename: The fullpath of the program. |
155 |
|
|
* @argv0: The basename of argv[0]. |
156 |
|
|
* |
157 |
|
|
* Returns 0 on success, negative value otherwise. |
158 |
|
|
*/ |
159 |
|
|
int ccs_check_argv0_perm(const struct path_info *filename, const char *argv0) |
160 |
kumaneko |
111 |
{ |
161 |
|
|
int error = 0; |
162 |
kumaneko |
815 |
struct domain_info * const domain = current->domain_info; |
163 |
|
|
const u8 profile = domain->profile; |
164 |
kumaneko |
1052 |
const u8 mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_ARGV0); |
165 |
kumaneko |
856 |
const bool is_enforce = (mode == 3); |
166 |
kumaneko |
1052 |
if (!filename || !argv0 || !*argv0) |
167 |
|
|
return 0; |
168 |
|
|
error = check_argv0_acl(filename, argv0); |
169 |
|
|
audit_argv0_log(filename, argv0, !error, profile, mode); |
170 |
|
|
if (!error) |
171 |
|
|
return 0; |
172 |
|
|
if (ccs_verbose_mode()) |
173 |
|
|
printk(KERN_WARNING "TOMOYO-%s: Run %s as %s denied for %s\n", |
174 |
|
|
ccs_get_msg(is_enforce), filename->name, argv0, |
175 |
|
|
ccs_get_last_name(domain)); |
176 |
|
|
if (is_enforce) |
177 |
|
|
return ccs_check_supervisor("%s\n" |
178 |
|
|
KEYWORD_ALLOW_ARGV0 "%s %s\n", |
179 |
|
|
domain->domainname->name, |
180 |
|
|
filename->name, argv0); |
181 |
|
|
else if (mode == 1 && ccs_check_domain_quota(domain)) |
182 |
|
|
update_argv0_entry(filename->name, argv0, domain, NULL, false); |
183 |
kumaneko |
856 |
return 0; |
184 |
kumaneko |
111 |
} |
185 |
|
|
|
186 |
kumaneko |
1052 |
/** |
187 |
|
|
* ccs_write_argv0_policy - Write "struct argv0_acl_record" list. |
188 |
|
|
* |
189 |
|
|
* @data: String to parse. |
190 |
|
|
* @domain: Pointer to "struct domain_info". |
191 |
|
|
* @condition: Pointer to "struct condition_list". May be NULL. |
192 |
|
|
* @is_delete: True if it is a delete request. |
193 |
|
|
* |
194 |
|
|
* Returns 0 on success, negative value otherwise. |
195 |
|
|
*/ |
196 |
|
|
int ccs_write_argv0_policy(char *data, struct domain_info *domain, |
197 |
|
|
const struct condition_list *condition, |
198 |
|
|
const bool is_delete) |
199 |
kumaneko |
111 |
{ |
200 |
|
|
char *argv0 = strchr(data, ' '); |
201 |
kumaneko |
1052 |
if (!argv0) |
202 |
|
|
return -EINVAL; |
203 |
kumaneko |
111 |
*argv0++ = '\0'; |
204 |
kumaneko |
1052 |
return update_argv0_entry(data, argv0, domain, condition, is_delete); |
205 |
kumaneko |
111 |
} |