selinuxfs.c 42 KB

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