|
5 | 5 | # include <sandbox.h> |
6 | 6 | # include <sys/ipc.h> |
7 | 7 | # include <sys/shm.h> |
| 8 | +# include <sys/msg.h> |
| 9 | +# include <sys/sem.h> |
8 | 10 |
|
9 | 11 | /* This definition is undocumented but depended upon by all major browsers. */ |
10 | 12 | extern "C" int |
11 | 13 | sandbox_init_with_parameters(const char * profile, uint64_t flags, const char * const parameters[], char ** errorbuf); |
12 | 14 |
|
13 | | -/* Darwin IPC cleanup structures and constants */ |
| 15 | +/* Darwin IPC structures and constants */ |
14 | 16 | # define IPCS_MAGIC 0x00000001 |
15 | 17 | # define IPCS_SHM_ITER 0x00000002 |
| 18 | +# define IPCS_SEM_ITER 0x00000020 |
| 19 | +# define IPCS_MSG_ITER 0x00000200 |
16 | 20 | # define IPCS_SHM_SYSCTL "kern.sysv.ipcs.shm" |
| 21 | +# define IPCS_MSG_SYSCTL "kern.sysv.ipcs.msg" |
| 22 | +# define IPCS_SEM_SYSCTL "kern.sysv.ipcs.sem" |
17 | 23 |
|
18 | | -struct IPCS_command |
| 24 | +struct IpcsCommand |
19 | 25 | { |
20 | 26 | uint32_t ipcs_magic; |
21 | 27 | uint32_t ipcs_op; |
@@ -223,31 +229,97 @@ struct DarwinDerivationBuilder : DerivationBuilderImpl |
223 | 229 |
|
224 | 230 | void cleanupSysVIPCForUser(uid_t uid) |
225 | 231 | { |
226 | | - struct IPCS_command ic; |
227 | | - struct shmid_ds shm_ds; |
| 232 | + struct IpcsCommand ic; |
228 | 233 | size_t ic_size = sizeof(ic); |
| 234 | + // IPC ids to cleanup |
| 235 | + std::vector<int> shm_ids, msg_ids, sem_ids; |
| 236 | + |
| 237 | + { |
| 238 | + struct shmid_ds shm_ds; |
| 239 | + ic.ipcs_magic = IPCS_MAGIC; |
| 240 | + ic.ipcs_op = IPCS_SHM_ITER; |
| 241 | + ic.ipcs_cursor = 0; |
| 242 | + ic.ipcs_data = &shm_ds; |
| 243 | + ic.ipcs_datalen = sizeof(shm_ds); |
| 244 | + |
| 245 | + while (true) { |
| 246 | + memset(&shm_ds, 0, sizeof(shm_ds)); |
| 247 | + |
| 248 | + if (sysctlbyname(IPCS_SHM_SYSCTL, &ic, &ic_size, &ic, ic_size) != 0) { |
| 249 | + break; |
| 250 | + } |
| 251 | + |
| 252 | + if (shm_ds.shm_perm.uid == uid) { |
| 253 | + int shmid = shmget(shm_ds.shm_perm._key, 0, 0); |
| 254 | + if (shmid != -1) { |
| 255 | + shm_ids.push_back(shmid); |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + } |
229 | 260 |
|
230 | | - ic.ipcs_magic = IPCS_MAGIC; |
231 | | - ic.ipcs_op = IPCS_SHM_ITER; |
232 | | - ic.ipcs_cursor = 0; |
233 | | - ic.ipcs_data = &shm_ds; |
234 | | - ic.ipcs_datalen = sizeof(shm_ds); |
| 261 | + for (auto id : shm_ids) { |
| 262 | + if (shmctl(id, IPC_RMID, NULL) == 0) |
| 263 | + debug("removed shared memory segment with shmid %d", id); |
| 264 | + } |
| 265 | + |
| 266 | + { |
| 267 | + struct msqid_ds msg_ds; |
| 268 | + ic.ipcs_magic = IPCS_MAGIC; |
| 269 | + ic.ipcs_op = IPCS_MSG_ITER; |
| 270 | + ic.ipcs_cursor = 0; |
| 271 | + ic.ipcs_data = &msg_ds; |
| 272 | + ic.ipcs_datalen = sizeof(msg_ds); |
235 | 273 |
|
236 | | - while (true) { |
237 | | - memset(&shm_ds, 0, sizeof(shm_ds)); |
| 274 | + while (true) { |
| 275 | + memset(&msg_ds, 0, sizeof(msg_ds)); |
238 | 276 |
|
239 | | - if (sysctlbyname(IPCS_SHM_SYSCTL, &ic, &ic_size, &ic, ic_size) != 0) { |
240 | | - break; |
| 277 | + if (sysctlbyname(IPCS_MSG_SYSCTL, &ic, &ic_size, &ic, ic_size) != 0) { |
| 278 | + break; |
| 279 | + } |
| 280 | + |
| 281 | + if (msg_ds.msg_perm.uid == uid) { |
| 282 | + int msgid = msgget(msg_ds.msg_perm._key, 0); |
| 283 | + if (msgid != -1) { |
| 284 | + msg_ids.push_back(msgid); |
| 285 | + } |
| 286 | + } |
241 | 287 | } |
| 288 | + } |
| 289 | + |
| 290 | + for (auto id : msg_ids) { |
| 291 | + if (msgctl(id, IPC_RMID, NULL) == 0) |
| 292 | + debug("removed message queue with msgid %d", id); |
| 293 | + } |
| 294 | + |
| 295 | + { |
| 296 | + struct semid_ds sem_ds; |
| 297 | + ic.ipcs_magic = IPCS_MAGIC; |
| 298 | + ic.ipcs_op = IPCS_SEM_ITER; |
| 299 | + ic.ipcs_cursor = 0; |
| 300 | + ic.ipcs_data = &sem_ds; |
| 301 | + ic.ipcs_datalen = sizeof(sem_ds); |
| 302 | + |
| 303 | + while (true) { |
| 304 | + memset(&sem_ds, 0, sizeof(sem_ds)); |
242 | 305 |
|
243 | | - if (shm_ds.shm_perm.uid == uid) { |
244 | | - int shmid = shmget(shm_ds.shm_perm._key, 0, 0); |
245 | | - if (shmid != -1) { |
246 | | - if (shmctl(shmid, IPC_RMID, NULL) == 0) |
247 | | - debug("removed shared memory segment with shmid %d (key: 0x%x)", shmid, shm_ds.shm_perm._key); |
| 306 | + if (sysctlbyname(IPCS_SEM_SYSCTL, &ic, &ic_size, &ic, ic_size) != 0) { |
| 307 | + break; |
| 308 | + } |
| 309 | + |
| 310 | + if (sem_ds.sem_perm.uid == uid) { |
| 311 | + int semid = semget(sem_ds.sem_perm._key, 0, 0); |
| 312 | + if (semid != -1) { |
| 313 | + sem_ids.push_back(semid); |
| 314 | + } |
248 | 315 | } |
249 | 316 | } |
250 | 317 | } |
| 318 | + |
| 319 | + for (auto id : sem_ids) { |
| 320 | + if (semctl(id, 0, IPC_RMID) == 0) |
| 321 | + debug("removed semaphore with semid %d", id); |
| 322 | + } |
251 | 323 | } |
252 | 324 |
|
253 | 325 | void killSandbox(bool getStats) override |
|
0 commit comments