selinuxfs.c 38 KB

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