binfmt_flat.c 28 KB

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