selinuxfs.c 39 KB

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