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