selinuxfs.c 42 KB

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