binfmt_misc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. 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_ino = get_next_ino();
  435. inode->i_mode = mode;
  436. inode->i_atime = inode->i_mtime = inode->i_ctime =
  437. current_fs_time(inode->i_sb);
  438. }
  439. return inode;
  440. }
  441. static void bm_evict_inode(struct inode *inode)
  442. {
  443. end_writeback(inode);
  444. kfree(inode->i_private);
  445. }
  446. static void kill_node(Node *e)
  447. {
  448. struct dentry *dentry;
  449. write_lock(&entries_lock);
  450. dentry = e->dentry;
  451. if (dentry) {
  452. list_del_init(&e->list);
  453. e->dentry = NULL;
  454. }
  455. write_unlock(&entries_lock);
  456. if (dentry) {
  457. dentry->d_inode->i_nlink--;
  458. d_drop(dentry);
  459. dput(dentry);
  460. simple_release_fs(&bm_mnt, &entry_count);
  461. }
  462. }
  463. /* /<entry> */
  464. static ssize_t
  465. bm_entry_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos)
  466. {
  467. Node *e = file->f_path.dentry->d_inode->i_private;
  468. ssize_t res;
  469. char *page;
  470. if (!(page = (char*) __get_free_page(GFP_KERNEL)))
  471. return -ENOMEM;
  472. entry_status(e, page);
  473. res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
  474. free_page((unsigned long) page);
  475. return res;
  476. }
  477. static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
  478. size_t count, loff_t *ppos)
  479. {
  480. struct dentry *root;
  481. Node *e = file->f_path.dentry->d_inode->i_private;
  482. int res = parse_command(buffer, count);
  483. switch (res) {
  484. case 1: clear_bit(Enabled, &e->flags);
  485. break;
  486. case 2: set_bit(Enabled, &e->flags);
  487. break;
  488. case 3: root = dget(file->f_path.mnt->mnt_sb->s_root);
  489. mutex_lock(&root->d_inode->i_mutex);
  490. kill_node(e);
  491. mutex_unlock(&root->d_inode->i_mutex);
  492. dput(root);
  493. break;
  494. default: return res;
  495. }
  496. return count;
  497. }
  498. static const struct file_operations bm_entry_operations = {
  499. .read = bm_entry_read,
  500. .write = bm_entry_write,
  501. .llseek = default_llseek,
  502. };
  503. /* /register */
  504. static ssize_t bm_register_write(struct file *file, const char __user *buffer,
  505. size_t count, loff_t *ppos)
  506. {
  507. Node *e;
  508. struct inode *inode;
  509. struct dentry *root, *dentry;
  510. struct super_block *sb = file->f_path.mnt->mnt_sb;
  511. int err = 0;
  512. e = create_entry(buffer, count);
  513. if (IS_ERR(e))
  514. return PTR_ERR(e);
  515. root = dget(sb->s_root);
  516. mutex_lock(&root->d_inode->i_mutex);
  517. dentry = lookup_one_len(e->name, root, strlen(e->name));
  518. err = PTR_ERR(dentry);
  519. if (IS_ERR(dentry))
  520. goto out;
  521. err = -EEXIST;
  522. if (dentry->d_inode)
  523. goto out2;
  524. inode = bm_get_inode(sb, S_IFREG | 0644);
  525. err = -ENOMEM;
  526. if (!inode)
  527. goto out2;
  528. err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
  529. if (err) {
  530. iput(inode);
  531. inode = NULL;
  532. goto out2;
  533. }
  534. e->dentry = dget(dentry);
  535. inode->i_private = e;
  536. inode->i_fop = &bm_entry_operations;
  537. d_instantiate(dentry, inode);
  538. write_lock(&entries_lock);
  539. list_add(&e->list, &entries);
  540. write_unlock(&entries_lock);
  541. err = 0;
  542. out2:
  543. dput(dentry);
  544. out:
  545. mutex_unlock(&root->d_inode->i_mutex);
  546. dput(root);
  547. if (err) {
  548. kfree(e);
  549. return -EINVAL;
  550. }
  551. return count;
  552. }
  553. static const struct file_operations bm_register_operations = {
  554. .write = bm_register_write,
  555. .llseek = noop_llseek,
  556. };
  557. /* /status */
  558. static ssize_t
  559. bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  560. {
  561. char *s = enabled ? "enabled\n" : "disabled\n";
  562. return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
  563. }
  564. static ssize_t bm_status_write(struct file * file, const char __user * buffer,
  565. size_t count, loff_t *ppos)
  566. {
  567. int res = parse_command(buffer, count);
  568. struct dentry *root;
  569. switch (res) {
  570. case 1: enabled = 0; break;
  571. case 2: enabled = 1; break;
  572. case 3: root = dget(file->f_path.mnt->mnt_sb->s_root);
  573. mutex_lock(&root->d_inode->i_mutex);
  574. while (!list_empty(&entries))
  575. kill_node(list_entry(entries.next, Node, list));
  576. mutex_unlock(&root->d_inode->i_mutex);
  577. dput(root);
  578. default: return res;
  579. }
  580. return count;
  581. }
  582. static const struct file_operations bm_status_operations = {
  583. .read = bm_status_read,
  584. .write = bm_status_write,
  585. .llseek = default_llseek,
  586. };
  587. /* Superblock handling */
  588. static const struct super_operations s_ops = {
  589. .statfs = simple_statfs,
  590. .evict_inode = bm_evict_inode,
  591. };
  592. static int bm_fill_super(struct super_block * sb, void * data, int silent)
  593. {
  594. static struct tree_descr bm_files[] = {
  595. [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
  596. [3] = {"register", &bm_register_operations, S_IWUSR},
  597. /* last one */ {""}
  598. };
  599. int err = simple_fill_super(sb, 0x42494e4d, bm_files);
  600. if (!err)
  601. sb->s_op = &s_ops;
  602. return err;
  603. }
  604. static struct dentry *bm_mount(struct file_system_type *fs_type,
  605. int flags, const char *dev_name, void *data)
  606. {
  607. return mount_single(fs_type, flags, data, bm_fill_super);
  608. }
  609. static struct linux_binfmt misc_format = {
  610. .module = THIS_MODULE,
  611. .load_binary = load_misc_binary,
  612. };
  613. static struct file_system_type bm_fs_type = {
  614. .owner = THIS_MODULE,
  615. .name = "binfmt_misc",
  616. .mount = bm_mount,
  617. .kill_sb = kill_litter_super,
  618. };
  619. static int __init init_misc_binfmt(void)
  620. {
  621. int err = register_filesystem(&bm_fs_type);
  622. if (!err) {
  623. err = insert_binfmt(&misc_format);
  624. if (err)
  625. unregister_filesystem(&bm_fs_type);
  626. }
  627. return err;
  628. }
  629. static void __exit exit_misc_binfmt(void)
  630. {
  631. unregister_binfmt(&misc_format);
  632. unregister_filesystem(&bm_fs_type);
  633. }
  634. core_initcall(init_misc_binfmt);
  635. module_exit(exit_misc_binfmt);
  636. MODULE_LICENSE("GPL");