-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsmsb-run.c
139 lines (113 loc) · 2.58 KB
/
lsmsb-run.c
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
131
132
133
134
135
136
137
138
139
/*
* Tool for running programs under a given sandbox (CONFIG_SECURITY_LSMSB)
*
* Copyright (c) 2010 dborca
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define CHECK_SB '+'
#ifdef CHECK_SB
#define ATTR_MODE O_RDWR
#else
#define ATTR_MODE O_WRONLY
#endif
int
main(int argc, char **argv)
{
int rv;
int fd;
struct stat st;
uint8_t *buffer, ch;
const char *myself = argv[0];
if (argc < 2) {
printf("%s: missing operand\n", myself);
goto err1;
}
if (!strcmp(argv[1], "--help")) {
printf("usage: %s sandbox [COMMAND [ARG]...]\n", myself);
return 0;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror(argv[1]);
goto err1;
}
rv = fstat(fd, &st);
if (rv < 0) {
perror(argv[1]);
goto err2;
}
buffer = malloc(st.st_size);
if (!buffer) {
perror("buffer");
goto err2;
}
if (read(fd, buffer, st.st_size) != st.st_size) {
perror("read");
goto err3;
}
rv = open("/proc/self/attr/current", ATTR_MODE);
if (rv < 0) {
perror("/proc/self/attr/current");
goto err3;
}
close(fd);
fd = rv;
#ifdef CHECK_SB
if (read(fd, &ch, 1) != 1) {
perror("lsmsb");
goto err3;
}
if (ch != CHECK_SB) {
fprintf(stderr, "lsmsb: not available\n");
goto err3;
}
rv = lseek(fd, 0, SEEK_SET);
if (rv) {
perror("lsmsb");
goto err3;
}
#endif
if (write(fd, buffer, st.st_size) != st.st_size) {
perror("write");
goto err3;
}
free(buffer);
close(fd);
if (seteuid(getuid()) || setegid(getgid())) {
goto err1;
}
fprintf(stderr, "Sandbox installed\n");
if (argc <= 2) {
rv = execl("/bin/sh", "/bin/sh", NULL);
} else {
argv += 2;
rv = execvp(argv[0], argv);
}
return rv;
err3:
free(buffer);
err2:
close(fd);
err1:
return 127;
}