selinuxfs.c 43 KB

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