PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/security/selinux/selinuxfs.c

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