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", 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 %x",
  446. avd.allowed, 0xffffffff,
  447. avd.auditallow, avd.auditdeny,
  448. avd.seqno, avd.flags);
  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. page = (char *)get_zeroed_page(GFP_KERNEL);
  678. if (!page) {
  679. ret = -ENOMEM;
  680. goto out;
  681. }
  682. cur_enforcing = security_get_bool_value(index);
  683. if (cur_enforcing < 0) {
  684. ret = cur_enforcing;
  685. goto out;
  686. }
  687. length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
  688. bool_pending_values[index]);
  689. ret = simple_read_from_buffer(buf, count, ppos, page, length);
  690. out:
  691. mutex_unlock(&sel_mutex);
  692. if (page)
  693. free_page((unsigned long)page);
  694. return ret;
  695. }
  696. static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
  697. size_t count, loff_t *ppos)
  698. {
  699. char *page = NULL;
  700. ssize_t length;
  701. int new_value;
  702. struct inode *inode = filep->f_path.dentry->d_inode;
  703. unsigned index = inode->i_ino & SEL_INO_MASK;
  704. const char *name = filep->f_path.dentry->d_name.name;
  705. mutex_lock(&sel_mutex);
  706. length = task_has_security(current, SECURITY__SETBOOL);
  707. if (length)
  708. goto out;
  709. if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
  710. length = -EINVAL;
  711. goto out;
  712. }
  713. if (count >= PAGE_SIZE) {
  714. length = -ENOMEM;
  715. goto out;
  716. }
  717. if (*ppos != 0) {
  718. /* No partial writes. */
  719. length = -EINVAL;
  720. goto out;
  721. }
  722. page = (char *)get_zeroed_page(GFP_KERNEL);
  723. if (!page) {
  724. length = -ENOMEM;
  725. goto out;
  726. }
  727. length = -EFAULT;
  728. if (copy_from_user(page, buf, count))
  729. goto out;
  730. length = -EINVAL;
  731. if (sscanf(page, "%d", &new_value) != 1)
  732. goto out;
  733. if (new_value)
  734. new_value = 1;
  735. bool_pending_values[index] = new_value;
  736. length = count;
  737. out:
  738. mutex_unlock(&sel_mutex);
  739. if (page)
  740. free_page((unsigned long) page);
  741. return length;
  742. }
  743. static const struct file_operations sel_bool_ops = {
  744. .read = sel_read_bool,
  745. .write = sel_write_bool,
  746. };
  747. static ssize_t sel_commit_bools_write(struct file *filep,
  748. const char __user *buf,
  749. size_t count, loff_t *ppos)
  750. {
  751. char *page = NULL;
  752. ssize_t length;
  753. int new_value;
  754. mutex_lock(&sel_mutex);
  755. length = task_has_security(current, SECURITY__SETBOOL);
  756. if (length)
  757. goto out;
  758. if (count >= PAGE_SIZE) {
  759. length = -ENOMEM;
  760. goto out;
  761. }
  762. if (*ppos != 0) {
  763. /* No partial writes. */
  764. goto out;
  765. }
  766. page = (char *)get_zeroed_page(GFP_KERNEL);
  767. if (!page) {
  768. length = -ENOMEM;
  769. goto out;
  770. }
  771. length = -EFAULT;
  772. if (copy_from_user(page, buf, count))
  773. goto out;
  774. length = -EINVAL;
  775. if (sscanf(page, "%d", &new_value) != 1)
  776. goto out;
  777. if (new_value && bool_pending_values)
  778. security_set_bools(bool_num, bool_pending_values);
  779. length = count;
  780. out:
  781. mutex_unlock(&sel_mutex);
  782. if (page)
  783. free_page((unsigned long) page);
  784. return length;
  785. }
  786. static const struct file_operations sel_commit_bools_ops = {
  787. .write = sel_commit_bools_write,
  788. };
  789. static void sel_remove_entries(struct dentry *de)
  790. {
  791. struct list_head *node;
  792. spin_lock(&dcache_lock);
  793. node = de->d_subdirs.next;
  794. while (node != &de->d_subdirs) {
  795. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  796. list_del_init(node);
  797. if (d->d_inode) {
  798. d = dget_locked(d);
  799. spin_unlock(&dcache_lock);
  800. d_delete(d);
  801. simple_unlink(de->d_inode, d);
  802. dput(d);
  803. spin_lock(&dcache_lock);
  804. }
  805. node = de->d_subdirs.next;
  806. }
  807. spin_unlock(&dcache_lock);
  808. }
  809. #define BOOL_DIR_NAME "booleans"
  810. static int sel_make_bools(void)
  811. {
  812. int i, ret = 0;
  813. ssize_t len;
  814. struct dentry *dentry = NULL;
  815. struct dentry *dir = bool_dir;
  816. struct inode *inode = NULL;
  817. struct inode_security_struct *isec;
  818. char **names = NULL, *page;
  819. int num;
  820. int *values = NULL;
  821. u32 sid;
  822. /* remove any existing files */
  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