selinuxfs.c 30 KB

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