selinuxfs.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /* Updated: Karl MacMillan <kmacmillan@tresys.com>
  2. *
  3. * Added conditional policy language extensions
  4. *
  5. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  6. * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/fs.h>
  16. #include <linux/mutex.h>
  17. #include <linux/init.h>
  18. #include <linux/string.h>
  19. #include <linux/security.h>
  20. #include <linux/major.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/percpu.h>
  23. #include <linux/audit.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/semaphore.h>
  26. /* selinuxfs pseudo filesystem for exporting the security policy API.
  27. Based on the proc code and the fs/nfsd/nfsctl.c code. */
  28. #include "flask.h"
  29. #include "avc.h"
  30. #include "avc_ss.h"
  31. #include "security.h"
  32. #include "objsec.h"
  33. #include "conditional.h"
  34. unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
  35. #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
  36. #define SELINUX_COMPAT_NET_VALUE 0
  37. #else
  38. #define SELINUX_COMPAT_NET_VALUE 1
  39. #endif
  40. int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
  41. static int __init checkreqprot_setup(char *str)
  42. {
  43. selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
  44. return 1;
  45. }
  46. __setup("checkreqprot=", checkreqprot_setup);
  47. static int __init selinux_compat_net_setup(char *str)
  48. {
  49. selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
  50. return 1;
  51. }
  52. __setup("selinux_compat_net=", selinux_compat_net_setup);
  53. static DEFINE_MUTEX(sel_mutex);
  54. /* global data for booleans */
  55. static struct dentry *bool_dir = NULL;
  56. static int bool_num = 0;
  57. static int *bool_pending_values = NULL;
  58. extern void selnl_notify_setenforce(int val);
  59. /* Check whether a task is allowed to use a security operation. */
  60. static int task_has_security(struct task_struct *tsk,
  61. u32 perms)
  62. {
  63. struct task_security_struct *tsec;
  64. tsec = tsk->security;
  65. if (!tsec)
  66. return -EACCES;
  67. return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
  68. SECCLASS_SECURITY, perms, NULL);
  69. }
  70. enum sel_inos {
  71. SEL_ROOT_INO = 2,
  72. SEL_LOAD, /* load policy */
  73. SEL_ENFORCE, /* get or set enforcing status */
  74. SEL_CONTEXT, /* validate context */
  75. SEL_ACCESS, /* compute access decision */
  76. SEL_CREATE, /* compute create labeling decision */
  77. SEL_RELABEL, /* compute relabeling decision */
  78. SEL_USER, /* compute reachable user contexts */
  79. SEL_POLICYVERS, /* return policy version for this kernel */
  80. SEL_COMMIT_BOOLS, /* commit new boolean values */
  81. SEL_MLS, /* return if MLS policy is enabled */
  82. SEL_DISABLE, /* disable SELinux until next reboot */
  83. SEL_AVC, /* AVC management directory */
  84. SEL_MEMBER, /* compute polyinstantiation membership decision */
  85. SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
  86. SEL_COMPAT_NET, /* whether to use old compat network packet controls */
  87. };
  88. #define TMPBUFLEN 12
  89. static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
  90. size_t count, loff_t *ppos)
  91. {
  92. char tmpbuf[TMPBUFLEN];
  93. ssize_t length;
  94. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
  95. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  96. }
  97. #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
  98. static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
  99. size_t count, loff_t *ppos)
  100. {
  101. char *page;
  102. ssize_t length;
  103. int new_value;
  104. if (count >= PAGE_SIZE)
  105. return -ENOMEM;
  106. if (*ppos != 0) {
  107. /* No partial writes. */
  108. return -EINVAL;
  109. }
  110. page = (char*)get_zeroed_page(GFP_KERNEL);
  111. if (!page)
  112. return -ENOMEM;
  113. length = -EFAULT;
  114. if (copy_from_user(page, buf, count))
  115. goto out;
  116. length = -EINVAL;
  117. if (sscanf(page, "%d", &new_value) != 1)
  118. goto out;
  119. if (new_value != selinux_enforcing) {
  120. length = task_has_security(current, SECURITY__SETENFORCE);
  121. if (length)
  122. goto out;
  123. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
  124. "enforcing=%d old_enforcing=%d auid=%u", new_value,
  125. selinux_enforcing,
  126. audit_get_loginuid(current->audit_context));
  127. selinux_enforcing = new_value;
  128. if (selinux_enforcing)
  129. avc_ss_reset(0);
  130. selnl_notify_setenforce(selinux_enforcing);
  131. }
  132. length = count;
  133. out:
  134. free_page((unsigned long) page);
  135. return length;
  136. }
  137. #else
  138. #define sel_write_enforce NULL
  139. #endif
  140. static const struct file_operations sel_enforce_ops = {
  141. .read = sel_read_enforce,
  142. .write = sel_write_enforce,
  143. };
  144. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  145. static ssize_t sel_write_disable(struct file * file, const char __user * buf,
  146. size_t count, loff_t *ppos)
  147. {
  148. char *page;
  149. ssize_t length;
  150. int new_value;
  151. extern int selinux_disable(void);
  152. if (count >= PAGE_SIZE)
  153. return -ENOMEM;
  154. if (*ppos != 0) {
  155. /* No partial writes. */
  156. return -EINVAL;
  157. }
  158. page = (char*)get_zeroed_page(GFP_KERNEL);
  159. if (!page)
  160. return -ENOMEM;
  161. length = -EFAULT;
  162. if (copy_from_user(page, buf, count))
  163. goto out;
  164. length = -EINVAL;
  165. if (sscanf(page, "%d", &new_value) != 1)
  166. goto out;
  167. if (new_value) {
  168. length = selinux_disable();
  169. if (length < 0)
  170. goto out;
  171. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
  172. "selinux=0 auid=%u",
  173. audit_get_loginuid(current->audit_context));
  174. }
  175. length = count;
  176. out:
  177. free_page((unsigned long) page);
  178. return length;
  179. }
  180. #else
  181. #define sel_write_disable NULL
  182. #endif
  183. static const struct file_operations sel_disable_ops = {
  184. .write = sel_write_disable,
  185. };
  186. static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
  187. size_t count, loff_t *ppos)
  188. {
  189. char tmpbuf[TMPBUFLEN];
  190. ssize_t length;
  191. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
  192. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  193. }
  194. static const struct file_operations sel_policyvers_ops = {
  195. .read = sel_read_policyvers,
  196. };
  197. /* declaration for sel_write_load */
  198. static int sel_make_bools(void);
  199. static ssize_t sel_read_mls(struct file *filp, char __user *buf,
  200. size_t count, loff_t *ppos)
  201. {
  202. char tmpbuf[TMPBUFLEN];
  203. ssize_t length;
  204. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
  205. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  206. }
  207. static const struct file_operations sel_mls_ops = {
  208. .read = sel_read_mls,
  209. };
  210. static ssize_t sel_write_load(struct file * file, const char __user * buf,
  211. size_t count, loff_t *ppos)
  212. {
  213. int ret;
  214. ssize_t length;
  215. void *data = NULL;
  216. mutex_lock(&sel_mutex);
  217. length = task_has_security(current, SECURITY__LOAD_POLICY);
  218. if (length)
  219. goto out;
  220. if (*ppos != 0) {
  221. /* No partial writes. */
  222. length = -EINVAL;
  223. goto out;
  224. }
  225. if ((count > 64 * 1024 * 1024)
  226. || (data = vmalloc(count)) == NULL) {
  227. length = -ENOMEM;
  228. goto out;
  229. }
  230. length = -EFAULT;
  231. if (copy_from_user(data, buf, count) != 0)
  232. goto out;
  233. length = security_load_policy(data, count);
  234. if (length)
  235. goto out;
  236. ret = sel_make_bools();
  237. if (ret)
  238. length = ret;
  239. else
  240. length = count;
  241. audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
  242. "policy loaded auid=%u",
  243. audit_get_loginuid(current->audit_context));
  244. out:
  245. mutex_unlock(&sel_mutex);
  246. vfree(data);
  247. return length;
  248. }
  249. static const struct file_operations sel_load_ops = {
  250. .write = sel_write_load,
  251. };
  252. static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
  253. {
  254. char *canon;
  255. u32 sid, len;
  256. ssize_t length;
  257. length = task_has_security(current, SECURITY__CHECK_CONTEXT);
  258. if (length)
  259. return length;
  260. length = security_context_to_sid(buf, size, &sid);
  261. if (length < 0)
  262. return length;
  263. length = security_sid_to_context(sid, &canon, &len);
  264. if (length < 0)
  265. return length;
  266. if (len > SIMPLE_TRANSACTION_LIMIT) {
  267. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  268. "max\n", __FUNCTION__, len);
  269. length = -ERANGE;
  270. goto out;
  271. }
  272. memcpy(buf, canon, len);
  273. length = len;
  274. out:
  275. kfree(canon);
  276. return length;
  277. }
  278. static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
  279. size_t count, loff_t *ppos)
  280. {
  281. char tmpbuf[TMPBUFLEN];
  282. ssize_t length;
  283. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
  284. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  285. }
  286. static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
  287. size_t count, loff_t *ppos)
  288. {
  289. char *page;
  290. ssize_t length;
  291. unsigned int new_value;
  292. length = task_has_security(current, SECURITY__SETCHECKREQPROT);
  293. if (length)
  294. return length;
  295. if (count >= PAGE_SIZE)
  296. return -ENOMEM;
  297. if (*ppos != 0) {
  298. /* No partial writes. */
  299. return -EINVAL;
  300. }
  301. page = (char*)get_zeroed_page(GFP_KERNEL);
  302. if (!page)
  303. return -ENOMEM;
  304. length = -EFAULT;
  305. if (copy_from_user(page, buf, count))
  306. goto out;
  307. length = -EINVAL;
  308. if (sscanf(page, "%u", &new_value) != 1)
  309. goto out;
  310. selinux_checkreqprot = new_value ? 1 : 0;
  311. length = count;
  312. out:
  313. free_page((unsigned long) page);
  314. return length;
  315. }
  316. static const struct file_operations sel_checkreqprot_ops = {
  317. .read = sel_read_checkreqprot,
  318. .write = sel_write_checkreqprot,
  319. };
  320. static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
  321. size_t count, loff_t *ppos)
  322. {
  323. char tmpbuf[TMPBUFLEN];
  324. ssize_t length;
  325. length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
  326. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  327. }
  328. static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
  329. size_t count, loff_t *ppos)
  330. {
  331. char *page;
  332. ssize_t length;
  333. int new_value;
  334. length = task_has_security(current, SECURITY__LOAD_POLICY);
  335. if (length)
  336. return length;
  337. if (count >= PAGE_SIZE)
  338. return -ENOMEM;
  339. if (*ppos != 0) {
  340. /* No partial writes. */
  341. return -EINVAL;
  342. }
  343. page = (char*)get_zeroed_page(GFP_KERNEL);
  344. if (!page)
  345. return -ENOMEM;
  346. length = -EFAULT;
  347. if (copy_from_user(page, buf, count))
  348. goto out;
  349. length = -EINVAL;
  350. if (sscanf(page, "%d", &new_value) != 1)
  351. goto out;
  352. selinux_compat_net = new_value ? 1 : 0;
  353. length = count;
  354. out:
  355. free_page((unsigned long) page);
  356. return length;
  357. }
  358. static const struct file_operations sel_compat_net_ops = {
  359. .read = sel_read_compat_net,
  360. .write = sel_write_compat_net,
  361. };
  362. /*
  363. * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
  364. */
  365. static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
  366. static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
  367. static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
  368. static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
  369. static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
  370. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  371. [SEL_ACCESS] = sel_write_access,
  372. [SEL_CREATE] = sel_write_create,
  373. [SEL_RELABEL] = sel_write_relabel,
  374. [SEL_USER] = sel_write_user,
  375. [SEL_MEMBER] = sel_write_member,
  376. [SEL_CONTEXT] = sel_write_context,
  377. };
  378. static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  379. {
  380. ino_t ino = file->f_path.dentry->d_inode->i_ino;
  381. char *data;
  382. ssize_t rv;
  383. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  384. return -EINVAL;
  385. data = simple_transaction_get(file, buf, size);
  386. if (IS_ERR(data))
  387. return PTR_ERR(data);
  388. rv = write_op[ino](file, data, size);
  389. if (rv>0) {
  390. simple_transaction_set(file, rv);
  391. rv = size;
  392. }
  393. return rv;
  394. }
  395. static const struct file_operations transaction_ops = {
  396. .write = selinux_transaction_write,
  397. .read = simple_transaction_read,
  398. .release = simple_transaction_release,
  399. };
  400. /*
  401. * payload - write methods
  402. * If the method has a response, the response should be put in buf,
  403. * and the length returned. Otherwise return 0 or and -error.
  404. */
  405. static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
  406. {
  407. char *scon, *tcon;
  408. u32 ssid, tsid;
  409. u16 tclass;
  410. u32 req;
  411. struct av_decision avd;
  412. ssize_t length;
  413. length = task_has_security(current, SECURITY__COMPUTE_AV);
  414. if (length)
  415. return length;
  416. length = -ENOMEM;
  417. scon = kzalloc(size+1, GFP_KERNEL);
  418. if (!scon)
  419. return length;
  420. tcon = kzalloc(size+1, GFP_KERNEL);
  421. if (!tcon)
  422. goto out;
  423. length = -EINVAL;
  424. if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
  425. goto out2;
  426. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  427. if (length < 0)
  428. goto out2;
  429. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  430. if (length < 0)
  431. goto out2;
  432. length = security_compute_av(ssid, tsid, tclass, req, &avd);
  433. if (length < 0)
  434. goto out2;
  435. length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
  436. "%x %x %x %x %u",
  437. avd.allowed, avd.decided,
  438. avd.auditallow, avd.auditdeny,
  439. avd.seqno);
  440. out2:
  441. kfree(tcon);
  442. out:
  443. kfree(scon);
  444. return length;
  445. }
  446. static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
  447. {
  448. char *scon, *tcon;
  449. u32 ssid, tsid, newsid;
  450. u16 tclass;
  451. ssize_t length;
  452. char *newcon;
  453. u32 len;
  454. length = task_has_security(current, SECURITY__COMPUTE_CREATE);
  455. if (length)
  456. return length;
  457. length = -ENOMEM;
  458. scon = kzalloc(size+1, GFP_KERNEL);
  459. if (!scon)
  460. return length;
  461. tcon = kzalloc(size+1, GFP_KERNEL);
  462. if (!tcon)
  463. goto out;
  464. length = -EINVAL;
  465. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  466. goto out2;
  467. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  468. if (length < 0)
  469. goto out2;
  470. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  471. if (length < 0)
  472. goto out2;
  473. length = security_transition_sid(ssid, tsid, tclass, &newsid);
  474. if (length < 0)
  475. goto out2;
  476. length = security_sid_to_context(newsid, &newcon, &len);
  477. if (length < 0)
  478. goto out2;
  479. if (len > SIMPLE_TRANSACTION_LIMIT) {
  480. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  481. "max\n", __FUNCTION__, len);
  482. length = -ERANGE;
  483. goto out3;
  484. }
  485. memcpy(buf, newcon, len);
  486. length = len;
  487. out3:
  488. kfree(newcon);
  489. out2:
  490. kfree(tcon);
  491. out:
  492. kfree(scon);
  493. return length;
  494. }
  495. static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
  496. {
  497. char *scon, *tcon;
  498. u32 ssid, tsid, newsid;
  499. u16 tclass;
  500. ssize_t length;
  501. char *newcon;
  502. u32 len;
  503. length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
  504. if (length)
  505. return length;
  506. length = -ENOMEM;
  507. scon = kzalloc(size+1, GFP_KERNEL);
  508. if (!scon)
  509. return length;
  510. tcon = kzalloc(size+1, GFP_KERNEL);
  511. if (!tcon)
  512. goto out;
  513. length = -EINVAL;
  514. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  515. goto out2;
  516. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  517. if (length < 0)
  518. goto out2;
  519. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  520. if (length < 0)
  521. goto out2;
  522. length = security_change_sid(ssid, tsid, tclass, &newsid);
  523. if (length < 0)
  524. goto out2;
  525. length = security_sid_to_context(newsid, &newcon, &len);
  526. if (length < 0)
  527. goto out2;
  528. if (len > SIMPLE_TRANSACTION_LIMIT) {
  529. length = -ERANGE;
  530. goto out3;
  531. }
  532. memcpy(buf, newcon, len);
  533. length = len;
  534. out3:
  535. kfree(newcon);
  536. out2:
  537. kfree(tcon);
  538. out:
  539. kfree(scon);
  540. return length;
  541. }
  542. static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
  543. {
  544. char *con, *user, *ptr;
  545. u32 sid, *sids;
  546. ssize_t length;
  547. char *newcon;
  548. int i, rc;
  549. u32 len, nsids;
  550. length = task_has_security(current, SECURITY__COMPUTE_USER);
  551. if (length)
  552. return length;
  553. length = -ENOMEM;
  554. con = kzalloc(size+1, GFP_KERNEL);
  555. if (!con)
  556. return length;
  557. user = kzalloc(size+1, GFP_KERNEL);
  558. if (!user)
  559. goto out;
  560. length = -EINVAL;
  561. if (sscanf(buf, "%s %s", con, user) != 2)
  562. goto out2;
  563. length = security_context_to_sid(con, strlen(con)+1, &sid);
  564. if (length < 0)
  565. goto out2;
  566. length = security_get_user_sids(sid, user, &sids, &nsids);
  567. if (length < 0)
  568. goto out2;
  569. length = sprintf(buf, "%u", nsids) + 1;
  570. ptr = buf + length;
  571. for (i = 0; i < nsids; i++) {
  572. rc = security_sid_to_context(sids[i], &newcon, &len);
  573. if (rc) {
  574. length = rc;
  575. goto out3;
  576. }
  577. if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
  578. kfree(newcon);
  579. length = -ERANGE;
  580. goto out3;
  581. }
  582. memcpy(ptr, newcon, len);
  583. kfree(newcon);
  584. ptr += len;
  585. length += len;
  586. }
  587. out3:
  588. kfree(sids);
  589. out2:
  590. kfree(user);
  591. out:
  592. kfree(con);
  593. return length;
  594. }
  595. static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
  596. {
  597. char *scon, *tcon;
  598. u32 ssid, tsid, newsid;
  599. u16 tclass;
  600. ssize_t length;
  601. char *newcon;
  602. u32 len;
  603. length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
  604. if (length)
  605. return length;
  606. length = -ENOMEM;
  607. scon = kzalloc(size+1, GFP_KERNEL);
  608. if (!scon)
  609. return length;
  610. tcon = kzalloc(size+1, GFP_KERNEL);
  611. if (!tcon)
  612. goto out;
  613. length = -EINVAL;
  614. if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
  615. goto out2;
  616. length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
  617. if (length < 0)
  618. goto out2;
  619. length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
  620. if (length < 0)
  621. goto out2;
  622. length = security_member_sid(ssid, tsid, tclass, &newsid);
  623. if (length < 0)
  624. goto out2;
  625. length = security_sid_to_context(newsid, &newcon, &len);
  626. if (length < 0)
  627. goto out2;
  628. if (len > SIMPLE_TRANSACTION_LIMIT) {
  629. printk(KERN_ERR "%s: context size (%u) exceeds payload "
  630. "max\n", __FUNCTION__, len);
  631. length = -ERANGE;
  632. goto out3;
  633. }
  634. memcpy(buf, newcon, len);
  635. length = len;
  636. out3:
  637. kfree(newcon);
  638. out2:
  639. kfree(tcon);
  640. out:
  641. kfree(scon);
  642. return length;
  643. }
  644. static struct inode *sel_make_inode(struct super_block *sb, int mode)
  645. {
  646. struct inode *ret = new_inode(sb);
  647. if (ret) {
  648. ret->i_mode = mode;
  649. ret->i_uid = ret->i_gid = 0;
  650. ret->i_blocks = 0;
  651. ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
  652. }
  653. return ret;
  654. }
  655. #define BOOL_INO_OFFSET 30
  656. static ssize_t sel_read_bool(struct file *filep, char __user *buf,
  657. size_t count, loff_t *ppos)
  658. {
  659. char *page = NULL;
  660. ssize_t length;
  661. ssize_t ret;
  662. int cur_enforcing;
  663. struct inode *inode;
  664. mutex_lock(&sel_mutex);
  665. ret = -EFAULT;
  666. /* check to see if this file has been deleted */
  667. if (!filep->f_op)
  668. goto out;
  669. if (count > PAGE_SIZE) {
  670. ret = -EINVAL;
  671. goto out;
  672. }
  673. if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
  674. ret = -ENOMEM;
  675. goto out;
  676. }
  677. inode = filep->f_path.dentry->d_inode;
  678. cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
  679. if (cur_enforcing < 0) {
  680. ret = cur_enforcing;
  681. goto out;
  682. }
  683. length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
  684. bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
  685. ret = simple_read_from_buffer(buf, count, ppos, page, length);
  686. out:
  687. mutex_unlock(&sel_mutex);
  688. if (page)
  689. free_page((unsigned long)page);
  690. return ret;
  691. }
  692. static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
  693. size_t count, loff_t *ppos)
  694. {
  695. char *page = NULL;
  696. ssize_t length = -EFAULT;
  697. int new_value;
  698. struct inode *inode;
  699. mutex_lock(&sel_mutex);
  700. length = task_has_security(current, SECURITY__SETBOOL);
  701. if (length)
  702. goto out;
  703. /* check to see if this file has been deleted */
  704. if (!filep->f_op)
  705. goto out;
  706. if (count >= PAGE_SIZE) {
  707. length = -ENOMEM;
  708. goto out;
  709. }
  710. if (*ppos != 0) {
  711. /* No partial writes. */
  712. goto out;
  713. }
  714. page = (char*)get_zeroed_page(GFP_KERNEL);
  715. if (!page) {
  716. length = -ENOMEM;
  717. goto out;
  718. }
  719. if (copy_from_user(page, buf, count))
  720. goto out;
  721. length = -EINVAL;
  722. if (sscanf(page, "%d", &new_value) != 1)
  723. goto out;
  724. if (new_value)
  725. new_value = 1;
  726. inode = filep->f_path.dentry->d_inode;
  727. bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
  728. length = count;
  729. out:
  730. mutex_unlock(&sel_mutex);
  731. if (page)
  732. free_page((unsigned long) page);
  733. return length;
  734. }
  735. static const struct file_operations sel_bool_ops = {
  736. .read = sel_read_bool,
  737. .write = sel_write_bool,
  738. };
  739. static ssize_t sel_commit_bools_write(struct file *filep,
  740. const char __user *buf,
  741. size_t count, loff_t *ppos)
  742. {
  743. char *page = NULL;
  744. ssize_t length = -EFAULT;
  745. int new_value;
  746. mutex_lock(&sel_mutex);
  747. length = task_has_security(current, SECURITY__SETBOOL);
  748. if (length)
  749. goto out;
  750. /* check to see if this file has been deleted */
  751. if (!filep->f_op)
  752. goto out;
  753. if (count >= PAGE_SIZE) {
  754. length = -ENOMEM;
  755. goto out;
  756. }
  757. if (*ppos != 0) {
  758. /* No partial writes. */
  759. goto out;
  760. }
  761. page = (char*)get_zeroed_page(GFP_KERNEL);
  762. if (!page) {
  763. length = -ENOMEM;
  764. goto out;
  765. }
  766. if (copy_from_user(page, buf, count))
  767. goto out;
  768. length = -EINVAL;
  769. if (sscanf(page, "%d", &new_value) != 1)
  770. goto out;
  771. if (new_value && bool_pending_values) {
  772. security_set_bools(bool_num, bool_pending_values);
  773. }
  774. length = count;
  775. out:
  776. mutex_unlock(&sel_mutex);
  777. if (page)
  778. free_page((unsigned long) page);
  779. return length;
  780. }
  781. static const struct file_operations sel_commit_bools_ops = {
  782. .write = sel_commit_bools_write,
  783. };
  784. /* delete booleans - partial revoke() from
  785. * fs/proc/generic.c proc_kill_inodes */
  786. static void sel_remove_bools(struct dentry *de)
  787. {
  788. struct list_head *p, *node;
  789. struct super_block *sb = de->d_sb;
  790. spin_lock(&dcache_lock);
  791. node = de->d_subdirs.next;
  792. while (node != &de->d_subdirs) {
  793. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  794. list_del_init(node);
  795. if (d->d_inode) {
  796. d = dget_locked(d);
  797. spin_unlock(&dcache_lock);
  798. d_delete(d);
  799. simple_unlink(de->d_inode, d);
  800. dput(d);
  801. spin_lock(&dcache_lock);
  802. }
  803. node = de->d_subdirs.next;
  804. }
  805. spin_unlock(&dcache_lock);
  806. file_list_lock();
  807. list_for_each(p, &sb->s_files) {
  808. struct file * filp = list_entry(p, struct file, f_u.fu_list);
  809. struct dentry * dentry = filp->f_path.dentry;
  810. if (dentry->d_parent != de) {
  811. continue;
  812. }
  813. filp->f_op = NULL;
  814. }
  815. file_list_unlock();
  816. }
  817. #define BOOL_DIR_NAME "booleans"
  818. static int sel_make_bools(void)
  819. {
  820. int i, ret = 0;
  821. ssize_t len;
  822. struct dentry *dentry = NULL;
  823. struct dentry *dir = bool_dir;
  824. struct inode *inode = NULL;
  825. struct inode_security_struct *isec;
  826. char **names = NULL, *page;
  827. int num;
  828. int *values = NULL;
  829. u32 sid;
  830. /* remove any existing files */
  831. kfree(bool_pending_values);
  832. bool_pending_values = NULL;
  833. sel_remove_bools(dir);
  834. if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
  835. return -ENOMEM;
  836. ret = security_get_bools(&num, &names, &values);
  837. if (ret != 0)
  838. goto out;
  839. for (i = 0; i < num; i++) {
  840. dentry = d_alloc_name(dir, names[i]);
  841. if (!dentry) {
  842. ret = -ENOMEM;
  843. goto err;
  844. }
  845. inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
  846. if (!inode) {
  847. ret = -ENOMEM;
  848. goto err;
  849. }
  850. len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
  851. if (len < 0) {
  852. ret = -EINVAL;
  853. goto err;
  854. } else if (len >= PAGE_SIZE) {
  855. ret = -ENAMETOOLONG;
  856. goto err;
  857. }
  858. isec = (struct inode_security_struct*)inode->i_security;
  859. if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
  860. goto err;
  861. isec->sid = sid;
  862. isec->initialized = 1;
  863. inode->i_fop = &sel_bool_ops;
  864. inode->i_ino = i + BOOL_INO_OFFSET;
  865. d_add(dentry, inode);
  866. }
  867. bool_num = num;
  868. bool_pending_values = values;
  869. out:
  870. free_page((unsigned long)page);
  871. if (names) {
  872. for (i = 0; i < num; i++)
  873. kfree(names[i]);
  874. kfree(names);
  875. }
  876. return ret;
  877. err:
  878. kfree(values);
  879. sel_remove_bools(dir);
  880. ret = -ENOMEM;
  881. goto out;
  882. }
  883. #define NULL_FILE_NAME "null"
  884. struct dentry *selinux_null = NULL;
  885. static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
  886. size_t count, loff_t *ppos)
  887. {
  888. char tmpbuf[TMPBUFLEN];
  889. ssize_t length;
  890. length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
  891. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  892. }
  893. static ssize_t sel_write_avc_cache_threshold(struct file * file,
  894. const char __user * buf,
  895. size_t count, loff_t *ppos)
  896. {
  897. char *page;
  898. ssize_t ret;
  899. int new_value;
  900. if (count >= PAGE_SIZE) {
  901. ret = -ENOMEM;
  902. goto out;
  903. }
  904. if (*ppos != 0) {
  905. /* No partial writes. */
  906. ret = -EINVAL;
  907. goto out;
  908. }
  909. page = (char*)get_zeroed_page(GFP_KERNEL);
  910. if (!page) {
  911. ret = -ENOMEM;
  912. goto out;
  913. }
  914. if (copy_from_user(page, buf, count)) {
  915. ret = -EFAULT;
  916. goto out_free;
  917. }
  918. if (sscanf(page, "%u", &new_value) != 1) {
  919. ret = -EINVAL;
  920. goto out;
  921. }
  922. if (new_value != avc_cache_threshold) {
  923. ret = task_has_security(current, SECURITY__SETSECPARAM);
  924. if (ret)
  925. goto out_free;
  926. avc_cache_threshold = new_value;
  927. }
  928. ret = count;
  929. out_free:
  930. free_page((unsigned long)page);
  931. out:
  932. return ret;
  933. }
  934. static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
  935. size_t count, loff_t *ppos)
  936. {
  937. char *page;
  938. ssize_t ret = 0;
  939. page = (char *)__get_free_page(GFP_KERNEL);
  940. if (!page) {
  941. ret = -ENOMEM;
  942. goto out;
  943. }
  944. ret = avc_get_hash_stats(page);
  945. if (ret >= 0)
  946. ret = simple_read_from_buffer(buf, count, ppos, page, ret);
  947. free_page((unsigned long)page);
  948. out:
  949. return ret;
  950. }
  951. static const struct file_operations sel_avc_cache_threshold_ops = {
  952. .read = sel_read_avc_cache_threshold,
  953. .write = sel_write_avc_cache_threshold,
  954. };
  955. static const struct file_operations sel_avc_hash_stats_ops = {
  956. .read = sel_read_avc_hash_stats,
  957. };
  958. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  959. static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
  960. {
  961. int cpu;
  962. for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
  963. if (!cpu_possible(cpu))
  964. continue;
  965. *idx = cpu + 1;
  966. return &per_cpu(avc_cache_stats, cpu);
  967. }
  968. return NULL;
  969. }
  970. static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
  971. {
  972. loff_t n = *pos - 1;
  973. if (*pos == 0)
  974. return SEQ_START_TOKEN;
  975. return sel_avc_get_stat_idx(&n);
  976. }
  977. static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  978. {
  979. return sel_avc_get_stat_idx(pos);
  980. }
  981. static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
  982. {
  983. struct avc_cache_stats *st = v;
  984. if (v == SEQ_START_TOKEN)
  985. seq_printf(seq, "lookups hits misses allocations reclaims "
  986. "frees\n");
  987. else
  988. seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
  989. st->hits, st->misses, st->allocations,
  990. st->reclaims, st->frees);
  991. return 0;
  992. }
  993. static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
  994. { }
  995. static struct seq_operations sel_avc_cache_stats_seq_ops = {
  996. .start = sel_avc_stats_seq_start,
  997. .next = sel_avc_stats_seq_next,
  998. .show = sel_avc_stats_seq_show,
  999. .stop = sel_avc_stats_seq_stop,
  1000. };
  1001. static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
  1002. {
  1003. return seq_open(file, &sel_avc_cache_stats_seq_ops);
  1004. }
  1005. static const struct file_operations sel_avc_cache_stats_ops = {
  1006. .open = sel_open_avc_cache_stats,
  1007. .read = seq_read,
  1008. .llseek = seq_lseek,
  1009. .release = seq_release,
  1010. };
  1011. #endif
  1012. static int sel_make_avc_files(struct dentry *dir)
  1013. {
  1014. int i, ret = 0;
  1015. static struct tree_descr files[] = {
  1016. { "cache_threshold",
  1017. &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
  1018. { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
  1019. #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
  1020. { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
  1021. #endif
  1022. };
  1023. for (i = 0; i < ARRAY_SIZE(files); i++) {
  1024. struct inode *inode;
  1025. struct dentry *dentry;
  1026. dentry = d_alloc_name(dir, files[i].name);
  1027. if (!dentry) {
  1028. ret = -ENOMEM;
  1029. goto out;
  1030. }
  1031. inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
  1032. if (!inode) {
  1033. ret = -ENOMEM;
  1034. goto out;
  1035. }
  1036. inode->i_fop = files[i].ops;
  1037. d_add(dentry, inode);
  1038. }
  1039. out:
  1040. return ret;
  1041. }
  1042. static int sel_make_dir(struct inode *dir, struct dentry *dentry)
  1043. {
  1044. int ret = 0;
  1045. struct inode *inode;
  1046. inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1047. if (!inode) {
  1048. ret = -ENOMEM;
  1049. goto out;
  1050. }
  1051. inode->i_op = &simple_dir_inode_operations;
  1052. inode->i_fop = &simple_dir_operations;
  1053. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  1054. inc_nlink(inode);
  1055. d_add(dentry, inode);
  1056. /* bump link count on parent directory, too */
  1057. inc_nlink(dir);
  1058. out:
  1059. return ret;
  1060. }
  1061. static int sel_fill_super(struct super_block * sb, void * data, int silent)
  1062. {
  1063. int ret;
  1064. struct dentry *dentry;
  1065. struct inode *inode, *root_inode;
  1066. struct inode_security_struct *isec;
  1067. static struct tree_descr selinux_files[] = {
  1068. [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
  1069. [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
  1070. [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
  1071. [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
  1072. [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
  1073. [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
  1074. [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
  1075. [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
  1076. [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
  1077. [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
  1078. [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
  1079. [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
  1080. [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
  1081. [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
  1082. /* last one */ {""}
  1083. };
  1084. ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
  1085. if (ret)
  1086. goto err;
  1087. root_inode = sb->s_root->d_inode;
  1088. dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
  1089. if (!dentry) {
  1090. ret = -ENOMEM;
  1091. goto err;
  1092. }
  1093. ret = sel_make_dir(root_inode, dentry);
  1094. if (ret)
  1095. goto err;
  1096. bool_dir = dentry;
  1097. dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
  1098. if (!dentry) {
  1099. ret = -ENOMEM;
  1100. goto err;
  1101. }
  1102. inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
  1103. if (!inode) {
  1104. ret = -ENOMEM;
  1105. goto err;
  1106. }
  1107. isec = (struct inode_security_struct*)inode->i_security;
  1108. isec->sid = SECINITSID_DEVNULL;
  1109. isec->sclass = SECCLASS_CHR_FILE;
  1110. isec->initialized = 1;
  1111. init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
  1112. d_add(dentry, inode);
  1113. selinux_null = dentry;
  1114. dentry = d_alloc_name(sb->s_root, "avc");
  1115. if (!dentry) {
  1116. ret = -ENOMEM;
  1117. goto err;
  1118. }
  1119. ret = sel_make_dir(root_inode, dentry);
  1120. if (ret)
  1121. goto err;
  1122. ret = sel_make_avc_files(dentry);
  1123. if (ret)
  1124. goto err;
  1125. out:
  1126. return ret;
  1127. err:
  1128. printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
  1129. goto out;
  1130. }
  1131. static int sel_get_sb(struct file_system_type *fs_type,
  1132. int flags, const char *dev_name, void *data,
  1133. struct vfsmount *mnt)
  1134. {
  1135. return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
  1136. }
  1137. static struct file_system_type sel_fs_type = {
  1138. .name = "selinuxfs",
  1139. .get_sb = sel_get_sb,
  1140. .kill_sb = kill_litter_super,
  1141. };
  1142. struct vfsmount *selinuxfs_mount;
  1143. static int __init init_sel_fs(void)
  1144. {
  1145. int err;
  1146. if (!selinux_enabled)
  1147. return 0;
  1148. err = register_filesystem(&sel_fs_type);
  1149. if (!err) {
  1150. selinuxfs_mount = kern_mount(&sel_fs_type);
  1151. if (IS_ERR(selinuxfs_mount)) {
  1152. printk(KERN_ERR "selinuxfs: could not mount!\n");
  1153. err = PTR_ERR(selinuxfs_mount);
  1154. selinuxfs_mount = NULL;
  1155. }
  1156. }
  1157. return err;
  1158. }
  1159. __initcall(init_sel_fs);
  1160. #ifdef CONFIG_SECURITY_SELINUX_DISABLE
  1161. void exit_sel_fs(void)
  1162. {
  1163. unregister_filesystem(&sel_fs_type);
  1164. }
  1165. #endif