blob: 5b3c061a452a6025eb0fe54f34d24a02ec4c8938 [file] [log] [blame]
Warner Loshb211b362021-04-29 18:34:08 -06001/*
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 */
blueswir184778502008-10-26 20:33:16 +000017
Peter Maydell22311972016-01-29 17:49:53 +000018#include "qemu/osdep.h"
blueswir184778502008-10-26 20:33:16 +000019
20#include "qemu.h"
21
blueswir184778502008-10-26 20:33:16 +000022/* ??? This should really be somewhere else. */
23abi_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);
Warner Losh58b3beb2021-04-23 16:23:53 -060029 if (!host_ptr) {
blueswir184778502008-10-26 20:33:16 +000030 return -TARGET_EFAULT;
Warner Losh58b3beb2021-04-23 16:23:53 -060031 }
blueswir184778502008-10-26 20:33:16 +000032 memcpy(host_ptr, src, len);
33 unlock_user(host_ptr, dest, 1);
34 return 0;
35}
36
Warner Loshca0fd2e2021-04-23 09:05:14 -060037static int count(char **vec)
blueswir184778502008-10-26 20:33:16 +000038{
39 int i;
40
Warner Loshca0fd2e2021-04-23 09:05:14 -060041 for (i = 0; *vec; i++) {
blueswir184778502008-10-26 20:33:16 +000042 vec++;
43 }
44
Warner Loshfa054632021-04-23 16:22:19 -060045 return i;
blueswir184778502008-10-26 20:33:16 +000046}
47
Warner Loshafcbcff2021-04-29 10:04:28 -060048static int prepare_binprm(struct bsd_binprm *bprm)
blueswir184778502008-10-26 20:33:16 +000049{
50 struct stat st;
51 int mode;
Peter Maydell4a65a862017-07-18 17:26:33 +010052 int retval;
blueswir184778502008-10-26 20:33:16 +000053
Warner Loshca0fd2e2021-04-23 09:05:14 -060054 if (fstat(bprm->fd, &st) < 0) {
Warner Loshfa054632021-04-23 16:22:19 -060055 return -errno;
blueswir184778502008-10-26 20:33:16 +000056 }
57
58 mode = st.st_mode;
Warner Loshca0fd2e2021-04-23 09:05:14 -060059 if (!S_ISREG(mode)) { /* Must be regular file */
Warner Loshfa054632021-04-23 16:22:19 -060060 return -EACCES;
blueswir184778502008-10-26 20:33:16 +000061 }
Warner Loshca0fd2e2021-04-23 09:05:14 -060062 if (!(mode & 0111)) { /* Must have at least one execute bit set */
Warner Loshfa054632021-04-23 16:22:19 -060063 return -EACCES;
blueswir184778502008-10-26 20:33:16 +000064 }
65
66 bprm->e_uid = geteuid();
67 bprm->e_gid = getegid();
blueswir184778502008-10-26 20:33:16 +000068
69 /* Set-uid? */
Warner Loshca0fd2e2021-04-23 09:05:14 -060070 if (mode & S_ISUID) {
blueswir184778502008-10-26 20:33:16 +000071 bprm->e_uid = st.st_uid;
blueswir184778502008-10-26 20:33:16 +000072 }
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;
blueswir184778502008-10-26 20:33:16 +000082 }
83
84 memset(bprm->buf, 0, sizeof(bprm->buf));
85 retval = lseek(bprm->fd, 0L, SEEK_SET);
Warner Loshca0fd2e2021-04-23 09:05:14 -060086 if (retval >= 0) {
blueswir184778502008-10-26 20:33:16 +000087 retval = read(bprm->fd, bprm->buf, 128);
88 }
Warner Loshca0fd2e2021-04-23 09:05:14 -060089 if (retval < 0) {
blueswir184778502008-10-26 20:33:16 +000090 perror("prepare_binprm");
91 exit(-1);
Warner Losh58b3beb2021-04-23 16:23:53 -060092 } else {
Warner Loshfa054632021-04-23 16:22:19 -060093 return retval;
blueswir184778502008-10-26 20:33:16 +000094 }
95}
96
97/* Construct the envp and argv tables on the target stack. */
98abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
Warner Loshffa03662021-04-30 08:17:23 -060099 abi_ulong stringp)
blueswir184778502008-10-26 20:33:16 +0000100{
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;
blueswir184778502008-10-26 20:33:16 +0000109 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
Warner Losh1b50ff62021-04-29 19:34:34 -0600133static 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
Warner Loshca0fd2e2021-04-23 09:05:14 -0600147int loader_exec(const char *filename, char **argv, char **envp,
Warner Loshd37853f2021-04-29 18:45:13 -0600148 struct target_pt_regs *regs, struct image_info *infop,
149 struct bsd_binprm *bprm)
blueswir184778502008-10-26 20:33:16 +0000150{
Warner Losh1b50ff62021-04-29 19:34:34 -0600151 char *path, fullpath[PATH_MAX];
Warner Losh223005f2021-04-29 18:47:51 -0600152 int retval, i;
blueswir184778502008-10-26 20:33:16 +0000153
Warner Losh223005f2021-04-29 18:47:51 -0600154 bprm->p = TARGET_PAGE_SIZE * MAX_ARG_PAGES;
Warner Loshd37853f2021-04-29 18:45:13 -0600155 for (i = 0; i < MAX_ARG_PAGES; i++) { /* clear page-table */
156 bprm->page[i] = NULL;
Warner Losh58b3beb2021-04-23 16:23:53 -0600157 }
Warner Losh1b50ff62021-04-29 19:34:34 -0600158
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);
Warner Losh58b3beb2021-04-23 16:23:53 -0600176 if (retval < 0) {
Warner Losh1b50ff62021-04-29 19:34:34 -0600177 g_free(path);
blueswir184778502008-10-26 20:33:16 +0000178 return retval;
Warner Losh58b3beb2021-04-23 16:23:53 -0600179 }
blueswir184778502008-10-26 20:33:16 +0000180
Warner Losh1b50ff62021-04-29 19:34:34 -0600181 bprm->fullpath = path;
Warner Loshd37853f2021-04-29 18:45:13 -0600182 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);
blueswir184778502008-10-26 20:33:16 +0000190
Warner Loshca0fd2e2021-04-23 09:05:14 -0600191 if (retval >= 0) {
Warner Loshd37853f2021-04-29 18:45:13 -0600192 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);
blueswir184778502008-10-26 20:33:16 +0000197 } else {
Peter Maydell9bb93182014-06-02 13:24:37 +0100198 fprintf(stderr, "Unknown binary format\n");
blueswir184778502008-10-26 20:33:16 +0000199 return -1;
200 }
201 }
202
Warner Loshca0fd2e2021-04-23 09:05:14 -0600203 if (retval >= 0) {
blueswir184778502008-10-26 20:33:16 +0000204 /* 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*/
Warner Loshca0fd2e2021-04-23 09:05:14 -0600210 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
Warner Loshd37853f2021-04-29 18:45:13 -0600211 g_free(bprm->page[i]);
blueswir184778502008-10-26 20:33:16 +0000212 }
Warner Loshfa054632021-04-23 16:22:19 -0600213 return retval;
blueswir184778502008-10-26 20:33:16 +0000214}