binfmt_flat.c 28 KB

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