selinuxfs.c 44 KB

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