binfmt_flat.c 28 KB

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