selinuxfs.c 39 KB

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