selinuxfs.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. /* Updated: Karl MacMillan <kmacmillan@tresys.com>
  2. *
  3. * Added conditional policy language extensions
  4. *
  5. * Updated: Hewlett-Packard <paul.moore@hp.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. /* selinuxfs pseudo filesystem for exporting the security policy API.
  31. Based on the proc code and the fs/nfsd/nfsctl.c code. */
  32. #include "flask.h"
  33. #include "avc.h"
  34. #include "avc_ss.h"
  35. #include "security.h"
  36. #include "objsec.h"
  37. #include "conditional.h"
  38. /* Policy capability filenames */
  39. static char *policycap_names[] = {
  40. "network_peer_controls",
  41. "open_perms"
  42. };
  43. unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
  44. static int __init checkreqprot_setup(char *str)
  45. {
  46. unsigned long checkreqprot;
  47. if (!strict_strtoul(str, 0, &checkreqprot))
  48. selinux_checkreqprot = checkreqprot ? 1 : 0;
  49. return 1;
  50. }
  51. __setup("checkreqprot=", checkreqprot_setup);
  52. static DEFINE_MUTEX(sel_mutex);
  53. /* global data for booleans */
  54. static struct dentry *bool_dir;
  55. static int bool_num;
  56. static char **bool_pending_names;
  57. static int *bool_pending_values;
  58. /* global data for classes */
  59. static struct dentry *class_dir;
  60. static unsigned long last_class_ino;
  61. static char policy_opened;
  62. /* global data for policy capabilities */
  63. static struct dentry *policycap_dir;
  64. extern void selnl_notify_setenforce(int val);
  65. /* Check whether a task is allowed to use a security operation. */
  66. static int task_has_security(struct task_struct *tsk,
  67. u32 perms)
  68. {
  69. const struct task_security_struct *tsec;
  70. u32 sid = 0;
  71. rcu_read_lock();
  72. tsec = __task_cred(tsk)->security;
  73. if (tsec)
  74. sid = tsec->sid;
  75. rcu_read_unlock();
  76. if (!tsec)
  77. return -EACCES;
  78. return avc_has_perm(sid, SECINITSID_SECURITY,
  79. SECCLASS_SECURITY, perms, NULL);
  80. }
  81. enum sel_inos {
  82. SEL_ROOT_INO = 2,
  83. SEL_LOAD, /* load policy */
  84. SEL_ENFORCE, /* get or set enforcing status */
  85. SEL_CONTEXT, /* validate context */
  86. SEL_ACCESS, /* compute access decision */
  87. SEL_CREATE, /* compute create labeling decision */
  88. SEL_RELABEL, /* compute relabeling decision */
  89. SEL_USER, /* compute reachable user contexts */
  90. SEL_POLICYVERS, /* return policy version for this kernel */
  91. SEL_COMMIT_BOOLS, /* commit new boolean values */
  92. SEL_MLS, /* return if MLS policy is enabled */
  93. SEL_DISABLE, /* disable SELinux until next reboot */
  94. SEL_MEMBER, /* compute polyinstantiation membership decision */
  95. SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
  96. SEL_COMPAT_NET, /* whether to use old compat network packet controls */
  97. SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
  98. SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
  99. SEL_STATUS, /* export current status using mmap() */
  100. SEL_POLICY, /* allow userspace to read the in kernel policy */
  101. SEL_INO_NEXT, /* The next inode number to use */
  102. };
  103. static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
  104. #define SEL_INITCON_INO_OFFSET 0x01000000
  105. #define SEL_BOOL_INO_OFFSET 0x02000000
  106. #define SEL_CLASS_INO_OFFSET 0x04000000
  107. #define SEL_POLICYCAP_INO_OFFSET 0x08000000
  108. #define SEL_INO_MASK 0x00ffffff
  109. #define TMPBUFLEN 12
  110. static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
  111. size_t count, loff_t *ppos)
  112. {
  113. char tmpbuf[TMPBUFLEN];
  114. ssize_t length;
  115. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
  116. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  117. }
  118. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  119. static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
  120. size_t count, loff_t *ppos)
  121. {
  122. char *page = NULL;
  123. ssize_t length;
  124. int new_value;
  125. length = -ENOMEM;
  126. if (count >= PAGE_SIZE)
  127. goto out;
  128. /* No partial writes. */
  129. length = EINVAL;
  130. if (*ppos != 0)
  131. goto out;
  132. length = -ENOMEM;
  133. page = (char *)get_zeroed_page(GFP_KERNEL);
  134. if (!page)
  135. goto out;
  136. length = -EFAULT;
  137. if (copy_from_user(page, buf, count))
  138. goto out;
  139. length = -EINVAL;
  140. if (sscanf(page, "%d", &new_value) != 1)
  141. goto out;
  142. if (new_value != selinux_enforcing) {
  143. length = task_has_security(current, SECURITY__SETENFORCE);
  144. if (length)
  145. goto out;
  146. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
  147. "enforcing=%d old_enforcing=%d auid=%u ses=%u",
  148. new_value, selinux_enforcing,
  149. audit_get_loginuid(current),
  150. audit_get_sessionid(current));
  151. selinux_enforcing = new_value;
  152. if (selinux_enforcing)
  153. avc_ss_reset(0);
  154. selnl_notify_setenforce(selinux_enforcing);
  155. selinux_status_update_setenforce(selinux_enforcing);
  156. }
  157. length = count;
  158. out:
  159. free_page((unsigned long) page);
  160. return length;
  161. }
  162. #else
  163. #define sel_write_enforce NULL
  164. #endif
  165. static const struct file_operations sel_enforce_ops = {
  166. .read = sel_read_enforce,
  167. .write = sel_write_enforce,
  168. .llseek = generic_file_llseek,
  169. };
  170. static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
  171. size_t count, loff_t *ppos)
  172. {
  173. char tmpbuf[TMPBUFLEN];
  174. ssize_t length;
  175. ino_t ino = filp->f_path.dentry->d_inode->i_ino;
  176. int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
  177. security_get_reject_unknown() : !security_get_allow_unknown();
  178. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
  179. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  180. }
  181. static const struct file_operations sel_handle_unknown_ops = {
  182. .read = sel_read_handle_unknown,
  183. .llseek = generic_file_llseek,
  184. };
  185. static int sel_open_handle_status(struct inode *inode, struct file *filp)
  186. {
  187. struct page *status = selinux_kernel_status_page();
  188. if (!status)
  189. return -ENOMEM;
  190. filp->private_data = status;
  191. return 0;
  192. }
  193. static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
  194. size_t count, loff_t *ppos)
  195. {
  196. struct page *status = filp->private_data;
  197. BUG_ON(!status);
  198. return simple_read_from_buffer(buf, count, ppos,
  199. page_address(status),
  200. sizeof(struct selinux_kernel_status));
  201. }
  202. static int sel_mmap_handle_status(struct file *filp,
  203. struct vm_area_struct *vma)
  204. {
  205. struct page *status = filp->private_data;
  206. unsigned long size = vma->vm_end - vma->vm_start;
  207. BUG_ON(!status);
  208. /* only allows one page from the head */
  209. if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
  210. return -EIO;
  211. /* disallow writable mapping */
  212. if (vma->vm_flags & VM_WRITE)
  213. return -EPERM;
  214. /* disallow mprotect() turns it into writable */
  215. vma->vm_flags &= ~VM_MAYWRITE;
  216. return remap_pfn_range(vma, vma->vm_start,
  217. page_to_pfn(status),
  218. size, vma->vm_page_prot);
  219. }
  220. static const struct file_operations sel_handle_status_ops = {
  221. .open = sel_open_handle_status,
  222. .read = sel_read_handle_status,
  223. .mmap = sel_mmap_handle_status,
  224. .llseek = generic_file_llseek,
  225. };
  226. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  227. static ssize_t sel_write_disable(struct file *file, const char __user *buf,
  228. size_t count, loff_t *ppos)
  229. {
  230. char *page = NULL;
  231. ssize_t length;
  232. int new_value;
  233. extern int selinux_disable(void);
  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 int sel_make_dir(struct inode *dir, struct dentry *dentry,
  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. 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. u32 ssid, tsid, newsid;
  611. u16 tclass;
  612. ssize_t length;
  613. char *newcon = NULL;
  614. u32 len;
  615. length = task_has_security(current, SECURITY__COMPUTE_CREATE);
  616. if (length)
  617. goto out;
  618. length = -ENOMEM;
  619. scon = kzalloc(size + 1, GFP_KERNEL);
  620. if (!scon)
  621. goto out;
  622. length = -ENOMEM;
  623. tcon = kzalloc(size + 1, GFP_KERNEL);
  624. if (!tcon)
  625. goto out;
  626. length = -EINVAL;
  627. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  628. goto out;
  629. length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
  630. if (length)
  631. goto out;
  632. length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
  633. if (length)
  634. goto out;
  635. length = security_transition_sid_user(ssid, tsid, tclass, &newsid);
  636. if (length)
  637. goto out;
  638. length = security_sid_to_context(newsid, &newcon, &len);
  639. if (length)
  640. goto out;
  641. length = -ERANGE;
  642. if (len > SIMPLE_TRANSACTION_LIMIT) {
  643. printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
  644. "payload max\n", __func__, len);
  645. goto out;
  646. }
  647. memcpy(buf, newcon, len);
  648. length = len;
  649. out:
  650. kfree(newcon);
  651. kfree(tcon);
  652. kfree(scon);
  653. return length;
  654. }
  655. static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
  656. {
  657. char *scon = NULL, *tcon = NULL;
  658. u32 ssid, tsid, newsid;
  659. u16 tclass;
  660. ssize_t length;
  661. char *newcon = NULL;
  662. u32 len;
  663. length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
  664. if (length)
  665. goto out;
  666. length = -ENOMEM;
  667. scon = kzalloc(size + 1, GFP_KERNEL);
  668. if (!scon)
  669. goto out;
  670. length = -ENOMEM;
  671. tcon = kzalloc(size + 1, GFP_KERNEL);
  672. if (!tcon)
  673. goto out;
  674. length = -EINVAL;
  675. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  676. goto out;
  677. length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
  678. if (length)
  679. goto out;
  680. length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
  681. if (length)
  682. goto out;
  683. length = security_change_sid(ssid, tsid, tclass, &newsid);
  684. if (length)
  685. goto out;
  686. length = security_sid_to_context(newsid, &newcon, &len);
  687. if (length)
  688. goto out;
  689. length = -ERANGE;
  690. if (len > SIMPLE_TRANSACTION_LIMIT)
  691. goto out;
  692. memcpy(buf, newcon, len);
  693. length = len;
  694. out:
  695. kfree(newcon);
  696. kfree(tcon);
  697. kfree(scon);
  698. return length;
  699. }
  700. static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
  701. {
  702. char *con = NULL, *user = NULL, *ptr;
  703. u32 sid, *sids = NULL;
  704. ssize_t length;
  705. char *newcon;
  706. int i, rc;
  707. u32 len, nsids;
  708. length = task_has_security(current, SECURITY__COMPUTE_USER);
  709. if (length)
  710. goto out;
  711. length = -ENOMEM;
  712. con = kzalloc(size + 1, GFP_KERNEL);
  713. if (!con)
  714. goto out;
  715. length = -ENOMEM;
  716. user = kzalloc(size + 1, GFP_KERNEL);
  717. if (!user)
  718. goto out;
  719. length = -EINVAL;
  720. if (sscanf(buf, "%s %s", con, user) != 2)
  721. goto out;
  722. length = security_context_to_sid(con, strlen(con) + 1, &sid);
  723. if (length)
  724. goto out;
  725. length = security_get_user_sids(sid, user, &sids, &nsids);
  726. if (length)
  727. goto out;
  728. length = sprintf(buf, "%u", nsids) + 1;
  729. ptr = buf + length;
  730. for (i = 0; i < nsids; i++) {
  731. rc = security_sid_to_context(sids[i], &newcon, &len);
  732. if (rc) {
  733. length = rc;
  734. goto out;
  735. }
  736. if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
  737. kfree(newcon);
  738. length = -ERANGE;
  739. goto out;
  740. }
  741. memcpy(ptr, newcon, len);
  742. kfree(newcon);
  743. ptr += len;
  744. length += len;
  745. }
  746. out:
  747. kfree(sids);
  748. kfree(user);
  749. kfree(con);
  750. return length;
  751. }
  752. static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
  753. {
  754. char *scon = NULL, *tcon = NULL;
  755. u32 ssid, tsid, newsid;
  756. u16 tclass;
  757. ssize_t length;
  758. char *newcon = NULL;
  759. u32 len;
  760. length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
  761. if (length)
  762. goto out;
  763. length = -ENOMEM;
  764. scon = kzalloc(size + 1, GFP_KERNEL);
  765. if (!scon)
  766. goto out;
  767. length = -ENOMEM;
  768. tcon = kzalloc(size + 1, GFP_KERNEL);
  769. if (!tcon)
  770. goto out;
  771. length = -EINVAL;
  772. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  773. goto out;
  774. length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
  775. if (length)
  776. goto out;
  777. length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
  778. if (length)
  779. goto out;
  780. length = security_member_sid(ssid, tsid, tclass, &newsid);
  781. if (length)
  782. goto out;
  783. length = security_sid_to_context(newsid, &newcon, &len);
  784. if (length)
  785. goto out;
  786. length = -ERANGE;
  787. if (len > SIMPLE_TRANSACTION_LIMIT) {
  788. printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
  789. "payload max\n", __func__, len);
  790. goto out;
  791. }
  792. memcpy(buf, newcon, len);
  793. length = len;
  794. out:
  795. kfree(newcon);
  796. kfree(tcon);
  797. kfree(scon);
  798. return length;
  799. }
  800. static struct inode *sel_make_inode(struct super_block *sb, int mode)
  801. {
  802. struct inode *ret = new_inode(sb);
  803. if (ret) {
  804. ret->i_mode = mode;
  805. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  806. }
  807. return ret;
  808. }
  809. static ssize_t sel_read_bool(struct file *filep, char __user *buf,
  810. size_t count, loff_t *ppos)
  811. {
  812. char *page = NULL;
  813. ssize_t length;
  814. ssize_t ret;
  815. int cur_enforcing;
  816. struct inode *inode = filep->f_path.dentry->d_inode;
  817. unsigned index = inode->i_ino & SEL_INO_MASK;
  818. const char *name = filep->f_path.dentry->d_name.name;
  819. mutex_lock(&sel_mutex);
  820. ret = -EINVAL;
  821. if (index >= bool_num || strcmp(name, bool_pending_names[index]))
  822. goto out;
  823. ret = -ENOMEM;
  824. page = (char *)get_zeroed_page(GFP_KERNEL);
  825. if (!page)
  826. goto out;
  827. cur_enforcing = security_get_bool_value(index);
  828. if (cur_enforcing < 0) {
  829. ret = cur_enforcing;
  830. goto out;
  831. }
  832. length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
  833. bool_pending_values[index]);
  834. ret = simple_read_from_buffer(buf, count, ppos, page, length);
  835. out:
  836. mutex_unlock(&sel_mutex);
  837. free_page((unsigned long)page);
  838. return ret;
  839. }
  840. static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
  841. size_t count, loff_t *ppos)
  842. {
  843. char *page = NULL;
  844. ssize_t length;
  845. int new_value;
  846. struct inode *inode = filep->f_path.dentry->d_inode;
  847. unsigned index = inode->i_ino & SEL_INO_MASK;
  848. const char *name = filep->f_path.dentry->d_name.name;
  849. mutex_lock(&sel_mutex);
  850. length = task_has_security(current, SECURITY__SETBOOL);
  851. if (length)
  852. goto out;
  853. length = -EINVAL;
  854. if (index >= bool_num || strcmp(name, bool_pending_names[index]))
  855. goto out;
  856. length = -ENOMEM;
  857. if (count >= PAGE_SIZE)
  858. goto out;
  859. /* No partial writes. */
  860. length = -EINVAL;
  861. if (*ppos != 0)
  862. goto out;
  863. length = -ENOMEM;
  864. page = (char *)get_zeroed_page(GFP_KERNEL);
  865. if (!page)
  866. goto out;
  867. length = -EFAULT;
  868. if (copy_from_user(page, buf, count))
  869. goto out;
  870. length = -EINVAL;
  871. if (sscanf(page, "%d", &new_value) != 1)
  872. goto out;
  873. if (new_value)
  874. new_value = 1;
  875. bool_pending_values[index] = new_value;
  876. length = count;
  877. out:
  878. mutex_unlock(&sel_mutex);
  879. free_page((unsigned long) page);
  880. return length;
  881. }
  882. static const struct file_operations sel_bool_ops = {
  883. .read = sel_read_bool,
  884. .write = sel_write_bool,
  885. .llseek = generic_file_llseek,
  886. };
  887. static ssize_t sel_commit_bools_write(struct file *filep,
  888. const char __user *buf,
  889. size_t count, loff_t *ppos)
  890. {
  891. char *page = NULL;
  892. ssize_t length;
  893. int new_value;
  894. mutex_lock(&sel_mutex);
  895. length = task_has_security(current, SECURITY__SETBOOL);
  896. if (length)
  897. goto out;
  898. length = -ENOMEM;
  899. if (count >= PAGE_SIZE)
  900. goto out;
  901. /* No partial writes. */
  902. length = -EINVAL;
  903. if (*ppos != 0)
  904. goto out;
  905. length = -ENOMEM;
  906. page = (char *)get_zeroed_page(GFP_KERNEL);
  907. if (!page)
  908. goto out;
  909. length = -EFAULT;
  910. if (copy_from_user(page, buf, count))
  911. goto out;
  912. length = -EINVAL;
  913. if (sscanf(page, "%d", &new_value) != 1)
  914. goto out;
  915. length = 0;
  916. if (new_value && bool_pending_values)
  917. length = security_set_bools(bool_num, bool_pending_values);
  918. if (!length)
  919. length = count;
  920. out:
  921. mutex_unlock(&sel_mutex);
  922. free_page((unsigned long) page);
  923. return length;
  924. }
  925. static const struct file_operations sel_commit_bools_ops = {
  926. .write = sel_commit_bools_write,
  927. .llseek = generic_file_llseek,
  928. };
  929. static void sel_remove_entries(struct dentry *de)
  930. {
  931. struct list_head *node;
  932. spin_lock(&de->d_lock);
  933. node = de->d_subdirs.next;
  934. while (node != &de->d_subdirs) {
  935. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  936. spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  937. list_del_init(node);
  938. if (d->d_inode) {
  939. dget_dlock(d);
  940. spin_unlock(&de->d_lock);
  941. spin_unlock(&d->d_lock);
  942. d_delete(d);
  943. simple_unlink(de->d_inode, d);
  944. dput(d);
  945. spin_lock(&de->d_lock);
  946. } else
  947. spin_unlock(&d->d_lock);
  948. node = de->d_subdirs.next;
  949. }
  950. spin_unlock(&de->d_lock);
  951. }
  952. #define BOOL_DIR_NAME "booleans"
  953. static int sel_make_bools(void)
  954. {
  955. int i, ret;
  956. ssize_t len;
  957. struct dentry *dentry = NULL;
  958. struct dentry *dir = bool_dir;
  959. struct inode *inode = NULL;
  960. struct inode_security_struct *isec;
  961. char **names = NULL, *page;
  962. int num;
  963. int *values = NULL;
  964. u32 sid;
  965. /* remove any existing files */
  966. for (i = 0; i < bool_num; i++)
  967. kfree(bool_pending_names[i]);
  968. kfree(bool_pending_names);
  969. kfree(bool_pending_values);
  970. bool_pending_names = NULL;
  971. bool_pending_values = NULL;
  972. sel_remove_entries(dir);
  973. ret = -ENOMEM;
  974. page = (char *)get_zeroed_page(GFP_KERNEL);
  975. if (!page)
  976. goto out;
  977. ret = security_get_bools(&num, &names, &values);
  978. if (ret)
  979. goto out;
  980. for (i = 0; i < num; i++) {
  981. ret = -ENOMEM;
  982. dentry = d_alloc_name(dir, names[i]);
  983. if (!dentry)
  984. goto out;
  985. ret = -ENOMEM;
  986. inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
  987. if (!inode)
  988. goto out;
  989. ret = -EINVAL;
  990. len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
  991. if (len < 0)
  992. goto out;
  993. ret = -ENAMETOOLONG;
  994. if (len >= PAGE_SIZE)
  995. goto out;
  996. isec = (struct inode_security_struct *)inode->i_security;
  997. ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
  998. if (ret)
  999. goto out;
  1000. isec->sid = sid;
  1001. isec->initialized = 1;
  1002. inode->i_fop = &sel_bool_ops;
  1003. inode->i_ino = i|SEL_BOOL_INO_OFFSET;
  1004. d_add(dentry, inode);
  1005. }
  1006. bool_num = num;
  1007. bool_pending_names = names;
  1008. bool_pending_values = values;
  1009. free_page((unsigned long)page);
  1010. return 0;
  1011. out:
  1012. free_page((unsigned long)page);
  1013. if (names) {
  1014. for (i = 0; i < num; i++)
  1015. kfree(names[i]);
  1016. kfree(names);
  1017. }
  1018. kfree(values);
  1019. sel_remove_entries(dir);
  1020. return ret;
  1021. }
  1022. #define NULL_FILE_NAME "null"
  1023. struct dentry *selinux_null;
  1024. static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
  1025. size_t count, loff_t *ppos)
  1026. {
  1027. char tmpbuf[TMPBUFLEN];
  1028. ssize_t length;
  1029. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
  1030. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1031. }
  1032. static ssize_t sel_write_avc_cache_threshold(struct file *file,
  1033. const char __user *buf,
  1034. size_t count, loff_t *ppos)
  1035. {
  1036. char *page = NULL;
  1037. ssize_t ret;
  1038. int new_value;
  1039. ret = task_has_security(current, SECURITY__SETSECPARAM);
  1040. if (ret)
  1041. goto out;
  1042. ret = -ENOMEM;
  1043. if (count >= PAGE_SIZE)
  1044. goto out;
  1045. /* No partial writes. */
  1046. ret = -EINVAL;
  1047. if (*ppos != 0)
  1048. goto out;
  1049. ret = -ENOMEM;
  1050. page = (char *)get_zeroed_page(GFP_KERNEL);
  1051. if (!page)
  1052. goto out;
  1053. ret = -EFAULT;
  1054. if (copy_from_user(page, buf, count))
  1055. goto out;
  1056. ret = -EINVAL;
  1057. if (sscanf(page, "%u", &new_value) != 1)
  1058. goto out;
  1059. avc_cache_threshold = new_value;
  1060. ret = count;
  1061. out:
  1062. free_page((unsigned long)page);
  1063. return ret;
  1064. }
  1065. static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
  1066. size_t count, loff_t *ppos)
  1067. {
  1068. char *page;
  1069. ssize_t length;
  1070. page = (char *)__get_free_page(GFP_KERNEL);
  1071. if (!page)
  1072. return -ENOMEM;
  1073. length = avc_get_hash_stats(page);
  1074. if (length >= 0)
  1075. length = simple_read_from_buffer(buf, count, ppos, page, length);
  1076. free_page((unsigned long)page);
  1077. return length;
  1078. }
  1079. static const struct file_operations sel_avc_cache_threshold_ops = {
  1080. .read = sel_read_avc_cache_threshold,
  1081. .write = sel_write_avc_cache_threshold,
  1082. .llseek = generic_file_llseek,
  1083. };
  1084. static const struct file_operations sel_avc_hash_stats_ops = {
  1085. .read = sel_read_avc_hash_stats,
  1086. .llseek = generic_file_llseek,
  1087. };
  1088. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1089. static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
  1090. {
  1091. int cpu;
  1092. for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
  1093. if (!cpu_possible(cpu))
  1094. continue;
  1095. *idx = cpu + 1;
  1096. return &per_cpu(avc_cache_stats, cpu);
  1097. }
  1098. return NULL;
  1099. }
  1100. static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
  1101. {
  1102. loff_t n = *pos - 1;
  1103. if (*pos == 0)
  1104. return SEQ_START_TOKEN;
  1105. return sel_avc_get_stat_idx(&n);
  1106. }
  1107. static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1108. {
  1109. return sel_avc_get_stat_idx(pos);
  1110. }
  1111. static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
  1112. {
  1113. struct avc_cache_stats *st = v;
  1114. if (v == SEQ_START_TOKEN)
  1115. seq_printf(seq, "lookups hits misses allocations reclaims "
  1116. "frees\n");
  1117. else {
  1118. unsigned int lookups = st->lookups;
  1119. unsigned int misses = st->misses;
  1120. unsigned int hits = lookups - misses;
  1121. seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
  1122. hits, misses, st->allocations,
  1123. st->reclaims, st->frees);
  1124. }
  1125. return 0;
  1126. }
  1127. static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
  1128. { }
  1129. static const struct seq_operations sel_avc_cache_stats_seq_ops = {
  1130. .start = sel_avc_stats_seq_start,
  1131. .next = sel_avc_stats_seq_next,
  1132. .show = sel_avc_stats_seq_show,
  1133. .stop = sel_avc_stats_seq_stop,
  1134. };
  1135. static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
  1136. {
  1137. return seq_open(file, &sel_avc_cache_stats_seq_ops);
  1138. }
  1139. static const struct file_operations sel_avc_cache_stats_ops = {
  1140. .open = sel_open_avc_cache_stats,
  1141. .read = seq_read,
  1142. .llseek = seq_lseek,
  1143. .release = seq_release,
  1144. };
  1145. #endif
  1146. static int sel_make_avc_files(struct dentry *dir)
  1147. {
  1148. int i;
  1149. static struct tree_descr files[] = {
  1150. { "cache_threshold",
  1151. &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
  1152. { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
  1153. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1154. { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
  1155. #endif
  1156. };
  1157. for (i = 0; i < ARRAY_SIZE(files); i++) {
  1158. struct inode *inode;
  1159. struct dentry *dentry;
  1160. dentry = d_alloc_name(dir, files[i].name);
  1161. if (!dentry)
  1162. return -ENOMEM;
  1163. inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
  1164. if (!inode)
  1165. return -ENOMEM;
  1166. inode->i_fop = files[i].ops;
  1167. inode->i_ino = ++sel_last_ino;
  1168. d_add(dentry, inode);
  1169. }
  1170. return 0;
  1171. }
  1172. static ssize_t sel_read_initcon(struct file *file, char __user *buf,
  1173. size_t count, loff_t *ppos)
  1174. {
  1175. struct inode *inode;
  1176. char *con;
  1177. u32 sid, len;
  1178. ssize_t ret;
  1179. inode = file->f_path.dentry->d_inode;
  1180. sid = inode->i_ino&SEL_INO_MASK;
  1181. ret = security_sid_to_context(sid, &con, &len);
  1182. if (ret)
  1183. return ret;
  1184. ret = simple_read_from_buffer(buf, count, ppos, con, len);
  1185. kfree(con);
  1186. return ret;
  1187. }
  1188. static const struct file_operations sel_initcon_ops = {
  1189. .read = sel_read_initcon,
  1190. .llseek = generic_file_llseek,
  1191. };
  1192. static int sel_make_initcon_files(struct dentry *dir)
  1193. {
  1194. int i;
  1195. for (i = 1; i <= SECINITSID_NUM; i++) {
  1196. struct inode *inode;
  1197. struct dentry *dentry;
  1198. dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
  1199. if (!dentry)
  1200. return -ENOMEM;
  1201. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1202. if (!inode)
  1203. return -ENOMEM;
  1204. inode->i_fop = &sel_initcon_ops;
  1205. inode->i_ino = i|SEL_INITCON_INO_OFFSET;
  1206. d_add(dentry, inode);
  1207. }
  1208. return 0;
  1209. }
  1210. static inline unsigned int sel_div(unsigned long a, unsigned long b)
  1211. {
  1212. return a / b - (a % b < 0);
  1213. }
  1214. static inline unsigned long sel_class_to_ino(u16 class)
  1215. {
  1216. return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
  1217. }
  1218. static inline u16 sel_ino_to_class(unsigned long ino)
  1219. {
  1220. return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
  1221. }
  1222. static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
  1223. {
  1224. return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
  1225. }
  1226. static inline u32 sel_ino_to_perm(unsigned long ino)
  1227. {
  1228. return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
  1229. }
  1230. static ssize_t sel_read_class(struct file *file, char __user *buf,
  1231. size_t count, loff_t *ppos)
  1232. {
  1233. ssize_t rc, len;
  1234. char *page;
  1235. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1236. page = (char *)__get_free_page(GFP_KERNEL);
  1237. if (!page)
  1238. return -ENOMEM;
  1239. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
  1240. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1241. free_page((unsigned long)page);
  1242. return rc;
  1243. }
  1244. static const struct file_operations sel_class_ops = {
  1245. .read = sel_read_class,
  1246. .llseek = generic_file_llseek,
  1247. };
  1248. static ssize_t sel_read_perm(struct file *file, char __user *buf,
  1249. size_t count, loff_t *ppos)
  1250. {
  1251. ssize_t rc, len;
  1252. char *page;
  1253. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1254. page = (char *)__get_free_page(GFP_KERNEL);
  1255. if (!page)
  1256. return -ENOMEM;
  1257. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
  1258. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1259. free_page((unsigned long)page);
  1260. return rc;
  1261. }
  1262. static const struct file_operations sel_perm_ops = {
  1263. .read = sel_read_perm,
  1264. .llseek = generic_file_llseek,
  1265. };
  1266. static ssize_t sel_read_policycap(struct file *file, char __user *buf,
  1267. size_t count, loff_t *ppos)
  1268. {
  1269. int value;
  1270. char tmpbuf[TMPBUFLEN];
  1271. ssize_t length;
  1272. unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
  1273. value = security_policycap_supported(i_ino & SEL_INO_MASK);
  1274. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
  1275. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1276. }
  1277. static const struct file_operations sel_policycap_ops = {
  1278. .read = sel_read_policycap,
  1279. .llseek = generic_file_llseek,
  1280. };
  1281. static int sel_make_perm_files(char *objclass, int classvalue,
  1282. struct dentry *dir)
  1283. {
  1284. int i, rc, nperms;
  1285. char **perms;
  1286. rc = security_get_permissions(objclass, &perms, &nperms);
  1287. if (rc)
  1288. return rc;
  1289. for (i = 0; i < nperms; i++) {
  1290. struct inode *inode;
  1291. struct dentry *dentry;
  1292. rc = -ENOMEM;
  1293. dentry = d_alloc_name(dir, perms[i]);
  1294. if (!dentry)
  1295. goto out;
  1296. rc = -ENOMEM;
  1297. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1298. if (!inode)
  1299. goto out;
  1300. inode->i_fop = &sel_perm_ops;
  1301. /* i+1 since perm values are 1-indexed */
  1302. inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
  1303. d_add(dentry, inode);
  1304. }
  1305. rc = 0;
  1306. out:
  1307. for (i = 0; i < nperms; i++)
  1308. kfree(perms[i]);
  1309. kfree(perms);
  1310. return rc;
  1311. }
  1312. static int sel_make_class_dir_entries(char *classname, int index,
  1313. struct dentry *dir)
  1314. {
  1315. struct dentry *dentry = NULL;
  1316. struct inode *inode = NULL;
  1317. int rc;
  1318. dentry = d_alloc_name(dir, "index");
  1319. if (!dentry)
  1320. return -ENOMEM;
  1321. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1322. if (!inode)
  1323. return -ENOMEM;
  1324. inode->i_fop = &sel_class_ops;
  1325. inode->i_ino = sel_class_to_ino(index);
  1326. d_add(dentry, inode);
  1327. dentry = d_alloc_name(dir, "perms");
  1328. if (!dentry)
  1329. return -ENOMEM;
  1330. rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
  1331. if (rc)
  1332. return rc;
  1333. rc = sel_make_perm_files(classname, index, dentry);
  1334. return rc;
  1335. }
  1336. static void sel_remove_classes(void)
  1337. {
  1338. struct list_head *class_node;
  1339. list_for_each(class_node, &class_dir->d_subdirs) {
  1340. struct dentry *class_subdir = list_entry(class_node,
  1341. struct dentry, d_u.d_child);
  1342. struct list_head *class_subdir_node;
  1343. list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
  1344. struct dentry *d = list_entry(class_subdir_node,
  1345. struct dentry, d_u.d_child);
  1346. if (d->d_inode)
  1347. if (d->d_inode->i_mode & S_IFDIR)
  1348. sel_remove_entries(d);
  1349. }
  1350. sel_remove_entries(class_subdir);
  1351. }
  1352. sel_remove_entries(class_dir);
  1353. }
  1354. static int sel_make_classes(void)
  1355. {
  1356. int rc, nclasses, i;
  1357. char **classes;
  1358. /* delete any existing entries */
  1359. sel_remove_classes();
  1360. rc = security_get_classes(&classes, &nclasses);
  1361. if (rc)
  1362. return rc;
  1363. /* +2 since classes are 1-indexed */
  1364. last_class_ino = sel_class_to_ino(nclasses + 2);
  1365. for (i = 0; i < nclasses; i++) {
  1366. struct dentry *class_name_dir;
  1367. rc = -ENOMEM;
  1368. class_name_dir = d_alloc_name(class_dir, classes[i]);
  1369. if (!class_name_dir)
  1370. goto out;
  1371. rc = sel_make_dir(class_dir->d_inode, class_name_dir,
  1372. &last_class_ino);
  1373. if (rc)
  1374. goto out;
  1375. /* i+1 since class values are 1-indexed */
  1376. rc = sel_make_class_dir_entries(classes[i], i + 1,
  1377. class_name_dir);
  1378. if (rc)
  1379. goto out;
  1380. }
  1381. rc = 0;
  1382. out:
  1383. for (i = 0; i < nclasses; i++)
  1384. kfree(classes[i]);
  1385. kfree(classes);
  1386. return rc;
  1387. }
  1388. static int sel_make_policycap(void)
  1389. {
  1390. unsigned int iter;
  1391. struct dentry *dentry = NULL;
  1392. struct inode *inode = NULL;
  1393. sel_remove_entries(policycap_dir);
  1394. for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
  1395. if (iter < ARRAY_SIZE(policycap_names))
  1396. dentry = d_alloc_name(policycap_dir,
  1397. policycap_names[iter]);
  1398. else
  1399. dentry = d_alloc_name(policycap_dir, "unknown");
  1400. if (dentry == NULL)
  1401. return -ENOMEM;
  1402. inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
  1403. if (inode == NULL)
  1404. return -ENOMEM;
  1405. inode->i_fop = &sel_policycap_ops;
  1406. inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
  1407. d_add(dentry, inode);
  1408. }
  1409. return 0;
  1410. }
  1411. static int sel_make_dir(struct inode *dir, struct dentry *dentry,
  1412. unsigned long *ino)
  1413. {
  1414. struct inode *inode;
  1415. inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1416. if (!inode)
  1417. return -ENOMEM;
  1418. inode->i_op = &simple_dir_inode_operations;
  1419. inode->i_fop = &simple_dir_operations;
  1420. inode->i_ino = ++(*ino);
  1421. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  1422. inc_nlink(inode);
  1423. d_add(dentry, inode);
  1424. /* bump link count on parent directory, too */
  1425. inc_nlink(dir);
  1426. return 0;
  1427. }
  1428. static int sel_fill_super(struct super_block *sb, void *data, int silent)
  1429. {
  1430. int ret;
  1431. struct dentry *dentry;
  1432. struct inode *inode, *root_inode;
  1433. struct inode_security_struct *isec;
  1434. static struct tree_descr selinux_files[] = {
  1435. [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
  1436. [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
  1437. [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
  1438. [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
  1439. [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
  1440. [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
  1441. [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
  1442. [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
  1443. [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
  1444. [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
  1445. [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
  1446. [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
  1447. [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
  1448. [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
  1449. [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
  1450. [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
  1451. [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUSR},
  1452. /* last one */ {""}
  1453. };
  1454. ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
  1455. if (ret)
  1456. goto err;
  1457. root_inode = sb->s_root->d_inode;
  1458. ret = -ENOMEM;
  1459. dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
  1460. if (!dentry)
  1461. goto err;
  1462. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1463. if (ret)
  1464. goto err;
  1465. bool_dir = dentry;
  1466. ret = -ENOMEM;
  1467. dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
  1468. if (!dentry)
  1469. goto err;
  1470. ret = -ENOMEM;
  1471. inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
  1472. if (!inode)
  1473. goto err;
  1474. inode->i_ino = ++sel_last_ino;
  1475. isec = (struct inode_security_struct *)inode->i_security;
  1476. isec->sid = SECINITSID_DEVNULL;
  1477. isec->sclass = SECCLASS_CHR_FILE;
  1478. isec->initialized = 1;
  1479. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
  1480. d_add(dentry, inode);
  1481. selinux_null = dentry;
  1482. ret = -ENOMEM;
  1483. dentry = d_alloc_name(sb->s_root, "avc");
  1484. if (!dentry)
  1485. goto err;
  1486. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1487. if (ret)
  1488. goto err;
  1489. ret = sel_make_avc_files(dentry);
  1490. if (ret)
  1491. goto err;
  1492. ret = -ENOMEM;
  1493. dentry = d_alloc_name(sb->s_root, "initial_contexts");
  1494. if (!dentry)
  1495. goto err;
  1496. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1497. if (ret)
  1498. goto err;
  1499. ret = sel_make_initcon_files(dentry);
  1500. if (ret)
  1501. goto err;
  1502. ret = -ENOMEM;
  1503. dentry = d_alloc_name(sb->s_root, "class");
  1504. if (!dentry)
  1505. goto err;
  1506. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1507. if (ret)
  1508. goto err;
  1509. class_dir = dentry;
  1510. ret = -ENOMEM;
  1511. dentry = d_alloc_name(sb->s_root, "policy_capabilities");
  1512. if (!dentry)
  1513. goto err;
  1514. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1515. if (ret)
  1516. goto err;
  1517. policycap_dir = dentry;
  1518. return 0;
  1519. err:
  1520. printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
  1521. __func__);
  1522. return ret;
  1523. }
  1524. static struct dentry *sel_mount(struct file_system_type *fs_type,
  1525. int flags, const char *dev_name, void *data)
  1526. {
  1527. return mount_single(fs_type, flags, data, sel_fill_super);
  1528. }
  1529. static struct file_system_type sel_fs_type = {
  1530. .name = "selinuxfs",
  1531. .mount = sel_mount,
  1532. .kill_sb = kill_litter_super,
  1533. };
  1534. struct vfsmount *selinuxfs_mount;
  1535. static int __init init_sel_fs(void)
  1536. {
  1537. int err;
  1538. if (!selinux_enabled)
  1539. return 0;
  1540. err = register_filesystem(&sel_fs_type);
  1541. if (err)
  1542. return err;
  1543. selinuxfs_mount = kern_mount(&sel_fs_type);
  1544. if (IS_ERR(selinuxfs_mount)) {
  1545. printk(KERN_ERR "selinuxfs: could not mount!\n");
  1546. err = PTR_ERR(selinuxfs_mount);
  1547. selinuxfs_mount = NULL;
  1548. }
  1549. return err;
  1550. }
  1551. __initcall(init_sel_fs);
  1552. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  1553. void exit_sel_fs(void)
  1554. {
  1555. unregister_filesystem(&sel_fs_type);
  1556. }
  1557. #endif