binfmt_misc.c 18 KB

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