binfmt_misc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 (1UL << 31)
  41. #define MISC_FMT_OPEN_BINARY (1UL << 30)
  42. #define MISC_FMT_CREDENTIALS (1UL << 29)
  43. #define MISC_FMT_OPEN_FILE (1UL << 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. bprm->interp_flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
  138. } else {
  139. retval = remove_arg_zero(bprm);
  140. if (retval)
  141. goto ret;
  142. }
  143. if (fmt->flags & MISC_FMT_OPEN_BINARY)
  144. bprm->have_execfd = 1;
  145. /* make argv[1] be the path to the binary */
  146. retval = copy_string_kernel(bprm->interp, bprm);
  147. if (retval < 0)
  148. goto ret;
  149. bprm->argc++;
  150. /* add the interp as argv[0] */
  151. retval = copy_string_kernel(fmt->interpreter, bprm);
  152. if (retval < 0)
  153. goto ret;
  154. bprm->argc++;
  155. /* Update interp in case binfmt_script needs it. */
  156. retval = bprm_change_interp(fmt->interpreter, bprm);
  157. if (retval < 0)
  158. goto ret;
  159. if (fmt->flags & MISC_FMT_OPEN_FILE) {
  160. interp_file = file_clone_open(fmt->interp_file);
  161. if (!IS_ERR(interp_file))
  162. deny_write_access(interp_file);
  163. } else {
  164. interp_file = open_exec(fmt->interpreter);
  165. }
  166. retval = PTR_ERR(interp_file);
  167. if (IS_ERR(interp_file))
  168. goto ret;
  169. bprm->interpreter = interp_file;
  170. if (fmt->flags & MISC_FMT_CREDENTIALS)
  171. bprm->execfd_creds = 1;
  172. retval = 0;
  173. ret:
  174. dput(fmt->dentry);
  175. return retval;
  176. }
  177. /* Command parsers */
  178. /*
  179. * parses and copies one argument enclosed in del from *sp to *dp,
  180. * recognising the \x special.
  181. * returns pointer to the copied argument or NULL in case of an
  182. * error (and sets err) or null argument length.
  183. */
  184. static char *scanarg(char *s, char del)
  185. {
  186. char c;
  187. while ((c = *s++) != del) {
  188. if (c == '\\' && *s == 'x') {
  189. s++;
  190. if (!isxdigit(*s++))
  191. return NULL;
  192. if (!isxdigit(*s++))
  193. return NULL;
  194. }
  195. }
  196. s[-1] ='\0';
  197. return s;
  198. }
  199. static char *check_special_flags(char *sfs, Node *e)
  200. {
  201. char *p = sfs;
  202. int cont = 1;
  203. /* special flags */
  204. while (cont) {
  205. switch (*p) {
  206. case 'P':
  207. pr_debug("register: flag: P (preserve argv0)\n");
  208. p++;
  209. e->flags |= MISC_FMT_PRESERVE_ARGV0;
  210. break;
  211. case 'O':
  212. pr_debug("register: flag: O (open binary)\n");
  213. p++;
  214. e->flags |= MISC_FMT_OPEN_BINARY;
  215. break;
  216. case 'C':
  217. pr_debug("register: flag: C (preserve creds)\n");
  218. p++;
  219. /* this flags also implies the
  220. open-binary flag */
  221. e->flags |= (MISC_FMT_CREDENTIALS |
  222. MISC_FMT_OPEN_BINARY);
  223. break;
  224. case 'F':
  225. pr_debug("register: flag: F: open interpreter file now\n");
  226. p++;
  227. e->flags |= MISC_FMT_OPEN_FILE;
  228. break;
  229. default:
  230. cont = 0;
  231. }
  232. }
  233. return p;
  234. }
  235. /*
  236. * This registers a new binary format, it recognises the syntax
  237. * ':name:type:offset:magic:mask:interpreter:flags'
  238. * where the ':' is the IFS, that can be chosen with the first char
  239. */
  240. static Node *create_entry(const char __user *buffer, size_t count)
  241. {
  242. Node *e;
  243. int memsize, err;
  244. char *buf, *p;
  245. char del;
  246. pr_debug("register: received %zu bytes\n", count);
  247. /* some sanity checks */
  248. err = -EINVAL;
  249. if ((count < 11) || (count > MAX_REGISTER_LENGTH))
  250. goto out;
  251. err = -ENOMEM;
  252. memsize = sizeof(Node) + count + 8;
  253. e = kmalloc(memsize, GFP_KERNEL);
  254. if (!e)
  255. goto out;
  256. p = buf = (char *)e + sizeof(Node);
  257. memset(e, 0, sizeof(Node));
  258. if (copy_from_user(buf, buffer, count))
  259. goto efault;
  260. del = *p++; /* delimeter */
  261. pr_debug("register: delim: %#x {%c}\n", del, del);
  262. /* Pad the buffer with the delim to simplify parsing below. */
  263. memset(buf + count, del, 8);
  264. /* Parse the 'name' field. */
  265. e->name = p;
  266. p = strchr(p, del);
  267. if (!p)
  268. goto einval;
  269. *p++ = '\0';
  270. if (!e->name[0] ||
  271. !strcmp(e->name, ".") ||
  272. !strcmp(e->name, "..") ||
  273. strchr(e->name, '/'))
  274. goto einval;
  275. pr_debug("register: name: {%s}\n", e->name);
  276. /* Parse the 'type' field. */
  277. switch (*p++) {
  278. case 'E':
  279. pr_debug("register: type: E (extension)\n");
  280. e->flags = 1 << Enabled;
  281. break;
  282. case 'M':
  283. pr_debug("register: type: M (magic)\n");
  284. e->flags = (1 << Enabled) | (1 << Magic);
  285. break;
  286. default:
  287. goto einval;
  288. }
  289. if (*p++ != del)
  290. goto einval;
  291. if (test_bit(Magic, &e->flags)) {
  292. /* Handle the 'M' (magic) format. */
  293. char *s;
  294. /* Parse the 'offset' field. */
  295. s = strchr(p, del);
  296. if (!s)
  297. goto einval;
  298. *s = '\0';
  299. if (p != s) {
  300. int r = kstrtoint(p, 10, &e->offset);
  301. if (r != 0 || e->offset < 0)
  302. goto einval;
  303. }
  304. p = s;
  305. if (*p++)
  306. goto einval;
  307. pr_debug("register: offset: %#x\n", e->offset);
  308. /* Parse the 'magic' field. */
  309. e->magic = p;
  310. p = scanarg(p, del);
  311. if (!p)
  312. goto einval;
  313. if (!e->magic[0])
  314. goto einval;
  315. if (USE_DEBUG)
  316. print_hex_dump_bytes(
  317. KBUILD_MODNAME ": register: magic[raw]: ",
  318. DUMP_PREFIX_NONE, e->magic, p - e->magic);
  319. /* Parse the 'mask' field. */
  320. e->mask = p;
  321. p = scanarg(p, del);
  322. if (!p)
  323. goto einval;
  324. if (!e->mask[0]) {
  325. e->mask = NULL;
  326. pr_debug("register: mask[raw]: none\n");
  327. } else if (USE_DEBUG)
  328. print_hex_dump_bytes(
  329. KBUILD_MODNAME ": register: mask[raw]: ",
  330. DUMP_PREFIX_NONE, e->mask, p - e->mask);
  331. /*
  332. * Decode the magic & mask fields.
  333. * Note: while we might have accepted embedded NUL bytes from
  334. * above, the unescape helpers here will stop at the first one
  335. * it encounters.
  336. */
  337. e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
  338. if (e->mask &&
  339. string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
  340. goto einval;
  341. if (e->size > BINPRM_BUF_SIZE ||
  342. BINPRM_BUF_SIZE - e->size < e->offset)
  343. goto einval;
  344. pr_debug("register: magic/mask length: %i\n", e->size);
  345. if (USE_DEBUG) {
  346. print_hex_dump_bytes(
  347. KBUILD_MODNAME ": register: magic[decoded]: ",
  348. DUMP_PREFIX_NONE, e->magic, e->size);
  349. if (e->mask) {
  350. int i;
  351. char *masked = kmalloc(e->size, GFP_KERNEL);
  352. print_hex_dump_bytes(
  353. KBUILD_MODNAME ": register: mask[decoded]: ",
  354. DUMP_PREFIX_NONE, e->mask, e->size);
  355. if (masked) {
  356. for (i = 0; i < e->size; ++i)
  357. masked[i] = e->magic[i] & e->mask[i];
  358. print_hex_dump_bytes(
  359. KBUILD_MODNAME ": register: magic[masked]: ",
  360. DUMP_PREFIX_NONE, masked, e->size);
  361. kfree(masked);
  362. }
  363. }
  364. }
  365. } else {
  366. /* Handle the 'E' (extension) format. */
  367. /* Skip the 'offset' field. */
  368. p = strchr(p, del);
  369. if (!p)
  370. goto einval;
  371. *p++ = '\0';
  372. /* Parse the 'magic' field. */
  373. e->magic = p;
  374. p = strchr(p, del);
  375. if (!p)
  376. goto einval;
  377. *p++ = '\0';
  378. if (!e->magic[0] || strchr(e->magic, '/'))
  379. goto einval;
  380. pr_debug("register: extension: {%s}\n", e->magic);
  381. /* Skip the 'mask' field. */
  382. p = strchr(p, del);
  383. if (!p)
  384. goto einval;
  385. *p++ = '\0';
  386. }
  387. /* Parse the 'interpreter' field. */
  388. e->interpreter = p;
  389. p = strchr(p, del);
  390. if (!p)
  391. goto einval;
  392. *p++ = '\0';
  393. if (!e->interpreter[0])
  394. goto einval;
  395. pr_debug("register: interpreter: {%s}\n", e->interpreter);
  396. /* Parse the 'flags' field. */
  397. p = check_special_flags(p, e);
  398. if (*p == '\n')
  399. p++;
  400. if (p != buf + count)
  401. goto einval;
  402. return e;
  403. out:
  404. return ERR_PTR(err);
  405. efault:
  406. kfree(e);
  407. return ERR_PTR(-EFAULT);
  408. einval:
  409. kfree(e);
  410. return ERR_PTR(-EINVAL);
  411. }
  412. /*
  413. * Set status of entry/binfmt_misc:
  414. * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
  415. */
  416. static int parse_command(const char __user *buffer, size_t count)
  417. {
  418. char s[4];
  419. if (count > 3)
  420. return -EINVAL;
  421. if (copy_from_user(s, buffer, count))
  422. return -EFAULT;
  423. if (!count)
  424. return 0;
  425. if (s[count - 1] == '\n')
  426. count--;
  427. if (count == 1 && s[0] == '0')
  428. return 1;
  429. if (count == 1 && s[0] == '1')
  430. return 2;
  431. if (count == 2 && s[0] == '-' && s[1] == '1')
  432. return 3;
  433. return -EINVAL;
  434. }
  435. /* generic stuff */
  436. static void entry_status(Node *e, char *page)
  437. {
  438. char *dp = page;
  439. const char *status = "disabled";
  440. if (test_bit(Enabled, &e->flags))
  441. status = "enabled";
  442. if (!VERBOSE_STATUS) {
  443. sprintf(page, "%s\n", status);
  444. return;
  445. }
  446. dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter);
  447. /* print the special flags */
  448. dp += sprintf(dp, "flags: ");
  449. if (e->flags & MISC_FMT_PRESERVE_ARGV0)
  450. *dp++ = 'P';
  451. if (e->flags & MISC_FMT_OPEN_BINARY)
  452. *dp++ = 'O';
  453. if (e->flags & MISC_FMT_CREDENTIALS)
  454. *dp++ = 'C';
  455. if (e->flags & MISC_FMT_OPEN_FILE)
  456. *dp++ = 'F';
  457. *dp++ = '\n';
  458. if (!test_bit(Magic, &e->flags)) {
  459. sprintf(dp, "extension .%s\n", e->magic);
  460. } else {
  461. dp += sprintf(dp, "offset %i\nmagic ", e->offset);
  462. dp = bin2hex(dp, e->magic, e->size);
  463. if (e->mask) {
  464. dp += sprintf(dp, "\nmask ");
  465. dp = bin2hex(dp, e->mask, e->size);
  466. }
  467. *dp++ = '\n';
  468. *dp = '\0';
  469. }
  470. }
  471. static struct inode *bm_get_inode(struct super_block *sb, int mode)
  472. {
  473. struct inode *inode = new_inode(sb);
  474. if (inode) {
  475. inode->i_ino = get_next_ino();
  476. inode->i_mode = mode;
  477. inode->i_atime = inode->i_mtime = inode->i_ctime =
  478. current_time(inode);
  479. }
  480. return inode;
  481. }
  482. static void bm_evict_inode(struct inode *inode)
  483. {
  484. Node *e = inode->i_private;
  485. if (e && e->flags & MISC_FMT_OPEN_FILE)
  486. filp_close(e->interp_file, NULL);
  487. clear_inode(inode);
  488. kfree(e);
  489. }
  490. static void kill_node(Node *e)
  491. {
  492. struct dentry *dentry;
  493. write_lock(&entries_lock);
  494. list_del_init(&e->list);
  495. write_unlock(&entries_lock);
  496. dentry = e->dentry;
  497. drop_nlink(d_inode(dentry));
  498. d_drop(dentry);
  499. dput(dentry);
  500. simple_release_fs(&bm_mnt, &entry_count);
  501. }
  502. /* /<entry> */
  503. static ssize_t
  504. bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  505. {
  506. Node *e = file_inode(file)->i_private;
  507. ssize_t res;
  508. char *page;
  509. page = (char *) __get_free_page(GFP_KERNEL);
  510. if (!page)
  511. return -ENOMEM;
  512. entry_status(e, page);
  513. res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
  514. free_page((unsigned long) page);
  515. return res;
  516. }
  517. static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
  518. size_t count, loff_t *ppos)
  519. {
  520. struct dentry *root;
  521. Node *e = file_inode(file)->i_private;
  522. int res = parse_command(buffer, count);
  523. switch (res) {
  524. case 1:
  525. /* Disable this handler. */
  526. clear_bit(Enabled, &e->flags);
  527. break;
  528. case 2:
  529. /* Enable this handler. */
  530. set_bit(Enabled, &e->flags);
  531. break;
  532. case 3:
  533. /* Delete this handler. */
  534. root = file_inode(file)->i_sb->s_root;
  535. inode_lock(d_inode(root));
  536. if (!list_empty(&e->list))
  537. kill_node(e);
  538. inode_unlock(d_inode(root));
  539. break;
  540. default:
  541. return res;
  542. }
  543. return count;
  544. }
  545. static const struct file_operations bm_entry_operations = {
  546. .read = bm_entry_read,
  547. .write = bm_entry_write,
  548. .llseek = default_llseek,
  549. };
  550. /* /register */
  551. static ssize_t bm_register_write(struct file *file, const char __user *buffer,
  552. size_t count, loff_t *ppos)
  553. {
  554. Node *e;
  555. struct inode *inode;
  556. struct super_block *sb = file_inode(file)->i_sb;
  557. struct dentry *root = sb->s_root, *dentry;
  558. int err = 0;
  559. struct file *f = NULL;
  560. e = create_entry(buffer, count);
  561. if (IS_ERR(e))
  562. return PTR_ERR(e);
  563. if (e->flags & MISC_FMT_OPEN_FILE) {
  564. f = open_exec(e->interpreter);
  565. if (IS_ERR(f)) {
  566. pr_notice("register: failed to install interpreter file %s\n",
  567. e->interpreter);
  568. kfree(e);
  569. return PTR_ERR(f);
  570. }
  571. e->interp_file = f;
  572. }
  573. inode_lock(d_inode(root));
  574. dentry = lookup_one_len(e->name, root, strlen(e->name));
  575. err = PTR_ERR(dentry);
  576. if (IS_ERR(dentry))
  577. goto out;
  578. err = -EEXIST;
  579. if (d_really_is_positive(dentry))
  580. goto out2;
  581. inode = bm_get_inode(sb, S_IFREG | 0644);
  582. err = -ENOMEM;
  583. if (!inode)
  584. goto out2;
  585. err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
  586. if (err) {
  587. iput(inode);
  588. inode = NULL;
  589. goto out2;
  590. }
  591. e->dentry = dget(dentry);
  592. inode->i_private = e;
  593. inode->i_fop = &bm_entry_operations;
  594. d_instantiate(dentry, inode);
  595. write_lock(&entries_lock);
  596. list_add(&e->list, &entries);
  597. write_unlock(&entries_lock);
  598. err = 0;
  599. out2:
  600. dput(dentry);
  601. out:
  602. inode_unlock(d_inode(root));
  603. if (err) {
  604. if (f)
  605. filp_close(f, NULL);
  606. kfree(e);
  607. return err;
  608. }
  609. return count;
  610. }
  611. static const struct file_operations bm_register_operations = {
  612. .write = bm_register_write,
  613. .llseek = noop_llseek,
  614. };
  615. /* /status */
  616. static ssize_t
  617. bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  618. {
  619. char *s = enabled ? "enabled\n" : "disabled\n";
  620. return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
  621. }
  622. static ssize_t bm_status_write(struct file *file, const char __user *buffer,
  623. size_t count, loff_t *ppos)
  624. {
  625. int res = parse_command(buffer, count);
  626. struct dentry *root;
  627. switch (res) {
  628. case 1:
  629. /* Disable all handlers. */
  630. enabled = 0;
  631. break;
  632. case 2:
  633. /* Enable all handlers. */
  634. enabled = 1;
  635. break;
  636. case 3:
  637. /* Delete all handlers. */
  638. root = file_inode(file)->i_sb->s_root;
  639. inode_lock(d_inode(root));
  640. while (!list_empty(&entries))
  641. kill_node(list_first_entry(&entries, Node, list));
  642. inode_unlock(d_inode(root));
  643. break;
  644. default:
  645. return res;
  646. }
  647. return count;
  648. }
  649. static const struct file_operations bm_status_operations = {
  650. .read = bm_status_read,
  651. .write = bm_status_write,
  652. .llseek = default_llseek,
  653. };
  654. /* Superblock handling */
  655. static const struct super_operations s_ops = {
  656. .statfs = simple_statfs,
  657. .evict_inode = bm_evict_inode,
  658. };
  659. static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
  660. {
  661. int err;
  662. static const struct tree_descr bm_files[] = {
  663. [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
  664. [3] = {"register", &bm_register_operations, S_IWUSR},
  665. /* last one */ {""}
  666. };
  667. err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
  668. if (!err)
  669. sb->s_op = &s_ops;
  670. return err;
  671. }
  672. static int bm_get_tree(struct fs_context *fc)
  673. {
  674. return get_tree_single(fc, bm_fill_super);
  675. }
  676. static const struct fs_context_operations bm_context_ops = {
  677. .get_tree = bm_get_tree,
  678. };
  679. static int bm_init_fs_context(struct fs_context *fc)
  680. {
  681. fc->ops = &bm_context_ops;
  682. return 0;
  683. }
  684. static struct linux_binfmt misc_format = {
  685. .module = THIS_MODULE,
  686. .load_binary = load_misc_binary,
  687. };
  688. static struct file_system_type bm_fs_type = {
  689. .owner = THIS_MODULE,
  690. .name = "binfmt_misc",
  691. .init_fs_context = bm_init_fs_context,
  692. .kill_sb = kill_litter_super,
  693. };
  694. MODULE_ALIAS_FS("binfmt_misc");
  695. static int __init init_misc_binfmt(void)
  696. {
  697. int err = register_filesystem(&bm_fs_type);
  698. if (!err)
  699. insert_binfmt(&misc_format);
  700. return err;
  701. }
  702. static void __exit exit_misc_binfmt(void)
  703. {
  704. unregister_binfmt(&misc_format);
  705. unregister_filesystem(&bm_fs_type);
  706. }
  707. core_initcall(init_misc_binfmt);
  708. module_exit(exit_misc_binfmt);
  709. MODULE_LICENSE("GPL");