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