selinuxfs.c 38 KB

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