selinuxfs.c 42 KB

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