binfmt_flat.c 28 KB

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