binfmt_misc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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/admin-guide/binfmt-misc.rst 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. const 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. int retval;
  119. int fd_binary = -1;
  120. retval = -ENOEXEC;
  121. if (!enabled)
  122. return retval;
  123. /* to keep locking time low, we copy the interpreter string */
  124. read_lock(&entries_lock);
  125. fmt = check_file(bprm);
  126. if (fmt)
  127. dget(fmt->dentry);
  128. read_unlock(&entries_lock);
  129. if (!fmt)
  130. return retval;
  131. /* Need to be able to load the file after exec */
  132. retval = -ENOENT;
  133. if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
  134. goto ret;
  135. if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
  136. retval = remove_arg_zero(bprm);
  137. if (retval)
  138. goto ret;
  139. }
  140. if (fmt->flags & MISC_FMT_OPEN_BINARY) {
  141. /* if the binary should be opened on behalf of the
  142. * interpreter than keep it open and assign descriptor
  143. * to it
  144. */
  145. fd_binary = get_unused_fd_flags(0);
  146. if (fd_binary < 0) {
  147. retval = fd_binary;
  148. goto ret;
  149. }
  150. fd_install(fd_binary, bprm->file);
  151. /* if the binary is not readable than enforce mm->dumpable=0
  152. regardless of the interpreter's permissions */
  153. would_dump(bprm, bprm->file);
  154. allow_write_access(bprm->file);
  155. bprm->file = NULL;
  156. /* mark the bprm that fd should be passed to interp */
  157. bprm->interp_flags |= BINPRM_FLAGS_EXECFD;
  158. bprm->interp_data = fd_binary;
  159. } else {
  160. allow_write_access(bprm->file);
  161. fput(bprm->file);
  162. bprm->file = NULL;
  163. }
  164. /* make argv[1] be the path to the binary */
  165. retval = copy_strings_kernel(1, &bprm->interp, bprm);
  166. if (retval < 0)
  167. goto error;
  168. bprm->argc++;
  169. /* add the interp as argv[0] */
  170. retval = copy_strings_kernel(1, &fmt->interpreter, bprm);
  171. if (retval < 0)
  172. goto error;
  173. bprm->argc++;
  174. /* Update interp in case binfmt_script needs it. */
  175. retval = bprm_change_interp(fmt->interpreter, bprm);
  176. if (retval < 0)
  177. goto error;
  178. if (fmt->flags & MISC_FMT_OPEN_FILE) {
  179. interp_file = file_clone_open(fmt->interp_file);
  180. if (!IS_ERR(interp_file))
  181. deny_write_access(interp_file);
  182. } else {
  183. interp_file = open_exec(fmt->interpreter);
  184. }
  185. retval = PTR_ERR(interp_file);
  186. if (IS_ERR(interp_file))
  187. goto error;
  188. bprm->file = interp_file;
  189. if (fmt->flags & MISC_FMT_CREDENTIALS) {
  190. loff_t pos = 0;
  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, bprm->buf, BINPRM_BUF_SIZE,
  197. &pos);
  198. } else
  199. retval = prepare_binprm(bprm);
  200. if (retval < 0)
  201. goto error;
  202. retval = search_binary_handler(bprm);
  203. if (retval < 0)
  204. goto error;
  205. ret:
  206. dput(fmt->dentry);
  207. return retval;
  208. error:
  209. if (fd_binary > 0)
  210. ksys_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. if (p != s) {
  338. int r = kstrtoint(p, 10, &e->offset);
  339. if (r != 0 || e->offset < 0)
  340. goto einval;
  341. }
  342. p = s;
  343. if (*p++)
  344. goto einval;
  345. pr_debug("register: offset: %#x\n", e->offset);
  346. /* Parse the 'magic' field. */
  347. e->magic = p;
  348. p = scanarg(p, del);
  349. if (!p)
  350. goto einval;
  351. if (!e->magic[0])
  352. goto einval;
  353. if (USE_DEBUG)
  354. print_hex_dump_bytes(
  355. KBUILD_MODNAME ": register: magic[raw]: ",
  356. DUMP_PREFIX_NONE, e->magic, p - e->magic);
  357. /* Parse the 'mask' field. */
  358. e->mask = p;
  359. p = scanarg(p, del);
  360. if (!p)
  361. goto einval;
  362. if (!e->mask[0]) {
  363. e->mask = NULL;
  364. pr_debug("register: mask[raw]: none\n");
  365. } else if (USE_DEBUG)
  366. print_hex_dump_bytes(
  367. KBUILD_MODNAME ": register: mask[raw]: ",
  368. DUMP_PREFIX_NONE, e->mask, p - e->mask);
  369. /*
  370. * Decode the magic & mask fields.
  371. * Note: while we might have accepted embedded NUL bytes from
  372. * above, the unescape helpers here will stop at the first one
  373. * it encounters.
  374. */
  375. e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
  376. if (e->mask &&
  377. string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
  378. goto einval;
  379. if (e->size > BINPRM_BUF_SIZE ||
  380. BINPRM_BUF_SIZE - e->size < e->offset)
  381. goto einval;
  382. pr_debug("register: magic/mask length: %i\n", e->size);
  383. if (USE_DEBUG) {
  384. print_hex_dump_bytes(
  385. KBUILD_MODNAME ": register: magic[decoded]: ",
  386. DUMP_PREFIX_NONE, e->magic, e->size);
  387. if (e->mask) {
  388. int i;
  389. char *masked = kmalloc(e->size, GFP_KERNEL);
  390. print_hex_dump_bytes(
  391. KBUILD_MODNAME ": register: mask[decoded]: ",
  392. DUMP_PREFIX_NONE, e->mask, e->size);
  393. if (masked) {
  394. for (i = 0; i < e->size; ++i)
  395. masked[i] = e->magic[i] & e->mask[i];
  396. print_hex_dump_bytes(
  397. KBUILD_MODNAME ": register: magic[masked]: ",
  398. DUMP_PREFIX_NONE, masked, e->size);
  399. kfree(masked);
  400. }
  401. }
  402. }
  403. } else {
  404. /* Handle the 'E' (extension) format. */
  405. /* Skip the 'offset' field. */
  406. p = strchr(p, del);
  407. if (!p)
  408. goto einval;
  409. *p++ = '\0';
  410. /* Parse the 'magic' field. */
  411. e->magic = p;
  412. p = strchr(p, del);
  413. if (!p)
  414. goto einval;
  415. *p++ = '\0';
  416. if (!e->magic[0] || strchr(e->magic, '/'))
  417. goto einval;
  418. pr_debug("register: extension: {%s}\n", e->magic);
  419. /* Skip the 'mask' field. */
  420. p = strchr(p, del);
  421. if (!p)
  422. goto einval;
  423. *p++ = '\0';
  424. }
  425. /* Parse the 'interpreter' field. */
  426. e->interpreter = p;
  427. p = strchr(p, del);
  428. if (!p)
  429. goto einval;
  430. *p++ = '\0';
  431. if (!e->interpreter[0])
  432. goto einval;
  433. pr_debug("register: interpreter: {%s}\n", e->interpreter);
  434. /* Parse the 'flags' field. */
  435. p = check_special_flags(p, e);
  436. if (*p == '\n')
  437. p++;
  438. if (p != buf + count)
  439. goto einval;
  440. return e;
  441. out:
  442. return ERR_PTR(err);
  443. efault:
  444. kfree(e);
  445. return ERR_PTR(-EFAULT);
  446. einval:
  447. kfree(e);
  448. return ERR_PTR(-EINVAL);
  449. }
  450. /*
  451. * Set status of entry/binfmt_misc:
  452. * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
  453. */
  454. static int parse_command(const char __user *buffer, size_t count)
  455. {
  456. char s[4];
  457. if (count > 3)
  458. return -EINVAL;
  459. if (copy_from_user(s, buffer, count))
  460. return -EFAULT;
  461. if (!count)
  462. return 0;
  463. if (s[count - 1] == '\n')
  464. count--;
  465. if (count == 1 && s[0] == '0')
  466. return 1;
  467. if (count == 1 && s[0] == '1')
  468. return 2;
  469. if (count == 2 && s[0] == '-' && s[1] == '1')
  470. return 3;
  471. return -EINVAL;
  472. }
  473. /* generic stuff */
  474. static void entry_status(Node *e, char *page)
  475. {
  476. char *dp = page;
  477. const char *status = "disabled";
  478. if (test_bit(Enabled, &e->flags))
  479. status = "enabled";
  480. if (!VERBOSE_STATUS) {
  481. sprintf(page, "%s\n", status);
  482. return;
  483. }
  484. dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter);
  485. /* print the special flags */
  486. dp += sprintf(dp, "flags: ");
  487. if (e->flags & MISC_FMT_PRESERVE_ARGV0)
  488. *dp++ = 'P';
  489. if (e->flags & MISC_FMT_OPEN_BINARY)
  490. *dp++ = 'O';
  491. if (e->flags & MISC_FMT_CREDENTIALS)
  492. *dp++ = 'C';
  493. if (e->flags & MISC_FMT_OPEN_FILE)
  494. *dp++ = 'F';
  495. *dp++ = '\n';
  496. if (!test_bit(Magic, &e->flags)) {
  497. sprintf(dp, "extension .%s\n", e->magic);
  498. } else {
  499. dp += sprintf(dp, "offset %i\nmagic ", e->offset);
  500. dp = bin2hex(dp, e->magic, e->size);
  501. if (e->mask) {
  502. dp += sprintf(dp, "\nmask ");
  503. dp = bin2hex(dp, e->mask, e->size);
  504. }
  505. *dp++ = '\n';
  506. *dp = '\0';
  507. }
  508. }
  509. static struct inode *bm_get_inode(struct super_block *sb, int mode)
  510. {
  511. struct inode *inode = new_inode(sb);
  512. if (inode) {
  513. inode->i_ino = get_next_ino();
  514. inode->i_mode = mode;
  515. inode->i_atime = inode->i_mtime = inode->i_ctime =
  516. current_time(inode);
  517. }
  518. return inode;
  519. }
  520. static void bm_evict_inode(struct inode *inode)
  521. {
  522. Node *e = inode->i_private;
  523. if (e && e->flags & MISC_FMT_OPEN_FILE)
  524. filp_close(e->interp_file, NULL);
  525. clear_inode(inode);
  526. kfree(e);
  527. }
  528. static void kill_node(Node *e)
  529. {
  530. struct dentry *dentry;
  531. write_lock(&entries_lock);
  532. list_del_init(&e->list);
  533. write_unlock(&entries_lock);
  534. dentry = e->dentry;
  535. drop_nlink(d_inode(dentry));
  536. d_drop(dentry);
  537. dput(dentry);
  538. simple_release_fs(&bm_mnt, &entry_count);
  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. if (!list_empty(&e->list))
  575. kill_node(e);
  576. inode_unlock(d_inode(root));
  577. break;
  578. default:
  579. return res;
  580. }
  581. return count;
  582. }
  583. static const struct file_operations bm_entry_operations = {
  584. .read = bm_entry_read,
  585. .write = bm_entry_write,
  586. .llseek = default_llseek,
  587. };
  588. /* /register */
  589. static ssize_t bm_register_write(struct file *file, const char __user *buffer,
  590. size_t count, loff_t *ppos)
  591. {
  592. Node *e;
  593. struct inode *inode;
  594. struct super_block *sb = file_inode(file)->i_sb;
  595. struct dentry *root = sb->s_root, *dentry;
  596. int err = 0;
  597. e = create_entry(buffer, count);
  598. if (IS_ERR(e))
  599. return PTR_ERR(e);
  600. inode_lock(d_inode(root));
  601. dentry = lookup_one_len(e->name, root, strlen(e->name));
  602. err = PTR_ERR(dentry);
  603. if (IS_ERR(dentry))
  604. goto out;
  605. err = -EEXIST;
  606. if (d_really_is_positive(dentry))
  607. goto out2;
  608. inode = bm_get_inode(sb, S_IFREG | 0644);
  609. err = -ENOMEM;
  610. if (!inode)
  611. goto out2;
  612. err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
  613. if (err) {
  614. iput(inode);
  615. inode = NULL;
  616. goto out2;
  617. }
  618. if (e->flags & MISC_FMT_OPEN_FILE) {
  619. struct file *f;
  620. f = open_exec(e->interpreter);
  621. if (IS_ERR(f)) {
  622. err = PTR_ERR(f);
  623. pr_notice("register: failed to install interpreter file %s\n", e->interpreter);
  624. simple_release_fs(&bm_mnt, &entry_count);
  625. iput(inode);
  626. inode = NULL;
  627. goto out2;
  628. }
  629. e->interp_file = f;
  630. }
  631. e->dentry = dget(dentry);
  632. inode->i_private = e;
  633. inode->i_fop = &bm_entry_operations;
  634. d_instantiate(dentry, inode);
  635. write_lock(&entries_lock);
  636. list_add(&e->list, &entries);
  637. write_unlock(&entries_lock);
  638. err = 0;
  639. out2:
  640. dput(dentry);
  641. out:
  642. inode_unlock(d_inode(root));
  643. if (err) {
  644. kfree(e);
  645. return err;
  646. }
  647. return count;
  648. }
  649. static const struct file_operations bm_register_operations = {
  650. .write = bm_register_write,
  651. .llseek = noop_llseek,
  652. };
  653. /* /status */
  654. static ssize_t
  655. bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  656. {
  657. char *s = enabled ? "enabled\n" : "disabled\n";
  658. return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
  659. }
  660. static ssize_t bm_status_write(struct file *file, const char __user *buffer,
  661. size_t count, loff_t *ppos)
  662. {
  663. int res = parse_command(buffer, count);
  664. struct dentry *root;
  665. switch (res) {
  666. case 1:
  667. /* Disable all handlers. */
  668. enabled = 0;
  669. break;
  670. case 2:
  671. /* Enable all handlers. */
  672. enabled = 1;
  673. break;
  674. case 3:
  675. /* Delete all handlers. */
  676. root = file_inode(file)->i_sb->s_root;
  677. inode_lock(d_inode(root));
  678. while (!list_empty(&entries))
  679. kill_node(list_first_entry(&entries, Node, list));
  680. inode_unlock(d_inode(root));
  681. break;
  682. default:
  683. return res;
  684. }
  685. return count;
  686. }
  687. static const struct file_operations bm_status_operations = {
  688. .read = bm_status_read,
  689. .write = bm_status_write,
  690. .llseek = default_llseek,
  691. };
  692. /* Superblock handling */
  693. static const struct super_operations s_ops = {
  694. .statfs = simple_statfs,
  695. .evict_inode = bm_evict_inode,
  696. };
  697. static int bm_fill_super(struct super_block *sb, void *data, int silent)
  698. {
  699. int err;
  700. static const struct tree_descr bm_files[] = {
  701. [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
  702. [3] = {"register", &bm_register_operations, S_IWUSR},
  703. /* last one */ {""}
  704. };
  705. err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
  706. if (!err)
  707. sb->s_op = &s_ops;
  708. return err;
  709. }
  710. static struct dentry *bm_mount(struct file_system_type *fs_type,
  711. int flags, const char *dev_name, void *data)
  712. {
  713. return mount_single(fs_type, flags, data, bm_fill_super);
  714. }
  715. static struct linux_binfmt misc_format = {
  716. .module = THIS_MODULE,
  717. .load_binary = load_misc_binary,
  718. };
  719. static struct file_system_type bm_fs_type = {
  720. .owner = THIS_MODULE,
  721. .name = "binfmt_misc",
  722. .mount = bm_mount,
  723. .kill_sb = kill_litter_super,
  724. };
  725. MODULE_ALIAS_FS("binfmt_misc");
  726. static int __init init_misc_binfmt(void)
  727. {
  728. int err = register_filesystem(&bm_fs_type);
  729. if (!err)
  730. insert_binfmt(&misc_format);
  731. return err;
  732. }
  733. static void __exit exit_misc_binfmt(void)
  734. {
  735. unregister_binfmt(&misc_format);
  736. unregister_filesystem(&bm_fs_type);
  737. }
  738. core_initcall(init_misc_binfmt);
  739. module_exit(exit_misc_binfmt);
  740. MODULE_LICENSE("GPL");