selinuxfs.c 39 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 <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. #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
  45. #define SELINUX_COMPAT_NET_VALUE 0
  46. #else
  47. #define SELINUX_COMPAT_NET_VALUE 1
  48. #endif
  49. int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
  50. static int __init checkreqprot_setup(char *str)
  51. {
  52. unsigned long checkreqprot;
  53. if (!strict_strtoul(str, 0, &checkreqprot))
  54. selinux_checkreqprot = checkreqprot ? 1 : 0;
  55. return 1;
  56. }
  57. __setup("checkreqprot=", checkreqprot_setup);
  58. static int __init selinux_compat_net_setup(char *str)
  59. {
  60. unsigned long compat_net;
  61. if (!strict_strtoul(str, 0, &compat_net))
  62. selinux_compat_net = compat_net ? 1 : 0;
  63. return 1;
  64. }
  65. __setup("selinux_compat_net=", selinux_compat_net_setup);
  66. static DEFINE_MUTEX(sel_mutex);
  67. /* global data for booleans */
  68. static struct dentry *bool_dir;
  69. static int bool_num;
  70. static char **bool_pending_names;
  71. static int *bool_pending_values;
  72. /* global data for classes */
  73. static struct dentry *class_dir;
  74. static unsigned long last_class_ino;
  75. /* global data for policy capabilities */
  76. static struct dentry *policycap_dir;
  77. extern void selnl_notify_setenforce(int val);
  78. /* Check whether a task is allowed to use a security operation. */
  79. static int task_has_security(struct task_struct *tsk,
  80. u32 perms)
  81. {
  82. struct task_security_struct *tsec;
  83. tsec = tsk->cred->security;
  84. if (!tsec)
  85. return -EACCES;
  86. return avc_has_perm(tsec->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. 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. page = (char *)get_zeroed_page(GFP_KERNEL);
  735. if (!page) {
  736. ret = -ENOMEM;
  737. goto out;
  738. }
  739. cur_enforcing = security_get_bool_value(index);
  740. if (cur_enforcing < 0) {
  741. ret = cur_enforcing;
  742. goto out;
  743. }
  744. length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
  745. bool_pending_values[index]);
  746. ret = simple_read_from_buffer(buf, count, ppos, page, length);
  747. out:
  748. mutex_unlock(&sel_mutex);
  749. if (page)
  750. free_page((unsigned long)page);
  751. return ret;
  752. }
  753. static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
  754. size_t count, loff_t *ppos)
  755. {
  756. char *page = NULL;
  757. ssize_t length;
  758. int new_value;
  759. struct inode *inode = filep->f_path.dentry->d_inode;
  760. unsigned index = inode->i_ino & SEL_INO_MASK;
  761. const char *name = filep->f_path.dentry->d_name.name;
  762. mutex_lock(&sel_mutex);
  763. length = task_has_security(current, SECURITY__SETBOOL);
  764. if (length)
  765. goto out;
  766. if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
  767. length = -EINVAL;
  768. goto out;
  769. }
  770. if (count >= PAGE_SIZE) {
  771. length = -ENOMEM;
  772. goto out;
  773. }
  774. if (*ppos != 0) {
  775. /* No partial writes. */
  776. length = -EINVAL;
  777. goto out;
  778. }
  779. page = (char *)get_zeroed_page(GFP_KERNEL);
  780. if (!page) {
  781. length = -ENOMEM;
  782. goto out;
  783. }
  784. length = -EFAULT;
  785. if (copy_from_user(page, buf, count))
  786. goto out;
  787. length = -EINVAL;
  788. if (sscanf(page, "%d", &new_value) != 1)
  789. goto out;
  790. if (new_value)
  791. new_value = 1;
  792. bool_pending_values[index] = new_value;
  793. length = count;
  794. out:
  795. mutex_unlock(&sel_mutex);
  796. if (page)
  797. free_page((unsigned long) page);
  798. return length;
  799. }
  800. static const struct file_operations sel_bool_ops = {
  801. .read = sel_read_bool,
  802. .write = sel_write_bool,
  803. };
  804. static ssize_t sel_commit_bools_write(struct file *filep,
  805. const char __user *buf,
  806. size_t count, loff_t *ppos)
  807. {
  808. char *page = NULL;
  809. ssize_t length;
  810. int new_value;
  811. mutex_lock(&sel_mutex);
  812. length = task_has_security(current, SECURITY__SETBOOL);
  813. if (length)
  814. goto out;
  815. if (count >= PAGE_SIZE) {
  816. length = -ENOMEM;
  817. goto out;
  818. }
  819. if (*ppos != 0) {
  820. /* No partial writes. */
  821. goto out;
  822. }
  823. page = (char *)get_zeroed_page(GFP_KERNEL);
  824. if (!page) {
  825. length = -ENOMEM;
  826. goto out;
  827. }
  828. length = -EFAULT;
  829. if (copy_from_user(page, buf, count))
  830. goto out;
  831. length = -EINVAL;
  832. if (sscanf(page, "%d", &new_value) != 1)
  833. goto out;
  834. if (new_value && bool_pending_values)
  835. security_set_bools(bool_num, bool_pending_values);
  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. page = (char *)get_zeroed_page(GFP_KERNEL);
  886. if (!page)
  887. return -ENOMEM;
  888. ret = security_get_bools(&num, &names, &values);
  889. if (ret != 0)
  890. goto out;
  891. for (i = 0; i < num; i++) {
  892. dentry = d_alloc_name(dir, names[i]);
  893. if (!dentry) {
  894. ret = -ENOMEM;
  895. goto err;
  896. }
  897. inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
  898. if (!inode) {
  899. ret = -ENOMEM;
  900. goto err;
  901. }
  902. len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
  903. if (len < 0) {
  904. ret = -EINVAL;
  905. goto err;
  906. } else if (len >= PAGE_SIZE) {
  907. ret = -ENAMETOOLONG;
  908. goto err;
  909. }
  910. isec = (struct inode_security_struct *)inode->i_security;
  911. ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
  912. if (ret)
  913. goto err;
  914. isec->sid = sid;
  915. isec->initialized = 1;
  916. inode->i_fop = &sel_bool_ops;
  917. inode->i_ino = i|SEL_BOOL_INO_OFFSET;
  918. d_add(dentry, inode);
  919. }
  920. bool_num = num;
  921. bool_pending_names = names;
  922. bool_pending_values = values;
  923. out:
  924. free_page((unsigned long)page);
  925. return ret;
  926. err:
  927. if (names) {
  928. for (i = 0; i < num; i++)
  929. kfree(names[i]);
  930. kfree(names);
  931. }
  932. kfree(values);
  933. sel_remove_entries(dir);
  934. ret = -ENOMEM;
  935. goto out;
  936. }
  937. #define NULL_FILE_NAME "null"
  938. struct dentry *selinux_null;
  939. static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
  940. size_t count, loff_t *ppos)
  941. {
  942. char tmpbuf[TMPBUFLEN];
  943. ssize_t length;
  944. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
  945. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  946. }
  947. static ssize_t sel_write_avc_cache_threshold(struct file *file,
  948. const char __user *buf,
  949. size_t count, loff_t *ppos)
  950. {
  951. char *page;
  952. ssize_t ret;
  953. int new_value;
  954. if (count >= PAGE_SIZE) {
  955. ret = -ENOMEM;
  956. goto out;
  957. }
  958. if (*ppos != 0) {
  959. /* No partial writes. */
  960. ret = -EINVAL;
  961. goto out;
  962. }
  963. page = (char *)get_zeroed_page(GFP_KERNEL);
  964. if (!page) {
  965. ret = -ENOMEM;
  966. goto out;
  967. }
  968. if (copy_from_user(page, buf, count)) {
  969. ret = -EFAULT;
  970. goto out_free;
  971. }
  972. if (sscanf(page, "%u", &new_value) != 1) {
  973. ret = -EINVAL;
  974. goto out;
  975. }
  976. if (new_value != avc_cache_threshold) {
  977. ret = task_has_security(current, SECURITY__SETSECPARAM);
  978. if (ret)
  979. goto out_free;
  980. avc_cache_threshold = new_value;
  981. }
  982. ret = count;
  983. out_free:
  984. free_page((unsigned long)page);
  985. out:
  986. return ret;
  987. }
  988. static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
  989. size_t count, loff_t *ppos)
  990. {
  991. char *page;
  992. ssize_t ret = 0;
  993. page = (char *)__get_free_page(GFP_KERNEL);
  994. if (!page) {
  995. ret = -ENOMEM;
  996. goto out;
  997. }
  998. ret = avc_get_hash_stats(page);
  999. if (ret >= 0)
  1000. ret = simple_read_from_buffer(buf, count, ppos, page, ret);
  1001. free_page((unsigned long)page);
  1002. out:
  1003. return ret;
  1004. }
  1005. static const struct file_operations sel_avc_cache_threshold_ops = {
  1006. .read = sel_read_avc_cache_threshold,
  1007. .write = sel_write_avc_cache_threshold,
  1008. };
  1009. static const struct file_operations sel_avc_hash_stats_ops = {
  1010. .read = sel_read_avc_hash_stats,
  1011. };
  1012. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1013. static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
  1014. {
  1015. int cpu;
  1016. for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
  1017. if (!cpu_possible(cpu))
  1018. continue;
  1019. *idx = cpu + 1;
  1020. return &per_cpu(avc_cache_stats, cpu);
  1021. }
  1022. return NULL;
  1023. }
  1024. static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
  1025. {
  1026. loff_t n = *pos - 1;
  1027. if (*pos == 0)
  1028. return SEQ_START_TOKEN;
  1029. return sel_avc_get_stat_idx(&n);
  1030. }
  1031. static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1032. {
  1033. return sel_avc_get_stat_idx(pos);
  1034. }
  1035. static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
  1036. {
  1037. struct avc_cache_stats *st = v;
  1038. if (v == SEQ_START_TOKEN)
  1039. seq_printf(seq, "lookups hits misses allocations reclaims "
  1040. "frees\n");
  1041. else
  1042. seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
  1043. st->hits, st->misses, st->allocations,
  1044. st->reclaims, st->frees);
  1045. return 0;
  1046. }
  1047. static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
  1048. { }
  1049. static const struct seq_operations sel_avc_cache_stats_seq_ops = {
  1050. .start = sel_avc_stats_seq_start,
  1051. .next = sel_avc_stats_seq_next,
  1052. .show = sel_avc_stats_seq_show,
  1053. .stop = sel_avc_stats_seq_stop,
  1054. };
  1055. static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
  1056. {
  1057. return seq_open(file, &sel_avc_cache_stats_seq_ops);
  1058. }
  1059. static const struct file_operations sel_avc_cache_stats_ops = {
  1060. .open = sel_open_avc_cache_stats,
  1061. .read = seq_read,
  1062. .llseek = seq_lseek,
  1063. .release = seq_release,
  1064. };
  1065. #endif
  1066. static int sel_make_avc_files(struct dentry *dir)
  1067. {
  1068. int i, ret = 0;
  1069. static struct tree_descr files[] = {
  1070. { "cache_threshold",
  1071. &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
  1072. { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
  1073. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1074. { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
  1075. #endif
  1076. };
  1077. for (i = 0; i < ARRAY_SIZE(files); i++) {
  1078. struct inode *inode;
  1079. struct dentry *dentry;
  1080. dentry = d_alloc_name(dir, files[i].name);
  1081. if (!dentry) {
  1082. ret = -ENOMEM;
  1083. goto out;
  1084. }
  1085. inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
  1086. if (!inode) {
  1087. ret = -ENOMEM;
  1088. goto out;
  1089. }
  1090. inode->i_fop = files[i].ops;
  1091. inode->i_ino = ++sel_last_ino;
  1092. d_add(dentry, inode);
  1093. }
  1094. out:
  1095. return ret;
  1096. }
  1097. static ssize_t sel_read_initcon(struct file *file, char __user *buf,
  1098. size_t count, loff_t *ppos)
  1099. {
  1100. struct inode *inode;
  1101. char *con;
  1102. u32 sid, len;
  1103. ssize_t ret;
  1104. inode = file->f_path.dentry->d_inode;
  1105. sid = inode->i_ino&SEL_INO_MASK;
  1106. ret = security_sid_to_context(sid, &con, &len);
  1107. if (ret < 0)
  1108. return ret;
  1109. ret = simple_read_from_buffer(buf, count, ppos, con, len);
  1110. kfree(con);
  1111. return ret;
  1112. }
  1113. static const struct file_operations sel_initcon_ops = {
  1114. .read = sel_read_initcon,
  1115. };
  1116. static int sel_make_initcon_files(struct dentry *dir)
  1117. {
  1118. int i, ret = 0;
  1119. for (i = 1; i <= SECINITSID_NUM; i++) {
  1120. struct inode *inode;
  1121. struct dentry *dentry;
  1122. dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
  1123. if (!dentry) {
  1124. ret = -ENOMEM;
  1125. goto out;
  1126. }
  1127. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1128. if (!inode) {
  1129. ret = -ENOMEM;
  1130. goto out;
  1131. }
  1132. inode->i_fop = &sel_initcon_ops;
  1133. inode->i_ino = i|SEL_INITCON_INO_OFFSET;
  1134. d_add(dentry, inode);
  1135. }
  1136. out:
  1137. return ret;
  1138. }
  1139. static inline unsigned int sel_div(unsigned long a, unsigned long b)
  1140. {
  1141. return a / b - (a % b < 0);
  1142. }
  1143. static inline unsigned long sel_class_to_ino(u16 class)
  1144. {
  1145. return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
  1146. }
  1147. static inline u16 sel_ino_to_class(unsigned long ino)
  1148. {
  1149. return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
  1150. }
  1151. static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
  1152. {
  1153. return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
  1154. }
  1155. static inline u32 sel_ino_to_perm(unsigned long ino)
  1156. {
  1157. return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
  1158. }
  1159. static ssize_t sel_read_class(struct file *file, char __user *buf,
  1160. size_t count, loff_t *ppos)
  1161. {
  1162. ssize_t rc, len;
  1163. char *page;
  1164. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1165. page = (char *)__get_free_page(GFP_KERNEL);
  1166. if (!page) {
  1167. rc = -ENOMEM;
  1168. goto out;
  1169. }
  1170. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
  1171. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1172. free_page((unsigned long)page);
  1173. out:
  1174. return rc;
  1175. }
  1176. static const struct file_operations sel_class_ops = {
  1177. .read = sel_read_class,
  1178. };
  1179. static ssize_t sel_read_perm(struct file *file, char __user *buf,
  1180. size_t count, loff_t *ppos)
  1181. {
  1182. ssize_t rc, len;
  1183. char *page;
  1184. unsigned long ino = file->f_path.dentry->d_inode->i_ino;
  1185. page = (char *)__get_free_page(GFP_KERNEL);
  1186. if (!page) {
  1187. rc = -ENOMEM;
  1188. goto out;
  1189. }
  1190. len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
  1191. rc = simple_read_from_buffer(buf, count, ppos, page, len);
  1192. free_page((unsigned long)page);
  1193. out:
  1194. return rc;
  1195. }
  1196. static const struct file_operations sel_perm_ops = {
  1197. .read = sel_read_perm,
  1198. };
  1199. static ssize_t sel_read_policycap(struct file *file, char __user *buf,
  1200. size_t count, loff_t *ppos)
  1201. {
  1202. int value;
  1203. char tmpbuf[TMPBUFLEN];
  1204. ssize_t length;
  1205. unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
  1206. value = security_policycap_supported(i_ino & SEL_INO_MASK);
  1207. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
  1208. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1209. }
  1210. static const struct file_operations sel_policycap_ops = {
  1211. .read = sel_read_policycap,
  1212. };
  1213. static int sel_make_perm_files(char *objclass, int classvalue,
  1214. struct dentry *dir)
  1215. {
  1216. int i, rc = 0, nperms;
  1217. char **perms;
  1218. rc = security_get_permissions(objclass, &perms, &nperms);
  1219. if (rc)
  1220. goto out;
  1221. for (i = 0; i < nperms; i++) {
  1222. struct inode *inode;
  1223. struct dentry *dentry;
  1224. dentry = d_alloc_name(dir, perms[i]);
  1225. if (!dentry) {
  1226. rc = -ENOMEM;
  1227. goto out1;
  1228. }
  1229. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1230. if (!inode) {
  1231. rc = -ENOMEM;
  1232. goto out1;
  1233. }
  1234. inode->i_fop = &sel_perm_ops;
  1235. /* i+1 since perm values are 1-indexed */
  1236. inode->i_ino = sel_perm_to_ino(classvalue, i+1);
  1237. d_add(dentry, inode);
  1238. }
  1239. out1:
  1240. for (i = 0; i < nperms; i++)
  1241. kfree(perms[i]);
  1242. kfree(perms);
  1243. out:
  1244. return rc;
  1245. }
  1246. static int sel_make_class_dir_entries(char *classname, int index,
  1247. struct dentry *dir)
  1248. {
  1249. struct dentry *dentry = NULL;
  1250. struct inode *inode = NULL;
  1251. int rc;
  1252. dentry = d_alloc_name(dir, "index");
  1253. if (!dentry) {
  1254. rc = -ENOMEM;
  1255. goto out;
  1256. }
  1257. inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
  1258. if (!inode) {
  1259. rc = -ENOMEM;
  1260. goto out;
  1261. }
  1262. inode->i_fop = &sel_class_ops;
  1263. inode->i_ino = sel_class_to_ino(index);
  1264. d_add(dentry, inode);
  1265. dentry = d_alloc_name(dir, "perms");
  1266. if (!dentry) {
  1267. rc = -ENOMEM;
  1268. goto out;
  1269. }
  1270. rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
  1271. if (rc)
  1272. goto out;
  1273. rc = sel_make_perm_files(classname, index, dentry);
  1274. out:
  1275. return rc;
  1276. }
  1277. static void sel_remove_classes(void)
  1278. {
  1279. struct list_head *class_node;
  1280. list_for_each(class_node, &class_dir->d_subdirs) {
  1281. struct dentry *class_subdir = list_entry(class_node,
  1282. struct dentry, d_u.d_child);
  1283. struct list_head *class_subdir_node;
  1284. list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
  1285. struct dentry *d = list_entry(class_subdir_node,
  1286. struct dentry, d_u.d_child);
  1287. if (d->d_inode)
  1288. if (d->d_inode->i_mode & S_IFDIR)
  1289. sel_remove_entries(d);
  1290. }
  1291. sel_remove_entries(class_subdir);
  1292. }
  1293. sel_remove_entries(class_dir);
  1294. }
  1295. static int sel_make_classes(void)
  1296. {
  1297. int rc = 0, nclasses, i;
  1298. char **classes;
  1299. /* delete any existing entries */
  1300. sel_remove_classes();
  1301. rc = security_get_classes(&classes, &nclasses);
  1302. if (rc < 0)
  1303. goto out;
  1304. /* +2 since classes are 1-indexed */
  1305. last_class_ino = sel_class_to_ino(nclasses+2);
  1306. for (i = 0; i < nclasses; i++) {
  1307. struct dentry *class_name_dir;
  1308. class_name_dir = d_alloc_name(class_dir, classes[i]);
  1309. if (!class_name_dir) {
  1310. rc = -ENOMEM;
  1311. goto out1;
  1312. }
  1313. rc = sel_make_dir(class_dir->d_inode, class_name_dir,
  1314. &last_class_ino);
  1315. if (rc)
  1316. goto out1;
  1317. /* i+1 since class values are 1-indexed */
  1318. rc = sel_make_class_dir_entries(classes[i], i+1,
  1319. class_name_dir);
  1320. if (rc)
  1321. goto out1;
  1322. }
  1323. out1:
  1324. for (i = 0; i < nclasses; i++)
  1325. kfree(classes[i]);
  1326. kfree(classes);
  1327. out:
  1328. return rc;
  1329. }
  1330. static int sel_make_policycap(void)
  1331. {
  1332. unsigned int iter;
  1333. struct dentry *dentry = NULL;
  1334. struct inode *inode = NULL;
  1335. sel_remove_entries(policycap_dir);
  1336. for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
  1337. if (iter < ARRAY_SIZE(policycap_names))
  1338. dentry = d_alloc_name(policycap_dir,
  1339. policycap_names[iter]);
  1340. else
  1341. dentry = d_alloc_name(policycap_dir, "unknown");
  1342. if (dentry == NULL)
  1343. return -ENOMEM;
  1344. inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
  1345. if (inode == NULL)
  1346. return -ENOMEM;
  1347. inode->i_fop = &sel_policycap_ops;
  1348. inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
  1349. d_add(dentry, inode);
  1350. }
  1351. return 0;
  1352. }
  1353. static int sel_make_dir(struct inode *dir, struct dentry *dentry,
  1354. unsigned long *ino)
  1355. {
  1356. int ret = 0;
  1357. struct inode *inode;
  1358. inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1359. if (!inode) {
  1360. ret = -ENOMEM;
  1361. goto out;
  1362. }
  1363. inode->i_op = &simple_dir_inode_operations;
  1364. inode->i_fop = &simple_dir_operations;
  1365. inode->i_ino = ++(*ino);
  1366. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  1367. inc_nlink(inode);
  1368. d_add(dentry, inode);
  1369. /* bump link count on parent directory, too */
  1370. inc_nlink(dir);
  1371. out:
  1372. return ret;
  1373. }
  1374. static int sel_fill_super(struct super_block *sb, void *data, int silent)
  1375. {
  1376. int ret;
  1377. struct dentry *dentry;
  1378. struct inode *inode, *root_inode;
  1379. struct inode_security_struct *isec;
  1380. static struct tree_descr selinux_files[] = {
  1381. [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
  1382. [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
  1383. [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
  1384. [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
  1385. [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
  1386. [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
  1387. [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
  1388. [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
  1389. [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
  1390. [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
  1391. [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
  1392. [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
  1393. [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
  1394. [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
  1395. [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
  1396. [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
  1397. /* last one */ {""}
  1398. };
  1399. ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
  1400. if (ret)
  1401. goto err;
  1402. root_inode = sb->s_root->d_inode;
  1403. dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
  1404. if (!dentry) {
  1405. ret = -ENOMEM;
  1406. goto err;
  1407. }
  1408. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1409. if (ret)
  1410. goto err;
  1411. bool_dir = dentry;
  1412. dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
  1413. if (!dentry) {
  1414. ret = -ENOMEM;
  1415. goto err;
  1416. }
  1417. inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
  1418. if (!inode) {
  1419. ret = -ENOMEM;
  1420. goto err;
  1421. }
  1422. inode->i_ino = ++sel_last_ino;
  1423. isec = (struct inode_security_struct *)inode->i_security;
  1424. isec->sid = SECINITSID_DEVNULL;
  1425. isec->sclass = SECCLASS_CHR_FILE;
  1426. isec->initialized = 1;
  1427. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
  1428. d_add(dentry, inode);
  1429. selinux_null = dentry;
  1430. dentry = d_alloc_name(sb->s_root, "avc");
  1431. if (!dentry) {
  1432. ret = -ENOMEM;
  1433. goto err;
  1434. }
  1435. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1436. if (ret)
  1437. goto err;
  1438. ret = sel_make_avc_files(dentry);
  1439. if (ret)
  1440. goto err;
  1441. dentry = d_alloc_name(sb->s_root, "initial_contexts");
  1442. if (!dentry) {
  1443. ret = -ENOMEM;
  1444. goto err;
  1445. }
  1446. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1447. if (ret)
  1448. goto err;
  1449. ret = sel_make_initcon_files(dentry);
  1450. if (ret)
  1451. goto err;
  1452. dentry = d_alloc_name(sb->s_root, "class");
  1453. if (!dentry) {
  1454. ret = -ENOMEM;
  1455. goto err;
  1456. }
  1457. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1458. if (ret)
  1459. goto err;
  1460. class_dir = dentry;
  1461. dentry = d_alloc_name(sb->s_root, "policy_capabilities");
  1462. if (!dentry) {
  1463. ret = -ENOMEM;
  1464. goto err;
  1465. }
  1466. ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
  1467. if (ret)
  1468. goto err;
  1469. policycap_dir = dentry;
  1470. out:
  1471. return ret;
  1472. err:
  1473. printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
  1474. __func__);
  1475. goto out;
  1476. }
  1477. static int sel_get_sb(struct file_system_type *fs_type,
  1478. int flags, const char *dev_name, void *data,
  1479. struct vfsmount *mnt)
  1480. {
  1481. return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
  1482. }
  1483. static struct file_system_type sel_fs_type = {
  1484. .name = "selinuxfs",
  1485. .get_sb = sel_get_sb,
  1486. .kill_sb = kill_litter_super,
  1487. };
  1488. struct vfsmount *selinuxfs_mount;
  1489. static int __init init_sel_fs(void)
  1490. {
  1491. int err;
  1492. if (!selinux_enabled)
  1493. return 0;
  1494. err = register_filesystem(&sel_fs_type);
  1495. if (!err) {
  1496. selinuxfs_mount = kern_mount(&sel_fs_type);
  1497. if (IS_ERR(selinuxfs_mount)) {
  1498. printk(KERN_ERR "selinuxfs: could not mount!\n");
  1499. err = PTR_ERR(selinuxfs_mount);
  1500. selinuxfs_mount = NULL;
  1501. }
  1502. }
  1503. return err;
  1504. }
  1505. __initcall(init_sel_fs);
  1506. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  1507. void exit_sel_fs(void)
  1508. {
  1509. unregister_filesystem(&sel_fs_type);
  1510. }
  1511. #endif