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