selinuxfs.c 43 KB

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