binfmt_misc.c 18 KB

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