selinuxfs.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/fs.h>
  17. #include <linux/mutex.h>
  18. #include <linux/init.h>
  19. #include <linux/string.h>
  20. #include <linux/security.h>
  21. #include <linux/major.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/percpu.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. static int __init checkreqprot_setup(char *str)
  36. {
  37. selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
  38. return 1;
  39. }
  40. __setup("checkreqprot=", checkreqprot_setup);
  41. static DEFINE_MUTEX(sel_mutex);
  42. /* global data for booleans */
  43. static struct dentry *bool_dir = NULL;
  44. static int bool_num = 0;
  45. static int *bool_pending_values = NULL;
  46. extern void selnl_notify_setenforce(int val);
  47. /* Check whether a task is allowed to use a security operation. */
  48. static int task_has_security(struct task_struct *tsk,
  49. u32 perms)
  50. {
  51. struct task_security_struct *tsec;
  52. tsec = tsk->security;
  53. if (!tsec)
  54. return -EACCES;
  55. return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
  56. SECCLASS_SECURITY, perms, NULL);
  57. }
  58. enum sel_inos {
  59. SEL_ROOT_INO = 2,
  60. SEL_LOAD, /* load policy */
  61. SEL_ENFORCE, /* get or set enforcing status */
  62. SEL_CONTEXT, /* validate context */
  63. SEL_ACCESS, /* compute access decision */
  64. SEL_CREATE, /* compute create labeling decision */
  65. SEL_RELABEL, /* compute relabeling decision */
  66. SEL_USER, /* compute reachable user contexts */
  67. SEL_POLICYVERS, /* return policy version for this kernel */
  68. SEL_COMMIT_BOOLS, /* commit new boolean values */
  69. SEL_MLS, /* return if MLS policy is enabled */
  70. SEL_DISABLE, /* disable SELinux until next reboot */
  71. SEL_AVC, /* AVC management directory */
  72. SEL_MEMBER, /* compute polyinstantiation membership decision */
  73. SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
  74. };
  75. #define TMPBUFLEN 12
  76. static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
  77. size_t count, loff_t *ppos)
  78. {
  79. char tmpbuf[TMPBUFLEN];
  80. ssize_t length;
  81. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
  82. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  83. }
  84. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  85. static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
  86. size_t count, loff_t *ppos)
  87. {
  88. char *page;
  89. ssize_t length;
  90. int new_value;
  91. if (count >= PAGE_SIZE)
  92. return -ENOMEM;
  93. if (*ppos != 0) {
  94. /* No partial writes. */
  95. return -EINVAL;
  96. }
  97. page = (char*)get_zeroed_page(GFP_KERNEL);
  98. if (!page)
  99. return -ENOMEM;
  100. length = -EFAULT;
  101. if (copy_from_user(page, buf, count))
  102. goto out;
  103. length = -EINVAL;
  104. if (sscanf(page, "%d", &new_value) != 1)
  105. goto out;
  106. if (new_value != selinux_enforcing) {
  107. length = task_has_security(current, SECURITY__SETENFORCE);
  108. if (length)
  109. goto out;
  110. selinux_enforcing = new_value;
  111. if (selinux_enforcing)
  112. avc_ss_reset(0);
  113. selnl_notify_setenforce(selinux_enforcing);
  114. }
  115. length = count;
  116. out:
  117. free_page((unsigned long) page);
  118. return length;
  119. }
  120. #else
  121. #define sel_write_enforce NULL
  122. #endif
  123. static struct file_operations sel_enforce_ops = {
  124. .read = sel_read_enforce,
  125. .write = sel_write_enforce,
  126. };
  127. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  128. static ssize_t sel_write_disable(struct file * file, const char __user * buf,
  129. size_t count, loff_t *ppos)
  130. {
  131. char *page;
  132. ssize_t length;
  133. int new_value;
  134. extern int selinux_disable(void);
  135. if (count >= PAGE_SIZE)
  136. return -ENOMEM;
  137. if (*ppos != 0) {
  138. /* No partial writes. */
  139. return -EINVAL;
  140. }
  141. page = (char*)get_zeroed_page(GFP_KERNEL);
  142. if (!page)
  143. return -ENOMEM;
  144. length = -EFAULT;
  145. if (copy_from_user(page, buf, count))
  146. goto out;
  147. length = -EINVAL;
  148. if (sscanf(page, "%d", &new_value) != 1)
  149. goto out;
  150. if (new_value) {
  151. length = selinux_disable();
  152. if (length < 0)
  153. goto out;
  154. }
  155. length = count;
  156. out:
  157. free_page((unsigned long) page);
  158. return length;
  159. }
  160. #else
  161. #define sel_write_disable NULL
  162. #endif
  163. static struct file_operations sel_disable_ops = {
  164. .write = sel_write_disable,
  165. };
  166. static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
  167. size_t count, loff_t *ppos)
  168. {
  169. char tmpbuf[TMPBUFLEN];
  170. ssize_t length;
  171. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
  172. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  173. }
  174. static struct file_operations sel_policyvers_ops = {
  175. .read = sel_read_policyvers,
  176. };
  177. /* declaration for sel_write_load */
  178. static int sel_make_bools(void);
  179. static ssize_t sel_read_mls(struct file *filp, char __user *buf,
  180. size_t count, loff_t *ppos)
  181. {
  182. char tmpbuf[TMPBUFLEN];
  183. ssize_t length;
  184. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
  185. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  186. }
  187. static struct file_operations sel_mls_ops = {
  188. .read = sel_read_mls,
  189. };
  190. static ssize_t sel_write_load(struct file * file, const char __user * buf,
  191. size_t count, loff_t *ppos)
  192. {
  193. int ret;
  194. ssize_t length;
  195. void *data = NULL;
  196. mutex_lock(&sel_mutex);
  197. length = task_has_security(current, SECURITY__LOAD_POLICY);
  198. if (length)
  199. goto out;
  200. if (*ppos != 0) {
  201. /* No partial writes. */
  202. length = -EINVAL;
  203. goto out;
  204. }
  205. if ((count > 64 * 1024 * 1024)
  206. || (data = vmalloc(count)) == NULL) {
  207. length = -ENOMEM;
  208. goto out;
  209. }
  210. length = -EFAULT;
  211. if (copy_from_user(data, buf, count) != 0)
  212. goto out;
  213. length = security_load_policy(data, count);
  214. if (length)
  215. goto out;
  216. ret = sel_make_bools();
  217. if (ret)
  218. length = ret;
  219. else
  220. length = count;
  221. out:
  222. mutex_unlock(&sel_mutex);
  223. vfree(data);
  224. return length;
  225. }
  226. static struct file_operations sel_load_ops = {
  227. .write = sel_write_load,
  228. };
  229. static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
  230. {
  231. char *canon;
  232. u32 sid, len;
  233. ssize_t length;
  234. length = task_has_security(current, SECURITY__CHECK_CONTEXT);
  235. if (length)
  236. return length;
  237. length = security_context_to_sid(buf, size, &sid);
  238. if (length < 0)
  239. return length;
  240. length = security_sid_to_context(sid, &canon, &len);
  241. if (length < 0)
  242. return length;
  243. if (len > SIMPLE_TRANSACTION_LIMIT) {
  244. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  245. "max\n", __FUNCTION__, len);
  246. length = -ERANGE;
  247. goto out;
  248. }
  249. memcpy(buf, canon, len);
  250. length = len;
  251. out:
  252. kfree(canon);
  253. return length;
  254. }
  255. static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
  256. size_t count, loff_t *ppos)
  257. {
  258. char tmpbuf[TMPBUFLEN];
  259. ssize_t length;
  260. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
  261. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  262. }
  263. static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
  264. size_t count, loff_t *ppos)
  265. {
  266. char *page;
  267. ssize_t length;
  268. unsigned int new_value;
  269. length = task_has_security(current, SECURITY__SETCHECKREQPROT);
  270. if (length)
  271. return length;
  272. if (count >= PAGE_SIZE)
  273. return -ENOMEM;
  274. if (*ppos != 0) {
  275. /* No partial writes. */
  276. return -EINVAL;
  277. }
  278. page = (char*)get_zeroed_page(GFP_KERNEL);
  279. if (!page)
  280. return -ENOMEM;
  281. length = -EFAULT;
  282. if (copy_from_user(page, buf, count))
  283. goto out;
  284. length = -EINVAL;
  285. if (sscanf(page, "%u", &new_value) != 1)
  286. goto out;
  287. selinux_checkreqprot = new_value ? 1 : 0;
  288. length = count;
  289. out:
  290. free_page((unsigned long) page);
  291. return length;
  292. }
  293. static struct file_operations sel_checkreqprot_ops = {
  294. .read = sel_read_checkreqprot,
  295. .write = sel_write_checkreqprot,
  296. };
  297. /*
  298. * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
  299. */
  300. static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
  301. static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
  302. static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
  303. static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
  304. static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
  305. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  306. [SEL_ACCESS] = sel_write_access,
  307. [SEL_CREATE] = sel_write_create,
  308. [SEL_RELABEL] = sel_write_relabel,
  309. [SEL_USER] = sel_write_user,
  310. [SEL_MEMBER] = sel_write_member,
  311. [SEL_CONTEXT] = sel_write_context,
  312. };
  313. static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  314. {
  315. ino_t ino = file->f_dentry->d_inode->i_ino;
  316. char *data;
  317. ssize_t rv;
  318. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  319. return -EINVAL;
  320. data = simple_transaction_get(file, buf, size);
  321. if (IS_ERR(data))
  322. return PTR_ERR(data);
  323. rv = write_op[ino](file, data, size);
  324. if (rv>0) {
  325. simple_transaction_set(file, rv);
  326. rv = size;
  327. }
  328. return rv;
  329. }
  330. static struct file_operations transaction_ops = {
  331. .write = selinux_transaction_write,
  332. .read = simple_transaction_read,
  333. .release = simple_transaction_release,
  334. };
  335. /*
  336. * payload - write methods
  337. * If the method has a response, the response should be put in buf,
  338. * and the length returned. Otherwise return 0 or and -error.
  339. */
  340. static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
  341. {
  342. char *scon, *tcon;
  343. u32 ssid, tsid;
  344. u16 tclass;
  345. u32 req;
  346. struct av_decision avd;
  347. ssize_t length;
  348. length = task_has_security(current, SECURITY__COMPUTE_AV);
  349. if (length)
  350. return length;
  351. length = -ENOMEM;
  352. scon = kzalloc(size+1, GFP_KERNEL);
  353. if (!scon)
  354. return length;
  355. tcon = kzalloc(size+1, GFP_KERNEL);
  356. if (!tcon)
  357. goto out;
  358. length = -EINVAL;
  359. if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
  360. goto out2;
  361. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  362. if (length < 0)
  363. goto out2;
  364. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  365. if (length < 0)
  366. goto out2;
  367. length = security_compute_av(ssid, tsid, tclass, req, &avd);
  368. if (length < 0)
  369. goto out2;
  370. length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
  371. "%x %x %x %x %u",
  372. avd.allowed, avd.decided,
  373. avd.auditallow, avd.auditdeny,
  374. avd.seqno);
  375. out2:
  376. kfree(tcon);
  377. out:
  378. kfree(scon);
  379. return length;
  380. }
  381. static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
  382. {
  383. char *scon, *tcon;
  384. u32 ssid, tsid, newsid;
  385. u16 tclass;
  386. ssize_t length;
  387. char *newcon;
  388. u32 len;
  389. length = task_has_security(current, SECURITY__COMPUTE_CREATE);
  390. if (length)
  391. return length;
  392. length = -ENOMEM;
  393. scon = kzalloc(size+1, GFP_KERNEL);
  394. if (!scon)
  395. return length;
  396. tcon = kzalloc(size+1, GFP_KERNEL);
  397. if (!tcon)
  398. goto out;
  399. length = -EINVAL;
  400. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  401. goto out2;
  402. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  403. if (length < 0)
  404. goto out2;
  405. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  406. if (length < 0)
  407. goto out2;
  408. length = security_transition_sid(ssid, tsid, tclass, &newsid);
  409. if (length < 0)
  410. goto out2;
  411. length = security_sid_to_context(newsid, &newcon, &len);
  412. if (length < 0)
  413. goto out2;
  414. if (len > SIMPLE_TRANSACTION_LIMIT) {
  415. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  416. "max\n", __FUNCTION__, len);
  417. length = -ERANGE;
  418. goto out3;
  419. }
  420. memcpy(buf, newcon, len);
  421. length = len;
  422. out3:
  423. kfree(newcon);
  424. out2:
  425. kfree(tcon);
  426. out:
  427. kfree(scon);
  428. return length;
  429. }
  430. static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
  431. {
  432. char *scon, *tcon;
  433. u32 ssid, tsid, newsid;
  434. u16 tclass;
  435. ssize_t length;
  436. char *newcon;
  437. u32 len;
  438. length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
  439. if (length)
  440. return length;
  441. length = -ENOMEM;
  442. scon = kzalloc(size+1, GFP_KERNEL);
  443. if (!scon)
  444. return length;
  445. tcon = kzalloc(size+1, GFP_KERNEL);
  446. if (!tcon)
  447. goto out;
  448. length = -EINVAL;
  449. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  450. goto out2;
  451. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  452. if (length < 0)
  453. goto out2;
  454. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  455. if (length < 0)
  456. goto out2;
  457. length = security_change_sid(ssid, tsid, tclass, &newsid);
  458. if (length < 0)
  459. goto out2;
  460. length = security_sid_to_context(newsid, &newcon, &len);
  461. if (length < 0)
  462. goto out2;
  463. if (len > SIMPLE_TRANSACTION_LIMIT) {
  464. length = -ERANGE;
  465. goto out3;
  466. }
  467. memcpy(buf, newcon, len);
  468. length = len;
  469. out3:
  470. kfree(newcon);
  471. out2:
  472. kfree(tcon);
  473. out:
  474. kfree(scon);
  475. return length;
  476. }
  477. static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
  478. {
  479. char *con, *user, *ptr;
  480. u32 sid, *sids;
  481. ssize_t length;
  482. char *newcon;
  483. int i, rc;
  484. u32 len, nsids;
  485. length = task_has_security(current, SECURITY__COMPUTE_USER);
  486. if (length)
  487. return length;
  488. length = -ENOMEM;
  489. con = kzalloc(size+1, GFP_KERNEL);
  490. if (!con)
  491. return length;
  492. user = kzalloc(size+1, GFP_KERNEL);
  493. if (!user)
  494. goto out;
  495. length = -EINVAL;
  496. if (sscanf(buf, "%s %s", con, user) != 2)
  497. goto out2;
  498. length = security_context_to_sid(con, strlen(con)+1, &sid);
  499. if (length < 0)
  500. goto out2;
  501. length = security_get_user_sids(sid, user, &sids, &nsids);
  502. if (length < 0)
  503. goto out2;
  504. length = sprintf(buf, "%u", nsids) + 1;
  505. ptr = buf + length;
  506. for (i = 0; i < nsids; i++) {
  507. rc = security_sid_to_context(sids[i], &newcon, &len);
  508. if (rc) {
  509. length = rc;
  510. goto out3;
  511. }
  512. if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
  513. kfree(newcon);
  514. length = -ERANGE;
  515. goto out3;
  516. }
  517. memcpy(ptr, newcon, len);
  518. kfree(newcon);
  519. ptr += len;
  520. length += len;
  521. }
  522. out3:
  523. kfree(sids);
  524. out2:
  525. kfree(user);
  526. out:
  527. kfree(con);
  528. return length;
  529. }
  530. static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
  531. {
  532. char *scon, *tcon;
  533. u32 ssid, tsid, newsid;
  534. u16 tclass;
  535. ssize_t length;
  536. char *newcon;
  537. u32 len;
  538. length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
  539. if (length)
  540. return length;
  541. length = -ENOMEM;
  542. scon = kzalloc(size+1, GFP_KERNEL);
  543. if (!scon)
  544. return length;
  545. tcon = kzalloc(size+1, GFP_KERNEL);
  546. if (!tcon)
  547. goto out;
  548. length = -EINVAL;
  549. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  550. goto out2;
  551. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  552. if (length < 0)
  553. goto out2;
  554. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  555. if (length < 0)
  556. goto out2;
  557. length = security_member_sid(ssid, tsid, tclass, &newsid);
  558. if (length < 0)
  559. goto out2;
  560. length = security_sid_to_context(newsid, &newcon, &len);
  561. if (length < 0)
  562. goto out2;
  563. if (len > SIMPLE_TRANSACTION_LIMIT) {
  564. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  565. "max\n", __FUNCTION__, len);
  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 struct inode *sel_make_inode(struct super_block *sb, int mode)
  580. {
  581. struct inode *ret = new_inode(sb);
  582. if (ret) {
  583. ret->i_mode = mode;
  584. ret->i_uid = ret->i_gid = 0;
  585. ret->i_blksize = PAGE_CACHE_SIZE;
  586. ret->i_blocks = 0;
  587. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  588. }
  589. return ret;
  590. }
  591. #define BOOL_INO_OFFSET 30
  592. static ssize_t sel_read_bool(struct file *filep, char __user *buf,
  593. size_t count, loff_t *ppos)
  594. {
  595. char *page = NULL;
  596. ssize_t length;
  597. ssize_t ret;
  598. int cur_enforcing;
  599. struct inode *inode;
  600. mutex_lock(&sel_mutex);
  601. ret = -EFAULT;
  602. /* check to see if this file has been deleted */
  603. if (!filep->f_op)
  604. goto out;
  605. if (count > PAGE_SIZE) {
  606. ret = -EINVAL;
  607. goto out;
  608. }
  609. if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
  610. ret = -ENOMEM;
  611. goto out;
  612. }
  613. inode = filep->f_dentry->d_inode;
  614. cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
  615. if (cur_enforcing < 0) {
  616. ret = cur_enforcing;
  617. goto out;
  618. }
  619. length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
  620. bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
  621. ret = simple_read_from_buffer(buf, count, ppos, page, length);
  622. out:
  623. mutex_unlock(&sel_mutex);
  624. if (page)
  625. free_page((unsigned long)page);
  626. return ret;
  627. }
  628. static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
  629. size_t count, loff_t *ppos)
  630. {
  631. char *page = NULL;
  632. ssize_t length = -EFAULT;
  633. int new_value;
  634. struct inode *inode;
  635. mutex_lock(&sel_mutex);
  636. length = task_has_security(current, SECURITY__SETBOOL);
  637. if (length)
  638. goto out;
  639. /* check to see if this file has been deleted */
  640. if (!filep->f_op)
  641. goto out;
  642. if (count >= PAGE_SIZE) {
  643. length = -ENOMEM;
  644. goto out;
  645. }
  646. if (*ppos != 0) {
  647. /* No partial writes. */
  648. goto out;
  649. }
  650. page = (char*)get_zeroed_page(GFP_KERNEL);
  651. if (!page) {
  652. length = -ENOMEM;
  653. goto out;
  654. }
  655. if (copy_from_user(page, buf, count))
  656. goto out;
  657. length = -EINVAL;
  658. if (sscanf(page, "%d", &new_value) != 1)
  659. goto out;
  660. if (new_value)
  661. new_value = 1;
  662. inode = filep->f_dentry->d_inode;
  663. bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
  664. length = count;
  665. out:
  666. mutex_unlock(&sel_mutex);
  667. if (page)
  668. free_page((unsigned long) page);
  669. return length;
  670. }
  671. static struct file_operations sel_bool_ops = {
  672. .read = sel_read_bool,
  673. .write = sel_write_bool,
  674. };
  675. static ssize_t sel_commit_bools_write(struct file *filep,
  676. const char __user *buf,
  677. size_t count, loff_t *ppos)
  678. {
  679. char *page = NULL;
  680. ssize_t length = -EFAULT;
  681. int new_value;
  682. mutex_lock(&sel_mutex);
  683. length = task_has_security(current, SECURITY__SETBOOL);
  684. if (length)
  685. goto out;
  686. /* check to see if this file has been deleted */
  687. if (!filep->f_op)
  688. goto out;
  689. if (count >= PAGE_SIZE) {
  690. length = -ENOMEM;
  691. goto out;
  692. }
  693. if (*ppos != 0) {
  694. /* No partial writes. */
  695. goto out;
  696. }
  697. page = (char*)get_zeroed_page(GFP_KERNEL);
  698. if (!page) {
  699. length = -ENOMEM;
  700. goto out;
  701. }
  702. if (copy_from_user(page, buf, count))
  703. goto out;
  704. length = -EINVAL;
  705. if (sscanf(page, "%d", &new_value) != 1)
  706. goto out;
  707. if (new_value && bool_pending_values) {
  708. security_set_bools(bool_num, bool_pending_values);
  709. }
  710. length = count;
  711. out:
  712. mutex_unlock(&sel_mutex);
  713. if (page)
  714. free_page((unsigned long) page);
  715. return length;
  716. }
  717. static struct file_operations sel_commit_bools_ops = {
  718. .write = sel_commit_bools_write,
  719. };
  720. /* delete booleans - partial revoke() from
  721. * fs/proc/generic.c proc_kill_inodes */
  722. static void sel_remove_bools(struct dentry *de)
  723. {
  724. struct list_head *p, *node;
  725. struct super_block *sb = de->d_sb;
  726. spin_lock(&dcache_lock);
  727. node = de->d_subdirs.next;
  728. while (node != &de->d_subdirs) {
  729. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  730. list_del_init(node);
  731. if (d->d_inode) {
  732. d = dget_locked(d);
  733. spin_unlock(&dcache_lock);
  734. d_delete(d);
  735. simple_unlink(de->d_inode, d);
  736. dput(d);
  737. spin_lock(&dcache_lock);
  738. }
  739. node = de->d_subdirs.next;
  740. }
  741. spin_unlock(&dcache_lock);
  742. file_list_lock();
  743. list_for_each(p, &sb->s_files) {
  744. struct file * filp = list_entry(p, struct file, f_u.fu_list);
  745. struct dentry * dentry = filp->f_dentry;
  746. if (dentry->d_parent != de) {
  747. continue;
  748. }
  749. filp->f_op = NULL;
  750. }
  751. file_list_unlock();
  752. }
  753. #define BOOL_DIR_NAME "booleans"
  754. static int sel_make_bools(void)
  755. {
  756. int i, ret = 0;
  757. ssize_t len;
  758. struct dentry *dentry = NULL;
  759. struct dentry *dir = bool_dir;
  760. struct inode *inode = NULL;
  761. struct inode_security_struct *isec;
  762. char **names = NULL, *page;
  763. int num;
  764. int *values = NULL;
  765. u32 sid;
  766. /* remove any existing files */
  767. kfree(bool_pending_values);
  768. bool_pending_values = NULL;
  769. sel_remove_bools(dir);
  770. if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
  771. return -ENOMEM;
  772. ret = security_get_bools(&num, &names, &values);
  773. if (ret != 0)
  774. goto out;
  775. for (i = 0; i < num; i++) {
  776. dentry = d_alloc_name(dir, names[i]);
  777. if (!dentry) {
  778. ret = -ENOMEM;
  779. goto err;
  780. }
  781. inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
  782. if (!inode) {
  783. ret = -ENOMEM;
  784. goto err;
  785. }
  786. len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
  787. if (len < 0) {
  788. ret = -EINVAL;
  789. goto err;
  790. } else if (len >= PAGE_SIZE) {
  791. ret = -ENAMETOOLONG;
  792. goto err;
  793. }
  794. isec = (struct inode_security_struct*)inode->i_security;
  795. if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
  796. goto err;
  797. isec->sid = sid;
  798. isec->initialized = 1;
  799. inode->i_fop = &sel_bool_ops;
  800. inode->i_ino = i + BOOL_INO_OFFSET;
  801. d_add(dentry, inode);
  802. }
  803. bool_num = num;
  804. bool_pending_values = values;
  805. out:
  806. free_page((unsigned long)page);
  807. if (names) {
  808. for (i = 0; i < num; i++)
  809. kfree(names[i]);
  810. kfree(names);
  811. }
  812. return ret;
  813. err:
  814. kfree(values);
  815. d_genocide(dir);
  816. ret = -ENOMEM;
  817. goto out;
  818. }
  819. #define NULL_FILE_NAME "null"
  820. struct dentry *selinux_null = NULL;
  821. static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
  822. size_t count, loff_t *ppos)
  823. {
  824. char tmpbuf[TMPBUFLEN];
  825. ssize_t length;
  826. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
  827. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  828. }
  829. static ssize_t sel_write_avc_cache_threshold(struct file * file,
  830. const char __user * buf,
  831. size_t count, loff_t *ppos)
  832. {
  833. char *page;
  834. ssize_t ret;
  835. int new_value;
  836. if (count >= PAGE_SIZE) {
  837. ret = -ENOMEM;
  838. goto out;
  839. }
  840. if (*ppos != 0) {
  841. /* No partial writes. */
  842. ret = -EINVAL;
  843. goto out;
  844. }
  845. page = (char*)get_zeroed_page(GFP_KERNEL);
  846. if (!page) {
  847. ret = -ENOMEM;
  848. goto out;
  849. }
  850. if (copy_from_user(page, buf, count)) {
  851. ret = -EFAULT;
  852. goto out_free;
  853. }
  854. if (sscanf(page, "%u", &new_value) != 1) {
  855. ret = -EINVAL;
  856. goto out;
  857. }
  858. if (new_value != avc_cache_threshold) {
  859. ret = task_has_security(current, SECURITY__SETSECPARAM);
  860. if (ret)
  861. goto out_free;
  862. avc_cache_threshold = new_value;
  863. }
  864. ret = count;
  865. out_free:
  866. free_page((unsigned long)page);
  867. out:
  868. return ret;
  869. }
  870. static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
  871. size_t count, loff_t *ppos)
  872. {
  873. char *page;
  874. ssize_t ret = 0;
  875. page = (char *)__get_free_page(GFP_KERNEL);
  876. if (!page) {
  877. ret = -ENOMEM;
  878. goto out;
  879. }
  880. ret = avc_get_hash_stats(page);
  881. if (ret >= 0)
  882. ret = simple_read_from_buffer(buf, count, ppos, page, ret);
  883. free_page((unsigned long)page);
  884. out:
  885. return ret;
  886. }
  887. static struct file_operations sel_avc_cache_threshold_ops = {
  888. .read = sel_read_avc_cache_threshold,
  889. .write = sel_write_avc_cache_threshold,
  890. };
  891. static struct file_operations sel_avc_hash_stats_ops = {
  892. .read = sel_read_avc_hash_stats,
  893. };
  894. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  895. static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
  896. {
  897. int cpu;
  898. for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
  899. if (!cpu_possible(cpu))
  900. continue;
  901. *idx = cpu + 1;
  902. return &per_cpu(avc_cache_stats, cpu);
  903. }
  904. return NULL;
  905. }
  906. static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
  907. {
  908. loff_t n = *pos - 1;
  909. if (*pos == 0)
  910. return SEQ_START_TOKEN;
  911. return sel_avc_get_stat_idx(&n);
  912. }
  913. static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  914. {
  915. return sel_avc_get_stat_idx(pos);
  916. }
  917. static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
  918. {
  919. struct avc_cache_stats *st = v;
  920. if (v == SEQ_START_TOKEN)
  921. seq_printf(seq, "lookups hits misses allocations reclaims "
  922. "frees\n");
  923. else
  924. seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
  925. st->hits, st->misses, st->allocations,
  926. st->reclaims, st->frees);
  927. return 0;
  928. }
  929. static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
  930. { }
  931. static struct seq_operations sel_avc_cache_stats_seq_ops = {
  932. .start = sel_avc_stats_seq_start,
  933. .next = sel_avc_stats_seq_next,
  934. .show = sel_avc_stats_seq_show,
  935. .stop = sel_avc_stats_seq_stop,
  936. };
  937. static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
  938. {
  939. return seq_open(file, &sel_avc_cache_stats_seq_ops);
  940. }
  941. static struct file_operations sel_avc_cache_stats_ops = {
  942. .open = sel_open_avc_cache_stats,
  943. .read = seq_read,
  944. .llseek = seq_lseek,
  945. .release = seq_release,
  946. };
  947. #endif
  948. static int sel_make_avc_files(struct dentry *dir)
  949. {
  950. int i, ret = 0;
  951. static struct tree_descr files[] = {
  952. { "cache_threshold",
  953. &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
  954. { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
  955. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  956. { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
  957. #endif
  958. };
  959. for (i = 0; i < ARRAY_SIZE(files); i++) {
  960. struct inode *inode;
  961. struct dentry *dentry;
  962. dentry = d_alloc_name(dir, files[i].name);
  963. if (!dentry) {
  964. ret = -ENOMEM;
  965. goto err;
  966. }
  967. inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
  968. if (!inode) {
  969. ret = -ENOMEM;
  970. goto err;
  971. }
  972. inode->i_fop = files[i].ops;
  973. d_add(dentry, inode);
  974. }
  975. out:
  976. return ret;
  977. err:
  978. d_genocide(dir);
  979. goto out;
  980. }
  981. static int sel_make_dir(struct super_block *sb, struct dentry *dentry)
  982. {
  983. int ret = 0;
  984. struct inode *inode;
  985. inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
  986. if (!inode) {
  987. ret = -ENOMEM;
  988. goto out;
  989. }
  990. inode->i_op = &simple_dir_inode_operations;
  991. inode->i_fop = &simple_dir_operations;
  992. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  993. inode->i_nlink++;
  994. d_add(dentry, inode);
  995. out:
  996. return ret;
  997. }
  998. static int sel_fill_super(struct super_block * sb, void * data, int silent)
  999. {
  1000. int ret;
  1001. struct dentry *dentry;
  1002. struct inode *inode;
  1003. struct inode_security_struct *isec;
  1004. static struct tree_descr selinux_files[] = {
  1005. [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
  1006. [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
  1007. [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
  1008. [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
  1009. [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
  1010. [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
  1011. [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
  1012. [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
  1013. [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
  1014. [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
  1015. [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
  1016. [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
  1017. [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
  1018. /* last one */ {""}
  1019. };
  1020. ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
  1021. if (ret)
  1022. goto err;
  1023. dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
  1024. if (!dentry) {
  1025. ret = -ENOMEM;
  1026. goto err;
  1027. }
  1028. ret = sel_make_dir(sb, dentry);
  1029. if (ret)
  1030. goto err;
  1031. bool_dir = dentry;
  1032. ret = sel_make_bools();
  1033. if (ret)
  1034. goto err;
  1035. dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
  1036. if (!dentry) {
  1037. ret = -ENOMEM;
  1038. goto err;
  1039. }
  1040. inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
  1041. if (!inode) {
  1042. ret = -ENOMEM;
  1043. goto err;
  1044. }
  1045. isec = (struct inode_security_struct*)inode->i_security;
  1046. isec->sid = SECINITSID_DEVNULL;
  1047. isec->sclass = SECCLASS_CHR_FILE;
  1048. isec->initialized = 1;
  1049. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
  1050. d_add(dentry, inode);
  1051. selinux_null = dentry;
  1052. dentry = d_alloc_name(sb->s_root, "avc");
  1053. if (!dentry) {
  1054. ret = -ENOMEM;
  1055. goto err;
  1056. }
  1057. ret = sel_make_dir(sb, dentry);
  1058. if (ret)
  1059. goto err;
  1060. ret = sel_make_avc_files(dentry);
  1061. if (ret)
  1062. goto err;
  1063. out:
  1064. return ret;
  1065. err:
  1066. printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
  1067. goto out;
  1068. }
  1069. static struct super_block *sel_get_sb(struct file_system_type *fs_type,
  1070. int flags, const char *dev_name, void *data)
  1071. {
  1072. return get_sb_single(fs_type, flags, data, sel_fill_super);
  1073. }
  1074. static struct file_system_type sel_fs_type = {
  1075. .name = "selinuxfs",
  1076. .get_sb = sel_get_sb,
  1077. .kill_sb = kill_litter_super,
  1078. };
  1079. struct vfsmount *selinuxfs_mount;
  1080. static int __init init_sel_fs(void)
  1081. {
  1082. int err;
  1083. if (!selinux_enabled)
  1084. return 0;
  1085. err = register_filesystem(&sel_fs_type);
  1086. if (!err) {
  1087. selinuxfs_mount = kern_mount(&sel_fs_type);
  1088. if (IS_ERR(selinuxfs_mount)) {
  1089. printk(KERN_ERR "selinuxfs: could not mount!\n");
  1090. err = PTR_ERR(selinuxfs_mount);
  1091. selinuxfs_mount = NULL;
  1092. }
  1093. }
  1094. return err;
  1095. }
  1096. __initcall(init_sel_fs);
  1097. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  1098. void exit_sel_fs(void)
  1099. {
  1100. unregister_filesystem(&sel_fs_type);
  1101. }
  1102. #endif