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_ino = get_next_ino();
  809. ret->i_mode = mode;
  810. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  811. }
  812. return ret;
  813. }
  814. static ssize_t sel_read_bool(struct file *filep, char __user *buf,
  815. size_t count, loff_t *ppos)
  816. {
  817. char *page = NULL;
  818. ssize_t length;
  819. ssize_t ret;
  820. int cur_enforcing;
  821. struct inode *inode = filep->f_path.dentry->d_inode;
  822. unsigned index = inode->i_ino & SEL_INO_MASK;
  823. const char *name = filep->f_path.dentry->d_name.name;
  824. mutex_lock(&sel_mutex);
  825. if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
  826. ret = -EINVAL;
  827. goto out;
  828. }
  829. page = (char *)get_zeroed_page(GFP_KERNEL);
  830. if (!page) {
  831. ret = -ENOMEM;
  832. goto out;
  833. }
  834. cur_enforcing = security_get_bool_value(index);
  835. if (cur_enforcing < 0) {
  836. ret = cur_enforcing;
  837. goto out;
  838. }
  839. length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
  840. bool_pending_values[index]);
  841. ret = simple_read_from_buffer(buf, count, ppos, page, length);
  842. out:
  843. mutex_unlock(&sel_mutex);
  844. if (page)
  845. free_page((unsigned long)page);
  846. return ret;
  847. }
  848. static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
  849. size_t count, loff_t *ppos)
  850. {
  851. char *page = NULL;
  852. ssize_t length;
  853. int new_value;
  854. struct inode *inode = filep->f_path.dentry->d_inode;
  855. unsigned index = inode->i_ino & SEL_INO_MASK;
  856. const char *name = filep->f_path.dentry->d_name.name;
  857. mutex_lock(&sel_mutex);
  858. length = task_has_security(current, SECURITY__SETBOOL);
  859. if (length)
  860. goto out;
  861. if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
  862. length = -EINVAL;
  863. goto out;
  864. }
  865. if (count >= PAGE_SIZE) {
  866. length = -ENOMEM;
  867. goto out;
  868. }
  869. if (*ppos != 0) {
  870. /* No partial writes. */
  871. length = -EINVAL;
  872. goto out;
  873. }
  874. page = (char *)get_zeroed_page(GFP_KERNEL);
  875. if (!page) {
  876. length = -ENOMEM;
  877. goto out;
  878. }
  879. length = -EFAULT;
  880. if (copy_from_user(page, buf, count))
  881. goto out;
  882. length = -EINVAL;
  883. if (sscanf(page, "%d", &new_value) != 1)
  884. goto out;
  885. if (new_value)
  886. new_value = 1;
  887. bool_pending_values[index] = new_value;
  888. length = count;
  889. out:
  890. mutex_unlock(&sel_mutex);
  891. if (page)
  892. free_page((unsigned long) page);
  893. return length;
  894. }
  895. static const struct file_operations sel_bool_ops = {
  896. .read = sel_read_bool,
  897. .write = sel_write_bool,
  898. .llseek = generic_file_llseek,
  899. };
  900. static ssize_t sel_commit_bools_write(struct file *filep,
  901. const char __user *buf,
  902. size_t count, loff_t *ppos)
  903. {
  904. char *page = NULL;
  905. ssize_t length;
  906. int new_value;
  907. mutex_lock(&sel_mutex);
  908. length = task_has_security(current, SECURITY__SETBOOL);
  909. if (length)
  910. goto out;
  911. if (count >= PAGE_SIZE) {
  912. length = -ENOMEM;
  913. goto out;
  914. }
  915. if (*ppos != 0) {
  916. /* No partial writes. */
  917. goto out;
  918. }
  919. page = (char *)get_zeroed_page(GFP_KERNEL);
  920. if (!page) {
  921. length = -ENOMEM;
  922. goto out;
  923. }
  924. length = -EFAULT;
  925. if (copy_from_user(page, buf, count))
  926. goto out;
  927. length = -EINVAL;
  928. if (sscanf(page, "%d", &new_value) != 1)
  929. goto out;
  930. if (new_value && bool_pending_values)
  931. security_set_bools(bool_num, bool_pending_values);
  932. length = count;
  933. out:
  934. mutex_unlock(&sel_mutex);
  935. if (page)
  936. free_page((unsigned long) page);
  937. return length;
  938. }
  939. static const struct file_operations sel_commit_bools_ops = {
  940. .write = sel_commit_bools_write,
  941. .llseek = generic_file_llseek,
  942. };
  943. static void sel_remove_entries(struct dentry *de)
  944. {
  945. struct list_head *node;
  946. spin_lock(&dcache_lock);
  947. node = de->d_subdirs.next;
  948. while (node != &de->d_subdirs) {
  949. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  950. list_del_init(node);
  951. if (d->d_inode) {
  952. d = dget_locked(d);
  953. spin_unlock(&dcache_lock);
  954. d_delete(d);
  955. simple_unlink(de->d_inode, d);
  956. dput(d);
  957. spin_lock(&dcache_lock);
  958. }
  959. node = de->d_subdirs.next;
  960. }
  961. spin_unlock(&dcache_lock);
  962. }
  963. #define BOOL_DIR_NAME "booleans"
  964. static int sel_make_bools(void)
  965. {
  966. int i, ret = 0;
  967. ssize_t len;
  968. struct dentry *dentry = NULL;
  969. struct dentry *dir = bool_dir;
  970. struct inode *inode = NULL;
  971. struct inode_security_struct *isec;
  972. char **names = NULL, *page;
  973. int num;
  974. int *values = NULL;
  975. u32 sid;
  976. /* remove any existing files */
  977. for (i = 0; i < bool_num; i++)
  978. kfree(bool_pending_names[i]);
  979. kfree(bool_pending_names);
  980. kfree(bool_pending_values);
  981. bool_pending_names = NULL;
  982. bool_pending_values = NULL;
  983. sel_remove_entries(dir);
  984. page = (char *)get_zeroed_page(GFP_KERNEL);
  985. if (!page)
  986. return -ENOMEM;
  987. ret = security_get_bools(&num, &names, &values);
  988. if (ret != 0)
  989. goto out;
  990. for (i = 0; i < num; i++) {
  991. dentry = d_alloc_name(dir, names[i]);
  992. if (!dentry) {
  993. ret = -ENOMEM;
  994. goto err;
  995. }
  996. inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
  997. if (!inode) {
  998. ret = -ENOMEM;
  999. goto err;
  1000. }
  1001. len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
  1002. if (len < 0) {
  1003. ret = -EINVAL;
  1004. goto err;
  1005. } else if (len >= PAGE_SIZE) {
  1006. ret = -ENAMETOOLONG;
  1007. goto err;
  1008. }
  1009. isec = (struct inode_security_struct *)inode->i_security;
  1010. ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
  1011. if (ret)
  1012. goto err;
  1013. isec->sid = sid;
  1014. isec->initialized = 1;
  1015. inode->i_fop = &sel_bool_ops;
  1016. inode->i_ino = i|SEL_BOOL_INO_OFFSET;
  1017. d_add(dentry, inode);
  1018. }
  1019. bool_num = num;
  1020. bool_pending_names = names;
  1021. bool_pending_values = values;
  1022. out:
  1023. free_page((unsigned long)page);
  1024. return ret;
  1025. err:
  1026. if (names) {
  1027. for (i = 0; i < num; i++)
  1028. kfree(names[i]);
  1029. kfree(names);
  1030. }
  1031. kfree(values);
  1032. sel_remove_entries(dir);
  1033. ret = -ENOMEM;
  1034. goto out;
  1035. }
  1036. #define NULL_FILE_NAME "null"
  1037. struct dentry *selinux_null;
  1038. static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
  1039. size_t count, loff_t *ppos)
  1040. {
  1041. char tmpbuf[TMPBUFLEN];
  1042. ssize_t length;
  1043. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
  1044. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1045. }
  1046. static ssize_t sel_write_avc_cache_threshold(struct file *file,
  1047. const char __user *buf,
  1048. size_t count, loff_t *ppos)
  1049. {
  1050. char *page;
  1051. ssize_t ret;
  1052. int new_value;
  1053. if (count >= PAGE_SIZE) {
  1054. ret = -ENOMEM;
  1055. goto out;
  1056. }
  1057. if (*ppos != 0) {
  1058. /* No partial writes. */
  1059. ret = -EINVAL;
  1060. goto out;
  1061. }
  1062. page = (char *)get_zeroed_page(GFP_KERNEL);
  1063. if (!page) {
  1064. ret = -ENOMEM;
  1065. goto out;
  1066. }
  1067. if (copy_from_user(page, buf, count)) {
  1068. ret = -EFAULT;
  1069. goto out_free;
  1070. }
  1071. if (sscanf(page, "%u", &new_value) != 1) {
  1072. ret = -EINVAL;
  1073. goto out;
  1074. }
  1075. if (new_value != avc_cache_threshold) {
  1076. ret = task_has_security(current, SECURITY__SETSECPARAM);
  1077. if (ret)
  1078. goto out_free;
  1079. avc_cache_threshold = new_value;
  1080. }
  1081. ret = count;
  1082. out_free:
  1083. free_page((unsigned long)page);
  1084. out:
  1085. return ret;
  1086. }
  1087. static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
  1088. size_t count, loff_t *ppos)
  1089. {
  1090. char *page;
  1091. ssize_t ret = 0;
  1092. page = (char *)__get_free_page(GFP_KERNEL);
  1093. if (!page) {
  1094. ret = -ENOMEM;
  1095. goto out;
  1096. }
  1097. ret = avc_get_hash_stats(page);
  1098. if (ret >= 0)
  1099. ret = simple_read_from_buffer(buf, count, ppos, page, ret);
  1100. free_page((unsigned long)page);
  1101. out:
  1102. return ret;
  1103. }
  1104. static const struct file_operations sel_avc_cache_threshold_ops = {
  1105. .read = sel_read_avc_cache_threshold,
  1106. .write = sel_write_avc_cache_threshold,
  1107. .llseek = generic_file_llseek,
  1108. };
  1109. static const struct file_operations sel_avc_hash_stats_ops = {
  1110. .read = sel_read_avc_hash_stats,
  1111. .llseek = generic_file_llseek,
  1112. };
  1113. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1114. static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
  1115. {
  1116. int cpu;
  1117. for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
  1118. if (!cpu_possible(cpu))
  1119. continue;
  1120. *idx = cpu + 1;
  1121. return &per_cpu(avc_cache_stats, cpu);
  1122. }
  1123. return NULL;
  1124. }
  1125. static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
  1126. {
  1127. loff_t n = *pos - 1;
  1128. if (*pos == 0)
  1129. return SEQ_START_TOKEN;
  1130. return sel_avc_get_stat_idx(&n);
  1131. }
  1132. static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1133. {
  1134. return sel_avc_get_stat_idx(pos);
  1135. }
  1136. static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
  1137. {
  1138. struct avc_cache_stats *st = v;
  1139. if (v == SEQ_START_TOKEN)
  1140. seq_printf(seq, "lookups hits misses allocations reclaims "
  1141. "frees\n");
  1142. else
  1143. seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
  1144. st->hits, st->misses, st->allocations,
  1145. st->reclaims, st->frees);
  1146. return 0;
  1147. }
  1148. static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
  1149. { }
  1150. static const struct seq_operations sel_avc_cache_stats_seq_ops = {
  1151. .start = sel_avc_stats_seq_start,
  1152. .next = sel_avc_stats_seq_next,
  1153. .show = sel_avc_stats_seq_show,
  1154. .stop = sel_avc_stats_seq_stop,
  1155. };
  1156. static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
  1157. {
  1158. return seq_open(file, &sel_avc_cache_stats_seq_ops);
  1159. }
  1160. static const struct file_operations sel_avc_cache_stats_ops = {
  1161. .open = sel_open_avc_cache_stats,
  1162. .read = seq_read,
  1163. .llseek = seq_lseek,
  1164. .release = seq_release,
  1165. };
  1166. #endif
  1167. static int sel_make_avc_files(struct dentry *dir)
  1168. {
  1169. int i, ret = 0;
  1170. static struct tree_descr files[] = {
  1171. { "cache_threshold",
  1172. &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
  1173. { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
  1174. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1175. { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
  1176. #endif
  1177. };
  1178. for (i = 0; i < ARRAY_SIZE(files); i++) {
  1179. struct inode *inode;
  1180. struct dentry *dentry;
  1181. dentry = d_alloc_name(dir, files[i].name);
  1182. if (!dentry) {
  1183. ret = -ENOMEM;
  1184. goto out;
  1185. }
  1186. inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
  1187. if (!inode) {
  1188. ret = -ENOMEM;
  1189. goto out;
  1190. }
  1191. inode->i_fop = files[i].ops;
  1192. inode->i_ino = ++sel_last_ino;
  1193. d_add(dentry, inode);
  1194. }
  1195. out:
  1196. return ret;
  1197. }
  1198. static ssize_t sel_read_initcon(struct file *file, char __user *buf,
  1199. size_t count, loff_t *ppos)
  1200. {
  1201. struct inode *inode;
  1202. char *con;
  1203. u32 sid, len;
  1204. ssize_t ret;
  1205. inode = file->f_path.dentry->d_inode;
  1206. sid = inode->i_ino&SEL_INO_MASK;
  1207. ret = security_sid_to_context(sid, &con, &len);
  1208. if (ret < 0)
  1209. return ret;
  1210. ret = simple_read_from_buffer(buf, count, ppos, con, len);
  1211. kfree(con);
  1212. return ret;
  1213. }
  1214. static const struct file_operations sel_initcon_ops = {
  1215. .read = sel_read_initcon,
  1216. .llseek = generic_file_llseek,
  1217. };
  1218. static int sel_make_initcon_files(struct dentry *dir)
  1219. {
  1220. int i, ret = 0;
  1221. for (i = 1; i <= SECINITSID_NUM; i++) {
  1222. struct inode *inode;
  1223. struct dentry *dentry;
  1224. dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
  1225. if (!dentry) {
  1226. ret = -ENOMEM;
  1227. goto out;
  1228. }
  1229. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1230. if (!inode) {
  1231. ret = -ENOMEM;
  1232. goto out;
  1233. }
  1234. inode->i_fop = &sel_initcon_ops;
  1235. inode->i_ino = i|SEL_INITCON_INO_OFFSET;
  1236. d_add(dentry, inode);
  1237. }
  1238. out:
  1239. return ret;
  1240. }
  1241. static inline unsigned int sel_div(unsigned long a, unsigned long b)
  1242. {
  1243. return a / b - (a % b < 0);
  1244. }
  1245. static inline unsigned long sel_class_to_ino(u16 class)
  1246. {
  1247. return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
  1248. }
  1249. static inline u16 sel_ino_to_class(unsigned long ino)
  1250. {
  1251. return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
  1252. }
  1253. static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
  1254. {
  1255. return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
  1256. }
  1257. static inline u32 sel_ino_to_perm(unsigned long ino)
  1258. {
  1259. return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
  1260. }
  1261. static ssize_t sel_read_class(struct file *file, char __user *buf,
  1262. size_t count, loff_t *ppos)
  1263. {
  1264. ssize_t rc, len;
  1265. char *page;
  1266. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1267. page = (char *)__get_free_page(GFP_KERNEL);
  1268. if (!page) {
  1269. rc = -ENOMEM;
  1270. goto out;
  1271. }
  1272. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
  1273. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1274. free_page((unsigned long)page);
  1275. out:
  1276. return rc;
  1277. }
  1278. static const struct file_operations sel_class_ops = {
  1279. .read = sel_read_class,
  1280. .llseek = generic_file_llseek,
  1281. };
  1282. static ssize_t sel_read_perm(struct file *file, char __user *buf,
  1283. size_t count, loff_t *ppos)
  1284. {
  1285. ssize_t rc, len;
  1286. char *page;
  1287. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1288. page = (char *)__get_free_page(GFP_KERNEL);
  1289. if (!page) {
  1290. rc = -ENOMEM;
  1291. goto out;
  1292. }
  1293. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
  1294. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1295. free_page((unsigned long)page);
  1296. out:
  1297. return rc;
  1298. }
  1299. static const struct file_operations sel_perm_ops = {
  1300. .read = sel_read_perm,
  1301. .llseek = generic_file_llseek,
  1302. };
  1303. static ssize_t sel_read_policycap(struct file *file, char __user *buf,
  1304. size_t count, loff_t *ppos)
  1305. {
  1306. int value;
  1307. char tmpbuf[TMPBUFLEN];
  1308. ssize_t length;
  1309. unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
  1310. value = security_policycap_supported(i_ino & SEL_INO_MASK);
  1311. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
  1312. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1313. }
  1314. static const struct file_operations sel_policycap_ops = {
  1315. .read = sel_read_policycap,
  1316. .llseek = generic_file_llseek,
  1317. };
  1318. static int sel_make_perm_files(char *objclass, int classvalue,
  1319. struct dentry *dir)
  1320. {
  1321. int i, rc = 0, nperms;
  1322. char **perms;
  1323. rc = security_get_permissions(objclass, &perms, &nperms);
  1324. if (rc)
  1325. goto out;
  1326. for (i = 0; i < nperms; i++) {
  1327. struct inode *inode;
  1328. struct dentry *dentry;
  1329. dentry = d_alloc_name(dir, perms[i]);
  1330. if (!dentry) {
  1331. rc = -ENOMEM;
  1332. goto out1;
  1333. }
  1334. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1335. if (!inode) {
  1336. rc = -ENOMEM;
  1337. goto out1;
  1338. }
  1339. inode->i_fop = &sel_perm_ops;
  1340. /* i+1 since perm values are 1-indexed */
  1341. inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
  1342. d_add(dentry, inode);
  1343. }
  1344. out1:
  1345. for (i = 0; i < nperms; i++)
  1346. kfree(perms[i]);
  1347. kfree(perms);
  1348. out:
  1349. return rc;
  1350. }
  1351. static int sel_make_class_dir_entries(char *classname, int index,
  1352. struct dentry *dir)
  1353. {
  1354. struct dentry *dentry = NULL;
  1355. struct inode *inode = NULL;
  1356. int rc;
  1357. dentry = d_alloc_name(dir, "index");
  1358. if (!dentry) {
  1359. rc = -ENOMEM;
  1360. goto out;
  1361. }
  1362. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1363. if (!inode) {
  1364. rc = -ENOMEM;
  1365. goto out;
  1366. }
  1367. inode->i_fop = &sel_class_ops;
  1368. inode->i_ino = sel_class_to_ino(index);
  1369. d_add(dentry, inode);
  1370. dentry = d_alloc_name(dir, "perms");
  1371. if (!dentry) {
  1372. rc = -ENOMEM;
  1373. goto out;
  1374. }
  1375. rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
  1376. if (rc)
  1377. goto out;
  1378. rc = sel_make_perm_files(classname, index, dentry);
  1379. out:
  1380. return rc;
  1381. }
  1382. static void sel_remove_classes(void)
  1383. {
  1384. struct list_head *class_node;
  1385. list_for_each(class_node, &class_dir->d_subdirs) {
  1386. struct dentry *class_subdir = list_entry(class_node,
  1387. struct dentry, d_u.d_child);
  1388. struct list_head *class_subdir_node;
  1389. list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
  1390. struct dentry *d = list_entry(class_subdir_node,
  1391. struct dentry, d_u.d_child);
  1392. if (d->d_inode)
  1393. if (d->d_inode->i_mode & S_IFDIR)
  1394. sel_remove_entries(d);
  1395. }
  1396. sel_remove_entries(class_subdir);
  1397. }
  1398. sel_remove_entries(class_dir);
  1399. }
  1400. static int sel_make_classes(void)
  1401. {
  1402. int rc = 0, nclasses, i;
  1403. char **classes;
  1404. /* delete any existing entries */
  1405. sel_remove_classes();
  1406. rc = security_get_classes(&classes, &nclasses);
  1407. if (rc < 0)
  1408. goto out;
  1409. /* +2 since classes are 1-indexed */
  1410. last_class_ino = sel_class_to_ino(nclasses + 2);
  1411. for (i = 0; i < nclasses; i++) {
  1412. struct dentry *class_name_dir;
  1413. class_name_dir = d_alloc_name(class_dir, classes[i]);
  1414. if (!class_name_dir) {
  1415. rc = -ENOMEM;
  1416. goto out1;
  1417. }
  1418. rc = sel_make_dir(class_dir->d_inode, class_name_dir,
  1419. &last_class_ino);
  1420. if (rc)
  1421. goto out1;
  1422. /* i+1 since class values are 1-indexed */
  1423. rc = sel_make_class_dir_entries(classes[i], i + 1,
  1424. class_name_dir);
  1425. if (rc)
  1426. goto out1;
  1427. }
  1428. out1:
  1429. for (i = 0; i < nclasses; i++)
  1430. kfree(classes[i]);
  1431. kfree(classes);
  1432. out:
  1433. return rc;
  1434. }
  1435. static int sel_make_policycap(void)
  1436. {
  1437. unsigned int iter;
  1438. struct dentry *dentry = NULL;
  1439. struct inode *inode = NULL;
  1440. sel_remove_entries(policycap_dir);
  1441. for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
  1442. if (iter < ARRAY_SIZE(policycap_names))
  1443. dentry = d_alloc_name(policycap_dir,
  1444. policycap_names[iter]);
  1445. else
  1446. dentry = d_alloc_name(policycap_dir, "unknown");
  1447. if (dentry == NULL)
  1448. return -ENOMEM;
  1449. inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
  1450. if (inode == NULL)
  1451. return -ENOMEM;
  1452. inode->i_fop = &sel_policycap_ops;
  1453. inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
  1454. d_add(dentry, inode);
  1455. }
  1456. return 0;
  1457. }
  1458. static int sel_make_dir(struct inode *dir, struct dentry *dentry,
  1459. unsigned long *ino)
  1460. {
  1461. int ret = 0;
  1462. struct inode *inode;
  1463. inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1464. if (!inode) {
  1465. ret = -ENOMEM;
  1466. goto out;
  1467. }
  1468. inode->i_op = &simple_dir_inode_operations;
  1469. inode->i_fop = &simple_dir_operations;
  1470. inode->i_ino = ++(*ino);
  1471. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  1472. inc_nlink(inode);
  1473. d_add(dentry, inode);
  1474. /* bump link count on parent directory, too */
  1475. inc_nlink(dir);
  1476. out:
  1477. return ret;
  1478. }
  1479. static int sel_fill_super(struct super_block *sb, void *data, int silent)
  1480. {
  1481. int ret;
  1482. struct dentry *dentry;
  1483. struct inode *inode, *root_inode;
  1484. struct inode_security_struct *isec;
  1485. static struct tree_descr selinux_files[] = {
  1486. [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
  1487. [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
  1488. [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
  1489. [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
  1490. [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
  1491. [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
  1492. [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
  1493. [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
  1494. [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
  1495. [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
  1496. [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
  1497. [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
  1498. [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
  1499. [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
  1500. [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
  1501. [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
  1502. [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUSR},
  1503. /* last one */ {""}
  1504. };
  1505. ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
  1506. if (ret)
  1507. goto err;
  1508. root_inode = sb->s_root->d_inode;
  1509. dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
  1510. if (!dentry) {
  1511. ret = -ENOMEM;
  1512. goto err;
  1513. }
  1514. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1515. if (ret)
  1516. goto err;
  1517. bool_dir = dentry;
  1518. dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
  1519. if (!dentry) {
  1520. ret = -ENOMEM;
  1521. goto err;
  1522. }
  1523. inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
  1524. if (!inode) {
  1525. ret = -ENOMEM;
  1526. goto err;
  1527. }
  1528. inode->i_ino = ++sel_last_ino;
  1529. isec = (struct inode_security_struct *)inode->i_security;
  1530. isec->sid = SECINITSID_DEVNULL;
  1531. isec->sclass = SECCLASS_CHR_FILE;
  1532. isec->initialized = 1;
  1533. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
  1534. d_add(dentry, inode);
  1535. selinux_null = dentry;
  1536. dentry = d_alloc_name(sb->s_root, "avc");
  1537. if (!dentry) {
  1538. ret = -ENOMEM;
  1539. goto err;
  1540. }
  1541. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1542. if (ret)
  1543. goto err;
  1544. ret = sel_make_avc_files(dentry);
  1545. if (ret)
  1546. goto err;
  1547. dentry = d_alloc_name(sb->s_root, "initial_contexts");
  1548. if (!dentry) {
  1549. ret = -ENOMEM;
  1550. goto err;
  1551. }
  1552. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1553. if (ret)
  1554. goto err;
  1555. ret = sel_make_initcon_files(dentry);
  1556. if (ret)
  1557. goto err;
  1558. dentry = d_alloc_name(sb->s_root, "class");
  1559. if (!dentry) {
  1560. ret = -ENOMEM;
  1561. goto err;
  1562. }
  1563. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1564. if (ret)
  1565. goto err;
  1566. class_dir = dentry;
  1567. dentry = d_alloc_name(sb->s_root, "policy_capabilities");
  1568. if (!dentry) {
  1569. ret = -ENOMEM;
  1570. goto err;
  1571. }
  1572. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1573. if (ret)
  1574. goto err;
  1575. policycap_dir = dentry;
  1576. out:
  1577. return ret;
  1578. err:
  1579. printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
  1580. __func__);
  1581. goto out;
  1582. }
  1583. static struct dentry *sel_mount(struct file_system_type *fs_type,
  1584. int flags, const char *dev_name, void *data)
  1585. {
  1586. return mount_single(fs_type, flags, data, sel_fill_super);
  1587. }
  1588. static struct file_system_type sel_fs_type = {
  1589. .name = "selinuxfs",
  1590. .mount = sel_mount,
  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