root / linux-user / linuxload.c @ a7037b29
History | View | Annotate | Download (4.4 kB)
1 | e5fe0c52 | pbrook | /* Code for loading Linux executables. Mostly linux kenrel 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 | e5fe0c52 | pbrook | void memcpy_to_target(target_ulong dest, const void *src, |
17 | e5fe0c52 | pbrook | unsigned long len) |
18 | e5fe0c52 | pbrook | { |
19 | e5fe0c52 | pbrook | void *host_ptr;
|
20 | e5fe0c52 | pbrook | |
21 | e5fe0c52 | pbrook | host_ptr = lock_user(dest, len, 0);
|
22 | e5fe0c52 | pbrook | memcpy(host_ptr, src, len); |
23 | e5fe0c52 | pbrook | unlock_user(host_ptr, dest, 1);
|
24 | e5fe0c52 | pbrook | } |
25 | e5fe0c52 | pbrook | |
26 | e5fe0c52 | pbrook | static int in_group_p(gid_t g) |
27 | e5fe0c52 | pbrook | { |
28 | e5fe0c52 | pbrook | /* return TRUE if we're in the specified group, FALSE otherwise */
|
29 | e5fe0c52 | pbrook | int ngroup;
|
30 | e5fe0c52 | pbrook | int i;
|
31 | e5fe0c52 | pbrook | gid_t grouplist[NGROUPS]; |
32 | e5fe0c52 | pbrook | |
33 | e5fe0c52 | pbrook | ngroup = getgroups(NGROUPS, grouplist); |
34 | e5fe0c52 | pbrook | for(i = 0; i < ngroup; i++) { |
35 | e5fe0c52 | pbrook | if(grouplist[i] == g) {
|
36 | e5fe0c52 | pbrook | return 1; |
37 | e5fe0c52 | pbrook | } |
38 | e5fe0c52 | pbrook | } |
39 | e5fe0c52 | pbrook | return 0; |
40 | e5fe0c52 | pbrook | } |
41 | e5fe0c52 | pbrook | |
42 | e5fe0c52 | pbrook | static int count(char ** vec) |
43 | e5fe0c52 | pbrook | { |
44 | e5fe0c52 | pbrook | int i;
|
45 | e5fe0c52 | pbrook | |
46 | e5fe0c52 | pbrook | for(i = 0; *vec; i++) { |
47 | e5fe0c52 | pbrook | vec++; |
48 | e5fe0c52 | pbrook | } |
49 | e5fe0c52 | pbrook | |
50 | e5fe0c52 | pbrook | return(i);
|
51 | e5fe0c52 | pbrook | } |
52 | e5fe0c52 | pbrook | |
53 | e5fe0c52 | pbrook | static int prepare_binprm(struct linux_binprm *bprm) |
54 | e5fe0c52 | pbrook | { |
55 | e5fe0c52 | pbrook | struct stat st;
|
56 | e5fe0c52 | pbrook | int mode;
|
57 | e5fe0c52 | pbrook | int retval, id_change;
|
58 | e5fe0c52 | pbrook | |
59 | e5fe0c52 | pbrook | if(fstat(bprm->fd, &st) < 0) { |
60 | e5fe0c52 | pbrook | return(-errno);
|
61 | e5fe0c52 | pbrook | } |
62 | e5fe0c52 | pbrook | |
63 | e5fe0c52 | pbrook | mode = st.st_mode; |
64 | e5fe0c52 | pbrook | if(!S_ISREG(mode)) { /* Must be regular file */ |
65 | e5fe0c52 | pbrook | return(-EACCES);
|
66 | e5fe0c52 | pbrook | } |
67 | e5fe0c52 | pbrook | if(!(mode & 0111)) { /* Must have at least one execute bit set */ |
68 | e5fe0c52 | pbrook | return(-EACCES);
|
69 | e5fe0c52 | pbrook | } |
70 | e5fe0c52 | pbrook | |
71 | e5fe0c52 | pbrook | bprm->e_uid = geteuid(); |
72 | e5fe0c52 | pbrook | bprm->e_gid = getegid(); |
73 | e5fe0c52 | pbrook | id_change = 0;
|
74 | e5fe0c52 | pbrook | |
75 | e5fe0c52 | pbrook | /* Set-uid? */
|
76 | e5fe0c52 | pbrook | if(mode & S_ISUID) {
|
77 | e5fe0c52 | pbrook | bprm->e_uid = st.st_uid; |
78 | e5fe0c52 | pbrook | if(bprm->e_uid != geteuid()) {
|
79 | e5fe0c52 | pbrook | id_change = 1;
|
80 | e5fe0c52 | pbrook | } |
81 | e5fe0c52 | pbrook | } |
82 | e5fe0c52 | pbrook | |
83 | e5fe0c52 | pbrook | /* Set-gid? */
|
84 | e5fe0c52 | pbrook | /*
|
85 | e5fe0c52 | pbrook | * If setgid is set but no group execute bit then this
|
86 | e5fe0c52 | pbrook | * is a candidate for mandatory locking, not a setgid
|
87 | e5fe0c52 | pbrook | * executable.
|
88 | e5fe0c52 | pbrook | */
|
89 | e5fe0c52 | pbrook | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
|
90 | e5fe0c52 | pbrook | bprm->e_gid = st.st_gid; |
91 | e5fe0c52 | pbrook | if (!in_group_p(bprm->e_gid)) {
|
92 | e5fe0c52 | pbrook | id_change = 1;
|
93 | e5fe0c52 | pbrook | } |
94 | e5fe0c52 | pbrook | } |
95 | e5fe0c52 | pbrook | |
96 | e5fe0c52 | pbrook | memset(bprm->buf, 0, sizeof(bprm->buf)); |
97 | e5fe0c52 | pbrook | retval = lseek(bprm->fd, 0L, SEEK_SET);
|
98 | e5fe0c52 | pbrook | if(retval >= 0) { |
99 | e5fe0c52 | pbrook | retval = read(bprm->fd, bprm->buf, 128);
|
100 | e5fe0c52 | pbrook | } |
101 | e5fe0c52 | pbrook | if(retval < 0) { |
102 | e5fe0c52 | pbrook | perror("prepare_binprm");
|
103 | e5fe0c52 | pbrook | exit(-1);
|
104 | e5fe0c52 | pbrook | /* return(-errno); */
|
105 | e5fe0c52 | pbrook | } |
106 | e5fe0c52 | pbrook | else {
|
107 | e5fe0c52 | pbrook | return(retval);
|
108 | e5fe0c52 | pbrook | } |
109 | e5fe0c52 | pbrook | } |
110 | e5fe0c52 | pbrook | |
111 | e5fe0c52 | pbrook | /* Construct the envp and argv tables on the target stack. */
|
112 | e5fe0c52 | pbrook | target_ulong loader_build_argptr(int envc, int argc, target_ulong sp, |
113 | e5fe0c52 | pbrook | target_ulong stringp, int push_ptr)
|
114 | e5fe0c52 | pbrook | { |
115 | e5fe0c52 | pbrook | int n = sizeof(target_ulong); |
116 | e5fe0c52 | pbrook | target_ulong envp; |
117 | e5fe0c52 | pbrook | target_ulong argv; |
118 | e5fe0c52 | pbrook | |
119 | e5fe0c52 | pbrook | sp -= (envc + 1) * n;
|
120 | e5fe0c52 | pbrook | envp = sp; |
121 | e5fe0c52 | pbrook | sp -= (argc + 1) * n;
|
122 | e5fe0c52 | pbrook | argv = sp; |
123 | e5fe0c52 | pbrook | if (push_ptr) {
|
124 | e5fe0c52 | pbrook | sp -= n; tputl(sp, envp); |
125 | e5fe0c52 | pbrook | sp -= n; tputl(sp, argv); |
126 | e5fe0c52 | pbrook | } |
127 | e5fe0c52 | pbrook | sp -= n; tputl(sp, argc); |
128 | e5fe0c52 | pbrook | |
129 | e5fe0c52 | pbrook | while (argc-- > 0) { |
130 | e5fe0c52 | pbrook | tputl(argv, stringp); argv += n; |
131 | e5fe0c52 | pbrook | stringp += target_strlen(stringp) + 1;
|
132 | e5fe0c52 | pbrook | } |
133 | e5fe0c52 | pbrook | tputl(argv, 0);
|
134 | e5fe0c52 | pbrook | while (envc-- > 0) { |
135 | e5fe0c52 | pbrook | tputl(envp, stringp); envp += n; |
136 | e5fe0c52 | pbrook | stringp += target_strlen(stringp) + 1;
|
137 | e5fe0c52 | pbrook | } |
138 | e5fe0c52 | pbrook | tputl(envp, 0);
|
139 | e5fe0c52 | pbrook | |
140 | e5fe0c52 | pbrook | return sp;
|
141 | e5fe0c52 | pbrook | } |
142 | e5fe0c52 | pbrook | |
143 | e5fe0c52 | pbrook | int loader_exec(const char * filename, char ** argv, char ** envp, |
144 | e5fe0c52 | pbrook | struct target_pt_regs * regs, struct image_info *infop) |
145 | e5fe0c52 | pbrook | { |
146 | e5fe0c52 | pbrook | struct linux_binprm bprm;
|
147 | e5fe0c52 | pbrook | int retval;
|
148 | e5fe0c52 | pbrook | int i;
|
149 | e5fe0c52 | pbrook | |
150 | e5fe0c52 | pbrook | bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int); |
151 | e5fe0c52 | pbrook | for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */ |
152 | e5fe0c52 | pbrook | bprm.page[i] = 0;
|
153 | e5fe0c52 | pbrook | retval = open(filename, O_RDONLY); |
154 | e5fe0c52 | pbrook | if (retval < 0) |
155 | e5fe0c52 | pbrook | return retval;
|
156 | e5fe0c52 | pbrook | bprm.fd = retval; |
157 | e5fe0c52 | pbrook | bprm.filename = (char *)filename;
|
158 | e5fe0c52 | pbrook | bprm.argc = count(argv); |
159 | e5fe0c52 | pbrook | bprm.argv = argv; |
160 | e5fe0c52 | pbrook | bprm.envc = count(envp); |
161 | e5fe0c52 | pbrook | bprm.envp = envp; |
162 | e5fe0c52 | pbrook | |
163 | e5fe0c52 | pbrook | retval = prepare_binprm(&bprm); |
164 | e5fe0c52 | pbrook | |
165 | 38d0662a | pbrook | infop->host_argv = argv; |
166 | 38d0662a | pbrook | |
167 | e5fe0c52 | pbrook | if(retval>=0) { |
168 | e5fe0c52 | pbrook | if (bprm.buf[0] == 0x7f |
169 | e5fe0c52 | pbrook | && bprm.buf[1] == 'E' |
170 | e5fe0c52 | pbrook | && bprm.buf[2] == 'L' |
171 | e5fe0c52 | pbrook | && bprm.buf[3] == 'F') { |
172 | e5fe0c52 | pbrook | retval = load_elf_binary(&bprm,regs,infop); |
173 | e5fe0c52 | pbrook | #if defined(TARGET_HAS_BFLT)
|
174 | e5fe0c52 | pbrook | } else if (bprm.buf[0] == 'b' |
175 | e5fe0c52 | pbrook | && bprm.buf[1] == 'F' |
176 | e5fe0c52 | pbrook | && bprm.buf[2] == 'L' |
177 | e5fe0c52 | pbrook | && bprm.buf[3] == 'T') { |
178 | e5fe0c52 | pbrook | retval = load_flt_binary(&bprm,regs,infop); |
179 | e5fe0c52 | pbrook | #endif
|
180 | e5fe0c52 | pbrook | } else {
|
181 | e5fe0c52 | pbrook | fprintf(stderr, "Unknown binary format\n");
|
182 | e5fe0c52 | pbrook | return -1; |
183 | e5fe0c52 | pbrook | } |
184 | e5fe0c52 | pbrook | } |
185 | e5fe0c52 | pbrook | |
186 | e5fe0c52 | pbrook | if(retval>=0) { |
187 | e5fe0c52 | pbrook | /* success. Initialize important registers */
|
188 | e5fe0c52 | pbrook | do_init_thread(regs, infop); |
189 | e5fe0c52 | pbrook | return retval;
|
190 | e5fe0c52 | pbrook | } |
191 | e5fe0c52 | pbrook | |
192 | e5fe0c52 | pbrook | /* Something went wrong, return the inode and free the argument pages*/
|
193 | e5fe0c52 | pbrook | for (i=0 ; i<MAX_ARG_PAGES ; i++) { |
194 | e5fe0c52 | pbrook | free(bprm.page[i]); |
195 | e5fe0c52 | pbrook | } |
196 | e5fe0c52 | pbrook | return(retval);
|
197 | e5fe0c52 | pbrook | } |