bsdload.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Code for loading BSD 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 TARGET_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 in_group_p(gid_t g)
  24. {
  25. /* return TRUE if we're in the specified group, FALSE otherwise */
  26. int ngroup;
  27. int i;
  28. gid_t grouplist[TARGET_NGROUPS];
  29. ngroup = getgroups(TARGET_NGROUPS, grouplist);
  30. for(i = 0; i < ngroup; i++) {
  31. if(grouplist[i] == g) {
  32. return 1;
  33. }
  34. }
  35. return 0;
  36. }
  37. static int count(char ** vec)
  38. {
  39. int i;
  40. for(i = 0; *vec; i++) {
  41. vec++;
  42. }
  43. return(i);
  44. }
  45. static int prepare_binprm(struct linux_binprm *bprm)
  46. {
  47. struct stat st;
  48. int mode;
  49. int retval, id_change;
  50. if(fstat(bprm->fd, &st) < 0) {
  51. return(-errno);
  52. }
  53. mode = st.st_mode;
  54. if(!S_ISREG(mode)) { /* Must be regular file */
  55. return(-EACCES);
  56. }
  57. if(!(mode & 0111)) { /* Must have at least one execute bit set */
  58. return(-EACCES);
  59. }
  60. bprm->e_uid = geteuid();
  61. bprm->e_gid = getegid();
  62. id_change = 0;
  63. /* Set-uid? */
  64. if(mode & S_ISUID) {
  65. bprm->e_uid = st.st_uid;
  66. if(bprm->e_uid != geteuid()) {
  67. id_change = 1;
  68. }
  69. }
  70. /* Set-gid? */
  71. /*
  72. * If setgid is set but no group execute bit then this
  73. * is a candidate for mandatory locking, not a setgid
  74. * executable.
  75. */
  76. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  77. bprm->e_gid = st.st_gid;
  78. if (!in_group_p(bprm->e_gid)) {
  79. id_change = 1;
  80. }
  81. }
  82. memset(bprm->buf, 0, sizeof(bprm->buf));
  83. retval = lseek(bprm->fd, 0L, SEEK_SET);
  84. if(retval >= 0) {
  85. retval = read(bprm->fd, bprm->buf, 128);
  86. }
  87. if(retval < 0) {
  88. perror("prepare_binprm");
  89. exit(-1);
  90. /* return(-errno); */
  91. }
  92. else {
  93. return(retval);
  94. }
  95. }
  96. /* Construct the envp and argv tables on the target stack. */
  97. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  98. abi_ulong stringp, int push_ptr)
  99. {
  100. int n = sizeof(abi_ulong);
  101. abi_ulong envp;
  102. abi_ulong argv;
  103. sp -= (envc + 1) * n;
  104. envp = sp;
  105. sp -= (argc + 1) * n;
  106. argv = sp;
  107. if (push_ptr) {
  108. /* FIXME - handle put_user() failures */
  109. sp -= n;
  110. put_user_ual(envp, sp);
  111. sp -= n;
  112. put_user_ual(argv, sp);
  113. }
  114. sp -= n;
  115. /* FIXME - handle put_user() failures */
  116. put_user_ual(argc, sp);
  117. while (argc-- > 0) {
  118. /* FIXME - handle put_user() failures */
  119. put_user_ual(stringp, argv);
  120. argv += n;
  121. stringp += target_strlen(stringp) + 1;
  122. }
  123. /* FIXME - handle put_user() failures */
  124. put_user_ual(0, argv);
  125. while (envc-- > 0) {
  126. /* FIXME - handle put_user() failures */
  127. put_user_ual(stringp, envp);
  128. envp += n;
  129. stringp += target_strlen(stringp) + 1;
  130. }
  131. /* FIXME - handle put_user() failures */
  132. put_user_ual(0, envp);
  133. return sp;
  134. }
  135. int loader_exec(const char * filename, char ** argv, char ** envp,
  136. struct target_pt_regs * regs, struct image_info *infop)
  137. {
  138. struct linux_binprm bprm;
  139. int retval;
  140. int i;
  141. bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
  142. for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */
  143. bprm.page[i] = NULL;
  144. retval = open(filename, O_RDONLY);
  145. if (retval < 0)
  146. return retval;
  147. bprm.fd = retval;
  148. bprm.filename = (char *)filename;
  149. bprm.argc = count(argv);
  150. bprm.argv = argv;
  151. bprm.envc = count(envp);
  152. bprm.envp = envp;
  153. retval = prepare_binprm(&bprm);
  154. if(retval>=0) {
  155. if (bprm.buf[0] == 0x7f
  156. && bprm.buf[1] == 'E'
  157. && bprm.buf[2] == 'L'
  158. && bprm.buf[3] == 'F') {
  159. retval = load_elf_binary(&bprm,regs,infop);
  160. } else {
  161. fprintf(stderr, "Unknown binary format\n");
  162. return -1;
  163. }
  164. }
  165. if(retval>=0) {
  166. /* success. Initialize important registers */
  167. do_init_thread(regs, infop);
  168. return retval;
  169. }
  170. /* Something went wrong, return the inode and free the argument pages*/
  171. for (i=0 ; i<MAX_ARG_PAGES ; i++) {
  172. free(bprm.page[i]);
  173. }
  174. return(retval);
  175. }