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