selinuxfs.c 28 KB

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