1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/* $OpenBSD: exec.new.c,v 1.5 2000/05/30 21:59:30 mickey Exp $ */
/*
* Copyright (c) 1998 Michael Shalayeff
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Michael Shalayeff.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/reboot.h>
#ifndef INSECURE
#include <sys/stat.h>
#endif
#include "libsa.h"
#include <lib/libsa/exec.h>
extern int debug;
void
exec(path, loadaddr, howto)
char *path;
void *loadaddr;
int howto;
{
int fd;
struct stat sb;
const struct x_sw *sw;
struct x_param param;
union x_header hdr;
register u_char *pa;
register int save_err;
if ((fd = open(path, 0)) < 0 || fstat(fd, &sb))
return;
if (sb.st_mode & 2)
printf("non-secure file, check permissions!\n");
if (read(fd, (char *)&hdr, sizeof(hdr)) != sizeof(hdr))
return;
errno = 0; /* XXX */
/* probe obj format */
for (sw = execsw; sw->probe; sw++)
if (sw->probe(fd, &hdr))
break;
bzero (¶m, sizeof(param));
param.xp_execsw = sw;
param.xp_hdr = &hdr;
if (!sw->probe || sw->load(fd, ¶m)) {
errno = errno? errno : EFTYPE;
goto err;
}
#ifdef EXEC_DEBUG
if (debug)
printf("ep=%x, end=%x\n.text=%x,%u,%u\n.data=%x,%u,%u\n"
".bss=%x,%u,%u\n.sym=%x,%u,%u\n.str=%x,%u,%u\n",
param.xp_entry, param.xp_end,
param.text.addr, param.text.size, param.text.foff,
param.data.addr, param.data.size, param.data.foff,
param.bss.addr, param.bss.size, param.bss.foff,
param.sym.addr, param.sym.size, param.sym.foff,
param.str.addr, param.str.size, param.str.foff);
#endif
param.xp_end = (u_int)pa = loadaddr;
printf("%u", param.text.size);
/* .text */
if (lseek(fd, param.text.foff, SEEK_SET) < 0 ||
read(fd, pa+param.text.addr, param.text.size) != param.text.size)
goto err;
/* .data */
if (param.data.size) {
printf("+%u", param.data.size);
if (lseek(fd, param.data.foff, SEEK_SET) <= 0 ||
read(fd,pa+param.data.addr,param.data.size) != param.data.size)
goto err;
}
/* .bss */
printf("+%u", param.bss.size);
bzero (pa + param.bss.addr, param.bss.size);
param.xp_end = (int)pa + param.bss.addr + param.bss.size;
if (sw->ldsym && sw->ldsym(fd, ¶m)) {
errno = errno? errno : EFTYPE;
goto err;
}
/* ldsym will adjust the xp_end */
printf("=0x%x start=0x%x\n", param.xp_end, param.xp_entry);
/* call the joker */
machdep_exec(¶m, howto, loadaddr);
/* exec failed */
errno = ENOEXEC;
return;
err:
save_err = errno? errno: EIO;
close(fd);
errno = save_err;
}
|