binfmt_misc.c 15 KB

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