2
0

linuxload.c 4.0 KB

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