2
0

bsdload.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Code for loading BSD executables. Mostly linux kernel code. */
  2. #include "qemu/osdep.h"
  3. #include "qemu.h"
  4. #define TARGET_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. memset(bprm->buf, 0, sizeof(bprm->buf));
  56. retval = lseek(bprm->fd, 0L, SEEK_SET);
  57. if(retval >= 0) {
  58. retval = read(bprm->fd, bprm->buf, 128);
  59. }
  60. if(retval < 0) {
  61. perror("prepare_binprm");
  62. exit(-1);
  63. /* return(-errno); */
  64. }
  65. else {
  66. return(retval);
  67. }
  68. }
  69. /* Construct the envp and argv tables on the target stack. */
  70. abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
  71. abi_ulong stringp, int push_ptr)
  72. {
  73. int n = sizeof(abi_ulong);
  74. abi_ulong envp;
  75. abi_ulong argv;
  76. sp -= (envc + 1) * n;
  77. envp = sp;
  78. sp -= (argc + 1) * n;
  79. argv = sp;
  80. if (push_ptr) {
  81. /* FIXME - handle put_user() failures */
  82. sp -= n;
  83. put_user_ual(envp, sp);
  84. sp -= n;
  85. put_user_ual(argv, sp);
  86. }
  87. sp -= n;
  88. /* FIXME - handle put_user() failures */
  89. put_user_ual(argc, sp);
  90. while (argc-- > 0) {
  91. /* FIXME - handle put_user() failures */
  92. put_user_ual(stringp, argv);
  93. argv += n;
  94. stringp += target_strlen(stringp) + 1;
  95. }
  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(const char * filename, char ** argv, char ** envp,
  109. struct target_pt_regs * regs, struct image_info *infop)
  110. {
  111. struct linux_binprm bprm;
  112. int retval;
  113. int i;
  114. bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
  115. for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */
  116. bprm.page[i] = NULL;
  117. retval = open(filename, O_RDONLY);
  118. if (retval < 0)
  119. return retval;
  120. bprm.fd = retval;
  121. bprm.filename = (char *)filename;
  122. bprm.argc = count(argv);
  123. bprm.argv = argv;
  124. bprm.envc = count(envp);
  125. bprm.envp = envp;
  126. retval = prepare_binprm(&bprm);
  127. if(retval>=0) {
  128. if (bprm.buf[0] == 0x7f
  129. && bprm.buf[1] == 'E'
  130. && bprm.buf[2] == 'L'
  131. && bprm.buf[3] == 'F') {
  132. retval = load_elf_binary(&bprm,regs,infop);
  133. } else {
  134. fprintf(stderr, "Unknown binary format\n");
  135. return -1;
  136. }
  137. }
  138. if(retval>=0) {
  139. /* success. Initialize important registers */
  140. do_init_thread(regs, infop);
  141. return retval;
  142. }
  143. /* Something went wrong, return the inode and free the argument pages*/
  144. for (i=0 ; i<MAX_ARG_PAGES ; i++) {
  145. g_free(bprm.page[i]);
  146. }
  147. return(retval);
  148. }