selinuxfs.c 38 KB

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