binfmt_misc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. if (p != s) {
  322. int r = kstrtoint(p, 10, &e->offset);
  323. if (r != 0 || e->offset < 0)
  324. goto einval;
  325. }
  326. p = s;
  327. if (*p++)
  328. goto einval;
  329. pr_debug("register: offset: %#x\n", e->offset);
  330. /* Parse the 'magic' field. */
  331. e->magic = p;
  332. p = scanarg(p, del);
  333. if (!p)
  334. goto einval;
  335. if (!e->magic[0])
  336. goto einval;
  337. if (USE_DEBUG)
  338. print_hex_dump_bytes(
  339. KBUILD_MODNAME ": register: magic[raw]: ",
  340. DUMP_PREFIX_NONE, e->magic, p - e->magic);
  341. /* Parse the 'mask' field. */
  342. e->mask = p;
  343. p = scanarg(p, del);
  344. if (!p)
  345. goto einval;
  346. if (!e->mask[0]) {
  347. e->mask = NULL;
  348. pr_debug("register: mask[raw]: none\n");
  349. } else if (USE_DEBUG)
  350. print_hex_dump_bytes(
  351. KBUILD_MODNAME ": register: mask[raw]: ",
  352. DUMP_PREFIX_NONE, e->mask, p - e->mask);
  353. /*
  354. * Decode the magic & mask fields.
  355. * Note: while we might have accepted embedded NUL bytes from
  356. * above, the unescape helpers here will stop at the first one
  357. * it encounters.
  358. */
  359. e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
  360. if (e->mask &&
  361. string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
  362. goto einval;
  363. if (e->size > BINPRM_BUF_SIZE ||
  364. BINPRM_BUF_SIZE - e->size < e->offset)
  365. goto einval;
  366. pr_debug("register: magic/mask length: %i\n", e->size);
  367. if (USE_DEBUG) {
  368. print_hex_dump_bytes(
  369. KBUILD_MODNAME ": register: magic[decoded]: ",
  370. DUMP_PREFIX_NONE, e->magic, e->size);
  371. if (e->mask) {
  372. int i;
  373. char *masked = kmalloc(e->size, GFP_KERNEL);
  374. print_hex_dump_bytes(
  375. KBUILD_MODNAME ": register: mask[decoded]: ",
  376. DUMP_PREFIX_NONE, e->mask, e->size);
  377. if (masked) {
  378. for (i = 0; i < e->size; ++i)
  379. masked[i] = e->magic[i] & e->mask[i];
  380. print_hex_dump_bytes(
  381. KBUILD_MODNAME ": register: magic[masked]: ",
  382. DUMP_PREFIX_NONE, masked, e->size);
  383. kfree(masked);
  384. }
  385. }
  386. }
  387. } else {
  388. /* Handle the 'E' (extension) format. */
  389. /* Skip the 'offset' field. */
  390. p = strchr(p, del);
  391. if (!p)
  392. goto einval;
  393. *p++ = '\0';
  394. /* Parse the 'magic' field. */
  395. e->magic = p;
  396. p = strchr(p, del);
  397. if (!p)
  398. goto einval;
  399. *p++ = '\0';
  400. if (!e->magic[0] || strchr(e->magic, '/'))
  401. goto einval;
  402. pr_debug("register: extension: {%s}\n", e->magic);
  403. /* Skip the 'mask' field. */
  404. p = strchr(p, del);
  405. if (!p)
  406. goto einval;
  407. *p++ = '\0';
  408. }
  409. /* Parse the 'interpreter' field. */
  410. e->interpreter = p;
  411. p = strchr(p, del);
  412. if (!p)
  413. goto einval;
  414. *p++ = '\0';
  415. if (!e->interpreter[0])
  416. goto einval;
  417. pr_debug("register: interpreter: {%s}\n", e->interpreter);
  418. /* Parse the 'flags' field. */
  419. p = check_special_flags(p, e);
  420. if (*p == '\n')
  421. p++;
  422. if (p != buf + count)
  423. goto einval;
  424. return e;
  425. out:
  426. return ERR_PTR(err);
  427. efault:
  428. kfree(e);
  429. return ERR_PTR(-EFAULT);
  430. einval:
  431. kfree(e);
  432. return ERR_PTR(-EINVAL);
  433. }
  434. /*
  435. * Set status of entry/binfmt_misc:
  436. * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
  437. */
  438. static int parse_command(const char __user *buffer, size_t count)
  439. {
  440. char s[4];
  441. if (count > 3)
  442. return -EINVAL;
  443. if (copy_from_user(s, buffer, count))
  444. return -EFAULT;
  445. if (!count)
  446. return 0;
  447. if (s[count - 1] == '\n')
  448. count--;
  449. if (count == 1 && s[0] == '0')
  450. return 1;
  451. if (count == 1 && s[0] == '1')
  452. return 2;
  453. if (count == 2 && s[0] == '-' && s[1] == '1')
  454. return 3;
  455. return -EINVAL;
  456. }
  457. /* generic stuff */
  458. static void entry_status(Node *e, char *page)
  459. {
  460. char *dp = page;
  461. const char *status = "disabled";
  462. if (test_bit(Enabled, &e->flags))
  463. status = "enabled";
  464. if (!VERBOSE_STATUS) {
  465. sprintf(page, "%s\n", status);
  466. return;
  467. }
  468. dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter);
  469. /* print the special flags */
  470. dp += sprintf(dp, "flags: ");
  471. if (e->flags & MISC_FMT_PRESERVE_ARGV0)
  472. *dp++ = 'P';
  473. if (e->flags & MISC_FMT_OPEN_BINARY)
  474. *dp++ = 'O';
  475. if (e->flags & MISC_FMT_CREDENTIALS)
  476. *dp++ = 'C';
  477. *dp++ = '\n';
  478. if (!test_bit(Magic, &e->flags)) {
  479. sprintf(dp, "extension .%s\n", e->magic);
  480. } else {
  481. dp += sprintf(dp, "offset %i\nmagic ", e->offset);
  482. dp = bin2hex(dp, e->magic, e->size);
  483. if (e->mask) {
  484. dp += sprintf(dp, "\nmask ");
  485. dp = bin2hex(dp, e->mask, e->size);
  486. }
  487. *dp++ = '\n';
  488. *dp = '\0';
  489. }
  490. }
  491. static struct inode *bm_get_inode(struct super_block *sb, int mode)
  492. {
  493. struct inode *inode = new_inode(sb);
  494. if (inode) {
  495. inode->i_ino = get_next_ino();
  496. inode->i_mode = mode;
  497. inode->i_atime = inode->i_mtime = inode->i_ctime =
  498. current_fs_time(inode->i_sb);
  499. }
  500. return inode;
  501. }
  502. static void bm_evict_inode(struct inode *inode)
  503. {
  504. clear_inode(inode);
  505. kfree(inode->i_private);
  506. }
  507. static void kill_node(Node *e)
  508. {
  509. struct dentry *dentry;
  510. write_lock(&entries_lock);
  511. dentry = e->dentry;
  512. if (dentry) {
  513. list_del_init(&e->list);
  514. e->dentry = NULL;
  515. }
  516. write_unlock(&entries_lock);
  517. if (dentry) {
  518. drop_nlink(d_inode(dentry));
  519. d_drop(dentry);
  520. dput(dentry);
  521. simple_release_fs(&bm_mnt, &entry_count);
  522. }
  523. }
  524. /* /<entry> */
  525. static ssize_t
  526. bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  527. {
  528. Node *e = file_inode(file)->i_private;
  529. ssize_t res;
  530. char *page;
  531. page = (char *) __get_free_page(GFP_KERNEL);
  532. if (!page)
  533. return -ENOMEM;
  534. entry_status(e, page);
  535. res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
  536. free_page((unsigned long) page);
  537. return res;
  538. }
  539. static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
  540. size_t count, loff_t *ppos)
  541. {
  542. struct dentry *root;
  543. Node *e = file_inode(file)->i_private;
  544. int res = parse_command(buffer, count);
  545. switch (res) {
  546. case 1:
  547. /* Disable this handler. */
  548. clear_bit(Enabled, &e->flags);
  549. break;
  550. case 2:
  551. /* Enable this handler. */
  552. set_bit(Enabled, &e->flags);
  553. break;
  554. case 3:
  555. /* Delete this handler. */
  556. root = dget(file->f_path.dentry->d_sb->s_root);
  557. mutex_lock(&d_inode(root)->i_mutex);
  558. kill_node(e);
  559. mutex_unlock(&d_inode(root)->i_mutex);
  560. dput(root);
  561. break;
  562. default:
  563. return res;
  564. }
  565. return count;
  566. }
  567. static const struct file_operations bm_entry_operations = {
  568. .read = bm_entry_read,
  569. .write = bm_entry_write,
  570. .llseek = default_llseek,
  571. };
  572. /* /register */
  573. static ssize_t bm_register_write(struct file *file, const char __user *buffer,
  574. size_t count, loff_t *ppos)
  575. {
  576. Node *e;
  577. struct inode *inode;
  578. struct dentry *root, *dentry;
  579. struct super_block *sb = file->f_path.dentry->d_sb;
  580. int err = 0;
  581. e = create_entry(buffer, count);
  582. if (IS_ERR(e))
  583. return PTR_ERR(e);
  584. root = dget(sb->s_root);
  585. mutex_lock(&d_inode(root)->i_mutex);
  586. dentry = lookup_one_len(e->name, root, strlen(e->name));
  587. err = PTR_ERR(dentry);
  588. if (IS_ERR(dentry))
  589. goto out;
  590. err = -EEXIST;
  591. if (d_really_is_positive(dentry))
  592. goto out2;
  593. inode = bm_get_inode(sb, S_IFREG | 0644);
  594. err = -ENOMEM;
  595. if (!inode)
  596. goto out2;
  597. err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
  598. if (err) {
  599. iput(inode);
  600. inode = NULL;
  601. goto out2;
  602. }
  603. e->dentry = dget(dentry);
  604. inode->i_private = e;
  605. inode->i_fop = &bm_entry_operations;
  606. d_instantiate(dentry, inode);
  607. write_lock(&entries_lock);
  608. list_add(&e->list, &entries);
  609. write_unlock(&entries_lock);
  610. err = 0;
  611. out2:
  612. dput(dentry);
  613. out:
  614. mutex_unlock(&d_inode(root)->i_mutex);
  615. dput(root);
  616. if (err) {
  617. kfree(e);
  618. return -EINVAL;
  619. }
  620. return count;
  621. }
  622. static const struct file_operations bm_register_operations = {
  623. .write = bm_register_write,
  624. .llseek = noop_llseek,
  625. };
  626. /* /status */
  627. static ssize_t
  628. bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  629. {
  630. char *s = enabled ? "enabled\n" : "disabled\n";
  631. return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
  632. }
  633. static ssize_t bm_status_write(struct file *file, const char __user *buffer,
  634. size_t count, loff_t *ppos)
  635. {
  636. int res = parse_command(buffer, count);
  637. struct dentry *root;
  638. switch (res) {
  639. case 1:
  640. /* Disable all handlers. */
  641. enabled = 0;
  642. break;
  643. case 2:
  644. /* Enable all handlers. */
  645. enabled = 1;
  646. break;
  647. case 3:
  648. /* Delete all handlers. */
  649. root = dget(file->f_path.dentry->d_sb->s_root);
  650. mutex_lock(&d_inode(root)->i_mutex);
  651. while (!list_empty(&entries))
  652. kill_node(list_entry(entries.next, Node, list));
  653. mutex_unlock(&d_inode(root)->i_mutex);
  654. dput(root);
  655. break;
  656. default:
  657. return res;
  658. }
  659. return count;
  660. }
  661. static const struct file_operations bm_status_operations = {
  662. .read = bm_status_read,
  663. .write = bm_status_write,
  664. .llseek = default_llseek,
  665. };
  666. /* Superblock handling */
  667. static const struct super_operations s_ops = {
  668. .statfs = simple_statfs,
  669. .evict_inode = bm_evict_inode,
  670. };
  671. static int bm_fill_super(struct super_block *sb, void *data, int silent)
  672. {
  673. int err;
  674. static struct tree_descr bm_files[] = {
  675. [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
  676. [3] = {"register", &bm_register_operations, S_IWUSR},
  677. /* last one */ {""}
  678. };
  679. err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
  680. if (!err)
  681. sb->s_op = &s_ops;
  682. return err;
  683. }
  684. static struct dentry *bm_mount(struct file_system_type *fs_type,
  685. int flags, const char *dev_name, void *data)
  686. {
  687. return mount_single(fs_type, flags, data, bm_fill_super);
  688. }
  689. static struct linux_binfmt misc_format = {
  690. .module = THIS_MODULE,
  691. .load_binary = load_misc_binary,
  692. };
  693. static struct file_system_type bm_fs_type = {
  694. .owner = THIS_MODULE,
  695. .name = "binfmt_misc",
  696. .mount = bm_mount,
  697. .kill_sb = kill_litter_super,
  698. };
  699. MODULE_ALIAS_FS("binfmt_misc");
  700. static int __init init_misc_binfmt(void)
  701. {
  702. int err = register_filesystem(&bm_fs_type);
  703. if (!err)
  704. insert_binfmt(&misc_format);
  705. return err;
  706. }
  707. static void __exit exit_misc_binfmt(void)
  708. {
  709. unregister_binfmt(&misc_format);
  710. unregister_filesystem(&bm_fs_type);
  711. }
  712. core_initcall(init_misc_binfmt);
  713. module_exit(exit_misc_binfmt);
  714. MODULE_LICENSE("GPL");