summaryrefslogtreecommitdiffstats
path: root/sandbox/linux/seccomp/ipc.cc
blob: 3cfcbf0977322581f64edf65845ca79189abba89 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// 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 {
    int       sysnum;
    long long cookie;
    ShmAt     shmat_req;
  } __attribute__((packed)) request;
  request.sysnum             = __NR_shmat;
  request.cookie             = cookie();
  request.shmat_req.shmid    = shmid;
  request.shmat_req.shmaddr  = shmaddr;
  request.shmat_req.shmflg   = shmflg;

  long rc;
  SysCalls sys;
  if (write(sys, processFdPub(), &request, sizeof(request)) !=
      sizeof(request) ||
      read(sys, threadFdPub(), &rc, sizeof(rc)) != sizeof(rc)) {
    die("Failed to forward shmat() request [sandbox]");
  }
  Debug::elapsed(tm, __NR_shmat);
  return reinterpret_cast<void *>(rc);
}

int Sandbox::sandbox_shmctl(int shmid, int cmd, void* buf) {
  long long tm;
  Debug::syscall(&tm, __NR_shmctl, "Executing handler");

  struct {
    int       sysnum;
    long long cookie;
    ShmCtl    shmctl_req;
  } __attribute__((packed)) request;
  request.sysnum           = __NR_shmctl;
  request.cookie           = cookie();
  request.shmctl_req.shmid = shmid;
  request.shmctl_req.cmd   = cmd;
  request.shmctl_req.buf   = buf;

  long rc;
  SysCalls sys;
  if (write(sys, processFdPub(), &request, sizeof(request)) !=
      sizeof(request) ||
      read(sys, threadFdPub(), &rc, sizeof(rc)) != sizeof(rc)) {
    die("Failed to forward shmctl() request [sandbox]");
  }
  Debug::elapsed(tm, __NR_shmctl);
  return static_cast<int>(rc);
}

int Sandbox::sandbox_shmdt(const void* shmaddr) {
  long long tm;
  Debug::syscall(&tm, __NR_shmdt, "Executing handler");

  struct {
    int       sysnum;
    long long cookie;
    ShmDt     shmdt_req;
  } __attribute__((packed)) request;
  request.sysnum             = __NR_shmdt;
  request.cookie             = cookie();
  request.shmdt_req.shmaddr  = shmaddr;

  long rc;
  SysCalls sys;
  if (write(sys, processFdPub(), &request, sizeof(request)) !=
      sizeof(request) ||
      read(sys, threadFdPub(), &rc, sizeof(rc)) != sizeof(rc)) {
    die("Failed to forward shmdt() request [sandbox]");
  }
  Debug::elapsed(tm, __NR_shmdt);
  return static_cast<int>(rc);
}

int Sandbox::sandbox_shmget(int key, size_t size, int shmflg) {
  long long tm;
  Debug::syscall(&tm, __NR_shmget, "Executing handler");

  struct {
    int       sysnum;
    long long cookie;
    ShmGet    shmget_req;
  } __attribute__((packed)) request;
  request.sysnum            = __NR_shmget;
  request.cookie            = cookie();
  request.shmget_req.key    = key;
  request.shmget_req.size   = size;
  request.shmget_req.shmflg = shmflg;

  long rc;
  SysCalls sys;
  if (write(sys, processFdPub(), &request, sizeof(request)) !=
      sizeof(request) ||
      read(sys, threadFdPub(), &rc, sizeof(rc)) != sizeof(rc)) {
    die("Failed to forward shmget() request [sandbox]");
  }
  Debug::elapsed(tm, __NR_shmget);
  return static_cast<int>(rc);
}

bool Sandbox::process_shmat(int parentMapsFd, int sandboxFd, int threadFdPub,
                            int threadFd, SecureMem::Args* mem) {
  // Read request
  ShmAt shmat_req;
  SysCalls sys;
  if (read(sys, sandboxFd, &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 (shmat_req.shmaddr || shmat_req.shmflg || shmat_req.shmid != mem->shmId) {
    mem->shmId = -1;
    SecureMem::abandonSystemCall(threadFd, -EINVAL);
    return false;
  }

  mem->shmId = -1;
  SecureMem::sendSystemCall(threadFdPub, false, -1, mem,
                            __NR_shmat, shmat_req.shmid, shmat_req.shmaddr,
                            shmat_req.shmflg);
  return true;
}

bool Sandbox::process_shmctl(int parentMapsFd, int sandboxFd, int threadFdPub,
                             int threadFd, SecureMem::Args* mem) {
  // Read request
  ShmCtl shmctl_req;
  SysCalls sys;
  if (read(sys, sandboxFd, &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 ((shmctl_req.cmd & ~(IPC_64 | IPC_RMID)) || shmctl_req.buf) {
    mem->shmId = -1;
    SecureMem::abandonSystemCall(threadFd, -EINVAL);
    return false;
  }

  mem->shmId = -1;
  SecureMem::sendSystemCall(threadFdPub, false, -1, mem,
                            __NR_shmctl, shmctl_req.shmid, shmctl_req.cmd,
                            shmctl_req.buf);
  return true;
}

bool Sandbox::process_shmdt(int parentMapsFd, int sandboxFd, int threadFdPub,
                            int threadFd, SecureMem::Args* mem) {
  // Read request
  ShmDt shmdt_req;
  SysCalls sys;
  if (read(sys, sandboxFd, &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.
  ProtectedMap::const_iterator iter = protectedMap_.lower_bound(
      (void *)shmdt_req.shmaddr);
  if (iter != protectedMap_.begin()) {
    --iter;
  }
  for (; iter != protectedMap_.end() && iter->first <= shmdt_req.shmaddr;
       ++iter){
    if (shmdt_req.shmaddr < reinterpret_cast<void *>(
            reinterpret_cast<char *>(iter->first) + iter->second) &&
        shmdt_req.shmaddr >= iter->first) {
      mem->shmId = -1;
      SecureMem::abandonSystemCall(threadFd, -EINVAL);
      return false;
    }
  }

  mem->shmId = -1;
  SecureMem::sendSystemCall(threadFdPub, false, -1, mem,
                            __NR_shmdt, shmdt_req.shmaddr);
  return true;
}

bool Sandbox::process_shmget(int parentMapsFd, int sandboxFd, int threadFdPub,
                             int threadFd, SecureMem::Args* mem) {
  // Read request
  ShmGet shmget_req;
  SysCalls sys;
  if (read(sys, sandboxFd, &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 (shmget_req.key != IPC_PRIVATE || shmget_req.shmflg & ~0777) {
    mem->shmId = -1;
    SecureMem::abandonSystemCall(threadFd, -EINVAL);
    return false;
  }

  mem->shmId = -1;
  SecureMem::sendSystemCall(threadFdPub, false, -1, mem,
                            __NR_shmget, shmget_req.key, shmget_req.size,
                            shmget_req.shmflg);
  return true;
}
#endif

#if defined(__NR_ipc)
#ifndef SHMAT
#define SHMAT       21
#endif
#ifndef SHMDT
#define SHMDT       22
#endif
#ifndef SHMGET
#define SHMGET      23
#endif
#ifndef SHMCTL
#define SHMCTL      24
#endif

int 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 {
    int       sysnum;
    long long cookie;
    IPC       ipc_req;
  } __attribute__((packed)) request;
  request.sysnum         = __NR_ipc;
  request.cookie         = cookie();
  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;
  SysCalls sys;
  if (write(sys, processFdPub(), &request, sizeof(request)) !=
      sizeof(request) ||
      read(sys, threadFdPub(), &rc, sizeof(rc)) != sizeof(rc)) {
    die("Failed to forward ipc() request [sandbox]");
  }
  Debug::elapsed(tm, __NR_ipc, call);
  return static_cast<int>(rc);
}

bool Sandbox::process_ipc(int parentMapsFd, int sandboxFd, int threadFdPub,
                          int threadFd, SecureMem::Args* mem) {
  // Read request
  IPC ipc_req;
  SysCalls sys;
  if (read(sys, sandboxFd, &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.
  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 != mem->shmId) {
        goto deny;
      }
    accept:
      mem->shmId = -1;
      SecureMem::sendSystemCall(threadFdPub, false, -1, mem,
                                __NR_ipc, 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.
      ProtectedMap::const_iterator iter = protectedMap_.lower_bound(
          (void *)ipc_req.ptr);
      if (iter != protectedMap_.begin()) {
        --iter;
      }
      for (; iter != protectedMap_.end() && iter->first <=ipc_req.ptr; ++iter){
        if (ipc_req.ptr < reinterpret_cast<void *>(
                reinterpret_cast<char *>(iter->first) + iter->second) &&
            ipc_req.ptr >= iter->first) {
          goto deny;
        }
      }
      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:
      mem->shmId = -1;
      SecureMem::abandonSystemCall(threadFd, -EINVAL);
      return false;
  }
}
#endif

} // namespace