binfmt_misc.c 18 KB

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