selinuxfs.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. /* Updated: Karl MacMillan <kmacmillan@tresys.com>
  2. *
  3. * Added conditional policy language extensions
  4. *
  5. * Updated: Hewlett-Packard <paul.moore@hp.com>
  6. *
  7. * Added support for the policy capability bitmap
  8. *
  9. * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
  10. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  11. * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, version 2.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/slab.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/fs.h>
  21. #include <linux/mutex.h>
  22. #include <linux/init.h>
  23. #include <linux/string.h>
  24. #include <linux/security.h>
  25. #include <linux/major.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/percpu.h>
  28. #include <linux/audit.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/semaphore.h>
  31. /* selinuxfs pseudo filesystem for exporting the security policy API.
  32. Based on the proc code and the fs/nfsd/nfsctl.c code. */
  33. #include "flask.h"
  34. #include "avc.h"
  35. #include "avc_ss.h"
  36. #include "security.h"
  37. #include "objsec.h"
  38. #include "conditional.h"
  39. /* Policy capability filenames */
  40. static char *policycap_names[] = {
  41. "network_peer_controls",
  42. "open_perms"
  43. };
  44. unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
  45. #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
  46. #define SELINUX_COMPAT_NET_VALUE 0
  47. #else
  48. #define SELINUX_COMPAT_NET_VALUE 1
  49. #endif
  50. int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
  51. static int __init checkreqprot_setup(char *str)
  52. {
  53. selinux_checkreqprot = simple_strtoul(str, NULL, 0) ? 1 : 0;
  54. return 1;
  55. }
  56. __setup("checkreqprot=", checkreqprot_setup);
  57. static int __init selinux_compat_net_setup(char *str)
  58. {
  59. selinux_compat_net = simple_strtoul(str, NULL, 0) ? 1 : 0;
  60. return 1;
  61. }
  62. __setup("selinux_compat_net=", selinux_compat_net_setup);
  63. static DEFINE_MUTEX(sel_mutex);
  64. /* global data for booleans */
  65. static struct dentry *bool_dir;
  66. static int bool_num;
  67. static char **bool_pending_names;
  68. static int *bool_pending_values;
  69. /* global data for classes */
  70. static struct dentry *class_dir;
  71. static unsigned long last_class_ino;
  72. /* global data for policy capabilities */
  73. static struct dentry *policycap_dir;
  74. extern void selnl_notify_setenforce(int val);
  75. /* Check whether a task is allowed to use a security operation. */
  76. static int task_has_security(struct task_struct *tsk,
  77. u32 perms)
  78. {
  79. struct task_security_struct *tsec;
  80. tsec = tsk->security;
  81. if (!tsec)
  82. return -EACCES;
  83. return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
  84. SECCLASS_SECURITY, perms, NULL);
  85. }
  86. enum sel_inos {
  87. SEL_ROOT_INO = 2,
  88. SEL_LOAD, /* load policy */
  89. SEL_ENFORCE, /* get or set enforcing status */
  90. SEL_CONTEXT, /* validate context */
  91. SEL_ACCESS, /* compute access decision */
  92. SEL_CREATE, /* compute create labeling decision */
  93. SEL_RELABEL, /* compute relabeling decision */
  94. SEL_USER, /* compute reachable user contexts */
  95. SEL_POLICYVERS, /* return policy version for this kernel */
  96. SEL_COMMIT_BOOLS, /* commit new boolean values */
  97. SEL_MLS, /* return if MLS policy is enabled */
  98. SEL_DISABLE, /* disable SELinux until next reboot */
  99. SEL_MEMBER, /* compute polyinstantiation membership decision */
  100. SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
  101. SEL_COMPAT_NET, /* whether to use old compat network packet controls */
  102. SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
  103. SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
  104. SEL_INO_NEXT, /* The next inode number to use */
  105. };
  106. static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
  107. #define SEL_INITCON_INO_OFFSET 0x01000000
  108. #define SEL_BOOL_INO_OFFSET 0x02000000
  109. #define SEL_CLASS_INO_OFFSET 0x04000000
  110. #define SEL_POLICYCAP_INO_OFFSET 0x08000000
  111. #define SEL_INO_MASK 0x00ffffff
  112. #define TMPBUFLEN 12
  113. static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
  114. size_t count, loff_t *ppos)
  115. {
  116. char tmpbuf[TMPBUFLEN];
  117. ssize_t length;
  118. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
  119. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  120. }
  121. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  122. static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
  123. size_t count, loff_t *ppos)
  124. {
  125. char *page;
  126. ssize_t length;
  127. int new_value;
  128. if (count >= PAGE_SIZE)
  129. return -ENOMEM;
  130. if (*ppos != 0) {
  131. /* No partial writes. */
  132. return -EINVAL;
  133. }
  134. page = (char *)get_zeroed_page(GFP_KERNEL);
  135. if (!page)
  136. return -ENOMEM;
  137. length = -EFAULT;
  138. if (copy_from_user(page, buf, count))
  139. goto out;
  140. length = -EINVAL;
  141. if (sscanf(page, "%d", &new_value) != 1)
  142. goto out;
  143. if (new_value != selinux_enforcing) {
  144. length = task_has_security(current, SECURITY__SETENFORCE);
  145. if (length)
  146. goto out;
  147. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
  148. "enforcing=%d old_enforcing=%d auid=%u ses=%u",
  149. new_value, selinux_enforcing,
  150. audit_get_loginuid(current),
  151. audit_get_sessionid(current));
  152. selinux_enforcing = new_value;
  153. if (selinux_enforcing)
  154. avc_ss_reset(0);
  155. selnl_notify_setenforce(selinux_enforcing);
  156. }
  157. length = count;
  158. out:
  159. free_page((unsigned long) page);
  160. return length;
  161. }
  162. #else
  163. #define sel_write_enforce NULL
  164. #endif
  165. static const struct file_operations sel_enforce_ops = {
  166. .read = sel_read_enforce,
  167. .write = sel_write_enforce,
  168. };
  169. static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
  170. size_t count, loff_t *ppos)
  171. {
  172. char tmpbuf[TMPBUFLEN];
  173. ssize_t length;
  174. ino_t ino = filp->f_path.dentry->d_inode->i_ino;
  175. int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
  176. security_get_reject_unknown() : !security_get_allow_unknown();
  177. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
  178. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  179. }
  180. static const struct file_operations sel_handle_unknown_ops = {
  181. .read = sel_read_handle_unknown,
  182. };
  183. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  184. static ssize_t sel_write_disable(struct file *file, const char __user *buf,
  185. size_t count, loff_t *ppos)
  186. {
  187. char *page;
  188. ssize_t length;
  189. int new_value;
  190. extern int selinux_disable(void);
  191. if (count >= PAGE_SIZE)
  192. return -ENOMEM;
  193. if (*ppos != 0) {
  194. /* No partial writes. */
  195. return -EINVAL;
  196. }
  197. page = (char *)get_zeroed_page(GFP_KERNEL);
  198. if (!page)
  199. return -ENOMEM;
  200. length = -EFAULT;
  201. if (copy_from_user(page, buf, count))
  202. goto out;
  203. length = -EINVAL;
  204. if (sscanf(page, "%d", &new_value) != 1)
  205. goto out;
  206. if (new_value) {
  207. length = selinux_disable();
  208. if (length < 0)
  209. goto out;
  210. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
  211. "selinux=0 auid=%u ses=%u",
  212. audit_get_loginuid(current),
  213. audit_get_sessionid(current));
  214. }
  215. length = count;
  216. out:
  217. free_page((unsigned long) page);
  218. return length;
  219. }
  220. #else
  221. #define sel_write_disable NULL
  222. #endif
  223. static const struct file_operations sel_disable_ops = {
  224. .write = sel_write_disable,
  225. };
  226. static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
  227. size_t count, loff_t *ppos)
  228. {
  229. char tmpbuf[TMPBUFLEN];
  230. ssize_t length;
  231. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
  232. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  233. }
  234. static const struct file_operations sel_policyvers_ops = {
  235. .read = sel_read_policyvers,
  236. };
  237. /* declaration for sel_write_load */
  238. static int sel_make_bools(void);
  239. static int sel_make_classes(void);
  240. static int sel_make_policycap(void);
  241. /* declaration for sel_make_class_dirs */
  242. static int sel_make_dir(struct inode *dir, struct dentry *dentry,
  243. unsigned long *ino);
  244. static ssize_t sel_read_mls(struct file *filp, char __user *buf,
  245. size_t count, loff_t *ppos)
  246. {
  247. char tmpbuf[TMPBUFLEN];
  248. ssize_t length;
  249. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
  250. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  251. }
  252. static const struct file_operations sel_mls_ops = {
  253. .read = sel_read_mls,
  254. };
  255. static ssize_t sel_write_load(struct file *file, const char __user *buf,
  256. size_t count, loff_t *ppos)
  257. {
  258. int ret;
  259. ssize_t length;
  260. void *data = NULL;
  261. mutex_lock(&sel_mutex);
  262. length = task_has_security(current, SECURITY__LOAD_POLICY);
  263. if (length)
  264. goto out;
  265. if (*ppos != 0) {
  266. /* No partial writes. */
  267. length = -EINVAL;
  268. goto out;
  269. }
  270. if ((count > 64 * 1024 * 1024)
  271. || (data = vmalloc(count)) == NULL) {
  272. length = -ENOMEM;
  273. goto out;
  274. }
  275. length = -EFAULT;
  276. if (copy_from_user(data, buf, count) != 0)
  277. goto out;
  278. length = security_load_policy(data, count);
  279. if (length)
  280. goto out;
  281. ret = sel_make_bools();
  282. if (ret) {
  283. length = ret;
  284. goto out1;
  285. }
  286. ret = sel_make_classes();
  287. if (ret) {
  288. length = ret;
  289. goto out1;
  290. }
  291. ret = sel_make_policycap();
  292. if (ret)
  293. length = ret;
  294. else
  295. length = count;
  296. out1:
  297. printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n",
  298. (security_get_reject_unknown() ? "reject" :
  299. (security_get_allow_unknown() ? "allow" : "deny")));
  300. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
  301. "policy loaded auid=%u ses=%u",
  302. audit_get_loginuid(current),
  303. audit_get_sessionid(current));
  304. out:
  305. mutex_unlock(&sel_mutex);
  306. vfree(data);
  307. return length;
  308. }
  309. static const struct file_operations sel_load_ops = {
  310. .write = sel_write_load,
  311. };
  312. static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
  313. {
  314. char *canon;
  315. u32 sid, len;
  316. ssize_t length;
  317. length = task_has_security(current, SECURITY__CHECK_CONTEXT);
  318. if (length)
  319. return length;
  320. length = security_context_to_sid(buf, size, &sid);
  321. if (length < 0)
  322. return length;
  323. length = security_sid_to_context(sid, &canon, &len);
  324. if (length < 0)
  325. return length;
  326. if (len > SIMPLE_TRANSACTION_LIMIT) {
  327. printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
  328. "payload max\n", __func__, len);
  329. length = -ERANGE;
  330. goto out;
  331. }
  332. memcpy(buf, canon, len);
  333. length = len;
  334. out:
  335. kfree(canon);
  336. return length;
  337. }
  338. static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
  339. size_t count, loff_t *ppos)
  340. {
  341. char tmpbuf[TMPBUFLEN];
  342. ssize_t length;
  343. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
  344. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  345. }
  346. static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
  347. size_t count, loff_t *ppos)
  348. {
  349. char *page;
  350. ssize_t length;
  351. unsigned int new_value;
  352. length = task_has_security(current, SECURITY__SETCHECKREQPROT);
  353. if (length)
  354. return length;
  355. if (count >= PAGE_SIZE)
  356. return -ENOMEM;
  357. if (*ppos != 0) {
  358. /* No partial writes. */
  359. return -EINVAL;
  360. }
  361. page = (char *)get_zeroed_page(GFP_KERNEL);
  362. if (!page)
  363. return -ENOMEM;
  364. length = -EFAULT;
  365. if (copy_from_user(page, buf, count))
  366. goto out;
  367. length = -EINVAL;
  368. if (sscanf(page, "%u", &new_value) != 1)
  369. goto out;
  370. selinux_checkreqprot = new_value ? 1 : 0;
  371. length = count;
  372. out:
  373. free_page((unsigned long) page);
  374. return length;
  375. }
  376. static const struct file_operations sel_checkreqprot_ops = {
  377. .read = sel_read_checkreqprot,
  378. .write = sel_write_checkreqprot,
  379. };
  380. static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
  381. size_t count, loff_t *ppos)
  382. {
  383. char tmpbuf[TMPBUFLEN];
  384. ssize_t length;
  385. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
  386. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  387. }
  388. static ssize_t sel_write_compat_net(struct file *file, const char __user *buf,
  389. size_t count, loff_t *ppos)
  390. {
  391. char *page;
  392. ssize_t length;
  393. int new_value;
  394. length = task_has_security(current, SECURITY__LOAD_POLICY);
  395. if (length)
  396. return length;
  397. if (count >= PAGE_SIZE)
  398. return -ENOMEM;
  399. if (*ppos != 0) {
  400. /* No partial writes. */
  401. return -EINVAL;
  402. }
  403. page = (char *)get_zeroed_page(GFP_KERNEL);
  404. if (!page)
  405. return -ENOMEM;
  406. length = -EFAULT;
  407. if (copy_from_user(page, buf, count))
  408. goto out;
  409. length = -EINVAL;
  410. if (sscanf(page, "%d", &new_value) != 1)
  411. goto out;
  412. selinux_compat_net = new_value ? 1 : 0;
  413. length = count;
  414. out:
  415. free_page((unsigned long) page);
  416. return length;
  417. }
  418. static const struct file_operations sel_compat_net_ops = {
  419. .read = sel_read_compat_net,
  420. .write = sel_write_compat_net,
  421. };
  422. /*
  423. * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
  424. */
  425. static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
  426. static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
  427. static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
  428. static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
  429. static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
  430. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  431. [SEL_ACCESS] = sel_write_access,
  432. [SEL_CREATE] = sel_write_create,
  433. [SEL_RELABEL] = sel_write_relabel,
  434. [SEL_USER] = sel_write_user,
  435. [SEL_MEMBER] = sel_write_member,
  436. [SEL_CONTEXT] = sel_write_context,
  437. };
  438. static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  439. {
  440. ino_t ino = file->f_path.dentry->d_inode->i_ino;
  441. char *data;
  442. ssize_t rv;
  443. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  444. return -EINVAL;
  445. data = simple_transaction_get(file, buf, size);
  446. if (IS_ERR(data))
  447. return PTR_ERR(data);
  448. rv = write_op[ino](file, data, size);
  449. if (rv > 0) {
  450. simple_transaction_set(file, rv);
  451. rv = size;
  452. }
  453. return rv;
  454. }
  455. static const struct file_operations transaction_ops = {
  456. .write = selinux_transaction_write,
  457. .read = simple_transaction_read,
  458. .release = simple_transaction_release,
  459. };
  460. /*
  461. * payload - write methods
  462. * If the method has a response, the response should be put in buf,
  463. * and the length returned. Otherwise return 0 or and -error.
  464. */
  465. static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
  466. {
  467. char *scon, *tcon;
  468. u32 ssid, tsid;
  469. u16 tclass;
  470. u32 req;
  471. struct av_decision avd;
  472. ssize_t length;
  473. length = task_has_security(current, SECURITY__COMPUTE_AV);
  474. if (length)
  475. return length;
  476. length = -ENOMEM;
  477. scon = kzalloc(size+1, GFP_KERNEL);
  478. if (!scon)
  479. return length;
  480. tcon = kzalloc(size+1, GFP_KERNEL);
  481. if (!tcon)
  482. goto out;
  483. length = -EINVAL;
  484. if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
  485. goto out2;
  486. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  487. if (length < 0)
  488. goto out2;
  489. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  490. if (length < 0)
  491. goto out2;
  492. length = security_compute_av(ssid, tsid, tclass, req, &avd);
  493. if (length < 0)
  494. goto out2;
  495. length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
  496. "%x %x %x %x %u",
  497. avd.allowed, avd.decided,
  498. avd.auditallow, avd.auditdeny,
  499. avd.seqno);
  500. out2:
  501. kfree(tcon);
  502. out:
  503. kfree(scon);
  504. return length;
  505. }
  506. static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
  507. {
  508. char *scon, *tcon;
  509. u32 ssid, tsid, newsid;
  510. u16 tclass;
  511. ssize_t length;
  512. char *newcon;
  513. u32 len;
  514. length = task_has_security(current, SECURITY__COMPUTE_CREATE);
  515. if (length)
  516. return length;
  517. length = -ENOMEM;
  518. scon = kzalloc(size+1, GFP_KERNEL);
  519. if (!scon)
  520. return length;
  521. tcon = kzalloc(size+1, GFP_KERNEL);
  522. if (!tcon)
  523. goto out;
  524. length = -EINVAL;
  525. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  526. goto out2;
  527. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  528. if (length < 0)
  529. goto out2;
  530. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  531. if (length < 0)
  532. goto out2;
  533. length = security_transition_sid(ssid, tsid, tclass, &newsid);
  534. if (length < 0)
  535. goto out2;
  536. length = security_sid_to_context(newsid, &newcon, &len);
  537. if (length < 0)
  538. goto out2;
  539. if (len > SIMPLE_TRANSACTION_LIMIT) {
  540. printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
  541. "payload max\n", __func__, len);
  542. length = -ERANGE;
  543. goto out3;
  544. }
  545. memcpy(buf, newcon, len);
  546. length = len;
  547. out3:
  548. kfree(newcon);
  549. out2:
  550. kfree(tcon);
  551. out:
  552. kfree(scon);
  553. return length;
  554. }
  555. static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
  556. {
  557. char *scon, *tcon;
  558. u32 ssid, tsid, newsid;
  559. u16 tclass;
  560. ssize_t length;
  561. char *newcon;
  562. u32 len;
  563. length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
  564. if (length)
  565. return length;
  566. length = -ENOMEM;
  567. scon = kzalloc(size+1, GFP_KERNEL);
  568. if (!scon)
  569. return length;
  570. tcon = kzalloc(size+1, GFP_KERNEL);
  571. if (!tcon)
  572. goto out;
  573. length = -EINVAL;
  574. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  575. goto out2;
  576. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  577. if (length < 0)
  578. goto out2;
  579. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  580. if (length < 0)
  581. goto out2;
  582. length = security_change_sid(ssid, tsid, tclass, &newsid);
  583. if (length < 0)
  584. goto out2;
  585. length = security_sid_to_context(newsid, &newcon, &len);
  586. if (length < 0)
  587. goto out2;
  588. if (len > SIMPLE_TRANSACTION_LIMIT) {
  589. length = -ERANGE;
  590. goto out3;
  591. }
  592. memcpy(buf, newcon, len);
  593. length = len;
  594. out3:
  595. kfree(newcon);
  596. out2:
  597. kfree(tcon);
  598. out:
  599. kfree(scon);
  600. return length;
  601. }
  602. static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
  603. {
  604. char *con, *user, *ptr;
  605. u32 sid, *sids;
  606. ssize_t length;
  607. char *newcon;
  608. int i, rc;
  609. u32 len, nsids;
  610. length = task_has_security(current, SECURITY__COMPUTE_USER);
  611. if (length)
  612. return length;
  613. length = -ENOMEM;
  614. con = kzalloc(size+1, GFP_KERNEL);
  615. if (!con)
  616. return length;
  617. user = kzalloc(size+1, GFP_KERNEL);
  618. if (!user)
  619. goto out;
  620. length = -EINVAL;
  621. if (sscanf(buf, "%s %s", con, user) != 2)
  622. goto out2;
  623. length = security_context_to_sid(con, strlen(con)+1, &sid);
  624. if (length < 0)
  625. goto out2;
  626. length = security_get_user_sids(sid, user, &sids, &nsids);
  627. if (length < 0)
  628. goto out2;
  629. length = sprintf(buf, "%u", nsids) + 1;
  630. ptr = buf + length;
  631. for (i = 0; i < nsids; i++) {
  632. rc = security_sid_to_context(sids[i], &newcon, &len);
  633. if (rc) {
  634. length = rc;
  635. goto out3;
  636. }
  637. if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
  638. kfree(newcon);
  639. length = -ERANGE;
  640. goto out3;
  641. }
  642. memcpy(ptr, newcon, len);
  643. kfree(newcon);
  644. ptr += len;
  645. length += len;
  646. }
  647. out3:
  648. kfree(sids);
  649. out2:
  650. kfree(user);
  651. out:
  652. kfree(con);
  653. return length;
  654. }
  655. static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
  656. {
  657. char *scon, *tcon;
  658. u32 ssid, tsid, newsid;
  659. u16 tclass;
  660. ssize_t length;
  661. char *newcon;
  662. u32 len;
  663. length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
  664. if (length)
  665. return length;
  666. length = -ENOMEM;
  667. scon = kzalloc(size+1, GFP_KERNEL);
  668. if (!scon)
  669. return length;
  670. tcon = kzalloc(size+1, GFP_KERNEL);
  671. if (!tcon)
  672. goto out;
  673. length = -EINVAL;
  674. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  675. goto out2;
  676. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  677. if (length < 0)
  678. goto out2;
  679. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  680. if (length < 0)
  681. goto out2;
  682. length = security_member_sid(ssid, tsid, tclass, &newsid);
  683. if (length < 0)
  684. goto out2;
  685. length = security_sid_to_context(newsid, &newcon, &len);
  686. if (length < 0)
  687. goto out2;
  688. if (len > SIMPLE_TRANSACTION_LIMIT) {
  689. printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
  690. "payload max\n", __func__, len);
  691. length = -ERANGE;
  692. goto out3;
  693. }
  694. memcpy(buf, newcon, len);
  695. length = len;
  696. out3:
  697. kfree(newcon);
  698. out2:
  699. kfree(tcon);
  700. out:
  701. kfree(scon);
  702. return length;
  703. }
  704. static struct inode *sel_make_inode(struct super_block *sb, int mode)
  705. {
  706. struct inode *ret = new_inode(sb);
  707. if (ret) {
  708. ret->i_mode = mode;
  709. ret->i_uid = ret->i_gid = 0;
  710. ret->i_blocks = 0;
  711. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  712. }
  713. return ret;
  714. }
  715. static ssize_t sel_read_bool(struct file *filep, char __user *buf,
  716. size_t count, loff_t *ppos)
  717. {
  718. char *page = NULL;
  719. ssize_t length;
  720. ssize_t ret;
  721. int cur_enforcing;
  722. struct inode *inode = filep->f_path.dentry->d_inode;
  723. unsigned index = inode->i_ino & SEL_INO_MASK;
  724. const char *name = filep->f_path.dentry->d_name.name;
  725. mutex_lock(&sel_mutex);
  726. if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
  727. ret = -EINVAL;
  728. goto out;
  729. }
  730. if (count > PAGE_SIZE) {
  731. ret = -EINVAL;
  732. goto out;
  733. }
  734. page = (char *)get_zeroed_page(GFP_KERNEL);
  735. if (!page) {
  736. ret = -ENOMEM;
  737. goto out;
  738. }
  739. cur_enforcing = security_get_bool_value(index);
  740. if (cur_enforcing < 0) {
  741. ret = cur_enforcing;
  742. goto out;
  743. }
  744. length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
  745. bool_pending_values[index]);
  746. ret = simple_read_from_buffer(buf, count, ppos, page, length);
  747. out:
  748. mutex_unlock(&sel_mutex);
  749. if (page)
  750. free_page((unsigned long)page);
  751. return ret;
  752. }
  753. static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
  754. size_t count, loff_t *ppos)
  755. {
  756. char *page = NULL;
  757. ssize_t length;
  758. int new_value;
  759. struct inode *inode = filep->f_path.dentry->d_inode;
  760. unsigned index = inode->i_ino & SEL_INO_MASK;
  761. const char *name = filep->f_path.dentry->d_name.name;
  762. mutex_lock(&sel_mutex);
  763. length = task_has_security(current, SECURITY__SETBOOL);
  764. if (length)
  765. goto out;
  766. if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
  767. length = -EINVAL;
  768. goto out;
  769. }
  770. if (count >= PAGE_SIZE) {
  771. length = -ENOMEM;
  772. goto out;
  773. }
  774. if (*ppos != 0) {
  775. /* No partial writes. */
  776. length = -EINVAL;
  777. goto out;
  778. }
  779. page = (char *)get_zeroed_page(GFP_KERNEL);
  780. if (!page) {
  781. length = -ENOMEM;
  782. goto out;
  783. }
  784. length = -EFAULT;
  785. if (copy_from_user(page, buf, count))
  786. goto out;
  787. length = -EINVAL;
  788. if (sscanf(page, "%d", &new_value) != 1)
  789. goto out;
  790. if (new_value)
  791. new_value = 1;
  792. bool_pending_values[index] = new_value;
  793. length = count;
  794. out:
  795. mutex_unlock(&sel_mutex);
  796. if (page)
  797. free_page((unsigned long) page);
  798. return length;
  799. }
  800. static const struct file_operations sel_bool_ops = {
  801. .read = sel_read_bool,
  802. .write = sel_write_bool,
  803. };
  804. static ssize_t sel_commit_bools_write(struct file *filep,
  805. const char __user *buf,
  806. size_t count, loff_t *ppos)
  807. {
  808. char *page = NULL;
  809. ssize_t length;
  810. int new_value;
  811. mutex_lock(&sel_mutex);
  812. length = task_has_security(current, SECURITY__SETBOOL);
  813. if (length)
  814. goto out;
  815. if (count >= PAGE_SIZE) {
  816. length = -ENOMEM;
  817. goto out;
  818. }
  819. if (*ppos != 0) {
  820. /* No partial writes. */
  821. goto out;
  822. }
  823. page = (char *)get_zeroed_page(GFP_KERNEL);
  824. if (!page) {
  825. length = -ENOMEM;
  826. goto out;
  827. }
  828. length = -EFAULT;
  829. if (copy_from_user(page, buf, count))
  830. goto out;
  831. length = -EINVAL;
  832. if (sscanf(page, "%d", &new_value) != 1)
  833. goto out;
  834. if (new_value && bool_pending_values)
  835. security_set_bools(bool_num, bool_pending_values);
  836. length = count;
  837. out:
  838. mutex_unlock(&sel_mutex);
  839. if (page)
  840. free_page((unsigned long) page);
  841. return length;
  842. }
  843. static const struct file_operations sel_commit_bools_ops = {
  844. .write = sel_commit_bools_write,
  845. };
  846. static void sel_remove_entries(struct dentry *de)
  847. {
  848. struct list_head *node;
  849. spin_lock(&dcache_lock);
  850. node = de->d_subdirs.next;
  851. while (node != &de->d_subdirs) {
  852. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  853. list_del_init(node);
  854. if (d->d_inode) {
  855. d = dget_locked(d);
  856. spin_unlock(&dcache_lock);
  857. d_delete(d);
  858. simple_unlink(de->d_inode, d);
  859. dput(d);
  860. spin_lock(&dcache_lock);
  861. }
  862. node = de->d_subdirs.next;
  863. }
  864. spin_unlock(&dcache_lock);
  865. }
  866. #define BOOL_DIR_NAME "booleans"
  867. static int sel_make_bools(void)
  868. {
  869. int i, ret = 0;
  870. ssize_t len;
  871. struct dentry *dentry = NULL;
  872. struct dentry *dir = bool_dir;
  873. struct inode *inode = NULL;
  874. struct inode_security_struct *isec;
  875. char **names = NULL, *page;
  876. int num;
  877. int *values = NULL;
  878. u32 sid;
  879. /* remove any existing files */
  880. kfree(bool_pending_names);
  881. kfree(bool_pending_values);
  882. bool_pending_names = NULL;
  883. bool_pending_values = NULL;
  884. sel_remove_entries(dir);
  885. page = (char *)get_zeroed_page(GFP_KERNEL);
  886. if (!page)
  887. return -ENOMEM;
  888. ret = security_get_bools(&num, &names, &values);
  889. if (ret != 0)
  890. goto out;
  891. for (i = 0; i < num; i++) {
  892. dentry = d_alloc_name(dir, names[i]);
  893. if (!dentry) {
  894. ret = -ENOMEM;
  895. goto err;
  896. }
  897. inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
  898. if (!inode) {
  899. ret = -ENOMEM;
  900. goto err;
  901. }
  902. len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
  903. if (len < 0) {
  904. ret = -EINVAL;
  905. goto err;
  906. } else if (len >= PAGE_SIZE) {
  907. ret = -ENAMETOOLONG;
  908. goto err;
  909. }
  910. isec = (struct inode_security_struct *)inode->i_security;
  911. ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
  912. if (ret)
  913. goto err;
  914. isec->sid = sid;
  915. isec->initialized = 1;
  916. inode->i_fop = &sel_bool_ops;
  917. inode->i_ino = i|SEL_BOOL_INO_OFFSET;
  918. d_add(dentry, inode);
  919. }
  920. bool_num = num;
  921. bool_pending_names = names;
  922. bool_pending_values = values;
  923. out:
  924. free_page((unsigned long)page);
  925. return ret;
  926. err:
  927. if (names) {
  928. for (i = 0; i < num; i++)
  929. kfree(names[i]);
  930. kfree(names);
  931. }
  932. kfree(values);
  933. sel_remove_entries(dir);
  934. ret = -ENOMEM;
  935. goto out;
  936. }
  937. #define NULL_FILE_NAME "null"
  938. struct dentry *selinux_null;
  939. static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
  940. size_t count, loff_t *ppos)
  941. {
  942. char tmpbuf[TMPBUFLEN];
  943. ssize_t length;
  944. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
  945. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  946. }
  947. static ssize_t sel_write_avc_cache_threshold(struct file *file,
  948. const char __user *buf,
  949. size_t count, loff_t *ppos)
  950. {
  951. char *page;
  952. ssize_t ret;
  953. int new_value;
  954. if (count >= PAGE_SIZE) {
  955. ret = -ENOMEM;
  956. goto out;
  957. }
  958. if (*ppos != 0) {
  959. /* No partial writes. */
  960. ret = -EINVAL;
  961. goto out;
  962. }
  963. page = (char *)get_zeroed_page(GFP_KERNEL);
  964. if (!page) {
  965. ret = -ENOMEM;
  966. goto out;
  967. }
  968. if (copy_from_user(page, buf, count)) {
  969. ret = -EFAULT;
  970. goto out_free;
  971. }
  972. if (sscanf(page, "%u", &new_value) != 1) {
  973. ret = -EINVAL;
  974. goto out;
  975. }
  976. if (new_value != avc_cache_threshold) {
  977. ret = task_has_security(current, SECURITY__SETSECPARAM);
  978. if (ret)
  979. goto out_free;
  980. avc_cache_threshold = new_value;
  981. }
  982. ret = count;
  983. out_free:
  984. free_page((unsigned long)page);
  985. out:
  986. return ret;
  987. }
  988. static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
  989. size_t count, loff_t *ppos)
  990. {
  991. char *page;
  992. ssize_t ret = 0;
  993. page = (char *)__get_free_page(GFP_KERNEL);
  994. if (!page) {
  995. ret = -ENOMEM;
  996. goto out;
  997. }
  998. ret = avc_get_hash_stats(page);
  999. if (ret >= 0)
  1000. ret = simple_read_from_buffer(buf, count, ppos, page, ret);
  1001. free_page((unsigned long)page);
  1002. out:
  1003. return ret;
  1004. }
  1005. static const struct file_operations sel_avc_cache_threshold_ops = {
  1006. .read = sel_read_avc_cache_threshold,
  1007. .write = sel_write_avc_cache_threshold,
  1008. };
  1009. static const struct file_operations sel_avc_hash_stats_ops = {
  1010. .read = sel_read_avc_hash_stats,
  1011. };
  1012. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1013. static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
  1014. {
  1015. int cpu;
  1016. for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
  1017. if (!cpu_possible(cpu))
  1018. continue;
  1019. *idx = cpu + 1;
  1020. return &per_cpu(avc_cache_stats, cpu);
  1021. }
  1022. return NULL;
  1023. }
  1024. static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
  1025. {
  1026. loff_t n = *pos - 1;
  1027. if (*pos == 0)
  1028. return SEQ_START_TOKEN;
  1029. return sel_avc_get_stat_idx(&n);
  1030. }
  1031. static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1032. {
  1033. return sel_avc_get_stat_idx(pos);
  1034. }
  1035. static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
  1036. {
  1037. struct avc_cache_stats *st = v;
  1038. if (v == SEQ_START_TOKEN)
  1039. seq_printf(seq, "lookups hits misses allocations reclaims "
  1040. "frees\n");
  1041. else
  1042. seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
  1043. st->hits, st->misses, st->allocations,
  1044. st->reclaims, st->frees);
  1045. return 0;
  1046. }
  1047. static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
  1048. { }
  1049. static const struct seq_operations sel_avc_cache_stats_seq_ops = {
  1050. .start = sel_avc_stats_seq_start,
  1051. .next = sel_avc_stats_seq_next,
  1052. .show = sel_avc_stats_seq_show,
  1053. .stop = sel_avc_stats_seq_stop,
  1054. };
  1055. static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
  1056. {
  1057. return seq_open(file, &sel_avc_cache_stats_seq_ops);
  1058. }
  1059. static const struct file_operations sel_avc_cache_stats_ops = {
  1060. .open = sel_open_avc_cache_stats,
  1061. .read = seq_read,
  1062. .llseek = seq_lseek,
  1063. .release = seq_release,
  1064. };
  1065. #endif
  1066. static int sel_make_avc_files(struct dentry *dir)
  1067. {
  1068. int i, ret = 0;
  1069. static struct tree_descr files[] = {
  1070. { "cache_threshold",
  1071. &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
  1072. { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
  1073. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1074. { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
  1075. #endif
  1076. };
  1077. for (i = 0; i < ARRAY_SIZE(files); i++) {
  1078. struct inode *inode;
  1079. struct dentry *dentry;
  1080. dentry = d_alloc_name(dir, files[i].name);
  1081. if (!dentry) {
  1082. ret = -ENOMEM;
  1083. goto out;
  1084. }
  1085. inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
  1086. if (!inode) {
  1087. ret = -ENOMEM;
  1088. goto out;
  1089. }
  1090. inode->i_fop = files[i].ops;
  1091. inode->i_ino = ++sel_last_ino;
  1092. d_add(dentry, inode);
  1093. }
  1094. out:
  1095. return ret;
  1096. }
  1097. static ssize_t sel_read_initcon(struct file *file, char __user *buf,
  1098. size_t count, loff_t *ppos)
  1099. {
  1100. struct inode *inode;
  1101. char *con;
  1102. u32 sid, len;
  1103. ssize_t ret;
  1104. inode = file->f_path.dentry->d_inode;
  1105. sid = inode->i_ino&SEL_INO_MASK;
  1106. ret = security_sid_to_context(sid, &con, &len);
  1107. if (ret < 0)
  1108. return ret;
  1109. ret = simple_read_from_buffer(buf, count, ppos, con, len);
  1110. kfree(con);
  1111. return ret;
  1112. }
  1113. static const struct file_operations sel_initcon_ops = {
  1114. .read = sel_read_initcon,
  1115. };
  1116. static int sel_make_initcon_files(struct dentry *dir)
  1117. {
  1118. int i, ret = 0;
  1119. for (i = 1; i <= SECINITSID_NUM; i++) {
  1120. struct inode *inode;
  1121. struct dentry *dentry;
  1122. dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
  1123. if (!dentry) {
  1124. ret = -ENOMEM;
  1125. goto out;
  1126. }
  1127. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1128. if (!inode) {
  1129. ret = -ENOMEM;
  1130. goto out;
  1131. }
  1132. inode->i_fop = &sel_initcon_ops;
  1133. inode->i_ino = i|SEL_INITCON_INO_OFFSET;
  1134. d_add(dentry, inode);
  1135. }
  1136. out:
  1137. return ret;
  1138. }
  1139. static inline unsigned int sel_div(unsigned long a, unsigned long b)
  1140. {
  1141. return a / b - (a % b < 0);
  1142. }
  1143. static inline unsigned long sel_class_to_ino(u16 class)
  1144. {
  1145. return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
  1146. }
  1147. static inline u16 sel_ino_to_class(unsigned long ino)
  1148. {
  1149. return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
  1150. }
  1151. static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
  1152. {
  1153. return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
  1154. }
  1155. static inline u32 sel_ino_to_perm(unsigned long ino)
  1156. {
  1157. return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
  1158. }
  1159. static ssize_t sel_read_class(struct file *file, char __user *buf,
  1160. size_t count, loff_t *ppos)
  1161. {
  1162. ssize_t rc, len;
  1163. char *page;
  1164. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1165. page = (char *)__get_free_page(GFP_KERNEL);
  1166. if (!page) {
  1167. rc = -ENOMEM;
  1168. goto out;
  1169. }
  1170. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
  1171. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1172. free_page((unsigned long)page);
  1173. out:
  1174. return rc;
  1175. }
  1176. static const struct file_operations sel_class_ops = {
  1177. .read = sel_read_class,
  1178. };
  1179. static ssize_t sel_read_perm(struct file *file, char __user *buf,
  1180. size_t count, loff_t *ppos)
  1181. {
  1182. ssize_t rc, len;
  1183. char *page;
  1184. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1185. page = (char *)__get_free_page(GFP_KERNEL);
  1186. if (!page) {
  1187. rc = -ENOMEM;
  1188. goto out;
  1189. }
  1190. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
  1191. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1192. free_page((unsigned long)page);
  1193. out:
  1194. return rc;
  1195. }
  1196. static const struct file_operations sel_perm_ops = {
  1197. .read = sel_read_perm,
  1198. };
  1199. static ssize_t sel_read_policycap(struct file *file, char __user *buf,
  1200. size_t count, loff_t *ppos)
  1201. {
  1202. int value;
  1203. char tmpbuf[TMPBUFLEN];
  1204. ssize_t length;
  1205. unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
  1206. value = security_policycap_supported(i_ino & SEL_INO_MASK);
  1207. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
  1208. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1209. }
  1210. static const struct file_operations sel_policycap_ops = {
  1211. .read = sel_read_policycap,
  1212. };
  1213. static int sel_make_perm_files(char *objclass, int classvalue,
  1214. struct dentry *dir)
  1215. {
  1216. int i, rc = 0, nperms;
  1217. char **perms;
  1218. rc = security_get_permissions(objclass, &perms, &nperms);
  1219. if (rc)
  1220. goto out;
  1221. for (i = 0; i < nperms; i++) {
  1222. struct inode *inode;
  1223. struct dentry *dentry;
  1224. dentry = d_alloc_name(dir, perms[i]);
  1225. if (!dentry) {
  1226. rc = -ENOMEM;
  1227. goto out1;
  1228. }
  1229. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1230. if (!inode) {
  1231. rc = -ENOMEM;
  1232. goto out1;
  1233. }
  1234. inode->i_fop = &sel_perm_ops;
  1235. /* i+1 since perm values are 1-indexed */
  1236. inode->i_ino = sel_perm_to_ino(classvalue, i+1);
  1237. d_add(dentry, inode);
  1238. }
  1239. out1:
  1240. for (i = 0; i < nperms; i++)
  1241. kfree(perms[i]);
  1242. kfree(perms);
  1243. out:
  1244. return rc;
  1245. }
  1246. static int sel_make_class_dir_entries(char *classname, int index,
  1247. struct dentry *dir)
  1248. {
  1249. struct dentry *dentry = NULL;
  1250. struct inode *inode = NULL;
  1251. int rc;
  1252. dentry = d_alloc_name(dir, "index");
  1253. if (!dentry) {
  1254. rc = -ENOMEM;
  1255. goto out;
  1256. }
  1257. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1258. if (!inode) {
  1259. rc = -ENOMEM;
  1260. goto out;
  1261. }
  1262. inode->i_fop = &sel_class_ops;
  1263. inode->i_ino = sel_class_to_ino(index);
  1264. d_add(dentry, inode);
  1265. dentry = d_alloc_name(dir, "perms");
  1266. if (!dentry) {
  1267. rc = -ENOMEM;
  1268. goto out;
  1269. }
  1270. rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
  1271. if (rc)
  1272. goto out;
  1273. rc = sel_make_perm_files(classname, index, dentry);
  1274. out:
  1275. return rc;
  1276. }
  1277. static void sel_remove_classes(void)
  1278. {
  1279. struct list_head *class_node;
  1280. list_for_each(class_node, &class_dir->d_subdirs) {
  1281. struct dentry *class_subdir = list_entry(class_node,
  1282. struct dentry, d_u.d_child);
  1283. struct list_head *class_subdir_node;
  1284. list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
  1285. struct dentry *d = list_entry(class_subdir_node,
  1286. struct dentry, d_u.d_child);
  1287. if (d->d_inode)
  1288. if (d->d_inode->i_mode & S_IFDIR)
  1289. sel_remove_entries(d);
  1290. }
  1291. sel_remove_entries(class_subdir);
  1292. }
  1293. sel_remove_entries(class_dir);
  1294. }
  1295. static int sel_make_classes(void)
  1296. {
  1297. int rc = 0, nclasses, i;
  1298. char **classes;
  1299. /* delete any existing entries */
  1300. sel_remove_classes();
  1301. rc = security_get_classes(&classes, &nclasses);
  1302. if (rc < 0)
  1303. goto out;
  1304. /* +2 since classes are 1-indexed */
  1305. last_class_ino = sel_class_to_ino(nclasses+2);
  1306. for (i = 0; i < nclasses; i++) {
  1307. struct dentry *class_name_dir;
  1308. class_name_dir = d_alloc_name(class_dir, classes[i]);
  1309. if (!class_name_dir) {
  1310. rc = -ENOMEM;
  1311. goto out1;
  1312. }
  1313. rc = sel_make_dir(class_dir->d_inode, class_name_dir,
  1314. &last_class_ino);
  1315. if (rc)
  1316. goto out1;
  1317. /* i+1 since class values are 1-indexed */
  1318. rc = sel_make_class_dir_entries(classes[i], i+1,
  1319. class_name_dir);
  1320. if (rc)
  1321. goto out1;
  1322. }
  1323. out1:
  1324. for (i = 0; i < nclasses; i++)
  1325. kfree(classes[i]);
  1326. kfree(classes);
  1327. out:
  1328. return rc;
  1329. }
  1330. static int sel_make_policycap(void)
  1331. {
  1332. unsigned int iter;
  1333. struct dentry *dentry = NULL;
  1334. struct inode *inode = NULL;
  1335. sel_remove_entries(policycap_dir);
  1336. for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
  1337. if (iter < ARRAY_SIZE(policycap_names))
  1338. dentry = d_alloc_name(policycap_dir,
  1339. policycap_names[iter]);
  1340. else
  1341. dentry = d_alloc_name(policycap_dir, "unknown");
  1342. if (dentry == NULL)
  1343. return -ENOMEM;
  1344. inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
  1345. if (inode == NULL)
  1346. return -ENOMEM;
  1347. inode->i_fop = &sel_policycap_ops;
  1348. inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
  1349. d_add(dentry, inode);
  1350. }
  1351. return 0;
  1352. }
  1353. static int sel_make_dir(struct inode *dir, struct dentry *dentry,
  1354. unsigned long *ino)
  1355. {
  1356. int ret = 0;
  1357. struct inode *inode;
  1358. inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1359. if (!inode) {
  1360. ret = -ENOMEM;
  1361. goto out;
  1362. }
  1363. inode->i_op = &simple_dir_inode_operations;
  1364. inode->i_fop = &simple_dir_operations;
  1365. inode->i_ino = ++(*ino);
  1366. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  1367. inc_nlink(inode);
  1368. d_add(dentry, inode);
  1369. /* bump link count on parent directory, too */
  1370. inc_nlink(dir);
  1371. out:
  1372. return ret;
  1373. }
  1374. static int sel_fill_super(struct super_block *sb, void *data, int silent)
  1375. {
  1376. int ret;
  1377. struct dentry *dentry;
  1378. struct inode *inode, *root_inode;
  1379. struct inode_security_struct *isec;
  1380. static struct tree_descr selinux_files[] = {
  1381. [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
  1382. [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
  1383. [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
  1384. [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
  1385. [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
  1386. [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
  1387. [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
  1388. [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
  1389. [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
  1390. [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
  1391. [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
  1392. [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
  1393. [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
  1394. [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
  1395. [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
  1396. [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
  1397. /* last one */ {""}
  1398. };
  1399. ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
  1400. if (ret)
  1401. goto err;
  1402. root_inode = sb->s_root->d_inode;
  1403. dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
  1404. if (!dentry) {
  1405. ret = -ENOMEM;
  1406. goto err;
  1407. }
  1408. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1409. if (ret)
  1410. goto err;
  1411. bool_dir = dentry;
  1412. dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
  1413. if (!dentry) {
  1414. ret = -ENOMEM;
  1415. goto err;
  1416. }
  1417. inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
  1418. if (!inode) {
  1419. ret = -ENOMEM;
  1420. goto err;
  1421. }
  1422. inode->i_ino = ++sel_last_ino;
  1423. isec = (struct inode_security_struct *)inode->i_security;
  1424. isec->sid = SECINITSID_DEVNULL;
  1425. isec->sclass = SECCLASS_CHR_FILE;
  1426. isec->initialized = 1;
  1427. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
  1428. d_add(dentry, inode);
  1429. selinux_null = dentry;
  1430. dentry = d_alloc_name(sb->s_root, "avc");
  1431. if (!dentry) {
  1432. ret = -ENOMEM;
  1433. goto err;
  1434. }
  1435. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1436. if (ret)
  1437. goto err;
  1438. ret = sel_make_avc_files(dentry);
  1439. if (ret)
  1440. goto err;
  1441. dentry = d_alloc_name(sb->s_root, "initial_contexts");
  1442. if (!dentry) {
  1443. ret = -ENOMEM;
  1444. goto err;
  1445. }
  1446. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1447. if (ret)
  1448. goto err;
  1449. ret = sel_make_initcon_files(dentry);
  1450. if (ret)
  1451. goto err;
  1452. dentry = d_alloc_name(sb->s_root, "class");
  1453. if (!dentry) {
  1454. ret = -ENOMEM;
  1455. goto err;
  1456. }
  1457. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1458. if (ret)
  1459. goto err;
  1460. class_dir = dentry;
  1461. dentry = d_alloc_name(sb->s_root, "policy_capabilities");
  1462. if (!dentry) {
  1463. ret = -ENOMEM;
  1464. goto err;
  1465. }
  1466. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1467. if (ret)
  1468. goto err;
  1469. policycap_dir = dentry;
  1470. out:
  1471. return ret;
  1472. err:
  1473. printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
  1474. __func__);
  1475. goto out;
  1476. }
  1477. static int sel_get_sb(struct file_system_type *fs_type,
  1478. int flags, const char *dev_name, void *data,
  1479. struct vfsmount *mnt)
  1480. {
  1481. return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
  1482. }
  1483. static struct file_system_type sel_fs_type = {
  1484. .name = "selinuxfs",
  1485. .get_sb = sel_get_sb,
  1486. .kill_sb = kill_litter_super,
  1487. };
  1488. struct vfsmount *selinuxfs_mount;
  1489. static int __init init_sel_fs(void)
  1490. {
  1491. int err;
  1492. if (!selinux_enabled)
  1493. return 0;
  1494. err = register_filesystem(&sel_fs_type);
  1495. if (!err) {
  1496. selinuxfs_mount = kern_mount(&sel_fs_type);
  1497. if (IS_ERR(selinuxfs_mount)) {
  1498. printk(KERN_ERR "selinuxfs: could not mount!\n");
  1499. err = PTR_ERR(selinuxfs_mount);
  1500. selinuxfs_mount = NULL;
  1501. }
  1502. }
  1503. return err;
  1504. }
  1505. __initcall(init_sel_fs);
  1506. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  1507. void exit_sel_fs(void)
  1508. {
  1509. unregister_filesystem(&sel_fs_type);
  1510. }
  1511. #endif