binfmt_misc.c 15 KB

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