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