selinuxfs.c 40 KB

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