selinuxfs.c 44 KB

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