binfmt_flat.c 28 KB

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