]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Updated: Karl MacMillan <kmacmillan@tresys.com> |
2 | * | |
1872981b | 3 | * Added conditional policy language extensions |
1da177e4 | 4 | * |
82c21bfa | 5 | * Updated: Hewlett-Packard <paul@paul-moore.com> |
3bb56b25 | 6 | * |
1872981b | 7 | * Added support for the policy capability bitmap |
3bb56b25 PM |
8 | * |
9 | * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. | |
1da177e4 LT |
10 | * Copyright (C) 2003 - 2004 Tresys Technology, LLC |
11 | * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> | |
12 | * This program is free software; you can redistribute it and/or modify | |
1872981b | 13 | * it under the terms of the GNU General Public License as published by |
1da177e4 LT |
14 | * the Free Software Foundation, version 2. |
15 | */ | |
16 | ||
1da177e4 LT |
17 | #include <linux/kernel.h> |
18 | #include <linux/pagemap.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/vmalloc.h> | |
21 | #include <linux/fs.h> | |
bb003079 | 22 | #include <linux/mutex.h> |
1da177e4 LT |
23 | #include <linux/init.h> |
24 | #include <linux/string.h> | |
25 | #include <linux/security.h> | |
26 | #include <linux/major.h> | |
27 | #include <linux/seq_file.h> | |
28 | #include <linux/percpu.h> | |
af601e46 | 29 | #include <linux/audit.h> |
f5269710 | 30 | #include <linux/uaccess.h> |
7a627e3b | 31 | #include <linux/kobject.h> |
0f7e4c33 | 32 | #include <linux/ctype.h> |
f1efc8c4 | 33 | #include <linux/lsm_hooks.h> |
1da177e4 LT |
34 | |
35 | /* selinuxfs pseudo filesystem for exporting the security policy API. | |
36 | Based on the proc code and the fs/nfsd/nfsctl.c code. */ | |
37 | ||
38 | #include "flask.h" | |
39 | #include "avc.h" | |
40 | #include "avc_ss.h" | |
41 | #include "security.h" | |
42 | #include "objsec.h" | |
43 | #include "conditional.h" | |
44 | ||
45 | unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; | |
46 | ||
47 | static int __init checkreqprot_setup(char *str) | |
48 | { | |
f5269710 | 49 | unsigned long checkreqprot; |
29707b20 | 50 | if (!kstrtoul(str, 0, &checkreqprot)) |
f5269710 | 51 | selinux_checkreqprot = checkreqprot ? 1 : 0; |
1da177e4 LT |
52 | return 1; |
53 | } | |
54 | __setup("checkreqprot=", checkreqprot_setup); | |
55 | ||
bb003079 | 56 | static DEFINE_MUTEX(sel_mutex); |
1da177e4 LT |
57 | |
58 | /* global data for booleans */ | |
1872981b EP |
59 | static struct dentry *bool_dir; |
60 | static int bool_num; | |
d313f948 | 61 | static char **bool_pending_names; |
1872981b | 62 | static int *bool_pending_values; |
1da177e4 | 63 | |
e47c8fc5 | 64 | /* global data for classes */ |
1872981b | 65 | static struct dentry *class_dir; |
e47c8fc5 CP |
66 | static unsigned long last_class_ino; |
67 | ||
cee74f47 EP |
68 | static char policy_opened; |
69 | ||
3bb56b25 | 70 | /* global data for policy capabilities */ |
1872981b | 71 | static struct dentry *policycap_dir; |
3bb56b25 | 72 | |
1da177e4 LT |
73 | enum sel_inos { |
74 | SEL_ROOT_INO = 2, | |
75 | SEL_LOAD, /* load policy */ | |
76 | SEL_ENFORCE, /* get or set enforcing status */ | |
77 | SEL_CONTEXT, /* validate context */ | |
78 | SEL_ACCESS, /* compute access decision */ | |
79 | SEL_CREATE, /* compute create labeling decision */ | |
80 | SEL_RELABEL, /* compute relabeling decision */ | |
81 | SEL_USER, /* compute reachable user contexts */ | |
82 | SEL_POLICYVERS, /* return policy version for this kernel */ | |
83 | SEL_COMMIT_BOOLS, /* commit new boolean values */ | |
84 | SEL_MLS, /* return if MLS policy is enabled */ | |
85 | SEL_DISABLE, /* disable SELinux until next reboot */ | |
1da177e4 LT |
86 | SEL_MEMBER, /* compute polyinstantiation membership decision */ |
87 | SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ | |
4e5ab4cb | 88 | SEL_COMPAT_NET, /* whether to use old compat network packet controls */ |
3f12070e EP |
89 | SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ |
90 | SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ | |
11904167 | 91 | SEL_STATUS, /* export current status using mmap() */ |
cee74f47 | 92 | SEL_POLICY, /* allow userspace to read the in kernel policy */ |
f9df6458 | 93 | SEL_VALIDATE_TRANS, /* compute validatetrans decision */ |
6174eafc | 94 | SEL_INO_NEXT, /* The next inode number to use */ |
1da177e4 LT |
95 | }; |
96 | ||
6174eafc JC |
97 | static unsigned long sel_last_ino = SEL_INO_NEXT - 1; |
98 | ||
3bb56b25 PM |
99 | #define SEL_INITCON_INO_OFFSET 0x01000000 |
100 | #define SEL_BOOL_INO_OFFSET 0x02000000 | |
101 | #define SEL_CLASS_INO_OFFSET 0x04000000 | |
102 | #define SEL_POLICYCAP_INO_OFFSET 0x08000000 | |
103 | #define SEL_INO_MASK 0x00ffffff | |
f0ee2e46 | 104 | |
1da177e4 LT |
105 | #define TMPBUFLEN 12 |
106 | static ssize_t sel_read_enforce(struct file *filp, char __user *buf, | |
107 | size_t count, loff_t *ppos) | |
108 | { | |
109 | char tmpbuf[TMPBUFLEN]; | |
110 | ssize_t length; | |
111 | ||
112 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing); | |
113 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
114 | } | |
115 | ||
116 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP | |
1872981b | 117 | static ssize_t sel_write_enforce(struct file *file, const char __user *buf, |
1da177e4 LT |
118 | size_t count, loff_t *ppos) |
119 | ||
120 | { | |
b77a493b | 121 | char *page = NULL; |
1da177e4 LT |
122 | ssize_t length; |
123 | int new_value; | |
124 | ||
bfd51626 | 125 | if (count >= PAGE_SIZE) |
8365a719 | 126 | return -ENOMEM; |
b77a493b EP |
127 | |
128 | /* No partial writes. */ | |
b77a493b | 129 | if (*ppos != 0) |
8365a719 | 130 | return -EINVAL; |
b77a493b | 131 | |
8365a719 AV |
132 | page = memdup_user_nul(buf, count); |
133 | if (IS_ERR(page)) | |
134 | return PTR_ERR(page); | |
1da177e4 LT |
135 | |
136 | length = -EINVAL; | |
137 | if (sscanf(page, "%d", &new_value) != 1) | |
138 | goto out; | |
139 | ||
ea49d10e SS |
140 | new_value = !!new_value; |
141 | ||
1da177e4 | 142 | if (new_value != selinux_enforcing) { |
be0554c9 SS |
143 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
144 | SECCLASS_SECURITY, SECURITY__SETENFORCE, | |
145 | NULL); | |
1da177e4 LT |
146 | if (length) |
147 | goto out; | |
af601e46 | 148 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
4746ec5b EP |
149 | "enforcing=%d old_enforcing=%d auid=%u ses=%u", |
150 | new_value, selinux_enforcing, | |
581abc09 | 151 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
4746ec5b | 152 | audit_get_sessionid(current)); |
1da177e4 LT |
153 | selinux_enforcing = new_value; |
154 | if (selinux_enforcing) | |
155 | avc_ss_reset(0); | |
156 | selnl_notify_setenforce(selinux_enforcing); | |
11904167 | 157 | selinux_status_update_setenforce(selinux_enforcing); |
8f408ab6 DJ |
158 | if (!selinux_enforcing) |
159 | call_lsm_notifier(LSM_POLICY_CHANGE, NULL); | |
1da177e4 LT |
160 | } |
161 | length = count; | |
162 | out: | |
8365a719 | 163 | kfree(page); |
1da177e4 LT |
164 | return length; |
165 | } | |
166 | #else | |
167 | #define sel_write_enforce NULL | |
168 | #endif | |
169 | ||
9c2e08c5 | 170 | static const struct file_operations sel_enforce_ops = { |
1da177e4 LT |
171 | .read = sel_read_enforce, |
172 | .write = sel_write_enforce, | |
57a62c23 | 173 | .llseek = generic_file_llseek, |
1da177e4 LT |
174 | }; |
175 | ||
3f12070e EP |
176 | static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, |
177 | size_t count, loff_t *ppos) | |
178 | { | |
179 | char tmpbuf[TMPBUFLEN]; | |
180 | ssize_t length; | |
496ad9aa | 181 | ino_t ino = file_inode(filp)->i_ino; |
3f12070e EP |
182 | int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? |
183 | security_get_reject_unknown() : !security_get_allow_unknown(); | |
184 | ||
185 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); | |
186 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
187 | } | |
188 | ||
189 | static const struct file_operations sel_handle_unknown_ops = { | |
190 | .read = sel_read_handle_unknown, | |
57a62c23 | 191 | .llseek = generic_file_llseek, |
3f12070e EP |
192 | }; |
193 | ||
11904167 KK |
194 | static int sel_open_handle_status(struct inode *inode, struct file *filp) |
195 | { | |
196 | struct page *status = selinux_kernel_status_page(); | |
197 | ||
198 | if (!status) | |
199 | return -ENOMEM; | |
200 | ||
201 | filp->private_data = status; | |
202 | ||
203 | return 0; | |
204 | } | |
205 | ||
206 | static ssize_t sel_read_handle_status(struct file *filp, char __user *buf, | |
207 | size_t count, loff_t *ppos) | |
208 | { | |
209 | struct page *status = filp->private_data; | |
210 | ||
211 | BUG_ON(!status); | |
212 | ||
213 | return simple_read_from_buffer(buf, count, ppos, | |
214 | page_address(status), | |
215 | sizeof(struct selinux_kernel_status)); | |
216 | } | |
217 | ||
218 | static int sel_mmap_handle_status(struct file *filp, | |
219 | struct vm_area_struct *vma) | |
220 | { | |
221 | struct page *status = filp->private_data; | |
222 | unsigned long size = vma->vm_end - vma->vm_start; | |
223 | ||
224 | BUG_ON(!status); | |
225 | ||
226 | /* only allows one page from the head */ | |
227 | if (vma->vm_pgoff > 0 || size != PAGE_SIZE) | |
228 | return -EIO; | |
229 | /* disallow writable mapping */ | |
230 | if (vma->vm_flags & VM_WRITE) | |
231 | return -EPERM; | |
232 | /* disallow mprotect() turns it into writable */ | |
233 | vma->vm_flags &= ~VM_MAYWRITE; | |
234 | ||
235 | return remap_pfn_range(vma, vma->vm_start, | |
236 | page_to_pfn(status), | |
237 | size, vma->vm_page_prot); | |
238 | } | |
239 | ||
240 | static const struct file_operations sel_handle_status_ops = { | |
241 | .open = sel_open_handle_status, | |
242 | .read = sel_read_handle_status, | |
243 | .mmap = sel_mmap_handle_status, | |
244 | .llseek = generic_file_llseek, | |
245 | }; | |
246 | ||
1da177e4 | 247 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
1872981b | 248 | static ssize_t sel_write_disable(struct file *file, const char __user *buf, |
1da177e4 LT |
249 | size_t count, loff_t *ppos) |
250 | ||
251 | { | |
8365a719 | 252 | char *page; |
1da177e4 LT |
253 | ssize_t length; |
254 | int new_value; | |
1da177e4 | 255 | |
bfd51626 | 256 | if (count >= PAGE_SIZE) |
8365a719 | 257 | return -ENOMEM; |
b77a493b EP |
258 | |
259 | /* No partial writes. */ | |
b77a493b | 260 | if (*ppos != 0) |
8365a719 | 261 | return -EINVAL; |
b77a493b | 262 | |
8365a719 AV |
263 | page = memdup_user_nul(buf, count); |
264 | if (IS_ERR(page)) | |
265 | return PTR_ERR(page); | |
1da177e4 LT |
266 | |
267 | length = -EINVAL; | |
268 | if (sscanf(page, "%d", &new_value) != 1) | |
269 | goto out; | |
270 | ||
271 | if (new_value) { | |
272 | length = selinux_disable(); | |
b77a493b | 273 | if (length) |
1da177e4 | 274 | goto out; |
af601e46 | 275 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
4746ec5b | 276 | "selinux=0 auid=%u ses=%u", |
581abc09 | 277 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
4746ec5b | 278 | audit_get_sessionid(current)); |
1da177e4 LT |
279 | } |
280 | ||
281 | length = count; | |
282 | out: | |
8365a719 | 283 | kfree(page); |
1da177e4 LT |
284 | return length; |
285 | } | |
286 | #else | |
287 | #define sel_write_disable NULL | |
288 | #endif | |
289 | ||
9c2e08c5 | 290 | static const struct file_operations sel_disable_ops = { |
1da177e4 | 291 | .write = sel_write_disable, |
57a62c23 | 292 | .llseek = generic_file_llseek, |
1da177e4 LT |
293 | }; |
294 | ||
295 | static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, | |
1872981b | 296 | size_t count, loff_t *ppos) |
1da177e4 LT |
297 | { |
298 | char tmpbuf[TMPBUFLEN]; | |
299 | ssize_t length; | |
300 | ||
301 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); | |
302 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
303 | } | |
304 | ||
9c2e08c5 | 305 | static const struct file_operations sel_policyvers_ops = { |
1da177e4 | 306 | .read = sel_read_policyvers, |
57a62c23 | 307 | .llseek = generic_file_llseek, |
1da177e4 LT |
308 | }; |
309 | ||
310 | /* declaration for sel_write_load */ | |
311 | static int sel_make_bools(void); | |
e47c8fc5 | 312 | static int sel_make_classes(void); |
3bb56b25 | 313 | static int sel_make_policycap(void); |
e47c8fc5 CP |
314 | |
315 | /* declaration for sel_make_class_dirs */ | |
a1c2aa1e | 316 | static struct dentry *sel_make_dir(struct dentry *dir, const char *name, |
e47c8fc5 | 317 | unsigned long *ino); |
1da177e4 LT |
318 | |
319 | static ssize_t sel_read_mls(struct file *filp, char __user *buf, | |
320 | size_t count, loff_t *ppos) | |
321 | { | |
322 | char tmpbuf[TMPBUFLEN]; | |
323 | ssize_t length; | |
324 | ||
0719aaf5 GT |
325 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", |
326 | security_mls_enabled()); | |
1da177e4 LT |
327 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
328 | } | |
329 | ||
9c2e08c5 | 330 | static const struct file_operations sel_mls_ops = { |
1da177e4 | 331 | .read = sel_read_mls, |
57a62c23 | 332 | .llseek = generic_file_llseek, |
1da177e4 LT |
333 | }; |
334 | ||
cee74f47 EP |
335 | struct policy_load_memory { |
336 | size_t len; | |
337 | void *data; | |
338 | }; | |
339 | ||
340 | static int sel_open_policy(struct inode *inode, struct file *filp) | |
341 | { | |
342 | struct policy_load_memory *plm = NULL; | |
343 | int rc; | |
344 | ||
345 | BUG_ON(filp->private_data); | |
346 | ||
347 | mutex_lock(&sel_mutex); | |
348 | ||
be0554c9 SS |
349 | rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
350 | SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); | |
cee74f47 EP |
351 | if (rc) |
352 | goto err; | |
353 | ||
354 | rc = -EBUSY; | |
355 | if (policy_opened) | |
356 | goto err; | |
357 | ||
358 | rc = -ENOMEM; | |
359 | plm = kzalloc(sizeof(*plm), GFP_KERNEL); | |
360 | if (!plm) | |
361 | goto err; | |
362 | ||
363 | if (i_size_read(inode) != security_policydb_len()) { | |
5955102c | 364 | inode_lock(inode); |
cee74f47 | 365 | i_size_write(inode, security_policydb_len()); |
5955102c | 366 | inode_unlock(inode); |
cee74f47 EP |
367 | } |
368 | ||
369 | rc = security_read_policy(&plm->data, &plm->len); | |
370 | if (rc) | |
371 | goto err; | |
372 | ||
373 | policy_opened = 1; | |
374 | ||
375 | filp->private_data = plm; | |
376 | ||
377 | mutex_unlock(&sel_mutex); | |
378 | ||
379 | return 0; | |
380 | err: | |
381 | mutex_unlock(&sel_mutex); | |
382 | ||
383 | if (plm) | |
384 | vfree(plm->data); | |
385 | kfree(plm); | |
386 | return rc; | |
387 | } | |
388 | ||
389 | static int sel_release_policy(struct inode *inode, struct file *filp) | |
390 | { | |
391 | struct policy_load_memory *plm = filp->private_data; | |
392 | ||
393 | BUG_ON(!plm); | |
394 | ||
395 | policy_opened = 0; | |
396 | ||
397 | vfree(plm->data); | |
398 | kfree(plm); | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
403 | static ssize_t sel_read_policy(struct file *filp, char __user *buf, | |
404 | size_t count, loff_t *ppos) | |
405 | { | |
406 | struct policy_load_memory *plm = filp->private_data; | |
407 | int ret; | |
408 | ||
409 | mutex_lock(&sel_mutex); | |
410 | ||
be0554c9 SS |
411 | ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
412 | SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); | |
cee74f47 EP |
413 | if (ret) |
414 | goto out; | |
415 | ||
416 | ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); | |
417 | out: | |
418 | mutex_unlock(&sel_mutex); | |
419 | return ret; | |
420 | } | |
421 | ||
11bac800 | 422 | static int sel_mmap_policy_fault(struct vm_fault *vmf) |
845ca30f | 423 | { |
11bac800 | 424 | struct policy_load_memory *plm = vmf->vma->vm_file->private_data; |
845ca30f EP |
425 | unsigned long offset; |
426 | struct page *page; | |
427 | ||
428 | if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) | |
429 | return VM_FAULT_SIGBUS; | |
430 | ||
431 | offset = vmf->pgoff << PAGE_SHIFT; | |
432 | if (offset >= roundup(plm->len, PAGE_SIZE)) | |
433 | return VM_FAULT_SIGBUS; | |
434 | ||
435 | page = vmalloc_to_page(plm->data + offset); | |
436 | get_page(page); | |
437 | ||
438 | vmf->page = page; | |
439 | ||
440 | return 0; | |
441 | } | |
442 | ||
7cbea8dc | 443 | static const struct vm_operations_struct sel_mmap_policy_ops = { |
845ca30f EP |
444 | .fault = sel_mmap_policy_fault, |
445 | .page_mkwrite = sel_mmap_policy_fault, | |
446 | }; | |
447 | ||
ad3fa08c | 448 | static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) |
845ca30f EP |
449 | { |
450 | if (vma->vm_flags & VM_SHARED) { | |
451 | /* do not allow mprotect to make mapping writable */ | |
452 | vma->vm_flags &= ~VM_MAYWRITE; | |
453 | ||
454 | if (vma->vm_flags & VM_WRITE) | |
455 | return -EACCES; | |
456 | } | |
457 | ||
314e51b9 | 458 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
845ca30f EP |
459 | vma->vm_ops = &sel_mmap_policy_ops; |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
cee74f47 EP |
464 | static const struct file_operations sel_policy_ops = { |
465 | .open = sel_open_policy, | |
466 | .read = sel_read_policy, | |
845ca30f | 467 | .mmap = sel_mmap_policy, |
cee74f47 | 468 | .release = sel_release_policy, |
47a93a5b | 469 | .llseek = generic_file_llseek, |
cee74f47 EP |
470 | }; |
471 | ||
1872981b | 472 | static ssize_t sel_write_load(struct file *file, const char __user *buf, |
1da177e4 LT |
473 | size_t count, loff_t *ppos) |
474 | ||
475 | { | |
1da177e4 LT |
476 | ssize_t length; |
477 | void *data = NULL; | |
478 | ||
bb003079 | 479 | mutex_lock(&sel_mutex); |
1da177e4 | 480 | |
be0554c9 SS |
481 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
482 | SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); | |
1da177e4 LT |
483 | if (length) |
484 | goto out; | |
485 | ||
b77a493b EP |
486 | /* No partial writes. */ |
487 | length = -EINVAL; | |
488 | if (*ppos != 0) | |
1da177e4 | 489 | goto out; |
1da177e4 | 490 | |
b77a493b EP |
491 | length = -EFBIG; |
492 | if (count > 64 * 1024 * 1024) | |
493 | goto out; | |
494 | ||
495 | length = -ENOMEM; | |
496 | data = vmalloc(count); | |
497 | if (!data) | |
1da177e4 | 498 | goto out; |
1da177e4 LT |
499 | |
500 | length = -EFAULT; | |
501 | if (copy_from_user(data, buf, count) != 0) | |
502 | goto out; | |
503 | ||
504 | length = security_load_policy(data, count); | |
4262fb51 GT |
505 | if (length) { |
506 | pr_warn_ratelimited("SELinux: failed to load policy\n"); | |
1da177e4 | 507 | goto out; |
4262fb51 | 508 | } |
1da177e4 | 509 | |
b77a493b | 510 | length = sel_make_bools(); |
4262fb51 GT |
511 | if (length) { |
512 | pr_err("SELinux: failed to load policy booleans\n"); | |
e47c8fc5 | 513 | goto out1; |
4262fb51 | 514 | } |
e47c8fc5 | 515 | |
b77a493b | 516 | length = sel_make_classes(); |
4262fb51 GT |
517 | if (length) { |
518 | pr_err("SELinux: failed to load policy classes\n"); | |
3bb56b25 | 519 | goto out1; |
4262fb51 | 520 | } |
3bb56b25 | 521 | |
b77a493b | 522 | length = sel_make_policycap(); |
4262fb51 GT |
523 | if (length) { |
524 | pr_err("SELinux: failed to load policy capabilities\n"); | |
b77a493b | 525 | goto out1; |
4262fb51 | 526 | } |
b77a493b EP |
527 | |
528 | length = count; | |
e47c8fc5 CP |
529 | |
530 | out1: | |
af601e46 | 531 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, |
4746ec5b | 532 | "policy loaded auid=%u ses=%u", |
581abc09 | 533 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
4746ec5b | 534 | audit_get_sessionid(current)); |
1da177e4 | 535 | out: |
bb003079 | 536 | mutex_unlock(&sel_mutex); |
1da177e4 LT |
537 | vfree(data); |
538 | return length; | |
539 | } | |
540 | ||
9c2e08c5 | 541 | static const struct file_operations sel_load_ops = { |
1da177e4 | 542 | .write = sel_write_load, |
57a62c23 | 543 | .llseek = generic_file_llseek, |
1da177e4 LT |
544 | }; |
545 | ||
1872981b | 546 | static ssize_t sel_write_context(struct file *file, char *buf, size_t size) |
1da177e4 | 547 | { |
b77a493b | 548 | char *canon = NULL; |
ce9982d0 | 549 | u32 sid, len; |
1da177e4 LT |
550 | ssize_t length; |
551 | ||
be0554c9 SS |
552 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
553 | SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); | |
1da177e4 | 554 | if (length) |
b77a493b | 555 | goto out; |
1da177e4 | 556 | |
52a4c640 | 557 | length = security_context_to_sid(buf, size, &sid, GFP_KERNEL); |
b77a493b EP |
558 | if (length) |
559 | goto out; | |
1da177e4 | 560 | |
ce9982d0 | 561 | length = security_sid_to_context(sid, &canon, &len); |
b77a493b EP |
562 | if (length) |
563 | goto out; | |
ce9982d0 | 564 | |
b77a493b | 565 | length = -ERANGE; |
ce9982d0 | 566 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
744ba35e EP |
567 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
568 | "payload max\n", __func__, len); | |
1da177e4 | 569 | goto out; |
ce9982d0 | 570 | } |
1da177e4 | 571 | |
ce9982d0 SS |
572 | memcpy(buf, canon, len); |
573 | length = len; | |
1da177e4 | 574 | out: |
ce9982d0 | 575 | kfree(canon); |
1da177e4 LT |
576 | return length; |
577 | } | |
578 | ||
1da177e4 LT |
579 | static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, |
580 | size_t count, loff_t *ppos) | |
581 | { | |
582 | char tmpbuf[TMPBUFLEN]; | |
583 | ssize_t length; | |
584 | ||
585 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot); | |
586 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
587 | } | |
588 | ||
1872981b | 589 | static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, |
1da177e4 LT |
590 | size_t count, loff_t *ppos) |
591 | { | |
8365a719 | 592 | char *page; |
1da177e4 LT |
593 | ssize_t length; |
594 | unsigned int new_value; | |
595 | ||
be0554c9 SS |
596 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
597 | SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, | |
598 | NULL); | |
1da177e4 | 599 | if (length) |
8365a719 | 600 | return length; |
1da177e4 | 601 | |
bfd51626 | 602 | if (count >= PAGE_SIZE) |
8365a719 | 603 | return -ENOMEM; |
b77a493b EP |
604 | |
605 | /* No partial writes. */ | |
b77a493b | 606 | if (*ppos != 0) |
8365a719 | 607 | return -EINVAL; |
b77a493b | 608 | |
8365a719 AV |
609 | page = memdup_user_nul(buf, count); |
610 | if (IS_ERR(page)) | |
611 | return PTR_ERR(page); | |
1da177e4 LT |
612 | |
613 | length = -EINVAL; | |
614 | if (sscanf(page, "%u", &new_value) != 1) | |
615 | goto out; | |
616 | ||
617 | selinux_checkreqprot = new_value ? 1 : 0; | |
618 | length = count; | |
619 | out: | |
8365a719 | 620 | kfree(page); |
1da177e4 LT |
621 | return length; |
622 | } | |
9c2e08c5 | 623 | static const struct file_operations sel_checkreqprot_ops = { |
1da177e4 LT |
624 | .read = sel_read_checkreqprot, |
625 | .write = sel_write_checkreqprot, | |
57a62c23 | 626 | .llseek = generic_file_llseek, |
1da177e4 LT |
627 | }; |
628 | ||
f9df6458 AP |
629 | static ssize_t sel_write_validatetrans(struct file *file, |
630 | const char __user *buf, | |
631 | size_t count, loff_t *ppos) | |
632 | { | |
633 | char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; | |
634 | char *req = NULL; | |
635 | u32 osid, nsid, tsid; | |
636 | u16 tclass; | |
637 | int rc; | |
638 | ||
be0554c9 SS |
639 | rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
640 | SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); | |
f9df6458 AP |
641 | if (rc) |
642 | goto out; | |
643 | ||
644 | rc = -ENOMEM; | |
645 | if (count >= PAGE_SIZE) | |
646 | goto out; | |
647 | ||
648 | /* No partial writes. */ | |
649 | rc = -EINVAL; | |
650 | if (*ppos != 0) | |
651 | goto out; | |
652 | ||
0b884d25 AV |
653 | req = memdup_user_nul(buf, count); |
654 | if (IS_ERR(req)) { | |
655 | rc = PTR_ERR(req); | |
656 | req = NULL; | |
f9df6458 | 657 | goto out; |
0b884d25 | 658 | } |
f9df6458 AP |
659 | |
660 | rc = -ENOMEM; | |
661 | oldcon = kzalloc(count + 1, GFP_KERNEL); | |
662 | if (!oldcon) | |
663 | goto out; | |
664 | ||
665 | newcon = kzalloc(count + 1, GFP_KERNEL); | |
666 | if (!newcon) | |
667 | goto out; | |
668 | ||
669 | taskcon = kzalloc(count + 1, GFP_KERNEL); | |
670 | if (!taskcon) | |
671 | goto out; | |
672 | ||
673 | rc = -EINVAL; | |
674 | if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) | |
675 | goto out; | |
676 | ||
677 | rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL); | |
678 | if (rc) | |
679 | goto out; | |
680 | ||
681 | rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL); | |
682 | if (rc) | |
683 | goto out; | |
684 | ||
685 | rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL); | |
686 | if (rc) | |
687 | goto out; | |
688 | ||
689 | rc = security_validate_transition_user(osid, nsid, tsid, tclass); | |
690 | if (!rc) | |
691 | rc = count; | |
692 | out: | |
693 | kfree(req); | |
694 | kfree(oldcon); | |
695 | kfree(newcon); | |
696 | kfree(taskcon); | |
697 | return rc; | |
698 | } | |
699 | ||
700 | static const struct file_operations sel_transition_ops = { | |
701 | .write = sel_write_validatetrans, | |
702 | .llseek = generic_file_llseek, | |
703 | }; | |
704 | ||
1da177e4 LT |
705 | /* |
706 | * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c | |
707 | */ | |
1872981b EP |
708 | static ssize_t sel_write_access(struct file *file, char *buf, size_t size); |
709 | static ssize_t sel_write_create(struct file *file, char *buf, size_t size); | |
710 | static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); | |
711 | static ssize_t sel_write_user(struct file *file, char *buf, size_t size); | |
712 | static ssize_t sel_write_member(struct file *file, char *buf, size_t size); | |
1da177e4 LT |
713 | |
714 | static ssize_t (*write_op[])(struct file *, char *, size_t) = { | |
715 | [SEL_ACCESS] = sel_write_access, | |
716 | [SEL_CREATE] = sel_write_create, | |
717 | [SEL_RELABEL] = sel_write_relabel, | |
718 | [SEL_USER] = sel_write_user, | |
719 | [SEL_MEMBER] = sel_write_member, | |
ce9982d0 | 720 | [SEL_CONTEXT] = sel_write_context, |
1da177e4 LT |
721 | }; |
722 | ||
723 | static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) | |
724 | { | |
496ad9aa | 725 | ino_t ino = file_inode(file)->i_ino; |
1da177e4 LT |
726 | char *data; |
727 | ssize_t rv; | |
728 | ||
6e20a64a | 729 | if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) |
1da177e4 LT |
730 | return -EINVAL; |
731 | ||
732 | data = simple_transaction_get(file, buf, size); | |
733 | if (IS_ERR(data)) | |
734 | return PTR_ERR(data); | |
735 | ||
1872981b EP |
736 | rv = write_op[ino](file, data, size); |
737 | if (rv > 0) { | |
1da177e4 LT |
738 | simple_transaction_set(file, rv); |
739 | rv = size; | |
740 | } | |
741 | return rv; | |
742 | } | |
743 | ||
9c2e08c5 | 744 | static const struct file_operations transaction_ops = { |
1da177e4 LT |
745 | .write = selinux_transaction_write, |
746 | .read = simple_transaction_read, | |
747 | .release = simple_transaction_release, | |
57a62c23 | 748 | .llseek = generic_file_llseek, |
1da177e4 LT |
749 | }; |
750 | ||
751 | /* | |
752 | * payload - write methods | |
753 | * If the method has a response, the response should be put in buf, | |
754 | * and the length returned. Otherwise return 0 or and -error. | |
755 | */ | |
756 | ||
1872981b | 757 | static ssize_t sel_write_access(struct file *file, char *buf, size_t size) |
1da177e4 | 758 | { |
b77a493b | 759 | char *scon = NULL, *tcon = NULL; |
1da177e4 LT |
760 | u32 ssid, tsid; |
761 | u16 tclass; | |
1da177e4 LT |
762 | struct av_decision avd; |
763 | ssize_t length; | |
764 | ||
be0554c9 SS |
765 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
766 | SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); | |
1da177e4 | 767 | if (length) |
b77a493b | 768 | goto out; |
1da177e4 LT |
769 | |
770 | length = -ENOMEM; | |
c1a7368a | 771 | scon = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 | 772 | if (!scon) |
b77a493b | 773 | goto out; |
1da177e4 | 774 | |
b77a493b | 775 | length = -ENOMEM; |
c1a7368a | 776 | tcon = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 LT |
777 | if (!tcon) |
778 | goto out; | |
1da177e4 LT |
779 | |
780 | length = -EINVAL; | |
19439d05 | 781 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
b77a493b | 782 | goto out; |
1da177e4 | 783 | |
44be2f65 | 784 | length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); |
b77a493b EP |
785 | if (length) |
786 | goto out; | |
787 | ||
44be2f65 | 788 | length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); |
b77a493b EP |
789 | if (length) |
790 | goto out; | |
1da177e4 | 791 | |
19439d05 | 792 | security_compute_av_user(ssid, tsid, tclass, &avd); |
1da177e4 LT |
793 | |
794 | length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, | |
8a6f83af | 795 | "%x %x %x %x %u %x", |
f1c6381a | 796 | avd.allowed, 0xffffffff, |
1da177e4 | 797 | avd.auditallow, avd.auditdeny, |
8a6f83af | 798 | avd.seqno, avd.flags); |
1da177e4 | 799 | out: |
b77a493b | 800 | kfree(tcon); |
1da177e4 LT |
801 | kfree(scon); |
802 | return length; | |
803 | } | |
804 | ||
1872981b | 805 | static ssize_t sel_write_create(struct file *file, char *buf, size_t size) |
1da177e4 | 806 | { |
b77a493b | 807 | char *scon = NULL, *tcon = NULL; |
f50a3ec9 | 808 | char *namebuf = NULL, *objname = NULL; |
1da177e4 LT |
809 | u32 ssid, tsid, newsid; |
810 | u16 tclass; | |
811 | ssize_t length; | |
b77a493b | 812 | char *newcon = NULL; |
1da177e4 | 813 | u32 len; |
f50a3ec9 | 814 | int nargs; |
1da177e4 | 815 | |
be0554c9 SS |
816 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
817 | SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, | |
818 | NULL); | |
1da177e4 | 819 | if (length) |
b77a493b | 820 | goto out; |
1da177e4 LT |
821 | |
822 | length = -ENOMEM; | |
c1a7368a | 823 | scon = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 | 824 | if (!scon) |
b77a493b | 825 | goto out; |
1da177e4 | 826 | |
b77a493b | 827 | length = -ENOMEM; |
c1a7368a | 828 | tcon = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 LT |
829 | if (!tcon) |
830 | goto out; | |
1da177e4 | 831 | |
f50a3ec9 KK |
832 | length = -ENOMEM; |
833 | namebuf = kzalloc(size + 1, GFP_KERNEL); | |
834 | if (!namebuf) | |
835 | goto out; | |
836 | ||
1da177e4 | 837 | length = -EINVAL; |
f50a3ec9 KK |
838 | nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); |
839 | if (nargs < 3 || nargs > 4) | |
b77a493b | 840 | goto out; |
0f7e4c33 KK |
841 | if (nargs == 4) { |
842 | /* | |
843 | * If and when the name of new object to be queried contains | |
844 | * either whitespace or multibyte characters, they shall be | |
845 | * encoded based on the percentage-encoding rule. | |
846 | * If not encoded, the sscanf logic picks up only left-half | |
847 | * of the supplied name; splitted by a whitespace unexpectedly. | |
848 | */ | |
849 | char *r, *w; | |
850 | int c1, c2; | |
851 | ||
852 | r = w = namebuf; | |
853 | do { | |
854 | c1 = *r++; | |
855 | if (c1 == '+') | |
856 | c1 = ' '; | |
857 | else if (c1 == '%') { | |
af7ff2c2 AS |
858 | c1 = hex_to_bin(*r++); |
859 | if (c1 < 0) | |
0f7e4c33 | 860 | goto out; |
af7ff2c2 AS |
861 | c2 = hex_to_bin(*r++); |
862 | if (c2 < 0) | |
0f7e4c33 KK |
863 | goto out; |
864 | c1 = (c1 << 4) | c2; | |
865 | } | |
866 | *w++ = c1; | |
867 | } while (c1 != '\0'); | |
868 | ||
f50a3ec9 | 869 | objname = namebuf; |
0f7e4c33 | 870 | } |
1da177e4 | 871 | |
44be2f65 | 872 | length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); |
b77a493b EP |
873 | if (length) |
874 | goto out; | |
875 | ||
44be2f65 | 876 | length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); |
b77a493b EP |
877 | if (length) |
878 | goto out; | |
1da177e4 | 879 | |
f50a3ec9 KK |
880 | length = security_transition_sid_user(ssid, tsid, tclass, |
881 | objname, &newsid); | |
b77a493b EP |
882 | if (length) |
883 | goto out; | |
1da177e4 LT |
884 | |
885 | length = security_sid_to_context(newsid, &newcon, &len); | |
b77a493b EP |
886 | if (length) |
887 | goto out; | |
1da177e4 | 888 | |
b77a493b | 889 | length = -ERANGE; |
1da177e4 | 890 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
744ba35e EP |
891 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
892 | "payload max\n", __func__, len); | |
b77a493b | 893 | goto out; |
1da177e4 LT |
894 | } |
895 | ||
896 | memcpy(buf, newcon, len); | |
897 | length = len; | |
b77a493b | 898 | out: |
1da177e4 | 899 | kfree(newcon); |
f50a3ec9 | 900 | kfree(namebuf); |
1da177e4 | 901 | kfree(tcon); |
1da177e4 LT |
902 | kfree(scon); |
903 | return length; | |
904 | } | |
905 | ||
1872981b | 906 | static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) |
1da177e4 | 907 | { |
b77a493b | 908 | char *scon = NULL, *tcon = NULL; |
1da177e4 LT |
909 | u32 ssid, tsid, newsid; |
910 | u16 tclass; | |
911 | ssize_t length; | |
b77a493b | 912 | char *newcon = NULL; |
1da177e4 LT |
913 | u32 len; |
914 | ||
be0554c9 SS |
915 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
916 | SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, | |
917 | NULL); | |
1da177e4 | 918 | if (length) |
b77a493b | 919 | goto out; |
1da177e4 LT |
920 | |
921 | length = -ENOMEM; | |
c1a7368a | 922 | scon = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 | 923 | if (!scon) |
b77a493b | 924 | goto out; |
1da177e4 | 925 | |
b77a493b | 926 | length = -ENOMEM; |
c1a7368a | 927 | tcon = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 LT |
928 | if (!tcon) |
929 | goto out; | |
1da177e4 LT |
930 | |
931 | length = -EINVAL; | |
932 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) | |
b77a493b | 933 | goto out; |
1da177e4 | 934 | |
44be2f65 | 935 | length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); |
b77a493b EP |
936 | if (length) |
937 | goto out; | |
938 | ||
44be2f65 | 939 | length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); |
b77a493b EP |
940 | if (length) |
941 | goto out; | |
1da177e4 LT |
942 | |
943 | length = security_change_sid(ssid, tsid, tclass, &newsid); | |
b77a493b EP |
944 | if (length) |
945 | goto out; | |
1da177e4 LT |
946 | |
947 | length = security_sid_to_context(newsid, &newcon, &len); | |
b77a493b EP |
948 | if (length) |
949 | goto out; | |
1da177e4 | 950 | |
b77a493b EP |
951 | length = -ERANGE; |
952 | if (len > SIMPLE_TRANSACTION_LIMIT) | |
953 | goto out; | |
1da177e4 LT |
954 | |
955 | memcpy(buf, newcon, len); | |
956 | length = len; | |
b77a493b | 957 | out: |
1da177e4 | 958 | kfree(newcon); |
1da177e4 | 959 | kfree(tcon); |
1da177e4 LT |
960 | kfree(scon); |
961 | return length; | |
962 | } | |
963 | ||
1872981b | 964 | static ssize_t sel_write_user(struct file *file, char *buf, size_t size) |
1da177e4 | 965 | { |
b77a493b EP |
966 | char *con = NULL, *user = NULL, *ptr; |
967 | u32 sid, *sids = NULL; | |
1da177e4 LT |
968 | ssize_t length; |
969 | char *newcon; | |
970 | int i, rc; | |
971 | u32 len, nsids; | |
972 | ||
be0554c9 SS |
973 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
974 | SECCLASS_SECURITY, SECURITY__COMPUTE_USER, | |
975 | NULL); | |
1da177e4 | 976 | if (length) |
6eab04a8 | 977 | goto out; |
1da177e4 LT |
978 | |
979 | length = -ENOMEM; | |
c1a7368a | 980 | con = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 | 981 | if (!con) |
6eab04a8 | 982 | goto out; |
1da177e4 | 983 | |
b77a493b | 984 | length = -ENOMEM; |
c1a7368a | 985 | user = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 LT |
986 | if (!user) |
987 | goto out; | |
1da177e4 LT |
988 | |
989 | length = -EINVAL; | |
990 | if (sscanf(buf, "%s %s", con, user) != 2) | |
b77a493b | 991 | goto out; |
1da177e4 | 992 | |
44be2f65 | 993 | length = security_context_str_to_sid(con, &sid, GFP_KERNEL); |
b77a493b EP |
994 | if (length) |
995 | goto out; | |
1da177e4 LT |
996 | |
997 | length = security_get_user_sids(sid, user, &sids, &nsids); | |
b77a493b EP |
998 | if (length) |
999 | goto out; | |
1da177e4 LT |
1000 | |
1001 | length = sprintf(buf, "%u", nsids) + 1; | |
1002 | ptr = buf + length; | |
1003 | for (i = 0; i < nsids; i++) { | |
1004 | rc = security_sid_to_context(sids[i], &newcon, &len); | |
1005 | if (rc) { | |
1006 | length = rc; | |
b77a493b | 1007 | goto out; |
1da177e4 LT |
1008 | } |
1009 | if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { | |
1010 | kfree(newcon); | |
1011 | length = -ERANGE; | |
b77a493b | 1012 | goto out; |
1da177e4 LT |
1013 | } |
1014 | memcpy(ptr, newcon, len); | |
1015 | kfree(newcon); | |
1016 | ptr += len; | |
1017 | length += len; | |
1018 | } | |
b77a493b | 1019 | out: |
1da177e4 | 1020 | kfree(sids); |
1da177e4 | 1021 | kfree(user); |
1da177e4 LT |
1022 | kfree(con); |
1023 | return length; | |
1024 | } | |
1025 | ||
1872981b | 1026 | static ssize_t sel_write_member(struct file *file, char *buf, size_t size) |
1da177e4 | 1027 | { |
b77a493b | 1028 | char *scon = NULL, *tcon = NULL; |
1da177e4 LT |
1029 | u32 ssid, tsid, newsid; |
1030 | u16 tclass; | |
1031 | ssize_t length; | |
b77a493b | 1032 | char *newcon = NULL; |
1da177e4 LT |
1033 | u32 len; |
1034 | ||
be0554c9 SS |
1035 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
1036 | SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, | |
1037 | NULL); | |
1da177e4 | 1038 | if (length) |
b77a493b | 1039 | goto out; |
1da177e4 LT |
1040 | |
1041 | length = -ENOMEM; | |
c1a7368a | 1042 | scon = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 | 1043 | if (!scon) |
6eab04a8 | 1044 | goto out; |
1da177e4 | 1045 | |
b77a493b | 1046 | length = -ENOMEM; |
c1a7368a | 1047 | tcon = kzalloc(size + 1, GFP_KERNEL); |
1da177e4 LT |
1048 | if (!tcon) |
1049 | goto out; | |
1da177e4 LT |
1050 | |
1051 | length = -EINVAL; | |
1052 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) | |
b77a493b | 1053 | goto out; |
1da177e4 | 1054 | |
44be2f65 | 1055 | length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); |
b77a493b EP |
1056 | if (length) |
1057 | goto out; | |
1058 | ||
44be2f65 | 1059 | length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); |
b77a493b EP |
1060 | if (length) |
1061 | goto out; | |
1da177e4 LT |
1062 | |
1063 | length = security_member_sid(ssid, tsid, tclass, &newsid); | |
b77a493b EP |
1064 | if (length) |
1065 | goto out; | |
1da177e4 LT |
1066 | |
1067 | length = security_sid_to_context(newsid, &newcon, &len); | |
b77a493b EP |
1068 | if (length) |
1069 | goto out; | |
1da177e4 | 1070 | |
b77a493b | 1071 | length = -ERANGE; |
1da177e4 | 1072 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
744ba35e EP |
1073 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
1074 | "payload max\n", __func__, len); | |
b77a493b | 1075 | goto out; |
1da177e4 LT |
1076 | } |
1077 | ||
1078 | memcpy(buf, newcon, len); | |
1079 | length = len; | |
b77a493b | 1080 | out: |
1da177e4 | 1081 | kfree(newcon); |
1da177e4 | 1082 | kfree(tcon); |
1da177e4 LT |
1083 | kfree(scon); |
1084 | return length; | |
1085 | } | |
1086 | ||
1087 | static struct inode *sel_make_inode(struct super_block *sb, int mode) | |
1088 | { | |
1089 | struct inode *ret = new_inode(sb); | |
1090 | ||
1091 | if (ret) { | |
1092 | ret->i_mode = mode; | |
078cd827 | 1093 | ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); |
1da177e4 LT |
1094 | } |
1095 | return ret; | |
1096 | } | |
1097 | ||
1da177e4 LT |
1098 | static ssize_t sel_read_bool(struct file *filep, char __user *buf, |
1099 | size_t count, loff_t *ppos) | |
1100 | { | |
1101 | char *page = NULL; | |
1102 | ssize_t length; | |
1da177e4 LT |
1103 | ssize_t ret; |
1104 | int cur_enforcing; | |
496ad9aa | 1105 | unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; |
d313f948 | 1106 | const char *name = filep->f_path.dentry->d_name.name; |
1da177e4 | 1107 | |
bb003079 | 1108 | mutex_lock(&sel_mutex); |
1da177e4 | 1109 | |
b77a493b EP |
1110 | ret = -EINVAL; |
1111 | if (index >= bool_num || strcmp(name, bool_pending_names[index])) | |
d313f948 | 1112 | goto out; |
1da177e4 | 1113 | |
b77a493b | 1114 | ret = -ENOMEM; |
1872981b | 1115 | page = (char *)get_zeroed_page(GFP_KERNEL); |
b77a493b | 1116 | if (!page) |
1da177e4 | 1117 | goto out; |
1da177e4 | 1118 | |
d313f948 | 1119 | cur_enforcing = security_get_bool_value(index); |
1da177e4 LT |
1120 | if (cur_enforcing < 0) { |
1121 | ret = cur_enforcing; | |
1122 | goto out; | |
1123 | } | |
1da177e4 | 1124 | length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, |
d313f948 | 1125 | bool_pending_values[index]); |
68bdcf28 | 1126 | ret = simple_read_from_buffer(buf, count, ppos, page, length); |
1da177e4 | 1127 | out: |
bb003079 | 1128 | mutex_unlock(&sel_mutex); |
b77a493b | 1129 | free_page((unsigned long)page); |
1da177e4 LT |
1130 | return ret; |
1131 | } | |
1132 | ||
1133 | static ssize_t sel_write_bool(struct file *filep, const char __user *buf, | |
1134 | size_t count, loff_t *ppos) | |
1135 | { | |
1136 | char *page = NULL; | |
d313f948 | 1137 | ssize_t length; |
1da177e4 | 1138 | int new_value; |
496ad9aa | 1139 | unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; |
d313f948 | 1140 | const char *name = filep->f_path.dentry->d_name.name; |
1da177e4 | 1141 | |
bb003079 | 1142 | mutex_lock(&sel_mutex); |
1da177e4 | 1143 | |
be0554c9 SS |
1144 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
1145 | SECCLASS_SECURITY, SECURITY__SETBOOL, | |
1146 | NULL); | |
1da177e4 LT |
1147 | if (length) |
1148 | goto out; | |
1149 | ||
b77a493b EP |
1150 | length = -EINVAL; |
1151 | if (index >= bool_num || strcmp(name, bool_pending_names[index])) | |
d313f948 | 1152 | goto out; |
d313f948 | 1153 | |
b77a493b EP |
1154 | length = -ENOMEM; |
1155 | if (count >= PAGE_SIZE) | |
1da177e4 | 1156 | goto out; |
d313f948 | 1157 | |
b77a493b EP |
1158 | /* No partial writes. */ |
1159 | length = -EINVAL; | |
1160 | if (*ppos != 0) | |
1da177e4 | 1161 | goto out; |
b77a493b | 1162 | |
8365a719 AV |
1163 | page = memdup_user_nul(buf, count); |
1164 | if (IS_ERR(page)) { | |
1165 | length = PTR_ERR(page); | |
1166 | page = NULL; | |
1da177e4 | 1167 | goto out; |
8365a719 | 1168 | } |
1da177e4 LT |
1169 | |
1170 | length = -EINVAL; | |
1171 | if (sscanf(page, "%d", &new_value) != 1) | |
1172 | goto out; | |
1173 | ||
1174 | if (new_value) | |
1175 | new_value = 1; | |
1176 | ||
d313f948 | 1177 | bool_pending_values[index] = new_value; |
1da177e4 LT |
1178 | length = count; |
1179 | ||
1180 | out: | |
bb003079 | 1181 | mutex_unlock(&sel_mutex); |
8365a719 | 1182 | kfree(page); |
1da177e4 LT |
1183 | return length; |
1184 | } | |
1185 | ||
9c2e08c5 | 1186 | static const struct file_operations sel_bool_ops = { |
1872981b EP |
1187 | .read = sel_read_bool, |
1188 | .write = sel_write_bool, | |
57a62c23 | 1189 | .llseek = generic_file_llseek, |
1da177e4 LT |
1190 | }; |
1191 | ||
1192 | static ssize_t sel_commit_bools_write(struct file *filep, | |
1193 | const char __user *buf, | |
1194 | size_t count, loff_t *ppos) | |
1195 | { | |
1196 | char *page = NULL; | |
d313f948 | 1197 | ssize_t length; |
1da177e4 LT |
1198 | int new_value; |
1199 | ||
bb003079 | 1200 | mutex_lock(&sel_mutex); |
1da177e4 | 1201 | |
be0554c9 SS |
1202 | length = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
1203 | SECCLASS_SECURITY, SECURITY__SETBOOL, | |
1204 | NULL); | |
1da177e4 LT |
1205 | if (length) |
1206 | goto out; | |
1207 | ||
b77a493b EP |
1208 | length = -ENOMEM; |
1209 | if (count >= PAGE_SIZE) | |
1da177e4 | 1210 | goto out; |
b77a493b EP |
1211 | |
1212 | /* No partial writes. */ | |
1213 | length = -EINVAL; | |
1214 | if (*ppos != 0) | |
1da177e4 | 1215 | goto out; |
b77a493b | 1216 | |
8365a719 AV |
1217 | page = memdup_user_nul(buf, count); |
1218 | if (IS_ERR(page)) { | |
1219 | length = PTR_ERR(page); | |
1220 | page = NULL; | |
1da177e4 | 1221 | goto out; |
8365a719 | 1222 | } |
1da177e4 LT |
1223 | |
1224 | length = -EINVAL; | |
1225 | if (sscanf(page, "%d", &new_value) != 1) | |
1226 | goto out; | |
1227 | ||
b77a493b | 1228 | length = 0; |
1872981b | 1229 | if (new_value && bool_pending_values) |
b77a493b | 1230 | length = security_set_bools(bool_num, bool_pending_values); |
1da177e4 | 1231 | |
b77a493b EP |
1232 | if (!length) |
1233 | length = count; | |
1da177e4 LT |
1234 | |
1235 | out: | |
bb003079 | 1236 | mutex_unlock(&sel_mutex); |
8365a719 | 1237 | kfree(page); |
1da177e4 LT |
1238 | return length; |
1239 | } | |
1240 | ||
9c2e08c5 | 1241 | static const struct file_operations sel_commit_bools_ops = { |
1872981b | 1242 | .write = sel_commit_bools_write, |
57a62c23 | 1243 | .llseek = generic_file_llseek, |
1da177e4 LT |
1244 | }; |
1245 | ||
0c92d7c7 | 1246 | static void sel_remove_entries(struct dentry *de) |
1da177e4 | 1247 | { |
ad52184b AV |
1248 | d_genocide(de); |
1249 | shrink_dcache_parent(de); | |
1da177e4 LT |
1250 | } |
1251 | ||
1252 | #define BOOL_DIR_NAME "booleans" | |
1253 | ||
1254 | static int sel_make_bools(void) | |
1255 | { | |
b77a493b | 1256 | int i, ret; |
1da177e4 LT |
1257 | ssize_t len; |
1258 | struct dentry *dentry = NULL; | |
1259 | struct dentry *dir = bool_dir; | |
1260 | struct inode *inode = NULL; | |
1261 | struct inode_security_struct *isec; | |
1262 | char **names = NULL, *page; | |
1263 | int num; | |
1264 | int *values = NULL; | |
1265 | u32 sid; | |
1266 | ||
1267 | /* remove any existing files */ | |
8007f102 XF |
1268 | for (i = 0; i < bool_num; i++) |
1269 | kfree(bool_pending_names[i]); | |
d313f948 | 1270 | kfree(bool_pending_names); |
9a5f04bf | 1271 | kfree(bool_pending_values); |
154c50ca | 1272 | bool_num = 0; |
d313f948 | 1273 | bool_pending_names = NULL; |
20c19e41 | 1274 | bool_pending_values = NULL; |
1da177e4 | 1275 | |
0c92d7c7 | 1276 | sel_remove_entries(dir); |
1da177e4 | 1277 | |
b77a493b | 1278 | ret = -ENOMEM; |
1872981b EP |
1279 | page = (char *)get_zeroed_page(GFP_KERNEL); |
1280 | if (!page) | |
b77a493b | 1281 | goto out; |
1da177e4 LT |
1282 | |
1283 | ret = security_get_bools(&num, &names, &values); | |
b77a493b | 1284 | if (ret) |
1da177e4 LT |
1285 | goto out; |
1286 | ||
1287 | for (i = 0; i < num; i++) { | |
b77a493b | 1288 | ret = -ENOMEM; |
1da177e4 | 1289 | dentry = d_alloc_name(dir, names[i]); |
b77a493b EP |
1290 | if (!dentry) |
1291 | goto out; | |
1292 | ||
1293 | ret = -ENOMEM; | |
1da177e4 | 1294 | inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); |
b77a493b EP |
1295 | if (!inode) |
1296 | goto out; | |
1da177e4 | 1297 | |
b77a493b | 1298 | ret = -ENAMETOOLONG; |
cc1dad71 | 1299 | len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); |
b77a493b EP |
1300 | if (len >= PAGE_SIZE) |
1301 | goto out; | |
1302 | ||
7a90fc39 | 1303 | isec = (struct inode_security_struct *)selinux_inode(inode); |
1872981b | 1304 | ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid); |
4262fb51 | 1305 | if (ret) { |
900fde06 GT |
1306 | pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", |
1307 | page); | |
1308 | sid = SECINITSID_SECURITY; | |
4262fb51 GT |
1309 | } |
1310 | ||
1da177e4 | 1311 | isec->sid = sid; |
42059112 | 1312 | isec->initialized = LABEL_INITIALIZED; |
1da177e4 | 1313 | inode->i_fop = &sel_bool_ops; |
bce34bc0 | 1314 | inode->i_ino = i|SEL_BOOL_INO_OFFSET; |
1da177e4 LT |
1315 | d_add(dentry, inode); |
1316 | } | |
1317 | bool_num = num; | |
d313f948 | 1318 | bool_pending_names = names; |
1da177e4 | 1319 | bool_pending_values = values; |
b77a493b EP |
1320 | |
1321 | free_page((unsigned long)page); | |
1322 | return 0; | |
1da177e4 LT |
1323 | out: |
1324 | free_page((unsigned long)page); | |
b77a493b | 1325 | |
1da177e4 | 1326 | if (names) { |
9a5f04bf JJ |
1327 | for (i = 0; i < num; i++) |
1328 | kfree(names[i]); | |
1da177e4 LT |
1329 | kfree(names); |
1330 | } | |
20c19e41 | 1331 | kfree(values); |
0c92d7c7 | 1332 | sel_remove_entries(dir); |
b77a493b EP |
1333 | |
1334 | return ret; | |
1da177e4 LT |
1335 | } |
1336 | ||
1337 | #define NULL_FILE_NAME "null" | |
1338 | ||
765927b2 | 1339 | struct path selinux_null; |
1da177e4 LT |
1340 | |
1341 | static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, | |
1342 | size_t count, loff_t *ppos) | |
1343 | { | |
1344 | char tmpbuf[TMPBUFLEN]; | |
1345 | ssize_t length; | |
1346 | ||
1347 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); | |
1348 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
1349 | } | |
1350 | ||
1872981b EP |
1351 | static ssize_t sel_write_avc_cache_threshold(struct file *file, |
1352 | const char __user *buf, | |
1da177e4 LT |
1353 | size_t count, loff_t *ppos) |
1354 | ||
1355 | { | |
8365a719 | 1356 | char *page; |
1da177e4 | 1357 | ssize_t ret; |
309c5fad | 1358 | unsigned int new_value; |
1da177e4 | 1359 | |
be0554c9 SS |
1360 | ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, |
1361 | SECCLASS_SECURITY, SECURITY__SETSECPARAM, | |
1362 | NULL); | |
b77a493b | 1363 | if (ret) |
8365a719 | 1364 | return ret; |
1da177e4 | 1365 | |
b77a493b | 1366 | if (count >= PAGE_SIZE) |
8365a719 | 1367 | return -ENOMEM; |
1da177e4 | 1368 | |
b77a493b | 1369 | /* No partial writes. */ |
b77a493b | 1370 | if (*ppos != 0) |
8365a719 | 1371 | return -EINVAL; |
1da177e4 | 1372 | |
8365a719 AV |
1373 | page = memdup_user_nul(buf, count); |
1374 | if (IS_ERR(page)) | |
1375 | return PTR_ERR(page); | |
1da177e4 | 1376 | |
b77a493b EP |
1377 | ret = -EINVAL; |
1378 | if (sscanf(page, "%u", &new_value) != 1) | |
1da177e4 | 1379 | goto out; |
1da177e4 | 1380 | |
b77a493b EP |
1381 | avc_cache_threshold = new_value; |
1382 | ||
1da177e4 | 1383 | ret = count; |
1da177e4 | 1384 | out: |
8365a719 | 1385 | kfree(page); |
1da177e4 LT |
1386 | return ret; |
1387 | } | |
1388 | ||
1389 | static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, | |
1390 | size_t count, loff_t *ppos) | |
1391 | { | |
1392 | char *page; | |
b77a493b | 1393 | ssize_t length; |
1da177e4 LT |
1394 | |
1395 | page = (char *)__get_free_page(GFP_KERNEL); | |
b77a493b EP |
1396 | if (!page) |
1397 | return -ENOMEM; | |
1398 | ||
1399 | length = avc_get_hash_stats(page); | |
1400 | if (length >= 0) | |
1401 | length = simple_read_from_buffer(buf, count, ppos, page, length); | |
1da177e4 | 1402 | free_page((unsigned long)page); |
b77a493b EP |
1403 | |
1404 | return length; | |
1da177e4 LT |
1405 | } |
1406 | ||
9c2e08c5 | 1407 | static const struct file_operations sel_avc_cache_threshold_ops = { |
1da177e4 LT |
1408 | .read = sel_read_avc_cache_threshold, |
1409 | .write = sel_write_avc_cache_threshold, | |
57a62c23 | 1410 | .llseek = generic_file_llseek, |
1da177e4 LT |
1411 | }; |
1412 | ||
9c2e08c5 | 1413 | static const struct file_operations sel_avc_hash_stats_ops = { |
1da177e4 | 1414 | .read = sel_read_avc_hash_stats, |
57a62c23 | 1415 | .llseek = generic_file_llseek, |
1da177e4 LT |
1416 | }; |
1417 | ||
1418 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS | |
1419 | static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) | |
1420 | { | |
1421 | int cpu; | |
1422 | ||
4f4b6c1a | 1423 | for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { |
1da177e4 LT |
1424 | if (!cpu_possible(cpu)) |
1425 | continue; | |
1426 | *idx = cpu + 1; | |
1427 | return &per_cpu(avc_cache_stats, cpu); | |
1428 | } | |
1429 | return NULL; | |
1430 | } | |
1431 | ||
1432 | static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) | |
1433 | { | |
1434 | loff_t n = *pos - 1; | |
1435 | ||
1436 | if (*pos == 0) | |
1437 | return SEQ_START_TOKEN; | |
1438 | ||
1439 | return sel_avc_get_stat_idx(&n); | |
1440 | } | |
1441 | ||
1442 | static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
1443 | { | |
1444 | return sel_avc_get_stat_idx(pos); | |
1445 | } | |
1446 | ||
1447 | static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) | |
1448 | { | |
1449 | struct avc_cache_stats *st = v; | |
1450 | ||
710a0647 ME |
1451 | if (v == SEQ_START_TOKEN) { |
1452 | seq_puts(seq, | |
1453 | "lookups hits misses allocations reclaims frees\n"); | |
1454 | } else { | |
257313b2 LT |
1455 | unsigned int lookups = st->lookups; |
1456 | unsigned int misses = st->misses; | |
1457 | unsigned int hits = lookups - misses; | |
1458 | seq_printf(seq, "%u %u %u %u %u %u\n", lookups, | |
1459 | hits, misses, st->allocations, | |
1da177e4 | 1460 | st->reclaims, st->frees); |
257313b2 | 1461 | } |
1da177e4 LT |
1462 | return 0; |
1463 | } | |
1464 | ||
1465 | static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) | |
1466 | { } | |
1467 | ||
1996a109 | 1468 | static const struct seq_operations sel_avc_cache_stats_seq_ops = { |
1da177e4 LT |
1469 | .start = sel_avc_stats_seq_start, |
1470 | .next = sel_avc_stats_seq_next, | |
1471 | .show = sel_avc_stats_seq_show, | |
1472 | .stop = sel_avc_stats_seq_stop, | |
1473 | }; | |
1474 | ||
1475 | static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) | |
1476 | { | |
1477 | return seq_open(file, &sel_avc_cache_stats_seq_ops); | |
1478 | } | |
1479 | ||
9c2e08c5 | 1480 | static const struct file_operations sel_avc_cache_stats_ops = { |
1da177e4 LT |
1481 | .open = sel_open_avc_cache_stats, |
1482 | .read = seq_read, | |
1483 | .llseek = seq_lseek, | |
1484 | .release = seq_release, | |
1485 | }; | |
1486 | #endif | |
1487 | ||
1488 | static int sel_make_avc_files(struct dentry *dir) | |
1489 | { | |
b77a493b | 1490 | int i; |
cda37124 | 1491 | static const struct tree_descr files[] = { |
1da177e4 LT |
1492 | { "cache_threshold", |
1493 | &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, | |
1494 | { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, | |
1495 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS | |
1496 | { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, | |
1497 | #endif | |
1498 | }; | |
1499 | ||
6e20a64a | 1500 | for (i = 0; i < ARRAY_SIZE(files); i++) { |
1da177e4 LT |
1501 | struct inode *inode; |
1502 | struct dentry *dentry; | |
1503 | ||
1504 | dentry = d_alloc_name(dir, files[i].name); | |
b77a493b EP |
1505 | if (!dentry) |
1506 | return -ENOMEM; | |
1da177e4 LT |
1507 | |
1508 | inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); | |
b77a493b EP |
1509 | if (!inode) |
1510 | return -ENOMEM; | |
1511 | ||
1da177e4 | 1512 | inode->i_fop = files[i].ops; |
6174eafc | 1513 | inode->i_ino = ++sel_last_ino; |
1da177e4 LT |
1514 | d_add(dentry, inode); |
1515 | } | |
b77a493b EP |
1516 | |
1517 | return 0; | |
1da177e4 LT |
1518 | } |
1519 | ||
1872981b | 1520 | static ssize_t sel_read_initcon(struct file *file, char __user *buf, |
f0ee2e46 JC |
1521 | size_t count, loff_t *ppos) |
1522 | { | |
f0ee2e46 JC |
1523 | char *con; |
1524 | u32 sid, len; | |
1525 | ssize_t ret; | |
1526 | ||
496ad9aa | 1527 | sid = file_inode(file)->i_ino&SEL_INO_MASK; |
f0ee2e46 | 1528 | ret = security_sid_to_context(sid, &con, &len); |
b77a493b | 1529 | if (ret) |
f0ee2e46 JC |
1530 | return ret; |
1531 | ||
1532 | ret = simple_read_from_buffer(buf, count, ppos, con, len); | |
1533 | kfree(con); | |
1534 | return ret; | |
1535 | } | |
1536 | ||
1537 | static const struct file_operations sel_initcon_ops = { | |
1538 | .read = sel_read_initcon, | |
57a62c23 | 1539 | .llseek = generic_file_llseek, |
f0ee2e46 JC |
1540 | }; |
1541 | ||
1542 | static int sel_make_initcon_files(struct dentry *dir) | |
1543 | { | |
b77a493b | 1544 | int i; |
f0ee2e46 JC |
1545 | |
1546 | for (i = 1; i <= SECINITSID_NUM; i++) { | |
1547 | struct inode *inode; | |
1548 | struct dentry *dentry; | |
1549 | dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); | |
b77a493b EP |
1550 | if (!dentry) |
1551 | return -ENOMEM; | |
f0ee2e46 JC |
1552 | |
1553 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); | |
b77a493b EP |
1554 | if (!inode) |
1555 | return -ENOMEM; | |
1556 | ||
f0ee2e46 JC |
1557 | inode->i_fop = &sel_initcon_ops; |
1558 | inode->i_ino = i|SEL_INITCON_INO_OFFSET; | |
1559 | d_add(dentry, inode); | |
1560 | } | |
b77a493b EP |
1561 | |
1562 | return 0; | |
f0ee2e46 JC |
1563 | } |
1564 | ||
e47c8fc5 CP |
1565 | static inline unsigned long sel_class_to_ino(u16 class) |
1566 | { | |
1567 | return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; | |
1568 | } | |
1569 | ||
1570 | static inline u16 sel_ino_to_class(unsigned long ino) | |
1571 | { | |
92ae9e82 | 1572 | return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); |
e47c8fc5 CP |
1573 | } |
1574 | ||
1575 | static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) | |
1576 | { | |
1577 | return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; | |
1578 | } | |
1579 | ||
1580 | static inline u32 sel_ino_to_perm(unsigned long ino) | |
1581 | { | |
1582 | return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); | |
1583 | } | |
1584 | ||
1872981b | 1585 | static ssize_t sel_read_class(struct file *file, char __user *buf, |
e47c8fc5 CP |
1586 | size_t count, loff_t *ppos) |
1587 | { | |
496ad9aa | 1588 | unsigned long ino = file_inode(file)->i_ino; |
cc1dad71 AV |
1589 | char res[TMPBUFLEN]; |
1590 | ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); | |
1591 | return simple_read_from_buffer(buf, count, ppos, res, len); | |
e47c8fc5 CP |
1592 | } |
1593 | ||
1594 | static const struct file_operations sel_class_ops = { | |
1595 | .read = sel_read_class, | |
57a62c23 | 1596 | .llseek = generic_file_llseek, |
e47c8fc5 CP |
1597 | }; |
1598 | ||
1872981b | 1599 | static ssize_t sel_read_perm(struct file *file, char __user *buf, |
e47c8fc5 CP |
1600 | size_t count, loff_t *ppos) |
1601 | { | |
496ad9aa | 1602 | unsigned long ino = file_inode(file)->i_ino; |
cc1dad71 AV |
1603 | char res[TMPBUFLEN]; |
1604 | ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); | |
1605 | return simple_read_from_buffer(buf, count, ppos, res, len); | |
e47c8fc5 CP |
1606 | } |
1607 | ||
1608 | static const struct file_operations sel_perm_ops = { | |
1609 | .read = sel_read_perm, | |
57a62c23 | 1610 | .llseek = generic_file_llseek, |
e47c8fc5 CP |
1611 | }; |
1612 | ||
3bb56b25 PM |
1613 | static ssize_t sel_read_policycap(struct file *file, char __user *buf, |
1614 | size_t count, loff_t *ppos) | |
1615 | { | |
1616 | int value; | |
1617 | char tmpbuf[TMPBUFLEN]; | |
1618 | ssize_t length; | |
496ad9aa | 1619 | unsigned long i_ino = file_inode(file)->i_ino; |
3bb56b25 PM |
1620 | |
1621 | value = security_policycap_supported(i_ino & SEL_INO_MASK); | |
1622 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); | |
1623 | ||
1624 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
1625 | } | |
1626 | ||
1627 | static const struct file_operations sel_policycap_ops = { | |
1628 | .read = sel_read_policycap, | |
57a62c23 | 1629 | .llseek = generic_file_llseek, |
3bb56b25 PM |
1630 | }; |
1631 | ||
e47c8fc5 CP |
1632 | static int sel_make_perm_files(char *objclass, int classvalue, |
1633 | struct dentry *dir) | |
1634 | { | |
b77a493b | 1635 | int i, rc, nperms; |
e47c8fc5 CP |
1636 | char **perms; |
1637 | ||
1638 | rc = security_get_permissions(objclass, &perms, &nperms); | |
1639 | if (rc) | |
b77a493b | 1640 | return rc; |
e47c8fc5 CP |
1641 | |
1642 | for (i = 0; i < nperms; i++) { | |
1643 | struct inode *inode; | |
1644 | struct dentry *dentry; | |
1645 | ||
b77a493b | 1646 | rc = -ENOMEM; |
e47c8fc5 | 1647 | dentry = d_alloc_name(dir, perms[i]); |
b77a493b EP |
1648 | if (!dentry) |
1649 | goto out; | |
e47c8fc5 | 1650 | |
b77a493b | 1651 | rc = -ENOMEM; |
e47c8fc5 | 1652 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); |
b77a493b EP |
1653 | if (!inode) |
1654 | goto out; | |
1655 | ||
e47c8fc5 CP |
1656 | inode->i_fop = &sel_perm_ops; |
1657 | /* i+1 since perm values are 1-indexed */ | |
c1a7368a | 1658 | inode->i_ino = sel_perm_to_ino(classvalue, i + 1); |
e47c8fc5 CP |
1659 | d_add(dentry, inode); |
1660 | } | |
b77a493b EP |
1661 | rc = 0; |
1662 | out: | |
e47c8fc5 CP |
1663 | for (i = 0; i < nperms; i++) |
1664 | kfree(perms[i]); | |
1665 | kfree(perms); | |
e47c8fc5 CP |
1666 | return rc; |
1667 | } | |
1668 | ||
1669 | static int sel_make_class_dir_entries(char *classname, int index, | |
1670 | struct dentry *dir) | |
1671 | { | |
1672 | struct dentry *dentry = NULL; | |
1673 | struct inode *inode = NULL; | |
1674 | int rc; | |
1675 | ||
1676 | dentry = d_alloc_name(dir, "index"); | |
b77a493b EP |
1677 | if (!dentry) |
1678 | return -ENOMEM; | |
e47c8fc5 CP |
1679 | |
1680 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); | |
b77a493b EP |
1681 | if (!inode) |
1682 | return -ENOMEM; | |
e47c8fc5 CP |
1683 | |
1684 | inode->i_fop = &sel_class_ops; | |
1685 | inode->i_ino = sel_class_to_ino(index); | |
1686 | d_add(dentry, inode); | |
1687 | ||
a1c2aa1e AV |
1688 | dentry = sel_make_dir(dir, "perms", &last_class_ino); |
1689 | if (IS_ERR(dentry)) | |
1690 | return PTR_ERR(dentry); | |
e47c8fc5 CP |
1691 | |
1692 | rc = sel_make_perm_files(classname, index, dentry); | |
1693 | ||
e47c8fc5 CP |
1694 | return rc; |
1695 | } | |
1696 | ||
e47c8fc5 CP |
1697 | static int sel_make_classes(void) |
1698 | { | |
b77a493b | 1699 | int rc, nclasses, i; |
e47c8fc5 CP |
1700 | char **classes; |
1701 | ||
1702 | /* delete any existing entries */ | |
ad52184b | 1703 | sel_remove_entries(class_dir); |
e47c8fc5 CP |
1704 | |
1705 | rc = security_get_classes(&classes, &nclasses); | |
b77a493b EP |
1706 | if (rc) |
1707 | return rc; | |
e47c8fc5 CP |
1708 | |
1709 | /* +2 since classes are 1-indexed */ | |
c1a7368a | 1710 | last_class_ino = sel_class_to_ino(nclasses + 2); |
e47c8fc5 CP |
1711 | |
1712 | for (i = 0; i < nclasses; i++) { | |
1713 | struct dentry *class_name_dir; | |
1714 | ||
a1c2aa1e | 1715 | class_name_dir = sel_make_dir(class_dir, classes[i], |
e47c8fc5 | 1716 | &last_class_ino); |
a1c2aa1e AV |
1717 | if (IS_ERR(class_name_dir)) { |
1718 | rc = PTR_ERR(class_name_dir); | |
b77a493b | 1719 | goto out; |
a1c2aa1e | 1720 | } |
e47c8fc5 CP |
1721 | |
1722 | /* i+1 since class values are 1-indexed */ | |
c1a7368a | 1723 | rc = sel_make_class_dir_entries(classes[i], i + 1, |
e47c8fc5 CP |
1724 | class_name_dir); |
1725 | if (rc) | |
b77a493b | 1726 | goto out; |
e47c8fc5 | 1727 | } |
b77a493b EP |
1728 | rc = 0; |
1729 | out: | |
e47c8fc5 CP |
1730 | for (i = 0; i < nclasses; i++) |
1731 | kfree(classes[i]); | |
1732 | kfree(classes); | |
e47c8fc5 CP |
1733 | return rc; |
1734 | } | |
1735 | ||
3bb56b25 PM |
1736 | static int sel_make_policycap(void) |
1737 | { | |
1738 | unsigned int iter; | |
1739 | struct dentry *dentry = NULL; | |
1740 | struct inode *inode = NULL; | |
1741 | ||
1742 | sel_remove_entries(policycap_dir); | |
1743 | ||
1744 | for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { | |
4dc2fce3 | 1745 | if (iter < ARRAY_SIZE(selinux_policycap_names)) |
3bb56b25 | 1746 | dentry = d_alloc_name(policycap_dir, |
4dc2fce3 | 1747 | selinux_policycap_names[iter]); |
3bb56b25 PM |
1748 | else |
1749 | dentry = d_alloc_name(policycap_dir, "unknown"); | |
1750 | ||
1751 | if (dentry == NULL) | |
1752 | return -ENOMEM; | |
1753 | ||
1754 | inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO); | |
1755 | if (inode == NULL) | |
1756 | return -ENOMEM; | |
1757 | ||
1758 | inode->i_fop = &sel_policycap_ops; | |
1759 | inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; | |
1760 | d_add(dentry, inode); | |
1761 | } | |
1762 | ||
1763 | return 0; | |
1764 | } | |
1765 | ||
a1c2aa1e | 1766 | static struct dentry *sel_make_dir(struct dentry *dir, const char *name, |
0dd4ae51 | 1767 | unsigned long *ino) |
1da177e4 | 1768 | { |
a1c2aa1e | 1769 | struct dentry *dentry = d_alloc_name(dir, name); |
1da177e4 LT |
1770 | struct inode *inode; |
1771 | ||
a1c2aa1e AV |
1772 | if (!dentry) |
1773 | return ERR_PTR(-ENOMEM); | |
1774 | ||
1775 | inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); | |
1776 | if (!inode) { | |
1777 | dput(dentry); | |
1778 | return ERR_PTR(-ENOMEM); | |
1779 | } | |
b77a493b | 1780 | |
1da177e4 LT |
1781 | inode->i_op = &simple_dir_inode_operations; |
1782 | inode->i_fop = &simple_dir_operations; | |
0dd4ae51 | 1783 | inode->i_ino = ++(*ino); |
40e906f8 | 1784 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
d8c76e6f | 1785 | inc_nlink(inode); |
1da177e4 | 1786 | d_add(dentry, inode); |
edb20fb5 | 1787 | /* bump link count on parent directory, too */ |
ce0b16dd | 1788 | inc_nlink(d_inode(dir)); |
b77a493b | 1789 | |
a1c2aa1e | 1790 | return dentry; |
1da177e4 LT |
1791 | } |
1792 | ||
1872981b | 1793 | static int sel_fill_super(struct super_block *sb, void *data, int silent) |
1da177e4 LT |
1794 | { |
1795 | int ret; | |
1796 | struct dentry *dentry; | |
a1c2aa1e | 1797 | struct inode *inode; |
1da177e4 LT |
1798 | struct inode_security_struct *isec; |
1799 | ||
cda37124 | 1800 | static const struct tree_descr selinux_files[] = { |
1da177e4 LT |
1801 | [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, |
1802 | [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, | |
ce9982d0 | 1803 | [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, |
1da177e4 LT |
1804 | [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, |
1805 | [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, | |
1806 | [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, | |
1807 | [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, | |
1808 | [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, | |
1809 | [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, | |
1810 | [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, | |
1811 | [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, | |
1812 | [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, | |
1813 | [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, | |
3f12070e EP |
1814 | [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, |
1815 | [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, | |
11904167 | 1816 | [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, |
72e8c859 | 1817 | [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, |
f9df6458 AP |
1818 | [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, |
1819 | S_IWUGO}, | |
1da177e4 LT |
1820 | /* last one */ {""} |
1821 | }; | |
1822 | ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); | |
1823 | if (ret) | |
161ce45a | 1824 | goto err; |
1da177e4 | 1825 | |
a1c2aa1e AV |
1826 | bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino); |
1827 | if (IS_ERR(bool_dir)) { | |
1828 | ret = PTR_ERR(bool_dir); | |
1829 | bool_dir = NULL; | |
161ce45a | 1830 | goto err; |
a1c2aa1e | 1831 | } |
1da177e4 | 1832 | |
b77a493b | 1833 | ret = -ENOMEM; |
1da177e4 | 1834 | dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); |
b77a493b | 1835 | if (!dentry) |
161ce45a | 1836 | goto err; |
1da177e4 | 1837 | |
b77a493b | 1838 | ret = -ENOMEM; |
1da177e4 | 1839 | inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); |
b77a493b | 1840 | if (!inode) |
161ce45a | 1841 | goto err; |
b77a493b | 1842 | |
6174eafc | 1843 | inode->i_ino = ++sel_last_ino; |
7a90fc39 | 1844 | isec = (struct inode_security_struct *)selinux_inode(inode); |
1da177e4 LT |
1845 | isec->sid = SECINITSID_DEVNULL; |
1846 | isec->sclass = SECCLASS_CHR_FILE; | |
42059112 | 1847 | isec->initialized = LABEL_INITIALIZED; |
1da177e4 LT |
1848 | |
1849 | init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); | |
1850 | d_add(dentry, inode); | |
765927b2 | 1851 | selinux_null.dentry = dentry; |
1da177e4 | 1852 | |
a1c2aa1e AV |
1853 | dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino); |
1854 | if (IS_ERR(dentry)) { | |
1855 | ret = PTR_ERR(dentry); | |
161ce45a | 1856 | goto err; |
a1c2aa1e | 1857 | } |
1da177e4 LT |
1858 | |
1859 | ret = sel_make_avc_files(dentry); | |
1860 | if (ret) | |
161ce45a | 1861 | goto err; |
f0ee2e46 | 1862 | |
a1c2aa1e AV |
1863 | dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino); |
1864 | if (IS_ERR(dentry)) { | |
1865 | ret = PTR_ERR(dentry); | |
f0ee2e46 | 1866 | goto err; |
a1c2aa1e | 1867 | } |
f0ee2e46 JC |
1868 | |
1869 | ret = sel_make_initcon_files(dentry); | |
1870 | if (ret) | |
1871 | goto err; | |
1872 | ||
a1c2aa1e AV |
1873 | class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino); |
1874 | if (IS_ERR(class_dir)) { | |
1875 | ret = PTR_ERR(class_dir); | |
1876 | class_dir = NULL; | |
3bb56b25 | 1877 | goto err; |
a1c2aa1e | 1878 | } |
3bb56b25 | 1879 | |
a1c2aa1e AV |
1880 | policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino); |
1881 | if (IS_ERR(policycap_dir)) { | |
1882 | ret = PTR_ERR(policycap_dir); | |
1883 | policycap_dir = NULL; | |
3bb56b25 | 1884 | goto err; |
a1c2aa1e | 1885 | } |
b77a493b | 1886 | return 0; |
161ce45a | 1887 | err: |
744ba35e EP |
1888 | printk(KERN_ERR "SELinux: %s: failed while creating inodes\n", |
1889 | __func__); | |
b77a493b | 1890 | return ret; |
1da177e4 LT |
1891 | } |
1892 | ||
fc14f2fe AV |
1893 | static struct dentry *sel_mount(struct file_system_type *fs_type, |
1894 | int flags, const char *dev_name, void *data) | |
1da177e4 | 1895 | { |
fc14f2fe | 1896 | return mount_single(fs_type, flags, data, sel_fill_super); |
1da177e4 LT |
1897 | } |
1898 | ||
1899 | static struct file_system_type sel_fs_type = { | |
1900 | .name = "selinuxfs", | |
fc14f2fe | 1901 | .mount = sel_mount, |
1da177e4 LT |
1902 | .kill_sb = kill_litter_super, |
1903 | }; | |
1904 | ||
1905 | struct vfsmount *selinuxfs_mount; | |
1906 | ||
1907 | static int __init init_sel_fs(void) | |
1908 | { | |
1909 | int err; | |
1910 | ||
1911 | if (!selinux_enabled) | |
1912 | return 0; | |
7a627e3b | 1913 | |
f9bb4882 EB |
1914 | err = sysfs_create_mount_point(fs_kobj, "selinux"); |
1915 | if (err) | |
1916 | return err; | |
7a627e3b | 1917 | |
1da177e4 | 1918 | err = register_filesystem(&sel_fs_type); |
7a627e3b | 1919 | if (err) { |
f9bb4882 | 1920 | sysfs_remove_mount_point(fs_kobj, "selinux"); |
b77a493b | 1921 | return err; |
7a627e3b | 1922 | } |
b77a493b | 1923 | |
765927b2 | 1924 | selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); |
b77a493b EP |
1925 | if (IS_ERR(selinuxfs_mount)) { |
1926 | printk(KERN_ERR "selinuxfs: could not mount!\n"); | |
1927 | err = PTR_ERR(selinuxfs_mount); | |
1928 | selinuxfs_mount = NULL; | |
1da177e4 | 1929 | } |
b77a493b | 1930 | |
1da177e4 LT |
1931 | return err; |
1932 | } | |
1933 | ||
1934 | __initcall(init_sel_fs); | |
1935 | ||
1936 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE | |
1937 | void exit_sel_fs(void) | |
1938 | { | |
f9bb4882 | 1939 | sysfs_remove_mount_point(fs_kobj, "selinux"); |
423e0ab0 | 1940 | kern_unmount(selinuxfs_mount); |
1da177e4 LT |
1941 | unregister_filesystem(&sel_fs_type); |
1942 | } | |
1943 | #endif |