binfmt_flat.c 28 KB

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