binfmt_misc.c 18 KB

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