selinuxfs.c 39 KB

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