selinuxfs.c 43 KB

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