selinuxfs.c 43 KB

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