selinuxfs.c 43 KB

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