binfmt_flat.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. // SPDX-License-Identifier: GPL-2.0
  2. /****************************************************************************/
  3. /*
  4. * linux/fs/binfmt_flat.c
  5. *
  6. * Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com>
  7. * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
  8. * Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com>
  9. * Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com>
  10. * based heavily on:
  11. *
  12. * linux/fs/binfmt_aout.c:
  13. * Copyright (C) 1991, 1992, 1996 Linus Torvalds
  14. * linux/fs/binfmt_flat.c for 2.0 kernel
  15. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
  16. * JAN/99 -- coded full program relocation (gerg@snapgear.com)
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/sched/task_stack.h>
  22. #include <linux/mm.h>
  23. #include <linux/mman.h>
  24. #include <linux/errno.h>
  25. #include <linux/signal.h>
  26. #include <linux/string.h>
  27. #include <linux/fs.h>
  28. #include <linux/file.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/user.h>
  31. #include <linux/slab.h>
  32. #include <linux/binfmts.h>
  33. #include <linux/personality.h>
  34. #include <linux/init.h>
  35. #include <linux/flat.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/vmalloc.h>
  38. #include <asm/byteorder.h>
  39. #include <linux/unaligned.h>
  40. #include <asm/cacheflush.h>
  41. #include <asm/page.h>
  42. #include <asm/flat.h>
  43. #ifndef flat_get_relocate_addr
  44. #define flat_get_relocate_addr(rel) (rel)
  45. #endif
  46. /****************************************************************************/
  47. /*
  48. * User data (data section and bss) needs to be aligned.
  49. * We pick 0x20 here because it is the max value elf2flt has always
  50. * used in producing FLAT files, and because it seems to be large
  51. * enough to make all the gcc alignment related tests happy.
  52. */
  53. #define FLAT_DATA_ALIGN (0x20)
  54. /*
  55. * User data (stack) also needs to be aligned.
  56. * Here we can be a bit looser than the data sections since this
  57. * needs to only meet arch ABI requirements.
  58. */
  59. #define FLAT_STACK_ALIGN max_t(unsigned long, sizeof(void *), ARCH_SLAB_MINALIGN)
  60. #define RELOC_FAILED 0xff00ff01 /* Relocation incorrect somewhere */
  61. #define UNLOADED_LIB 0x7ff000ff /* Placeholder for unused library */
  62. #define MAX_SHARED_LIBS (1)
  63. #ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
  64. #define DATA_START_OFFSET_WORDS (0)
  65. #define MAX_SHARED_LIBS_UPDATE (0)
  66. #else
  67. #define DATA_START_OFFSET_WORDS (MAX_SHARED_LIBS)
  68. #define MAX_SHARED_LIBS_UPDATE (MAX_SHARED_LIBS)
  69. #endif
  70. struct lib_info {
  71. struct {
  72. unsigned long start_code; /* Start of text segment */
  73. unsigned long start_data; /* Start of data segment */
  74. unsigned long start_brk; /* End of data segment */
  75. unsigned long text_len; /* Length of text segment */
  76. unsigned long entry; /* Start address for this module */
  77. unsigned long build_date; /* When this one was compiled */
  78. bool loaded; /* Has this library been loaded? */
  79. } lib_list[MAX_SHARED_LIBS];
  80. };
  81. static int load_flat_binary(struct linux_binprm *);
  82. static struct linux_binfmt flat_format = {
  83. .module = THIS_MODULE,
  84. .load_binary = load_flat_binary,
  85. };
  86. /****************************************************************************/
  87. /*
  88. * create_flat_tables() parses the env- and arg-strings in new user
  89. * memory and creates the pointer tables from them, and puts their
  90. * addresses on the "stack", recording the new stack pointer value.
  91. */
  92. static int create_flat_tables(struct linux_binprm *bprm, unsigned long arg_start)
  93. {
  94. char __user *p;
  95. unsigned long __user *sp;
  96. long i, len;
  97. p = (char __user *)arg_start;
  98. sp = (unsigned long __user *)current->mm->start_stack;
  99. sp -= bprm->envc + 1;
  100. sp -= bprm->argc + 1;
  101. if (IS_ENABLED(CONFIG_BINFMT_FLAT_ARGVP_ENVP_ON_STACK))
  102. sp -= 2; /* argvp + envp */
  103. sp -= 1; /* &argc */
  104. current->mm->start_stack = (unsigned long)sp & -FLAT_STACK_ALIGN;
  105. sp = (unsigned long __user *)current->mm->start_stack;
  106. if (put_user(bprm->argc, sp++))
  107. return -EFAULT;
  108. if (IS_ENABLED(CONFIG_BINFMT_FLAT_ARGVP_ENVP_ON_STACK)) {
  109. unsigned long argv, envp;
  110. argv = (unsigned long)(sp + 2);
  111. envp = (unsigned long)(sp + 2 + bprm->argc + 1);
  112. if (put_user(argv, sp++) || put_user(envp, sp++))
  113. return -EFAULT;
  114. }
  115. current->mm->arg_start = (unsigned long)p;
  116. for (i = bprm->argc; i > 0; i--) {
  117. if (put_user((unsigned long)p, sp++))
  118. return -EFAULT;
  119. len = strnlen_user(p, MAX_ARG_STRLEN);
  120. if (!len || len > MAX_ARG_STRLEN)
  121. return -EINVAL;
  122. p += len;
  123. }
  124. if (put_user(0, sp++))
  125. return -EFAULT;
  126. current->mm->arg_end = (unsigned long)p;
  127. current->mm->env_start = (unsigned long) p;
  128. for (i = bprm->envc; i > 0; i--) {
  129. if (put_user((unsigned long)p, sp++))
  130. return -EFAULT;
  131. len = strnlen_user(p, MAX_ARG_STRLEN);
  132. if (!len || len > MAX_ARG_STRLEN)
  133. return -EINVAL;
  134. p += len;
  135. }
  136. if (put_user(0, sp++))
  137. return -EFAULT;
  138. current->mm->env_end = (unsigned long)p;
  139. return 0;
  140. }
  141. /****************************************************************************/
  142. #ifdef CONFIG_BINFMT_ZFLAT
  143. #include <linux/zlib.h>
  144. #define LBUFSIZE 4000
  145. /* gzip flag byte */
  146. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  147. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  148. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  149. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  150. #define COMMENT 0x10 /* bit 4 set: file comment present */
  151. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  152. #define RESERVED 0xC0 /* bit 6,7: reserved */
  153. static int decompress_exec(struct linux_binprm *bprm, loff_t fpos, char *dst,
  154. long len, int fd)
  155. {
  156. unsigned char *buf;
  157. z_stream strm;
  158. int ret, retval;
  159. pr_debug("decompress_exec(offset=%llx,buf=%p,len=%lx)\n", fpos, dst, len);
  160. memset(&strm, 0, sizeof(strm));
  161. strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
  162. if (!strm.workspace)
  163. return -ENOMEM;
  164. buf = kmalloc(LBUFSIZE, GFP_KERNEL);
  165. if (!buf) {
  166. retval = -ENOMEM;
  167. goto out_free;
  168. }
  169. /* Read in first chunk of data and parse gzip header. */
  170. ret = kernel_read(bprm->file, buf, LBUFSIZE, &fpos);
  171. strm.next_in = buf;
  172. strm.avail_in = ret;
  173. strm.total_in = 0;
  174. retval = -ENOEXEC;
  175. /* Check minimum size -- gzip header */
  176. if (ret < 10) {
  177. pr_debug("file too small?\n");
  178. goto out_free_buf;
  179. }
  180. /* Check gzip magic number */
  181. if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) {
  182. pr_debug("unknown compression magic?\n");
  183. goto out_free_buf;
  184. }
  185. /* Check gzip method */
  186. if (buf[2] != 8) {
  187. pr_debug("unknown compression method?\n");
  188. goto out_free_buf;
  189. }
  190. /* Check gzip flags */
  191. if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) ||
  192. (buf[3] & RESERVED)) {
  193. pr_debug("unknown flags?\n");
  194. goto out_free_buf;
  195. }
  196. ret = 10;
  197. if (buf[3] & EXTRA_FIELD) {
  198. ret += 2 + buf[10] + (buf[11] << 8);
  199. if (unlikely(ret >= LBUFSIZE)) {
  200. pr_debug("buffer overflow (EXTRA)?\n");
  201. goto out_free_buf;
  202. }
  203. }
  204. if (buf[3] & ORIG_NAME) {
  205. while (ret < LBUFSIZE && buf[ret++] != 0)
  206. ;
  207. if (unlikely(ret == LBUFSIZE)) {
  208. pr_debug("buffer overflow (ORIG_NAME)?\n");
  209. goto out_free_buf;
  210. }
  211. }
  212. if (buf[3] & COMMENT) {
  213. while (ret < LBUFSIZE && buf[ret++] != 0)
  214. ;
  215. if (unlikely(ret == LBUFSIZE)) {
  216. pr_debug("buffer overflow (COMMENT)?\n");
  217. goto out_free_buf;
  218. }
  219. }
  220. strm.next_in += ret;
  221. strm.avail_in -= ret;
  222. strm.next_out = dst;
  223. strm.avail_out = len;
  224. strm.total_out = 0;
  225. if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
  226. pr_debug("zlib init failed?\n");
  227. goto out_free_buf;
  228. }
  229. while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
  230. ret = kernel_read(bprm->file, buf, LBUFSIZE, &fpos);
  231. if (ret <= 0)
  232. break;
  233. len -= ret;
  234. strm.next_in = buf;
  235. strm.avail_in = ret;
  236. strm.total_in = 0;
  237. }
  238. if (ret < 0) {
  239. pr_debug("decompression failed (%d), %s\n",
  240. ret, strm.msg);
  241. goto out_zlib;
  242. }
  243. retval = 0;
  244. out_zlib:
  245. zlib_inflateEnd(&strm);
  246. out_free_buf:
  247. kfree(buf);
  248. out_free:
  249. kfree(strm.workspace);
  250. return retval;
  251. }
  252. #endif /* CONFIG_BINFMT_ZFLAT */
  253. /****************************************************************************/
  254. static unsigned long
  255. calc_reloc(unsigned long r, struct lib_info *p)
  256. {
  257. unsigned long addr;
  258. unsigned long start_brk;
  259. unsigned long start_data;
  260. unsigned long text_len;
  261. unsigned long start_code;
  262. start_brk = p->lib_list[0].start_brk;
  263. start_data = p->lib_list[0].start_data;
  264. start_code = p->lib_list[0].start_code;
  265. text_len = p->lib_list[0].text_len;
  266. if (r > start_brk - start_data + text_len) {
  267. pr_err("reloc outside program 0x%lx (0 - 0x%lx/0x%lx)",
  268. r, start_brk-start_data+text_len, text_len);
  269. goto failed;
  270. }
  271. if (r < text_len) /* In text segment */
  272. addr = r + start_code;
  273. else /* In data segment */
  274. addr = r - text_len + start_data;
  275. /* Range checked already above so doing the range tests is redundant...*/
  276. return addr;
  277. failed:
  278. pr_cont(", killing %s!\n", current->comm);
  279. send_sig(SIGSEGV, current, 0);
  280. return RELOC_FAILED;
  281. }
  282. /****************************************************************************/
  283. #ifdef CONFIG_BINFMT_FLAT_OLD
  284. static void old_reloc(unsigned long rl)
  285. {
  286. static const char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
  287. flat_v2_reloc_t r;
  288. unsigned long __user *ptr;
  289. unsigned long val;
  290. r.value = rl;
  291. #if defined(CONFIG_COLDFIRE)
  292. ptr = (unsigned long __user *)(current->mm->start_code + r.reloc.offset);
  293. #else
  294. ptr = (unsigned long __user *)(current->mm->start_data + r.reloc.offset);
  295. #endif
  296. get_user(val, ptr);
  297. pr_debug("Relocation of variable at DATASEG+%x "
  298. "(address %p, currently %lx) into segment %s\n",
  299. r.reloc.offset, ptr, val, segment[r.reloc.type]);
  300. switch (r.reloc.type) {
  301. case OLD_FLAT_RELOC_TYPE_TEXT:
  302. val += current->mm->start_code;
  303. break;
  304. case OLD_FLAT_RELOC_TYPE_DATA:
  305. val += current->mm->start_data;
  306. break;
  307. case OLD_FLAT_RELOC_TYPE_BSS:
  308. val += current->mm->end_data;
  309. break;
  310. default:
  311. pr_err("Unknown relocation type=%x\n", r.reloc.type);
  312. break;
  313. }
  314. put_user(val, ptr);
  315. pr_debug("Relocation became %lx\n", val);
  316. }
  317. #endif /* CONFIG_BINFMT_FLAT_OLD */
  318. /****************************************************************************/
  319. static inline u32 __user *skip_got_header(u32 __user *rp)
  320. {
  321. if (IS_ENABLED(CONFIG_RISCV)) {
  322. /*
  323. * RISC-V has a 16 byte GOT PLT header for elf64-riscv
  324. * and 8 byte GOT PLT header for elf32-riscv.
  325. * Skip the whole GOT PLT header, since it is reserved
  326. * for the dynamic linker (ld.so).
  327. */
  328. u32 rp_val0, rp_val1;
  329. if (get_user(rp_val0, rp))
  330. return rp;
  331. if (get_user(rp_val1, rp + 1))
  332. return rp;
  333. if (rp_val0 == 0xffffffff && rp_val1 == 0xffffffff)
  334. rp += 4;
  335. else if (rp_val0 == 0xffffffff)
  336. rp += 2;
  337. }
  338. return rp;
  339. }
  340. static int load_flat_file(struct linux_binprm *bprm,
  341. struct lib_info *libinfo, unsigned long *extra_stack)
  342. {
  343. struct flat_hdr *hdr;
  344. unsigned long textpos, datapos, realdatastart;
  345. u32 text_len, data_len, bss_len, stack_len, full_data, flags;
  346. unsigned long len, memp, memp_size, extra, rlim;
  347. __be32 __user *reloc;
  348. u32 __user *rp;
  349. int i, rev, relocs;
  350. loff_t fpos;
  351. unsigned long start_code, end_code;
  352. ssize_t result;
  353. int ret;
  354. hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */
  355. text_len = ntohl(hdr->data_start);
  356. data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start);
  357. bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
  358. stack_len = ntohl(hdr->stack_size);
  359. if (extra_stack) {
  360. stack_len += *extra_stack;
  361. *extra_stack = stack_len;
  362. }
  363. relocs = ntohl(hdr->reloc_count);
  364. flags = ntohl(hdr->flags);
  365. rev = ntohl(hdr->rev);
  366. full_data = data_len + relocs * sizeof(unsigned long);
  367. if (strncmp(hdr->magic, "bFLT", 4)) {
  368. /*
  369. * Previously, here was a printk to tell people
  370. * "BINFMT_FLAT: bad header magic".
  371. * But for the kernel which also use ELF FD-PIC format, this
  372. * error message is confusing.
  373. * because a lot of people do not manage to produce good
  374. */
  375. ret = -ENOEXEC;
  376. goto err;
  377. }
  378. if (flags & FLAT_FLAG_KTRACE)
  379. pr_info("Loading file: %s\n", bprm->filename);
  380. #ifdef CONFIG_BINFMT_FLAT_OLD
  381. if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
  382. pr_err("bad flat file version 0x%x (supported 0x%lx and 0x%lx)\n",
  383. rev, FLAT_VERSION, OLD_FLAT_VERSION);
  384. ret = -ENOEXEC;
  385. goto err;
  386. }
  387. /*
  388. * fix up the flags for the older format, there were all kinds
  389. * of endian hacks, this only works for the simple cases
  390. */
  391. if (rev == OLD_FLAT_VERSION &&
  392. (flags || IS_ENABLED(CONFIG_BINFMT_FLAT_OLD_ALWAYS_RAM)))
  393. flags = FLAT_FLAG_RAM;
  394. #else /* CONFIG_BINFMT_FLAT_OLD */
  395. if (rev != FLAT_VERSION) {
  396. pr_err("bad flat file version 0x%x (supported 0x%lx)\n",
  397. rev, FLAT_VERSION);
  398. ret = -ENOEXEC;
  399. goto err;
  400. }
  401. #endif /* !CONFIG_BINFMT_FLAT_OLD */
  402. /*
  403. * Make sure the header params are sane.
  404. * 28 bits (256 MB) is way more than reasonable in this case.
  405. * If some top bits are set we have probable binary corruption.
  406. */
  407. if ((text_len | data_len | bss_len | stack_len | relocs | full_data) >> 28) {
  408. pr_err("bad header\n");
  409. ret = -ENOEXEC;
  410. goto err;
  411. }
  412. #ifndef CONFIG_BINFMT_ZFLAT
  413. if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
  414. pr_err("Support for ZFLAT executables is not enabled.\n");
  415. ret = -ENOEXEC;
  416. goto err;
  417. }
  418. #endif
  419. /*
  420. * Check initial limits. This avoids letting people circumvent
  421. * size limits imposed on them by creating programs with large
  422. * arrays in the data or bss.
  423. */
  424. rlim = rlimit(RLIMIT_DATA);
  425. if (rlim >= RLIM_INFINITY)
  426. rlim = ~0;
  427. if (data_len + bss_len > rlim) {
  428. ret = -ENOMEM;
  429. goto err;
  430. }
  431. /* Flush all traces of the currently running executable */
  432. ret = begin_new_exec(bprm);
  433. if (ret)
  434. goto err;
  435. /* OK, This is the point of no return */
  436. set_personality(PER_LINUX_32BIT);
  437. setup_new_exec(bprm);
  438. /*
  439. * calculate the extra space we need to map in
  440. */
  441. extra = max_t(unsigned long, bss_len + stack_len,
  442. relocs * sizeof(unsigned long));
  443. /*
  444. * there are a couple of cases here, the separate code/data
  445. * case, and then the fully copied to RAM case which lumps
  446. * it all together.
  447. */
  448. if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
  449. /*
  450. * this should give us a ROM ptr, but if it doesn't we don't
  451. * really care
  452. */
  453. pr_debug("ROM mapping of file (we hope)\n");
  454. textpos = vm_mmap(bprm->file, 0, text_len, PROT_READ|PROT_EXEC,
  455. MAP_PRIVATE, 0);
  456. if (!textpos || IS_ERR_VALUE(textpos)) {
  457. ret = textpos;
  458. if (!textpos)
  459. ret = -ENOMEM;
  460. pr_err("Unable to mmap process text, errno %d\n", ret);
  461. goto err;
  462. }
  463. len = data_len + extra +
  464. DATA_START_OFFSET_WORDS * sizeof(unsigned long);
  465. len = PAGE_ALIGN(len);
  466. realdatastart = vm_mmap(NULL, 0, len,
  467. PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
  468. if (realdatastart == 0 || IS_ERR_VALUE(realdatastart)) {
  469. ret = realdatastart;
  470. if (!realdatastart)
  471. ret = -ENOMEM;
  472. pr_err("Unable to allocate RAM for process data, "
  473. "errno %d\n", ret);
  474. vm_munmap(textpos, text_len);
  475. goto err;
  476. }
  477. datapos = ALIGN(realdatastart +
  478. DATA_START_OFFSET_WORDS * sizeof(unsigned long),
  479. FLAT_DATA_ALIGN);
  480. pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
  481. data_len + bss_len + stack_len, datapos);
  482. fpos = ntohl(hdr->data_start);
  483. #ifdef CONFIG_BINFMT_ZFLAT
  484. if (flags & FLAT_FLAG_GZDATA) {
  485. result = decompress_exec(bprm, fpos, (char *)datapos,
  486. full_data, 0);
  487. } else
  488. #endif
  489. {
  490. result = read_code(bprm->file, datapos, fpos,
  491. full_data);
  492. }
  493. if (IS_ERR_VALUE(result)) {
  494. ret = result;
  495. pr_err("Unable to read data+bss, errno %d\n", ret);
  496. vm_munmap(textpos, text_len);
  497. vm_munmap(realdatastart, len);
  498. goto err;
  499. }
  500. reloc = (__be32 __user *)
  501. (datapos + (ntohl(hdr->reloc_start) - text_len));
  502. memp = realdatastart;
  503. memp_size = len;
  504. } else {
  505. len = text_len + data_len + extra +
  506. DATA_START_OFFSET_WORDS * sizeof(u32);
  507. len = PAGE_ALIGN(len);
  508. textpos = vm_mmap(NULL, 0, len,
  509. PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
  510. if (!textpos || IS_ERR_VALUE(textpos)) {
  511. ret = textpos;
  512. if (!textpos)
  513. ret = -ENOMEM;
  514. pr_err("Unable to allocate RAM for process text/data, "
  515. "errno %d\n", ret);
  516. goto err;
  517. }
  518. realdatastart = textpos + ntohl(hdr->data_start);
  519. datapos = ALIGN(realdatastart +
  520. DATA_START_OFFSET_WORDS * sizeof(u32),
  521. FLAT_DATA_ALIGN);
  522. reloc = (__be32 __user *)
  523. (datapos + (ntohl(hdr->reloc_start) - text_len));
  524. memp = textpos;
  525. memp_size = len;
  526. #ifdef CONFIG_BINFMT_ZFLAT
  527. /*
  528. * load it all in and treat it like a RAM load from now on
  529. */
  530. if (flags & FLAT_FLAG_GZIP) {
  531. #ifndef CONFIG_MMU
  532. result = decompress_exec(bprm, sizeof(struct flat_hdr),
  533. (((char *)textpos) + sizeof(struct flat_hdr)),
  534. (text_len + full_data
  535. - sizeof(struct flat_hdr)),
  536. 0);
  537. memmove((void *) datapos, (void *) realdatastart,
  538. full_data);
  539. #else
  540. /*
  541. * This is used on MMU systems mainly for testing.
  542. * Let's use a kernel buffer to simplify things.
  543. */
  544. long unz_text_len = text_len - sizeof(struct flat_hdr);
  545. long unz_len = unz_text_len + full_data;
  546. char *unz_data = vmalloc(unz_len);
  547. if (!unz_data) {
  548. result = -ENOMEM;
  549. } else {
  550. result = decompress_exec(bprm, sizeof(struct flat_hdr),
  551. unz_data, unz_len, 0);
  552. if (result == 0 &&
  553. (copy_to_user((void __user *)textpos + sizeof(struct flat_hdr),
  554. unz_data, unz_text_len) ||
  555. copy_to_user((void __user *)datapos,
  556. unz_data + unz_text_len, full_data)))
  557. result = -EFAULT;
  558. vfree(unz_data);
  559. }
  560. #endif
  561. } else if (flags & FLAT_FLAG_GZDATA) {
  562. result = read_code(bprm->file, textpos, 0, text_len);
  563. if (!IS_ERR_VALUE(result)) {
  564. #ifndef CONFIG_MMU
  565. result = decompress_exec(bprm, text_len, (char *) datapos,
  566. full_data, 0);
  567. #else
  568. char *unz_data = vmalloc(full_data);
  569. if (!unz_data) {
  570. result = -ENOMEM;
  571. } else {
  572. result = decompress_exec(bprm, text_len,
  573. unz_data, full_data, 0);
  574. if (result == 0 &&
  575. copy_to_user((void __user *)datapos,
  576. unz_data, full_data))
  577. result = -EFAULT;
  578. vfree(unz_data);
  579. }
  580. #endif
  581. }
  582. } else
  583. #endif /* CONFIG_BINFMT_ZFLAT */
  584. {
  585. result = read_code(bprm->file, textpos, 0, text_len);
  586. if (!IS_ERR_VALUE(result))
  587. result = read_code(bprm->file, datapos,
  588. ntohl(hdr->data_start),
  589. full_data);
  590. }
  591. if (IS_ERR_VALUE(result)) {
  592. ret = result;
  593. pr_err("Unable to read code+data+bss, errno %d\n", ret);
  594. vm_munmap(textpos, text_len + data_len + extra +
  595. DATA_START_OFFSET_WORDS * sizeof(u32));
  596. goto err;
  597. }
  598. }
  599. start_code = textpos + sizeof(struct flat_hdr);
  600. end_code = textpos + text_len;
  601. text_len -= sizeof(struct flat_hdr); /* the real code len */
  602. /* The main program needs a little extra setup in the task structure */
  603. current->mm->start_code = start_code;
  604. current->mm->end_code = end_code;
  605. current->mm->start_data = datapos;
  606. current->mm->end_data = datapos + data_len;
  607. /*
  608. * set up the brk stuff, uses any slack left in data/bss/stack
  609. * allocation. We put the brk after the bss (between the bss
  610. * and stack) like other platforms.
  611. * Userspace code relies on the stack pointer starting out at
  612. * an address right at the end of a page.
  613. */
  614. current->mm->start_brk = datapos + data_len + bss_len;
  615. current->mm->brk = (current->mm->start_brk + 3) & ~3;
  616. #ifndef CONFIG_MMU
  617. current->mm->context.end_brk = memp + memp_size - stack_len;
  618. #endif
  619. if (flags & FLAT_FLAG_KTRACE) {
  620. pr_info("Mapping is %lx, Entry point is %x, data_start is %x\n",
  621. textpos, 0x00ffffff&ntohl(hdr->entry), ntohl(hdr->data_start));
  622. pr_info("%s %s: TEXT=%lx-%lx DATA=%lx-%lx BSS=%lx-%lx\n",
  623. "Load", bprm->filename,
  624. start_code, end_code, datapos, datapos + data_len,
  625. datapos + data_len, (datapos + data_len + bss_len + 3) & ~3);
  626. }
  627. /* Store the current module values into the global library structure */
  628. libinfo->lib_list[0].start_code = start_code;
  629. libinfo->lib_list[0].start_data = datapos;
  630. libinfo->lib_list[0].start_brk = datapos + data_len + bss_len;
  631. libinfo->lib_list[0].text_len = text_len;
  632. libinfo->lib_list[0].loaded = 1;
  633. libinfo->lib_list[0].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
  634. libinfo->lib_list[0].build_date = ntohl(hdr->build_date);
  635. /*
  636. * We just load the allocations into some temporary memory to
  637. * help simplify all this mumbo jumbo
  638. *
  639. * We've got two different sections of relocation entries.
  640. * The first is the GOT which resides at the beginning of the data segment
  641. * and is terminated with a -1. This one can be relocated in place.
  642. * The second is the extra relocation entries tacked after the image's
  643. * data segment. These require a little more processing as the entry is
  644. * really an offset into the image which contains an offset into the
  645. * image.
  646. */
  647. if (flags & FLAT_FLAG_GOTPIC) {
  648. rp = skip_got_header((u32 __user *) datapos);
  649. for (; ; rp++) {
  650. u32 addr, rp_val;
  651. if (get_user(rp_val, rp))
  652. return -EFAULT;
  653. if (rp_val == 0xffffffff)
  654. break;
  655. if (rp_val) {
  656. addr = calc_reloc(rp_val, libinfo);
  657. if (addr == RELOC_FAILED) {
  658. ret = -ENOEXEC;
  659. goto err;
  660. }
  661. if (put_user(addr, rp))
  662. return -EFAULT;
  663. }
  664. }
  665. }
  666. /*
  667. * Now run through the relocation entries.
  668. * We've got to be careful here as C++ produces relocatable zero
  669. * entries in the constructor and destructor tables which are then
  670. * tested for being not zero (which will always occur unless we're
  671. * based from address zero). This causes an endless loop as __start
  672. * is at zero. The solution used is to not relocate zero addresses.
  673. * This has the negative side effect of not allowing a global data
  674. * reference to be statically initialised to _stext (I've moved
  675. * __start to address 4 so that is okay).
  676. */
  677. if (rev > OLD_FLAT_VERSION) {
  678. for (i = 0; i < relocs; i++) {
  679. u32 addr, relval;
  680. __be32 tmp;
  681. /*
  682. * Get the address of the pointer to be
  683. * relocated (of course, the address has to be
  684. * relocated first).
  685. */
  686. if (get_user(tmp, reloc + i))
  687. return -EFAULT;
  688. relval = ntohl(tmp);
  689. addr = flat_get_relocate_addr(relval);
  690. rp = (u32 __user *)calc_reloc(addr, libinfo);
  691. if (rp == (u32 __user *)RELOC_FAILED) {
  692. ret = -ENOEXEC;
  693. goto err;
  694. }
  695. /* Get the pointer's value. */
  696. ret = flat_get_addr_from_rp(rp, relval, flags, &addr);
  697. if (unlikely(ret))
  698. goto err;
  699. if (addr != 0) {
  700. /*
  701. * Do the relocation. PIC relocs in the data section are
  702. * already in target order
  703. */
  704. if ((flags & FLAT_FLAG_GOTPIC) == 0) {
  705. /*
  706. * Meh, the same value can have a different
  707. * byte order based on a flag..
  708. */
  709. addr = ntohl((__force __be32)addr);
  710. }
  711. addr = calc_reloc(addr, libinfo);
  712. if (addr == RELOC_FAILED) {
  713. ret = -ENOEXEC;
  714. goto err;
  715. }
  716. /* Write back the relocated pointer. */
  717. ret = flat_put_addr_at_rp(rp, addr, relval);
  718. if (unlikely(ret))
  719. goto err;
  720. }
  721. }
  722. #ifdef CONFIG_BINFMT_FLAT_OLD
  723. } else {
  724. for (i = 0; i < relocs; i++) {
  725. __be32 relval;
  726. if (get_user(relval, reloc + i))
  727. return -EFAULT;
  728. old_reloc(ntohl(relval));
  729. }
  730. #endif /* CONFIG_BINFMT_FLAT_OLD */
  731. }
  732. flush_icache_user_range(start_code, end_code);
  733. /* zero the BSS, BRK and stack areas */
  734. if (clear_user((void __user *)(datapos + data_len), bss_len +
  735. (memp + memp_size - stack_len - /* end brk */
  736. libinfo->lib_list[0].start_brk) + /* start brk */
  737. stack_len))
  738. return -EFAULT;
  739. return 0;
  740. err:
  741. return ret;
  742. }
  743. /****************************************************************************/
  744. /*
  745. * These are the functions used to load flat style executables and shared
  746. * libraries. There is no binary dependent code anywhere else.
  747. */
  748. static int load_flat_binary(struct linux_binprm *bprm)
  749. {
  750. struct lib_info libinfo;
  751. struct pt_regs *regs = current_pt_regs();
  752. unsigned long stack_len = 0;
  753. unsigned long start_addr;
  754. int res;
  755. int i, j;
  756. memset(&libinfo, 0, sizeof(libinfo));
  757. /*
  758. * We have to add the size of our arguments to our stack size
  759. * otherwise it's too easy for users to create stack overflows
  760. * by passing in a huge argument list. And yes, we have to be
  761. * pedantic and include space for the argv/envp array as it may have
  762. * a lot of entries.
  763. */
  764. #ifndef CONFIG_MMU
  765. stack_len += PAGE_SIZE * MAX_ARG_PAGES - bprm->p; /* the strings */
  766. #endif
  767. stack_len += (bprm->argc + 1) * sizeof(char *); /* the argv array */
  768. stack_len += (bprm->envc + 1) * sizeof(char *); /* the envp array */
  769. stack_len = ALIGN(stack_len, FLAT_STACK_ALIGN);
  770. res = load_flat_file(bprm, &libinfo, &stack_len);
  771. if (res < 0)
  772. return res;
  773. /* Update data segment pointers for all libraries */
  774. for (i = 0; i < MAX_SHARED_LIBS_UPDATE; i++) {
  775. if (!libinfo.lib_list[i].loaded)
  776. continue;
  777. for (j = 0; j < MAX_SHARED_LIBS; j++) {
  778. unsigned long val = libinfo.lib_list[j].loaded ?
  779. libinfo.lib_list[j].start_data : UNLOADED_LIB;
  780. unsigned long __user *p = (unsigned long __user *)
  781. libinfo.lib_list[i].start_data;
  782. p -= j + 1;
  783. if (put_user(val, p))
  784. return -EFAULT;
  785. }
  786. }
  787. set_binfmt(&flat_format);
  788. #ifdef CONFIG_MMU
  789. res = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
  790. if (!res)
  791. res = create_flat_tables(bprm, bprm->p);
  792. #else
  793. /* Stash our initial stack pointer into the mm structure */
  794. current->mm->start_stack =
  795. ((current->mm->context.end_brk + stack_len + 3) & ~3) - 4;
  796. pr_debug("sp=%lx\n", current->mm->start_stack);
  797. /* copy the arg pages onto the stack */
  798. res = transfer_args_to_stack(bprm, &current->mm->start_stack);
  799. if (!res)
  800. res = create_flat_tables(bprm, current->mm->start_stack);
  801. #endif
  802. if (res)
  803. return res;
  804. /* Fake some return addresses to ensure the call chain will
  805. * initialise library in order for us. We are required to call
  806. * lib 1 first, then 2, ... and finally the main program (id 0).
  807. */
  808. start_addr = libinfo.lib_list[0].entry;
  809. #ifdef FLAT_PLAT_INIT
  810. FLAT_PLAT_INIT(regs);
  811. #endif
  812. finalize_exec(bprm);
  813. pr_debug("start_thread(regs=0x%p, entry=0x%lx, start_stack=0x%lx)\n",
  814. regs, start_addr, current->mm->start_stack);
  815. start_thread(regs, start_addr, current->mm->start_stack);
  816. return 0;
  817. }
  818. /****************************************************************************/
  819. static int __init init_flat_binfmt(void)
  820. {
  821. register_binfmt(&flat_format);
  822. return 0;
  823. }
  824. core_initcall(init_flat_binfmt);
  825. /****************************************************************************/