selinuxfs.c 43 KB

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