-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelexec.c
More file actions
129 lines (108 loc) · 3.21 KB
/
relexec.c
File metadata and controls
129 lines (108 loc) · 3.21 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <err.h>
ssize_t joinpath(char *dst, size_t dstsize, const char *rel) {
size_t rel_len = strlen(rel);
/* If rel is absolute, replace root */
if (rel[0] == '/') {
if (rel_len + 1 > dstsize) {
return -1;
}
strcpy(dst, rel);
return rel_len;
}
// dirname may modify the argument or return static memory, so use a copy
char pathbuf[PATH_MAX];
strcpy(pathbuf, dst);
char *rootdir = dirname(pathbuf);
// copy rootdir into and ensure a trailing slash
// also check that there is space to append rel to dst, return -1
// if insufficient, with errno set.
size_t end = strlen(rootdir);
if (dst[end - 1] != '/') {
if (end + rel_len + 2 > dstsize) {
errno = EOVERFLOW;
return -1;
}
strcpy(dst, rootdir);
dst[end++] = '/';
} else {
if (end + rel_len + 1 > dstsize) {
errno = EOVERFLOW;
return -1;
}
strcpy(dst, rootdir);
}
strcpy(dst + end, rel);
return end + rel_len;
}
static char linktarget[PATH_MAX];
/* Follow symlinks until we find a real file or another error occurs.
*
* Return a pointer to static char data on success, or NULL on error, in
* which case errno will be set.
*/
static const char *follow_link(const char *link) {
ssize_t res_len;
char linkbuf[PATH_MAX];
strcpy(linktarget, link);
for (;;) {
res_len = readlink(linktarget, linkbuf, PATH_MAX);
if (res_len != -1) {
linkbuf[res_len] = '\0';
if (linkbuf[0] == '/') {
strcpy(linktarget, linkbuf);
} else {
if (joinpath(linktarget, PATH_MAX, linkbuf) < 0) {
return NULL;
}
}
} else if (errno == EINVAL) {
// Was not a link, therefore we're done
return linktarget;
} else {
// Another error
return NULL;
}
}
}
int main(int argc, const char **argv) {
char **newargv;
char newinterp[PATH_MAX];
size_t bs;
if (argc < 3) {
strcpy(newinterp, argv[0]);
char *base = basename(newinterp);
fprintf(stderr, "usage: %s <interpreter> <script>\n", base);
return 2;
}
if (strlen(argv[2]) > PATH_MAX) {
err(2, "path too long\n");
}
/* Assemble the path to run */
const char *linkdest = follow_link(argv[2]);
if (!linkdest) {
err(2, "error reading %s", argv[2]);
}
strcpy(newinterp, linkdest);
if (joinpath(newinterp, PATH_MAX, argv[1]) < 0) {
err(2, "path too long\n");
}
/* Build argv, dropping the interpreter argument */
newargv = malloc(argc * sizeof(char *));
if (NULL == newargv) {
err(1, "Failed to allocate memory");
}
newargv[0] = newinterp;
memcpy(newargv + 1, argv + 2, sizeof(char *) * (argc - 2));
newargv[argc - 1] = NULL;
/* Exec the target */
execv(newinterp, newargv);
/* If exec failed, return an error */
err(127, "failed to execute %s", newinterp);
}