selinuxfs.c 28 KB

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