selinuxfs.c 38 KB

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