selinuxfs.c 29 KB

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