binfmt_misc.c 18 KB

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