binfmt_misc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. 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. if (file_permission(bprm->file, MAY_READ))
  133. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  134. allow_write_access(bprm->file);
  135. bprm->file = NULL;
  136. /* mark the bprm that fd should be passed to interp */
  137. bprm->interp_flags |= BINPRM_FLAGS_EXECFD;
  138. bprm->interp_data = fd_binary;
  139. } else {
  140. allow_write_access(bprm->file);
  141. fput(bprm->file);
  142. bprm->file = NULL;
  143. }
  144. /* make argv[1] be the path to the binary */
  145. retval = copy_strings_kernel (1, &bprm->interp, bprm);
  146. if (retval < 0)
  147. goto _error;
  148. bprm->argc++;
  149. /* add the interp as argv[0] */
  150. retval = copy_strings_kernel (1, &iname_addr, bprm);
  151. if (retval < 0)
  152. goto _error;
  153. bprm->argc ++;
  154. bprm->interp = iname; /* for binfmt_script */
  155. interp_file = open_exec (iname);
  156. retval = PTR_ERR (interp_file);
  157. if (IS_ERR (interp_file))
  158. goto _error;
  159. bprm->file = interp_file;
  160. if (fmt->flags & MISC_FMT_CREDENTIALS) {
  161. /*
  162. * No need to call prepare_binprm(), it's already been
  163. * done. bprm->buf is stale, update from interp_file.
  164. */
  165. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  166. retval = kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
  167. } else
  168. retval = prepare_binprm (bprm);
  169. if (retval < 0)
  170. goto _error;
  171. bprm->recursion_depth++;
  172. retval = search_binary_handler (bprm, regs);
  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 int unquote(char *from)
  206. {
  207. char c = 0, *s = from, *p = from;
  208. while ((c = *s++) != '\0') {
  209. if (c == '\\' && *s == 'x') {
  210. s++;
  211. c = toupper(*s++);
  212. *p = (c - (isdigit(c) ? '0' : 'A' - 10)) << 4;
  213. c = toupper(*s++);
  214. *p++ |= c - (isdigit(c) ? '0' : 'A' - 10);
  215. continue;
  216. }
  217. *p++ = c;
  218. }
  219. return p - from;
  220. }
  221. static char * check_special_flags (char * sfs, Node * e)
  222. {
  223. char * p = sfs;
  224. int cont = 1;
  225. /* special flags */
  226. while (cont) {
  227. switch (*p) {
  228. case 'P':
  229. p++;
  230. e->flags |= MISC_FMT_PRESERVE_ARGV0;
  231. break;
  232. case 'O':
  233. p++;
  234. e->flags |= MISC_FMT_OPEN_BINARY;
  235. break;
  236. case 'C':
  237. p++;
  238. /* this flags also implies the
  239. open-binary flag */
  240. e->flags |= (MISC_FMT_CREDENTIALS |
  241. MISC_FMT_OPEN_BINARY);
  242. break;
  243. default:
  244. cont = 0;
  245. }
  246. }
  247. return p;
  248. }
  249. /*
  250. * This registers a new binary format, it recognises the syntax
  251. * ':name:type:offset:magic:mask:interpreter:flags'
  252. * where the ':' is the IFS, that can be chosen with the first char
  253. */
  254. static Node *create_entry(const char __user *buffer, size_t count)
  255. {
  256. Node *e;
  257. int memsize, err;
  258. char *buf, *p;
  259. char del;
  260. /* some sanity checks */
  261. err = -EINVAL;
  262. if ((count < 11) || (count > 256))
  263. goto out;
  264. err = -ENOMEM;
  265. memsize = sizeof(Node) + count + 8;
  266. e = kmalloc(memsize, GFP_USER);
  267. if (!e)
  268. goto out;
  269. p = buf = (char *)e + sizeof(Node);
  270. memset(e, 0, sizeof(Node));
  271. if (copy_from_user(buf, buffer, count))
  272. goto Efault;
  273. del = *p++; /* delimeter */
  274. memset(buf+count, del, 8);
  275. e->name = p;
  276. p = strchr(p, del);
  277. if (!p)
  278. goto Einval;
  279. *p++ = '\0';
  280. if (!e->name[0] ||
  281. !strcmp(e->name, ".") ||
  282. !strcmp(e->name, "..") ||
  283. strchr(e->name, '/'))
  284. goto Einval;
  285. switch (*p++) {
  286. case 'E': e->flags = 1<<Enabled; break;
  287. case 'M': e->flags = (1<<Enabled) | (1<<Magic); break;
  288. default: goto Einval;
  289. }
  290. if (*p++ != del)
  291. goto Einval;
  292. if (test_bit(Magic, &e->flags)) {
  293. char *s = strchr(p, del);
  294. if (!s)
  295. goto Einval;
  296. *s++ = '\0';
  297. e->offset = simple_strtoul(p, &p, 10);
  298. if (*p++)
  299. goto Einval;
  300. e->magic = p;
  301. p = scanarg(p, del);
  302. if (!p)
  303. goto Einval;
  304. p[-1] = '\0';
  305. if (!e->magic[0])
  306. goto Einval;
  307. e->mask = p;
  308. p = scanarg(p, del);
  309. if (!p)
  310. goto Einval;
  311. p[-1] = '\0';
  312. if (!e->mask[0])
  313. e->mask = NULL;
  314. e->size = unquote(e->magic);
  315. if (e->mask && unquote(e->mask) != e->size)
  316. goto Einval;
  317. if (e->size + e->offset > BINPRM_BUF_SIZE)
  318. goto Einval;
  319. } else {
  320. p = strchr(p, del);
  321. if (!p)
  322. goto Einval;
  323. *p++ = '\0';
  324. e->magic = p;
  325. p = strchr(p, del);
  326. if (!p)
  327. goto Einval;
  328. *p++ = '\0';
  329. if (!e->magic[0] || strchr(e->magic, '/'))
  330. goto Einval;
  331. p = strchr(p, del);
  332. if (!p)
  333. goto Einval;
  334. *p++ = '\0';
  335. }
  336. e->interpreter = p;
  337. p = strchr(p, del);
  338. if (!p)
  339. goto Einval;
  340. *p++ = '\0';
  341. if (!e->interpreter[0])
  342. goto Einval;
  343. p = check_special_flags (p, e);
  344. if (*p == '\n')
  345. p++;
  346. if (p != buf + count)
  347. goto Einval;
  348. return e;
  349. out:
  350. return ERR_PTR(err);
  351. Efault:
  352. kfree(e);
  353. return ERR_PTR(-EFAULT);
  354. Einval:
  355. kfree(e);
  356. return ERR_PTR(-EINVAL);
  357. }
  358. /*
  359. * Set status of entry/binfmt_misc:
  360. * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
  361. */
  362. static int parse_command(const char __user *buffer, size_t count)
  363. {
  364. char s[4];
  365. if (!count)
  366. return 0;
  367. if (count > 3)
  368. return -EINVAL;
  369. if (copy_from_user(s, buffer, count))
  370. return -EFAULT;
  371. if (s[count-1] == '\n')
  372. count--;
  373. if (count == 1 && s[0] == '0')
  374. return 1;
  375. if (count == 1 && s[0] == '1')
  376. return 2;
  377. if (count == 2 && s[0] == '-' && s[1] == '1')
  378. return 3;
  379. return -EINVAL;
  380. }
  381. /* generic stuff */
  382. static void entry_status(Node *e, char *page)
  383. {
  384. char *dp;
  385. char *status = "disabled";
  386. const char * flags = "flags: ";
  387. if (test_bit(Enabled, &e->flags))
  388. status = "enabled";
  389. if (!VERBOSE_STATUS) {
  390. sprintf(page, "%s\n", status);
  391. return;
  392. }
  393. sprintf(page, "%s\ninterpreter %s\n", status, e->interpreter);
  394. dp = page + strlen(page);
  395. /* print the special flags */
  396. sprintf (dp, "%s", flags);
  397. dp += strlen (flags);
  398. if (e->flags & MISC_FMT_PRESERVE_ARGV0) {
  399. *dp ++ = 'P';
  400. }
  401. if (e->flags & MISC_FMT_OPEN_BINARY) {
  402. *dp ++ = 'O';
  403. }
  404. if (e->flags & MISC_FMT_CREDENTIALS) {
  405. *dp ++ = 'C';
  406. }
  407. *dp ++ = '\n';
  408. if (!test_bit(Magic, &e->flags)) {
  409. sprintf(dp, "extension .%s\n", e->magic);
  410. } else {
  411. int i;
  412. sprintf(dp, "offset %i\nmagic ", e->offset);
  413. dp = page + strlen(page);
  414. for (i = 0; i < e->size; i++) {
  415. sprintf(dp, "%02x", 0xff & (int) (e->magic[i]));
  416. dp += 2;
  417. }
  418. if (e->mask) {
  419. sprintf(dp, "\nmask ");
  420. dp += 6;
  421. for (i = 0; i < e->size; i++) {
  422. sprintf(dp, "%02x", 0xff & (int) (e->mask[i]));
  423. dp += 2;
  424. }
  425. }
  426. *dp++ = '\n';
  427. *dp = '\0';
  428. }
  429. }
  430. static struct inode *bm_get_inode(struct super_block *sb, int mode)
  431. {
  432. struct inode * inode = new_inode(sb);
  433. if (inode) {
  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_clear_inode(struct inode *inode)
  441. {
  442. kfree(inode->i_private);
  443. }
  444. static void kill_node(Node *e)
  445. {
  446. struct dentry *dentry;
  447. write_lock(&entries_lock);
  448. dentry = e->dentry;
  449. if (dentry) {
  450. list_del_init(&e->list);
  451. e->dentry = NULL;
  452. }
  453. write_unlock(&entries_lock);
  454. if (dentry) {
  455. dentry->d_inode->i_nlink--;
  456. d_drop(dentry);
  457. dput(dentry);
  458. simple_release_fs(&bm_mnt, &entry_count);
  459. }
  460. }
  461. /* /<entry> */
  462. static ssize_t
  463. bm_entry_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos)
  464. {
  465. Node *e = file->f_path.dentry->d_inode->i_private;
  466. ssize_t res;
  467. char *page;
  468. if (!(page = (char*) __get_free_page(GFP_KERNEL)))
  469. return -ENOMEM;
  470. entry_status(e, page);
  471. res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
  472. free_page((unsigned long) page);
  473. return res;
  474. }
  475. static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
  476. size_t count, loff_t *ppos)
  477. {
  478. struct dentry *root;
  479. Node *e = file->f_path.dentry->d_inode->i_private;
  480. int res = parse_command(buffer, count);
  481. switch (res) {
  482. case 1: clear_bit(Enabled, &e->flags);
  483. break;
  484. case 2: set_bit(Enabled, &e->flags);
  485. break;
  486. case 3: root = dget(file->f_path.mnt->mnt_sb->s_root);
  487. mutex_lock(&root->d_inode->i_mutex);
  488. kill_node(e);
  489. mutex_unlock(&root->d_inode->i_mutex);
  490. dput(root);
  491. break;
  492. default: return res;
  493. }
  494. return count;
  495. }
  496. static const struct file_operations bm_entry_operations = {
  497. .read = bm_entry_read,
  498. .write = bm_entry_write,
  499. };
  500. /* /register */
  501. static ssize_t bm_register_write(struct file *file, const char __user *buffer,
  502. size_t count, loff_t *ppos)
  503. {
  504. Node *e;
  505. struct inode *inode;
  506. struct dentry *root, *dentry;
  507. struct super_block *sb = file->f_path.mnt->mnt_sb;
  508. int err = 0;
  509. e = create_entry(buffer, count);
  510. if (IS_ERR(e))
  511. return PTR_ERR(e);
  512. root = dget(sb->s_root);
  513. mutex_lock(&root->d_inode->i_mutex);
  514. dentry = lookup_one_len(e->name, root, strlen(e->name));
  515. err = PTR_ERR(dentry);
  516. if (IS_ERR(dentry))
  517. goto out;
  518. err = -EEXIST;
  519. if (dentry->d_inode)
  520. goto out2;
  521. inode = bm_get_inode(sb, S_IFREG | 0644);
  522. err = -ENOMEM;
  523. if (!inode)
  524. goto out2;
  525. err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
  526. if (err) {
  527. iput(inode);
  528. inode = NULL;
  529. goto out2;
  530. }
  531. e->dentry = dget(dentry);
  532. inode->i_private = e;
  533. inode->i_fop = &bm_entry_operations;
  534. d_instantiate(dentry, inode);
  535. write_lock(&entries_lock);
  536. list_add(&e->list, &entries);
  537. write_unlock(&entries_lock);
  538. err = 0;
  539. out2:
  540. dput(dentry);
  541. out:
  542. mutex_unlock(&root->d_inode->i_mutex);
  543. dput(root);
  544. if (err) {
  545. kfree(e);
  546. return -EINVAL;
  547. }
  548. return count;
  549. }
  550. static const struct file_operations bm_register_operations = {
  551. .write = bm_register_write,
  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.mnt->mnt_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. };
  582. /* Superblock handling */
  583. static const struct super_operations s_ops = {
  584. .statfs = simple_statfs,
  585. .clear_inode = bm_clear_inode,
  586. };
  587. static int bm_fill_super(struct super_block * sb, void * data, int silent)
  588. {
  589. static struct tree_descr bm_files[] = {
  590. [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
  591. [3] = {"register", &bm_register_operations, S_IWUSR},
  592. /* last one */ {""}
  593. };
  594. int err = simple_fill_super(sb, 0x42494e4d, bm_files);
  595. if (!err)
  596. sb->s_op = &s_ops;
  597. return err;
  598. }
  599. static int bm_get_sb(struct file_system_type *fs_type,
  600. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  601. {
  602. return get_sb_single(fs_type, flags, data, bm_fill_super, mnt);
  603. }
  604. static struct linux_binfmt misc_format = {
  605. .module = THIS_MODULE,
  606. .load_binary = load_misc_binary,
  607. };
  608. static struct file_system_type bm_fs_type = {
  609. .owner = THIS_MODULE,
  610. .name = "binfmt_misc",
  611. .get_sb = bm_get_sb,
  612. .kill_sb = kill_litter_super,
  613. };
  614. static int __init init_misc_binfmt(void)
  615. {
  616. int err = register_filesystem(&bm_fs_type);
  617. if (!err) {
  618. err = register_binfmt(&misc_format);
  619. if (err)
  620. unregister_filesystem(&bm_fs_type);
  621. }
  622. return err;
  623. }
  624. static void __exit exit_misc_binfmt(void)
  625. {
  626. unregister_binfmt(&misc_format);
  627. unregister_filesystem(&bm_fs_type);
  628. }
  629. core_initcall(init_misc_binfmt);
  630. module_exit(exit_misc_binfmt);
  631. MODULE_LICENSE("GPL");