selinuxfs.c 29 KB

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