selinuxfs.c 54 KB

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