binfmt_flat.c 26 KB

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