selinuxfs.c 44 KB

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