selinuxfs.c 48 KB

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