selinuxfs.c 42 KB

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