-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.cc
276 lines (244 loc) · 8.42 KB
/
ipc.cc
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "debug.h"
#include "sandbox_impl.h"
namespace playground {
#ifndef IPC_PRIVATE
#define IPC_PRIVATE 0
#endif
#ifndef IPC_RMID
#define IPC_RMID 0
#endif
#ifndef IPC_64
#define IPC_64 256
#endif
#if defined(__NR_shmget)
void* Sandbox::sandbox_shmat(int shmid, const void* shmaddr, int shmflg) {
long long tm;
Debug::syscall(&tm, __NR_shmat, "Executing handler");
struct {
struct RequestHeader header;
ShmAt shmat_req;
} __attribute__((packed)) request;
request.shmat_req.shmid = shmid;
request.shmat_req.shmaddr = shmaddr;
request.shmat_req.shmflg = shmflg;
long rc = forwardSyscall(__NR_shmat, &request.header, sizeof(request));
Debug::elapsed(tm, __NR_shmat);
return reinterpret_cast<void *>(rc);
}
long Sandbox::sandbox_shmctl(int shmid, int cmd, void* buf) {
long long tm;
Debug::syscall(&tm, __NR_shmctl, "Executing handler");
struct {
struct RequestHeader header;
ShmCtl shmctl_req;
} __attribute__((packed)) request;
request.shmctl_req.shmid = shmid;
request.shmctl_req.cmd = cmd;
request.shmctl_req.buf = buf;
long rc = forwardSyscall(__NR_shmctl, &request.header, sizeof(request));
Debug::elapsed(tm, __NR_shmctl);
return rc;
}
long Sandbox::sandbox_shmdt(const void* shmaddr) {
long long tm;
Debug::syscall(&tm, __NR_shmdt, "Executing handler");
struct {
struct RequestHeader header;
ShmDt shmdt_req;
} __attribute__((packed)) request;
request.shmdt_req.shmaddr = shmaddr;
long rc = forwardSyscall(__NR_shmdt, &request.header, sizeof(request));
Debug::elapsed(tm, __NR_shmdt);
return rc;
}
long Sandbox::sandbox_shmget(int key, size_t size, int shmflg) {
long long tm;
Debug::syscall(&tm, __NR_shmget, "Executing handler");
struct {
struct RequestHeader header;
ShmGet shmget_req;
} __attribute__((packed)) request;
request.shmget_req.key = key;
request.shmget_req.size = size;
request.shmget_req.shmflg = shmflg;
long rc = forwardSyscall(__NR_shmget, &request.header, sizeof(request));
Debug::elapsed(tm, __NR_shmget);
return rc;
}
bool Sandbox::process_shmat(const SecureMem::SyscallRequestInfo* info) {
// Read request
ShmAt shmat_req;
SysCalls sys;
if (read(sys, info->trustedProcessFd, &shmat_req, sizeof(shmat_req)) !=
sizeof(shmat_req)) {
die("Failed to read parameters for shmat() [process]");
}
// We only allow attaching to the shm identifier that was returned by
// the most recent call to shmget(IPC_PRIVATE)
if (!g_policy.unrestricted_sysv_mem &&
(shmat_req.shmaddr || shmat_req.shmflg ||
shmat_req.shmid != info->mem->shmId)) {
info->mem->shmId = -1;
SecureMem::abandonSystemCall(*info, -EINVAL);
return false;
}
info->mem->shmId = -1;
SecureMem::sendSystemCall(*info, SecureMem::SEND_UNLOCKED, shmat_req.shmid,
const_cast<void*>(shmat_req.shmaddr),
shmat_req.shmflg);
return true;
}
bool Sandbox::process_shmctl(const SecureMem::SyscallRequestInfo* info) {
// Read request
ShmCtl shmctl_req;
SysCalls sys;
if (read(sys, info->trustedProcessFd, &shmctl_req, sizeof(shmctl_req)) !=
sizeof(shmctl_req)) {
die("Failed to read parameters for shmctl() [process]");
}
// The only shmctl() operation that we need to support is removal. This
// operation is generally safe.
if (!g_policy.unrestricted_sysv_mem &&
((shmctl_req.cmd & ~(IPC_64 | IPC_RMID)) || shmctl_req.buf)) {
info->mem->shmId = -1;
SecureMem::abandonSystemCall(*info, -EINVAL);
return false;
}
info->mem->shmId = -1;
SecureMem::sendSystemCall(*info, SecureMem::SEND_UNLOCKED, shmctl_req.shmid,
shmctl_req.cmd, shmctl_req.buf);
return true;
}
bool Sandbox::process_shmdt(const SecureMem::SyscallRequestInfo* info) {
// Read request
ShmDt shmdt_req;
SysCalls sys;
if (read(sys, info->trustedProcessFd, &shmdt_req, sizeof(shmdt_req)) !=
sizeof(shmdt_req)) {
die("Failed to read parameters for shmdt() [process]");
}
// Detaching shared memory segments it generally safe, but just in case
// of a kernel bug, we make sure that the address does not fall into any
// of the reserved memory regions.
if (!g_policy.unrestricted_sysv_mem &&
isRegionProtected((void *) shmdt_req.shmaddr, 0x1000)) {
info->mem->shmId = -1;
SecureMem::abandonSystemCall(*info, -EINVAL);
return false;
}
info->mem->shmId = -1;
SecureMem::sendSystemCall(*info, SecureMem::SEND_UNLOCKED,
const_cast<void*>(shmdt_req.shmaddr));
return true;
}
bool Sandbox::process_shmget(const SecureMem::SyscallRequestInfo* info) {
// Read request
ShmGet shmget_req;
SysCalls sys;
if (read(sys, info->trustedProcessFd, &shmget_req, sizeof(shmget_req)) !=
sizeof(shmget_req)) {
die("Failed to read parameters for shmget() [process]");
}
// We do not want to allow the sandboxed application to access arbitrary
// shared memory regions. We only allow it to access regions that it
// created itself.
if (!g_policy.unrestricted_sysv_mem &&
(shmget_req.key != IPC_PRIVATE || shmget_req.shmflg & ~0777)) {
info->mem->shmId = -1;
SecureMem::abandonSystemCall(*info, -EINVAL);
return false;
}
info->mem->shmId = -1;
SecureMem::sendSystemCall(*info, SecureMem::SEND_UNLOCKED, shmget_req.key,
shmget_req.size, shmget_req.shmflg);
return true;
}
#endif
#if defined(__NR_ipc)
long Sandbox::sandbox_ipc(unsigned call, int first, int second, int third,
void* ptr, long fifth) {
long long tm;
Debug::syscall(&tm, __NR_ipc, "Executing handler", call);
struct {
struct RequestHeader header;
IPC ipc_req;
} __attribute__((packed)) request;
request.ipc_req.call = call;
request.ipc_req.first = first;
request.ipc_req.second = second;
request.ipc_req.third = third;
request.ipc_req.ptr = ptr;
request.ipc_req.fifth = fifth;
long rc = forwardSyscall(__NR_ipc, &request.header, sizeof(request));
Debug::elapsed(tm, __NR_ipc, call);
return rc;
}
bool Sandbox::process_ipc(const SecureMem::SyscallRequestInfo* info) {
// Read request
IPC ipc_req;
SysCalls sys;
if (read(sys, info->trustedProcessFd, &ipc_req, sizeof(ipc_req)) !=
sizeof(ipc_req)) {
die("Failed to read parameters for ipc() [process]");
}
// We do not support all of the SysV IPC calls. In fact, we only support
// the minimum feature set necessary for Chrome's renderers to share memory
// with the X server.
if (g_policy.unrestricted_sysv_mem) {
goto accept;
}
switch (ipc_req.call) {
case SHMAT: {
// We only allow attaching to the shm identifier that was returned by
// the most recent call to shmget(IPC_PRIVATE)
if (ipc_req.ptr || ipc_req.second || ipc_req.first != info->mem->shmId) {
goto deny;
}
accept:
info->mem->shmId = -1;
SecureMem::sendSystemCall(*info, SecureMem::SEND_UNLOCKED, ipc_req.call,
ipc_req.first, ipc_req.second, ipc_req.third,
ipc_req.ptr, ipc_req.fifth);
return true;
}
case SHMCTL:
// The only shmctl() operation that we need to support is removal. This
// operation is generally safe.
if ((ipc_req.second & ~(IPC_64 | IPC_RMID)) || ipc_req.ptr) {
goto deny;
} else {
goto accept;
}
case SHMDT: {
// Detaching shared memory segments it generally safe, but just in case
// of a kernel bug, we make sure that the address does not fall into any
// of the reserved memory regions.
if (isRegionProtected(ipc_req.ptr, 0x1000)) {
goto deny;
} else {
goto accept;
}
}
case SHMGET:
// We do not want to allow the sandboxed application to access arbitrary
// shared memory regions. We only allow it to access regions that it
// created itself.
if (ipc_req.first != IPC_PRIVATE || ipc_req.third & ~0777) {
goto deny;
} else {
goto accept;
}
default:
// Other than SysV shared memory, we do not actually need to support any
// other SysV IPC calls.
deny:
info->mem->shmId = -1;
SecureMem::abandonSystemCall(*info, -EINVAL);
return false;
}
}
#endif
} // namespace