ths | 0458df2 | 2007-06-03 15:33:32 +0000 | [diff] [blame] | 1 | /* Code for loading Linux executables. Mostly linux kernel code. */ |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 2 | |
| 3 | #include <sys/types.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <errno.h> |
| 7 | #include <unistd.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | #include "qemu.h" |
| 12 | |
| 13 | #define NGROUPS 32 |
| 14 | |
| 15 | /* ??? This should really be somewhere else. */ |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 16 | abi_long memcpy_to_target(abi_ulong dest, const void *src, |
| 17 | unsigned long len) |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 18 | { |
| 19 | void *host_ptr; |
| 20 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 21 | host_ptr = lock_user(VERIFY_WRITE, dest, len, 0); |
| 22 | if (!host_ptr) |
| 23 | return -TARGET_EFAULT; |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 24 | memcpy(host_ptr, src, len); |
| 25 | unlock_user(host_ptr, dest, 1); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 26 | return 0; |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static int in_group_p(gid_t g) |
| 30 | { |
| 31 | /* return TRUE if we're in the specified group, FALSE otherwise */ |
| 32 | int ngroup; |
| 33 | int i; |
| 34 | gid_t grouplist[NGROUPS]; |
| 35 | |
| 36 | ngroup = getgroups(NGROUPS, grouplist); |
| 37 | for(i = 0; i < ngroup; i++) { |
| 38 | if(grouplist[i] == g) { |
| 39 | return 1; |
| 40 | } |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int count(char ** vec) |
| 46 | { |
| 47 | int i; |
| 48 | |
| 49 | for(i = 0; *vec; i++) { |
| 50 | vec++; |
| 51 | } |
| 52 | |
| 53 | return(i); |
| 54 | } |
| 55 | |
| 56 | static int prepare_binprm(struct linux_binprm *bprm) |
| 57 | { |
| 58 | struct stat st; |
| 59 | int mode; |
| 60 | int retval, id_change; |
| 61 | |
| 62 | if(fstat(bprm->fd, &st) < 0) { |
| 63 | return(-errno); |
| 64 | } |
| 65 | |
| 66 | mode = st.st_mode; |
| 67 | if(!S_ISREG(mode)) { /* Must be regular file */ |
| 68 | return(-EACCES); |
| 69 | } |
| 70 | if(!(mode & 0111)) { /* Must have at least one execute bit set */ |
| 71 | return(-EACCES); |
| 72 | } |
| 73 | |
| 74 | bprm->e_uid = geteuid(); |
| 75 | bprm->e_gid = getegid(); |
| 76 | id_change = 0; |
| 77 | |
| 78 | /* Set-uid? */ |
| 79 | if(mode & S_ISUID) { |
| 80 | bprm->e_uid = st.st_uid; |
| 81 | if(bprm->e_uid != geteuid()) { |
| 82 | id_change = 1; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* Set-gid? */ |
| 87 | /* |
| 88 | * If setgid is set but no group execute bit then this |
| 89 | * is a candidate for mandatory locking, not a setgid |
| 90 | * executable. |
| 91 | */ |
| 92 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
| 93 | bprm->e_gid = st.st_gid; |
| 94 | if (!in_group_p(bprm->e_gid)) { |
| 95 | id_change = 1; |
| 96 | } |
| 97 | } |
| 98 | |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 99 | retval = lseek(bprm->fd, 0L, SEEK_SET); |
| 100 | if(retval >= 0) { |
| 101 | retval = read(bprm->fd, bprm->buf, 128); |
| 102 | } |
| 103 | if(retval < 0) { |
| 104 | perror("prepare_binprm"); |
| 105 | exit(-1); |
| 106 | /* return(-errno); */ |
| 107 | } |
| 108 | else { |
| 109 | return(retval); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Construct the envp and argv tables on the target stack. */ |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 114 | abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp, |
| 115 | abi_ulong stringp, int push_ptr) |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 116 | { |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 117 | TaskState *ts = (TaskState *)thread_env->opaque; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 118 | int n = sizeof(abi_ulong); |
| 119 | abi_ulong envp; |
| 120 | abi_ulong argv; |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 121 | |
| 122 | sp -= (envc + 1) * n; |
| 123 | envp = sp; |
| 124 | sp -= (argc + 1) * n; |
| 125 | argv = sp; |
| 126 | if (push_ptr) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 127 | /* FIXME - handle put_user() failures */ |
| 128 | sp -= n; |
| 129 | put_user_ual(envp, sp); |
| 130 | sp -= n; |
| 131 | put_user_ual(argv, sp); |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 132 | } |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 133 | sp -= n; |
| 134 | /* FIXME - handle put_user() failures */ |
| 135 | put_user_ual(argc, sp); |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 136 | ts->info->arg_start = stringp; |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 137 | while (argc-- > 0) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 138 | /* FIXME - handle put_user() failures */ |
| 139 | put_user_ual(stringp, argv); |
| 140 | argv += n; |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 141 | stringp += target_strlen(stringp) + 1; |
| 142 | } |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 143 | ts->info->arg_end = stringp; |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 144 | /* FIXME - handle put_user() failures */ |
| 145 | put_user_ual(0, argv); |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 146 | while (envc-- > 0) { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 147 | /* FIXME - handle put_user() failures */ |
| 148 | put_user_ual(stringp, envp); |
| 149 | envp += n; |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 150 | stringp += target_strlen(stringp) + 1; |
| 151 | } |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 152 | /* FIXME - handle put_user() failures */ |
| 153 | put_user_ual(0, envp); |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 154 | |
| 155 | return sp; |
| 156 | } |
| 157 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 158 | int loader_exec(const char * filename, char ** argv, char ** envp, |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 159 | struct target_pt_regs * regs, struct image_info *infop, |
| 160 | struct linux_binprm *bprm) |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 161 | { |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 162 | int retval; |
| 163 | int i; |
| 164 | |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 165 | bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int); |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 166 | for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */ |
Blue Swirl | 7cba04f | 2009-08-01 10:13:20 +0000 | [diff] [blame] | 167 | bprm->page[i] = NULL; |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 168 | retval = open(filename, O_RDONLY); |
| 169 | if (retval < 0) |
| 170 | return retval; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 171 | bprm->fd = retval; |
| 172 | bprm->filename = (char *)filename; |
| 173 | bprm->argc = count(argv); |
| 174 | bprm->argv = argv; |
| 175 | bprm->envc = count(envp); |
| 176 | bprm->envp = envp; |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 177 | |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 178 | retval = prepare_binprm(bprm); |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 179 | |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 180 | infop->host_argv = argv; |
| 181 | |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 182 | if(retval>=0) { |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 183 | if (bprm->buf[0] == 0x7f |
| 184 | && bprm->buf[1] == 'E' |
| 185 | && bprm->buf[2] == 'L' |
| 186 | && bprm->buf[3] == 'F') { |
Richard Henderson | 6495a04 | 2010-04-22 17:24:58 -0700 | [diff] [blame] | 187 | retval = load_elf_binary(bprm, regs, infop); |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 188 | #if defined(TARGET_HAS_BFLT) |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 189 | } else if (bprm->buf[0] == 'b' |
| 190 | && bprm->buf[1] == 'F' |
| 191 | && bprm->buf[2] == 'L' |
| 192 | && bprm->buf[3] == 'T') { |
| 193 | retval = load_flt_binary(bprm,regs,infop); |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 194 | #endif |
| 195 | } else { |
| 196 | fprintf(stderr, "Unknown binary format\n"); |
| 197 | return -1; |
| 198 | } |
| 199 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 200 | |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 201 | if(retval>=0) { |
| 202 | /* success. Initialize important registers */ |
| 203 | do_init_thread(regs, infop); |
| 204 | return retval; |
| 205 | } |
| 206 | |
| 207 | /* Something went wrong, return the inode and free the argument pages*/ |
| 208 | for (i=0 ; i<MAX_ARG_PAGES ; i++) { |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 209 | free(bprm->page[i]); |
pbrook | e5fe0c5 | 2006-06-11 13:32:59 +0000 | [diff] [blame] | 210 | } |
| 211 | return(retval); |
| 212 | } |