selinuxfs.c 50 KB

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