PageRenderTime 40ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/binfmt_misc.c

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