selinuxfs.c 44 KB

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