selinuxfs.c 48 KB

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