selinuxfs.c 38 KB

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