selinuxfs.c 43 KB

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