selinuxfs.c 30 KB

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