]>
Commit | Line | Data |
---|---|---|
b211b368 WL |
1 | /* |
2 | * Load BSD executables. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
84778508 | 17 | |
2231197c | 18 | #include "qemu/osdep.h" |
84778508 BS |
19 | |
20 | #include "qemu.h" | |
21 | ||
84778508 BS |
22 | /* ??? This should really be somewhere else. */ |
23 | abi_long memcpy_to_target(abi_ulong dest, const void *src, | |
24 | unsigned long len) | |
25 | { | |
26 | void *host_ptr; | |
27 | ||
28 | host_ptr = lock_user(VERIFY_WRITE, dest, len, 0); | |
58b3beb4 | 29 | if (!host_ptr) { |
84778508 | 30 | return -TARGET_EFAULT; |
58b3beb4 | 31 | } |
84778508 BS |
32 | memcpy(host_ptr, src, len); |
33 | unlock_user(host_ptr, dest, 1); | |
34 | return 0; | |
35 | } | |
36 | ||
ca0fd2e3 | 37 | static int count(char **vec) |
84778508 BS |
38 | { |
39 | int i; | |
40 | ||
ca0fd2e3 | 41 | for (i = 0; *vec; i++) { |
84778508 BS |
42 | vec++; |
43 | } | |
44 | ||
fa054637 | 45 | return i; |
84778508 BS |
46 | } |
47 | ||
afcbcff8 | 48 | static int prepare_binprm(struct bsd_binprm *bprm) |
84778508 BS |
49 | { |
50 | struct stat st; | |
51 | int mode; | |
4a65a86a | 52 | int retval; |
84778508 | 53 | |
ca0fd2e3 | 54 | if (fstat(bprm->fd, &st) < 0) { |
fa054637 | 55 | return -errno; |
84778508 BS |
56 | } |
57 | ||
58 | mode = st.st_mode; | |
ca0fd2e3 | 59 | if (!S_ISREG(mode)) { /* Must be regular file */ |
fa054637 | 60 | return -EACCES; |
84778508 | 61 | } |
ca0fd2e3 | 62 | if (!(mode & 0111)) { /* Must have at least one execute bit set */ |
fa054637 | 63 | return -EACCES; |
84778508 BS |
64 | } |
65 | ||
66 | bprm->e_uid = geteuid(); | |
67 | bprm->e_gid = getegid(); | |
84778508 BS |
68 | |
69 | /* Set-uid? */ | |
ca0fd2e3 | 70 | if (mode & S_ISUID) { |
84778508 | 71 | bprm->e_uid = st.st_uid; |
84778508 BS |
72 | } |
73 | ||
74 | /* Set-gid? */ | |
75 | /* | |
76 | * If setgid is set but no group execute bit then this | |
77 | * is a candidate for mandatory locking, not a setgid | |
78 | * executable. | |
79 | */ | |
80 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { | |
81 | bprm->e_gid = st.st_gid; | |
84778508 BS |
82 | } |
83 | ||
84 | memset(bprm->buf, 0, sizeof(bprm->buf)); | |
85 | retval = lseek(bprm->fd, 0L, SEEK_SET); | |
ca0fd2e3 | 86 | if (retval >= 0) { |
84778508 BS |
87 | retval = read(bprm->fd, bprm->buf, 128); |
88 | } | |
ca0fd2e3 | 89 | if (retval < 0) { |
84778508 BS |
90 | perror("prepare_binprm"); |
91 | exit(-1); | |
58b3beb4 | 92 | } else { |
fa054637 | 93 | return retval; |
84778508 BS |
94 | } |
95 | } | |
96 | ||
97 | /* Construct the envp and argv tables on the target stack. */ | |
98 | abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp, | |
ffa03665 | 99 | abi_ulong stringp) |
84778508 BS |
100 | { |
101 | int n = sizeof(abi_ulong); | |
102 | abi_ulong envp; | |
103 | abi_ulong argv; | |
104 | ||
105 | sp -= (envc + 1) * n; | |
106 | envp = sp; | |
107 | sp -= (argc + 1) * n; | |
108 | argv = sp; | |
84778508 BS |
109 | sp -= n; |
110 | /* FIXME - handle put_user() failures */ | |
111 | put_user_ual(argc, sp); | |
112 | ||
113 | while (argc-- > 0) { | |
114 | /* FIXME - handle put_user() failures */ | |
115 | put_user_ual(stringp, argv); | |
116 | argv += n; | |
117 | stringp += target_strlen(stringp) + 1; | |
118 | } | |
119 | /* FIXME - handle put_user() failures */ | |
120 | put_user_ual(0, argv); | |
121 | while (envc-- > 0) { | |
122 | /* FIXME - handle put_user() failures */ | |
123 | put_user_ual(stringp, envp); | |
124 | envp += n; | |
125 | stringp += target_strlen(stringp) + 1; | |
126 | } | |
127 | /* FIXME - handle put_user() failures */ | |
128 | put_user_ual(0, envp); | |
129 | ||
130 | return sp; | |
131 | } | |
132 | ||
1b50ff64 WL |
133 | static bool is_there(const char *candidate) |
134 | { | |
135 | struct stat fin; | |
136 | ||
137 | /* XXX work around access(2) false positives for superuser */ | |
138 | if (access(candidate, X_OK) == 0 && stat(candidate, &fin) == 0 && | |
139 | S_ISREG(fin.st_mode) && (getuid() != 0 || | |
140 | (fin.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)) { | |
141 | return true; | |
142 | } | |
143 | ||
144 | return false; | |
145 | } | |
146 | ||
ca0fd2e3 | 147 | int loader_exec(const char *filename, char **argv, char **envp, |
d37853f9 WL |
148 | struct target_pt_regs *regs, struct image_info *infop, |
149 | struct bsd_binprm *bprm) | |
84778508 | 150 | { |
1b50ff64 | 151 | char *path, fullpath[PATH_MAX]; |
223005f0 | 152 | int retval, i; |
84778508 | 153 | |
223005f0 | 154 | bprm->p = TARGET_PAGE_SIZE * MAX_ARG_PAGES; |
d37853f9 WL |
155 | for (i = 0; i < MAX_ARG_PAGES; i++) { /* clear page-table */ |
156 | bprm->page[i] = NULL; | |
58b3beb4 | 157 | } |
1b50ff64 WL |
158 | |
159 | if (strchr(filename, '/') != NULL) { | |
160 | path = realpath(filename, fullpath); | |
161 | if (path == NULL) { | |
162 | /* Failed to resolve. */ | |
163 | return -1; | |
164 | } | |
165 | if (!is_there(path)) { | |
166 | return -1; | |
167 | } | |
168 | } else { | |
169 | path = g_find_program_in_path(filename); | |
170 | if (path == NULL) { | |
171 | return -1; | |
172 | } | |
173 | } | |
174 | ||
175 | retval = open(path, O_RDONLY); | |
58b3beb4 | 176 | if (retval < 0) { |
1b50ff64 | 177 | g_free(path); |
84778508 | 178 | return retval; |
58b3beb4 | 179 | } |
84778508 | 180 | |
1b50ff64 | 181 | bprm->fullpath = path; |
d37853f9 WL |
182 | bprm->fd = retval; |
183 | bprm->filename = (char *)filename; | |
184 | bprm->argc = count(argv); | |
185 | bprm->argv = argv; | |
186 | bprm->envc = count(envp); | |
187 | bprm->envp = envp; | |
188 | ||
189 | retval = prepare_binprm(bprm); | |
84778508 | 190 | |
ca0fd2e3 | 191 | if (retval >= 0) { |
d37853f9 WL |
192 | if (bprm->buf[0] == 0x7f |
193 | && bprm->buf[1] == 'E' | |
194 | && bprm->buf[2] == 'L' | |
195 | && bprm->buf[3] == 'F') { | |
196 | retval = load_elf_binary(bprm, regs, infop); | |
84778508 | 197 | } else { |
9bb93180 | 198 | fprintf(stderr, "Unknown binary format\n"); |
84778508 BS |
199 | return -1; |
200 | } | |
201 | } | |
202 | ||
ca0fd2e3 | 203 | if (retval >= 0) { |
84778508 BS |
204 | /* success. Initialize important registers */ |
205 | do_init_thread(regs, infop); | |
206 | return retval; | |
207 | } | |
208 | ||
209 | /* Something went wrong, return the inode and free the argument pages*/ | |
ca0fd2e3 | 210 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { |
d37853f9 | 211 | g_free(bprm->page[i]); |
84778508 | 212 | } |
fa054637 | 213 | return retval; |
84778508 | 214 | } |