selinuxfs.c 48 KB

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