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