binfmt_misc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * binfmt_misc.c
  4. *
  5. * Copyright (C) 1997 Richard Günther
  6. *
  7. * binfmt_misc detects binaries via a magic or filename extension and invokes
  8. * a specified wrapper. See Documentation/admin-guide/binfmt-misc.rst for more details.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/sched/mm.h>
  15. #include <linux/magic.h>
  16. #include <linux/binfmts.h>
  17. #include <linux/slab.h>
  18. #include <linux/ctype.h>
  19. #include <linux/string_helpers.h>
  20. #include <linux/file.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/namei.h>
  23. #include <linux/mount.h>
  24. #include <linux/fs_context.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/fs.h>
  27. #include <linux/uaccess.h>
  28. #include "internal.h"
  29. #ifdef DEBUG
  30. # define USE_DEBUG 1
  31. #else
  32. # define USE_DEBUG 0
  33. #endif
  34. enum {
  35. VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
  36. };
  37. static LIST_HEAD(entries);
  38. static int enabled = 1;
  39. enum {Enabled, Magic};
  40. #define MISC_FMT_PRESERVE_ARGV0 (1 << 31)
  41. #define MISC_FMT_OPEN_BINARY (1 << 30)
  42. #define MISC_FMT_CREDENTIALS (1 << 29)
  43. #define MISC_FMT_OPEN_FILE (1 << 28)
  44. typedef struct {
  45. struct list_head list;
  46. unsigned long flags; /* type, status, etc. */
  47. int offset; /* offset of magic */
  48. int size; /* size of magic/mask */
  49. char *magic; /* magic or filename extension */
  50. char *mask; /* mask, NULL for exact match */
  51. const char *interpreter; /* filename of interpreter */
  52. char *name;
  53. struct dentry *dentry;
  54. struct file *interp_file;
  55. } Node;
  56. static DEFINE_RWLOCK(entries_lock);
  57. static struct file_system_type bm_fs_type;
  58. static struct vfsmount *bm_mnt;
  59. static int entry_count;
  60. /*
  61. * Max length of the register string. Determined by:
  62. * - 7 delimiters
  63. * - name: ~50 bytes
  64. * - type: 1 byte
  65. * - offset: 3 bytes (has to be smaller than BINPRM_BUF_SIZE)
  66. * - magic: 128 bytes (512 in escaped form)
  67. * - mask: 128 bytes (512 in escaped form)
  68. * - interp: ~50 bytes
  69. * - flags: 5 bytes
  70. * Round that up a bit, and then back off to hold the internal data
  71. * (like struct Node).
  72. */
  73. #define MAX_REGISTER_LENGTH 1920
  74. /*
  75. * Check if we support the binfmt
  76. * if we do, return the node, else NULL
  77. * locking is done in load_misc_binary
  78. */
  79. static Node *check_file(struct linux_binprm *bprm)
  80. {
  81. char *p = strrchr(bprm->interp, '.');
  82. struct list_head *l;
  83. /* Walk all the registered handlers. */
  84. list_for_each(l, &entries) {
  85. Node *e = list_entry(l, Node, list);
  86. char *s;
  87. int j;
  88. /* Make sure this one is currently enabled. */
  89. if (!test_bit(Enabled, &e->flags))
  90. continue;
  91. /* Do matching based on extension if applicable. */
  92. if (!test_bit(Magic, &e->flags)) {
  93. if (p && !strcmp(e->magic, p + 1))
  94. return e;
  95. continue;
  96. }
  97. /* Do matching based on magic & mask. */
  98. s = bprm->buf + e->offset;
  99. if (e->mask) {
  100. for (j = 0; j < e->size; j++)
  101. if ((*s++ ^ e->magic[j]) & e->mask[j])
  102. break;
  103. } else {
  104. for (j = 0; j < e->size; j++)
  105. if ((*s++ ^ e->magic[j]))
  106. break;
  107. }
  108. if (j == e->size)
  109. return e;
  110. }
  111. return NULL;
  112. }
  113. /*
  114. * the loader itself
  115. */
  116. static int load_misc_binary(struct linux_binprm *bprm)
  117. {
  118. Node *fmt;
  119. struct file *interp_file = NULL;
  120. int retval;
  121. retval = -ENOEXEC;
  122. if (!enabled)
  123. return retval;
  124. /* to keep locking time low, we copy the interpreter string */
  125. read_lock(&entries_lock);
  126. fmt = check_file(bprm);
  127. if (fmt)
  128. dget(fmt->dentry);
  129. read_unlock(&entries_lock);
  130. if (!fmt)
  131. return retval;
  132. /* Need to be able to load the file after exec */
  133. retval = -ENOENT;
  134. if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
  135. goto ret;
  136. if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
  137. retval = remove_arg_zero(bprm);
  138. if (retval)
  139. goto ret;
  140. }
  141. if (fmt->flags & MISC_FMT_OPEN_BINARY)
  142. bprm->have_execfd = 1;
  143. /* make argv[1] be the path to the binary */
  144. retval = copy_string_kernel(bprm->interp, bprm);
  145. if (retval < 0)
  146. goto ret;
  147. bprm->argc++;
  148. /* add the interp as argv[0] */
  149. retval = copy_string_kernel(fmt->interpreter, bprm);
  150. if (retval < 0)
  151. goto ret;
  152. bprm->argc++;
  153. /* Update interp in case binfmt_script needs it. */
  154. retval = bprm_change_interp(fmt->interpreter, bprm);
  155. if (retval < 0)
  156. goto ret;
  157. if (fmt->flags & MISC_FMT_OPEN_FILE) {
  158. interp_file = file_clone_open(fmt->interp_file);
  159. if (!IS_ERR(interp_file))
  160. deny_write_access(interp_file);
  161. } else {
  162. interp_file = open_exec(fmt->interpreter);
  163. }
  164. retval = PTR_ERR(interp_file);
  165. if (IS_ERR(interp_file))
  166. goto ret;
  167. bprm->interpreter = interp_file;
  168. if (fmt->flags & MISC_FMT_CREDENTIALS)
  169. bprm->execfd_creds = 1;
  170. retval = 0;
  171. ret:
  172. dput(fmt->dentry);
  173. return retval;
  174. }
  175. /* Command parsers */
  176. /*
  177. * parses and copies one argument enclosed in del from *sp to *dp,
  178. * recognising the \x special.
  179. * returns pointer to the copied argument or NULL in case of an
  180. * error (and sets err) or null argument length.
  181. */
  182. static char *scanarg(char *s, char del)
  183. {
  184. char c;
  185. while ((c = *s++) != del) {
  186. if (c == '\\' && *s == 'x') {
  187. s++;
  188. if (!isxdigit(*s++))
  189. return NULL;
  190. if (!isxdigit(*s++))
  191. return NULL;
  192. }
  193. }
  194. s[-1] ='\0';
  195. return s;
  196. }
  197. static char *check_special_flags(char *sfs, Node *e)
  198. {
  199. char *p = sfs;
  200. int cont = 1;
  201. /* special flags */
  202. while (cont) {
  203. switch (*p) {
  204. case 'P':
  205. pr_debug("register: flag: P (preserve argv0)\n");
  206. p++;
  207. e->flags |= MISC_FMT_PRESERVE_ARGV0;
  208. break;
  209. case 'O':
  210. pr_debug("register: flag: O (open binary)\n");
  211. p++;
  212. e->flags |= MISC_FMT_OPEN_BINARY;
  213. break;
  214. case 'C':
  215. pr_debug("register: flag: C (preserve creds)\n");
  216. p++;
  217. /* this flags also implies the
  218. open-binary flag */
  219. e->flags |= (MISC_FMT_CREDENTIALS |
  220. MISC_FMT_OPEN_BINARY);
  221. break;
  222. case 'F':
  223. pr_debug("register: flag: F: open interpreter file now\n");
  224. p++;
  225. e->flags |= MISC_FMT_OPEN_FILE;
  226. break;
  227. default:
  228. cont = 0;
  229. }
  230. }
  231. return p;
  232. }
  233. /*
  234. * This registers a new binary format, it recognises the syntax
  235. * ':name:type:offset:magic:mask:interpreter:flags'
  236. * where the ':' is the IFS, that can be chosen with the first char
  237. */
  238. static Node *create_entry(const char __user *buffer, size_t count)
  239. {
  240. Node *e;
  241. int memsize, err;
  242. char *buf, *p;
  243. char del;
  244. pr_debug("register: received %zu bytes\n", count);
  245. /* some sanity checks */
  246. err = -EINVAL;
  247. if ((count < 11) || (count > MAX_REGISTER_LENGTH))
  248. goto out;
  249. err = -ENOMEM;
  250. memsize = sizeof(Node) + count + 8;
  251. e = kmalloc(memsize, GFP_KERNEL);
  252. if (!e)
  253. goto out;
  254. p = buf = (char *)e + sizeof(Node);
  255. memset(e, 0, sizeof(Node));
  256. if (copy_from_user(buf, buffer, count))
  257. goto efault;
  258. del = *p++; /* delimeter */
  259. pr_debug("register: delim: %#x {%c}\n", del, del);
  260. /* Pad the buffer with the delim to simplify parsing below. */
  261. memset(buf + count, del, 8);
  262. /* Parse the 'name' field. */
  263. e->name = p;
  264. p = strchr(p, del);
  265. if (!p)
  266. goto einval;
  267. *p++ = '\0';
  268. if (!e->name[0] ||
  269. !strcmp(e->name, ".") ||
  270. !strcmp(e->name, "..") ||
  271. strchr(e->name, '/'))
  272. goto einval;
  273. pr_debug("register: name: {%s}\n", e->name);
  274. /* Parse the 'type' field. */
  275. switch (*p++) {
  276. case 'E':
  277. pr_debug("register: type: E (extension)\n");
  278. e->flags = 1 << Enabled;
  279. break;
  280. case 'M':
  281. pr_debug("register: type: M (magic)\n");
  282. e->flags = (1 << Enabled) | (1 << Magic);
  283. break;
  284. default:
  285. goto einval;
  286. }
  287. if (*p++ != del)
  288. goto einval;
  289. if (test_bit(Magic, &e->flags)) {
  290. /* Handle the 'M' (magic) format. */
  291. char *s;
  292. /* Parse the 'offset' field. */
  293. s = strchr(p, del);
  294. if (!s)
  295. goto einval;
  296. *s = '\0';
  297. if (p != s) {
  298. int r = kstrtoint(p, 10, &e->offset);
  299. if (r != 0 || e->offset < 0)
  300. goto einval;
  301. }
  302. p = s;
  303. if (*p++)
  304. goto einval;
  305. pr_debug("register: offset: %#x\n", e->offset);
  306. /* Parse the 'magic' field. */
  307. e->magic = p;
  308. p = scanarg(p, del);
  309. if (!p)
  310. goto einval;
  311. if (!e->magic[0])
  312. goto einval;
  313. if (USE_DEBUG)
  314. print_hex_dump_bytes(
  315. KBUILD_MODNAME ": register: magic[raw]: ",
  316. DUMP_PREFIX_NONE, e->magic, p - e->magic);
  317. /* Parse the 'mask' field. */
  318. e->mask = p;
  319. p = scanarg(p, del);
  320. if (!p)
  321. goto einval;
  322. if (!e->mask[0]) {
  323. e->mask = NULL;
  324. pr_debug("register: mask[raw]: none\n");
  325. } else if (USE_DEBUG)
  326. print_hex_dump_bytes(
  327. KBUILD_MODNAME ": register: mask[raw]: ",
  328. DUMP_PREFIX_NONE, e->mask, p - e->mask);
  329. /*
  330. * Decode the magic & mask fields.
  331. * Note: while we might have accepted embedded NUL bytes from
  332. * above, the unescape helpers here will stop at the first one
  333. * it encounters.
  334. */
  335. e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
  336. if (e->mask &&
  337. string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
  338. goto einval;
  339. if (e->size > BINPRM_BUF_SIZE ||
  340. BINPRM_BUF_SIZE - e->size < e->offset)
  341. goto einval;
  342. pr_debug("register: magic/mask length: %i\n", e->size);
  343. if (USE_DEBUG) {
  344. print_hex_dump_bytes(
  345. KBUILD_MODNAME ": register: magic[decoded]: ",
  346. DUMP_PREFIX_NONE, e->magic, e->size);
  347. if (e->mask) {
  348. int i;
  349. char *masked = kmalloc(e->size, GFP_KERNEL);
  350. print_hex_dump_bytes(
  351. KBUILD_MODNAME ": register: mask[decoded]: ",
  352. DUMP_PREFIX_NONE, e->mask, e->size);
  353. if (masked) {
  354. for (i = 0; i < e->size; ++i)
  355. masked[i] = e->magic[i] & e->mask[i];
  356. print_hex_dump_bytes(
  357. KBUILD_MODNAME ": register: magic[masked]: ",
  358. DUMP_PREFIX_NONE, masked, e->size);
  359. kfree(masked);
  360. }
  361. }
  362. }
  363. } else {
  364. /* Handle the 'E' (extension) format. */
  365. /* Skip the 'offset' field. */
  366. p = strchr(p, del);
  367. if (!p)
  368. goto einval;
  369. *p++ = '\0';
  370. /* Parse the 'magic' field. */
  371. e->magic = p;
  372. p = strchr(p, del);
  373. if (!p)
  374. goto einval;
  375. *p++ = '\0';
  376. if (!e->magic[0] || strchr(e->magic, '/'))
  377. goto einval;
  378. pr_debug("register: extension: {%s}\n", e->magic);
  379. /* Skip the 'mask' field. */
  380. p = strchr(p, del);
  381. if (!p)
  382. goto einval;
  383. *p++ = '\0';
  384. }
  385. /* Parse the 'interpreter' field. */
  386. e->interpreter = p;
  387. p = strchr(p, del);
  388. if (!p)
  389. goto einval;
  390. *p++ = '\0';
  391. if (!e->interpreter[0])
  392. goto einval;
  393. pr_debug("register: interpreter: {%s}\n", e->interpreter);
  394. /* Parse the 'flags' field. */
  395. p = check_special_flags(p, e);
  396. if (*p == '\n')
  397. p++;
  398. if (p != buf + count)
  399. goto einval;
  400. return e;
  401. out:
  402. return ERR_PTR(err);
  403. efault:
  404. kfree(e);
  405. return ERR_PTR(-EFAULT);
  406. einval:
  407. kfree(e);
  408. return ERR_PTR(-EINVAL);
  409. }
  410. /*
  411. * Set status of entry/binfmt_misc:
  412. * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
  413. */
  414. static int parse_command(const char __user *buffer, size_t count)
  415. {
  416. char s[4];
  417. if (count > 3)
  418. return -EINVAL;
  419. if (copy_from_user(s, buffer, count))
  420. return -EFAULT;
  421. if (!count)
  422. return 0;
  423. if (s[count - 1] == '\n')
  424. count--;
  425. if (count == 1 && s[0] == '0')
  426. return 1;
  427. if (count == 1 && s[0] == '1')
  428. return 2;
  429. if (count == 2 && s[0] == '-' && s[1] == '1')
  430. return 3;
  431. return -EINVAL;
  432. }
  433. /* generic stuff */
  434. static void entry_status(Node *e, char *page)
  435. {
  436. char *dp = page;
  437. const char *status = "disabled";
  438. if (test_bit(Enabled, &e->flags))
  439. status = "enabled";
  440. if (!VERBOSE_STATUS) {
  441. sprintf(page, "%s\n", status);
  442. return;
  443. }
  444. dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter);
  445. /* print the special flags */
  446. dp += sprintf(dp, "flags: ");
  447. if (e->flags & MISC_FMT_PRESERVE_ARGV0)
  448. *dp++ = 'P';
  449. if (e->flags & MISC_FMT_OPEN_BINARY)
  450. *dp++ = 'O';
  451. if (e->flags & MISC_FMT_CREDENTIALS)
  452. *dp++ = 'C';
  453. if (e->flags & MISC_FMT_OPEN_FILE)
  454. *dp++ = 'F';
  455. *dp++ = '\n';
  456. if (!test_bit(Magic, &e->flags)) {
  457. sprintf(dp, "extension .%s\n", e->magic);
  458. } else {
  459. dp += sprintf(dp, "offset %i\nmagic ", e->offset);
  460. dp = bin2hex(dp, e->magic, e->size);
  461. if (e->mask) {
  462. dp += sprintf(dp, "\nmask ");
  463. dp = bin2hex(dp, e->mask, e->size);
  464. }
  465. *dp++ = '\n';
  466. *dp = '\0';
  467. }
  468. }
  469. static struct inode *bm_get_inode(struct super_block *sb, int mode)
  470. {
  471. struct inode *inode = new_inode(sb);
  472. if (inode) {
  473. inode->i_ino = get_next_ino();
  474. inode->i_mode = mode;
  475. inode->i_atime = inode->i_mtime = inode->i_ctime =
  476. current_time(inode);
  477. }
  478. return inode;
  479. }
  480. static void bm_evict_inode(struct inode *inode)
  481. {
  482. Node *e = inode->i_private;
  483. if (e && e->flags & MISC_FMT_OPEN_FILE)
  484. filp_close(e->interp_file, NULL);
  485. clear_inode(inode);
  486. kfree(e);
  487. }
  488. static void kill_node(Node *e)
  489. {
  490. struct dentry *dentry;
  491. write_lock(&entries_lock);
  492. list_del_init(&e->list);
  493. write_unlock(&entries_lock);
  494. dentry = e->dentry;
  495. drop_nlink(d_inode(dentry));
  496. d_drop(dentry);
  497. dput(dentry);
  498. simple_release_fs(&bm_mnt, &entry_count);
  499. }
  500. /* /<entry> */
  501. static ssize_t
  502. bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  503. {
  504. Node *e = file_inode(file)->i_private;
  505. ssize_t res;
  506. char *page;
  507. page = (char *) __get_free_page(GFP_KERNEL);
  508. if (!page)
  509. return -ENOMEM;
  510. entry_status(e, page);
  511. res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
  512. free_page((unsigned long) page);
  513. return res;
  514. }
  515. static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
  516. size_t count, loff_t *ppos)
  517. {
  518. struct dentry *root;
  519. Node *e = file_inode(file)->i_private;
  520. int res = parse_command(buffer, count);
  521. switch (res) {
  522. case 1:
  523. /* Disable this handler. */
  524. clear_bit(Enabled, &e->flags);
  525. break;
  526. case 2:
  527. /* Enable this handler. */
  528. set_bit(Enabled, &e->flags);
  529. break;
  530. case 3:
  531. /* Delete this handler. */
  532. root = file_inode(file)->i_sb->s_root;
  533. inode_lock(d_inode(root));
  534. if (!list_empty(&e->list))
  535. kill_node(e);
  536. inode_unlock(d_inode(root));
  537. break;
  538. default:
  539. return res;
  540. }
  541. return count;
  542. }
  543. static const struct file_operations bm_entry_operations = {
  544. .read = bm_entry_read,
  545. .write = bm_entry_write,
  546. .llseek = default_llseek,
  547. };
  548. /* /register */
  549. static ssize_t bm_register_write(struct file *file, const char __user *buffer,
  550. size_t count, loff_t *ppos)
  551. {
  552. Node *e;
  553. struct inode *inode;
  554. struct super_block *sb = file_inode(file)->i_sb;
  555. struct dentry *root = sb->s_root, *dentry;
  556. int err = 0;
  557. struct file *f = NULL;
  558. e = create_entry(buffer, count);
  559. if (IS_ERR(e))
  560. return PTR_ERR(e);
  561. if (e->flags & MISC_FMT_OPEN_FILE) {
  562. f = open_exec(e->interpreter);
  563. if (IS_ERR(f)) {
  564. pr_notice("register: failed to install interpreter file %s\n",
  565. e->interpreter);
  566. kfree(e);
  567. return PTR_ERR(f);
  568. }
  569. e->interp_file = f;
  570. }
  571. inode_lock(d_inode(root));
  572. dentry = lookup_one_len(e->name, root, strlen(e->name));
  573. err = PTR_ERR(dentry);
  574. if (IS_ERR(dentry))
  575. goto out;
  576. err = -EEXIST;
  577. if (d_really_is_positive(dentry))
  578. goto out2;
  579. inode = bm_get_inode(sb, S_IFREG | 0644);
  580. err = -ENOMEM;
  581. if (!inode)
  582. goto out2;
  583. err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
  584. if (err) {
  585. iput(inode);
  586. inode = NULL;
  587. goto out2;
  588. }
  589. e->dentry = dget(dentry);
  590. inode->i_private = e;
  591. inode->i_fop = &bm_entry_operations;
  592. d_instantiate(dentry, inode);
  593. write_lock(&entries_lock);
  594. list_add(&e->list, &entries);
  595. write_unlock(&entries_lock);
  596. err = 0;
  597. out2:
  598. dput(dentry);
  599. out:
  600. inode_unlock(d_inode(root));
  601. if (err) {
  602. if (f)
  603. filp_close(f, NULL);
  604. kfree(e);
  605. return err;
  606. }
  607. return count;
  608. }
  609. static const struct file_operations bm_register_operations = {
  610. .write = bm_register_write,
  611. .llseek = noop_llseek,
  612. };
  613. /* /status */
  614. static ssize_t
  615. bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  616. {
  617. char *s = enabled ? "enabled\n" : "disabled\n";
  618. return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
  619. }
  620. static ssize_t bm_status_write(struct file *file, const char __user *buffer,
  621. size_t count, loff_t *ppos)
  622. {
  623. int res = parse_command(buffer, count);
  624. struct dentry *root;
  625. switch (res) {
  626. case 1:
  627. /* Disable all handlers. */
  628. enabled = 0;
  629. break;
  630. case 2:
  631. /* Enable all handlers. */
  632. enabled = 1;
  633. break;
  634. case 3:
  635. /* Delete all handlers. */
  636. root = file_inode(file)->i_sb->s_root;
  637. inode_lock(d_inode(root));
  638. while (!list_empty(&entries))
  639. kill_node(list_first_entry(&entries, Node, list));
  640. inode_unlock(d_inode(root));
  641. break;
  642. default:
  643. return res;
  644. }
  645. return count;
  646. }
  647. static const struct file_operations bm_status_operations = {
  648. .read = bm_status_read,
  649. .write = bm_status_write,
  650. .llseek = default_llseek,
  651. };
  652. /* Superblock handling */
  653. static const struct super_operations s_ops = {
  654. .statfs = simple_statfs,
  655. .evict_inode = bm_evict_inode,
  656. };
  657. static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
  658. {
  659. int err;
  660. static const struct tree_descr bm_files[] = {
  661. [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
  662. [3] = {"register", &bm_register_operations, S_IWUSR},
  663. /* last one */ {""}
  664. };
  665. err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
  666. if (!err)
  667. sb->s_op = &s_ops;
  668. return err;
  669. }
  670. static int bm_get_tree(struct fs_context *fc)
  671. {
  672. return get_tree_single(fc, bm_fill_super);
  673. }
  674. static const struct fs_context_operations bm_context_ops = {
  675. .get_tree = bm_get_tree,
  676. };
  677. static int bm_init_fs_context(struct fs_context *fc)
  678. {
  679. fc->ops = &bm_context_ops;
  680. return 0;
  681. }
  682. static struct linux_binfmt misc_format = {
  683. .module = THIS_MODULE,
  684. .load_binary = load_misc_binary,
  685. };
  686. static struct file_system_type bm_fs_type = {
  687. .owner = THIS_MODULE,
  688. .name = "binfmt_misc",
  689. .init_fs_context = bm_init_fs_context,
  690. .kill_sb = kill_litter_super,
  691. };
  692. MODULE_ALIAS_FS("binfmt_misc");
  693. static int __init init_misc_binfmt(void)
  694. {
  695. int err = register_filesystem(&bm_fs_type);
  696. if (!err)
  697. insert_binfmt(&misc_format);
  698. return err;
  699. }
  700. static void __exit exit_misc_binfmt(void)
  701. {
  702. unregister_binfmt(&misc_format);
  703. unregister_filesystem(&bm_fs_type);
  704. }
  705. core_initcall(init_misc_binfmt);
  706. module_exit(exit_misc_binfmt);
  707. MODULE_LICENSE("GPL");
  708. MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);