2
0

linuxload.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Code for loading Linux executables. Mostly linux kernel code. */
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "qemu.h"
  10. #define NGROUPS 32
  11. /* ??? This should really be somewhere else. */
  12. abi_long memcpy_to_target(abi_ulong dest, const void *src,
  13. unsigned long len)
  14. {
  15. void *host_ptr;
  16. host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
  17. if (!host_ptr)
  18. return -TARGET_EFAULT;
  19. memcpy(host_ptr, src, len);
  20. unlock_user(host_ptr, dest, 1);
  21. return 0;
  22. }
  23. static int count(char ** vec)
  24. {
  25. int i;
  26. for(i = 0; *vec; i++) {
  27. vec++;
  28. }
  29. return(i);
  30. }
  31. static int prepare_binprm(struct linux_binprm *bprm)
  32. {
  33. struct stat st;
  34. int mode;
  35. int retval;
  36. if(fstat(bprm->fd, &st) < 0) {
  37. return(-errno);
  38. }
  39. mode = st.st_mode;
  40. if(!S_ISREG(mode)) { /* Must be regular file */
  41. return(-EACCES);
  42. }
  43. if(!(mode & 0111)) { /* Must have at least one execute bit set */
  44. return(-EACCES);
  45. }
  46. bprm->e_uid = geteuid();
  47. bprm->e_gid = getegid();
  48. /* Set-uid? */
  49. if(mode & S_ISUID) {
  50. bprm->e_uid = st.st_uid;
  51. }
  52. /* Set-gid? */
  53. /*
  54. * If setgid is set but no group execute bit then this
  55. * is a candidate for mandatory locking, not a setgid
  56. * executable.
  57. */
  58. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  59. bprm->e_gid = st.st_gid;
  60. }
  61. retval = read(bprm->fd, bprm->buf, BPRM_BUF_SIZE);
  62. if (retval < 0) {
  63. perror("prepare_binprm");
  64. exit(-1);
  65. }
  66. if (retval < BPRM_BUF_SIZE) {
  67. /* Make sure the rest of the loader won't read garbage. */
  68. memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval);
  69. }
  70. return retval;
  71. }
  72. /* Construct the envp and argv tables on the target stack. */
  73. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  74. abi_ulong stringp, int push_ptr)
  75. {
  76. TaskState *ts = (TaskState *)thread_cpu->opaque;
  77. int n = sizeof(abi_ulong);
  78. abi_ulong envp;
  79. abi_ulong argv;
  80. sp -= (envc + 1) * n;
  81. envp = sp;
  82. sp -= (argc + 1) * n;
  83. argv = sp;
  84. if (push_ptr) {
  85. /* FIXME - handle put_user() failures */
  86. sp -= n;
  87. put_user_ual(envp, sp);
  88. sp -= n;
  89. put_user_ual(argv, sp);
  90. }
  91. sp -= n;
  92. /* FIXME - handle put_user() failures */
  93. put_user_ual(argc, sp);
  94. ts->info->arg_start = stringp;
  95. while (argc-- > 0) {
  96. /* FIXME - handle put_user() failures */
  97. put_user_ual(stringp, argv);
  98. argv += n;
  99. stringp += target_strlen(stringp) + 1;
  100. }
  101. ts->info->arg_end = stringp;
  102. /* FIXME - handle put_user() failures */
  103. put_user_ual(0, argv);
  104. while (envc-- > 0) {
  105. /* FIXME - handle put_user() failures */
  106. put_user_ual(stringp, envp);
  107. envp += n;
  108. stringp += target_strlen(stringp) + 1;
  109. }
  110. /* FIXME - handle put_user() failures */
  111. put_user_ual(0, envp);
  112. return sp;
  113. }
  114. int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
  115. struct target_pt_regs * regs, struct image_info *infop,
  116. struct linux_binprm *bprm)
  117. {
  118. int retval;
  119. int i;
  120. bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
  121. memset(bprm->page, 0, sizeof(bprm->page));
  122. bprm->fd = fdexec;
  123. bprm->filename = (char *)filename;
  124. bprm->argc = count(argv);
  125. bprm->argv = argv;
  126. bprm->envc = count(envp);
  127. bprm->envp = envp;
  128. retval = prepare_binprm(bprm);
  129. if(retval>=0) {
  130. if (bprm->buf[0] == 0x7f
  131. && bprm->buf[1] == 'E'
  132. && bprm->buf[2] == 'L'
  133. && bprm->buf[3] == 'F') {
  134. retval = load_elf_binary(bprm, infop);
  135. #if defined(TARGET_HAS_BFLT)
  136. } else if (bprm->buf[0] == 'b'
  137. && bprm->buf[1] == 'F'
  138. && bprm->buf[2] == 'L'
  139. && bprm->buf[3] == 'T') {
  140. retval = load_flt_binary(bprm, infop);
  141. #endif
  142. } else {
  143. return -ENOEXEC;
  144. }
  145. }
  146. if(retval>=0) {
  147. /* success. Initialize important registers */
  148. do_init_thread(regs, infop);
  149. return retval;
  150. }
  151. /* Something went wrong, return the inode and free the argument pages*/
  152. for (i=0 ; i<MAX_ARG_PAGES ; i++) {
  153. g_free(bprm->page[i]);
  154. }
  155. return(retval);
  156. }