selinuxfs.c 50 KB

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