selinuxfs.c 53 KB

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