selinuxfs.c 44 KB

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