selinuxfs.c 40 KB

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