selinuxfs.c 43 KB

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