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