Statistics
| Branch: | Revision:

root / linux-user / linuxload.c @ 1a14026e

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 e5fe0c52 pbrook
    memset(bprm->buf, 0, sizeof(bprm->buf));
100 e5fe0c52 pbrook
    retval = lseek(bprm->fd, 0L, SEEK_SET);
101 e5fe0c52 pbrook
    if(retval >= 0) {
102 e5fe0c52 pbrook
        retval = read(bprm->fd, bprm->buf, 128);
103 e5fe0c52 pbrook
    }
104 e5fe0c52 pbrook
    if(retval < 0) {
105 e5fe0c52 pbrook
        perror("prepare_binprm");
106 e5fe0c52 pbrook
        exit(-1);
107 e5fe0c52 pbrook
        /* return(-errno); */
108 e5fe0c52 pbrook
    }
109 e5fe0c52 pbrook
    else {
110 e5fe0c52 pbrook
        return(retval);
111 e5fe0c52 pbrook
    }
112 e5fe0c52 pbrook
}
113 e5fe0c52 pbrook
114 e5fe0c52 pbrook
/* Construct the envp and argv tables on the target stack.  */
115 992f48a0 blueswir1
abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
116 992f48a0 blueswir1
                              abi_ulong stringp, int push_ptr)
117 e5fe0c52 pbrook
{
118 992f48a0 blueswir1
    int n = sizeof(abi_ulong);
119 992f48a0 blueswir1
    abi_ulong envp;
120 992f48a0 blueswir1
    abi_ulong argv;
121 e5fe0c52 pbrook
122 e5fe0c52 pbrook
    sp -= (envc + 1) * n;
123 e5fe0c52 pbrook
    envp = sp;
124 e5fe0c52 pbrook
    sp -= (argc + 1) * n;
125 e5fe0c52 pbrook
    argv = sp;
126 e5fe0c52 pbrook
    if (push_ptr) {
127 2f619698 bellard
        /* FIXME - handle put_user() failures */
128 2f619698 bellard
        sp -= n;
129 2f619698 bellard
        put_user_ual(envp, sp);
130 2f619698 bellard
        sp -= n;
131 2f619698 bellard
        put_user_ual(argv, sp);
132 e5fe0c52 pbrook
    }
133 2f619698 bellard
    sp -= n;
134 2f619698 bellard
    /* FIXME - handle put_user() failures */
135 2f619698 bellard
    put_user_ual(argc, sp);
136 e5fe0c52 pbrook
137 e5fe0c52 pbrook
    while (argc-- > 0) {
138 2f619698 bellard
        /* FIXME - handle put_user() failures */
139 2f619698 bellard
        put_user_ual(stringp, argv);
140 2f619698 bellard
        argv += n;
141 e5fe0c52 pbrook
        stringp += target_strlen(stringp) + 1;
142 e5fe0c52 pbrook
    }
143 2f619698 bellard
    /* FIXME - handle put_user() failures */
144 2f619698 bellard
    put_user_ual(0, argv);
145 e5fe0c52 pbrook
    while (envc-- > 0) {
146 2f619698 bellard
        /* FIXME - handle put_user() failures */
147 2f619698 bellard
        put_user_ual(stringp, envp);
148 2f619698 bellard
        envp += n;
149 e5fe0c52 pbrook
        stringp += target_strlen(stringp) + 1;
150 e5fe0c52 pbrook
    }
151 2f619698 bellard
    /* FIXME - handle put_user() failures */
152 2f619698 bellard
    put_user_ual(0, envp);
153 e5fe0c52 pbrook
154 e5fe0c52 pbrook
    return sp;
155 e5fe0c52 pbrook
}
156 e5fe0c52 pbrook
157 5fafdf24 ths
int loader_exec(const char * filename, char ** argv, char ** envp,
158 e5fe0c52 pbrook
             struct target_pt_regs * regs, struct image_info *infop)
159 e5fe0c52 pbrook
{
160 e5fe0c52 pbrook
    struct linux_binprm bprm;
161 e5fe0c52 pbrook
    int retval;
162 e5fe0c52 pbrook
    int i;
163 e5fe0c52 pbrook
164 e5fe0c52 pbrook
    bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
165 e5fe0c52 pbrook
    for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
166 e5fe0c52 pbrook
            bprm.page[i] = 0;
167 e5fe0c52 pbrook
    retval = open(filename, O_RDONLY);
168 e5fe0c52 pbrook
    if (retval < 0)
169 e5fe0c52 pbrook
        return retval;
170 e5fe0c52 pbrook
    bprm.fd = retval;
171 e5fe0c52 pbrook
    bprm.filename = (char *)filename;
172 e5fe0c52 pbrook
    bprm.argc = count(argv);
173 e5fe0c52 pbrook
    bprm.argv = argv;
174 e5fe0c52 pbrook
    bprm.envc = count(envp);
175 e5fe0c52 pbrook
    bprm.envp = envp;
176 e5fe0c52 pbrook
177 e5fe0c52 pbrook
    retval = prepare_binprm(&bprm);
178 e5fe0c52 pbrook
179 38d0662a pbrook
    infop->host_argv = argv;
180 38d0662a pbrook
181 e5fe0c52 pbrook
    if(retval>=0) {
182 e5fe0c52 pbrook
        if (bprm.buf[0] == 0x7f
183 e5fe0c52 pbrook
                && bprm.buf[1] == 'E'
184 e5fe0c52 pbrook
                && bprm.buf[2] == 'L'
185 e5fe0c52 pbrook
                && bprm.buf[3] == 'F') {
186 cb33da57 blueswir1
#ifndef TARGET_HAS_ELFLOAD32
187 e5fe0c52 pbrook
            retval = load_elf_binary(&bprm,regs,infop);
188 cb33da57 blueswir1
#else
189 cb33da57 blueswir1
            retval = load_elf_binary_multi(&bprm, regs, infop);
190 cb33da57 blueswir1
#endif
191 e5fe0c52 pbrook
#if defined(TARGET_HAS_BFLT)
192 e5fe0c52 pbrook
        } else if (bprm.buf[0] == 'b'
193 e5fe0c52 pbrook
                && bprm.buf[1] == 'F'
194 e5fe0c52 pbrook
                && bprm.buf[2] == 'L'
195 e5fe0c52 pbrook
                && bprm.buf[3] == 'T') {
196 e5fe0c52 pbrook
            retval = load_flt_binary(&bprm,regs,infop);
197 e5fe0c52 pbrook
#endif
198 e5fe0c52 pbrook
        } else {
199 e5fe0c52 pbrook
            fprintf(stderr, "Unknown binary format\n");
200 e5fe0c52 pbrook
            return -1;
201 e5fe0c52 pbrook
        }
202 e5fe0c52 pbrook
    }
203 3b46e624 ths
204 e5fe0c52 pbrook
    if(retval>=0) {
205 e5fe0c52 pbrook
        /* success.  Initialize important registers */
206 e5fe0c52 pbrook
        do_init_thread(regs, infop);
207 e5fe0c52 pbrook
        return retval;
208 e5fe0c52 pbrook
    }
209 e5fe0c52 pbrook
210 e5fe0c52 pbrook
    /* Something went wrong, return the inode and free the argument pages*/
211 e5fe0c52 pbrook
    for (i=0 ; i<MAX_ARG_PAGES ; i++) {
212 e5fe0c52 pbrook
        free(bprm.page[i]);
213 e5fe0c52 pbrook
    }
214 e5fe0c52 pbrook
    return(retval);
215 e5fe0c52 pbrook
}