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