selinuxfs.c 43 KB

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