binfmt_misc.c 15 KB

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