selinuxfs.c 44 KB

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