| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "debug.h" | |
| 6 #include "sandbox_impl.h" | |
| 7 | |
| 8 namespace playground { | |
| 9 | |
| 10 long Sandbox::sandbox_open(const char *pathname, int flags, mode_t mode) { | |
| 11 long long tm; | |
| 12 Debug::syscall(&tm, __NR_open, "Executing handler"); | |
| 13 size_t len = strlen(pathname); | |
| 14 struct Request { | |
| 15 int sysnum; | |
| 16 long long cookie; | |
| 17 Open open_req; | |
| 18 char pathname[0]; | |
| 19 } __attribute__((packed)) *request; | |
| 20 char data[sizeof(struct Request) + len]; | |
| 21 request = reinterpret_cast<struct Request*>(data); | |
| 22 request->sysnum = __NR_open; | |
| 23 request->cookie = cookie(); | |
| 24 request->open_req.path_length = len; | |
| 25 request->open_req.flags = flags; | |
| 26 request->open_req.mode = mode; | |
| 27 memcpy(request->pathname, pathname, len); | |
| 28 | |
| 29 long rc; | |
| 30 SysCalls sys; | |
| 31 if (write(sys, processFdPub(), request, sizeof(data)) != (int)sizeof(data) || | |
| 32 read(sys, threadFdPub(), &rc, sizeof(rc)) != sizeof(rc)) { | |
| 33 die("Failed to forward open() request [sandbox]"); | |
| 34 } | |
| 35 Debug::elapsed(tm, __NR_open); | |
| 36 return rc; | |
| 37 } | |
| 38 | |
| 39 bool Sandbox::process_open(int parentMapsFd, int sandboxFd, int threadFdPub, | |
| 40 int threadFd, SecureMem::Args* mem) { | |
| 41 // Read request | |
| 42 SysCalls sys; | |
| 43 Open open_req; | |
| 44 if (read(sys, sandboxFd, &open_req, sizeof(open_req)) != sizeof(open_req)) { | |
| 45 read_parm_failed: | |
| 46 die("Failed to read parameters for open() [process]"); | |
| 47 } | |
| 48 int rc = -ENAMETOOLONG; | |
| 49 if (open_req.path_length >= sizeof(mem->pathname)) { | |
| 50 char buf[32]; | |
| 51 while (open_req.path_length > 0) { | |
| 52 size_t len = open_req.path_length > sizeof(buf) ? | |
| 53 sizeof(buf) : open_req.path_length; | |
| 54 ssize_t i = read(sys, sandboxFd, buf, len); | |
| 55 if (i <= 0) { | |
| 56 goto read_parm_failed; | |
| 57 } | |
| 58 open_req.path_length -= i; | |
| 59 } | |
| 60 if (write(sys, threadFd, &rc, sizeof(rc)) != sizeof(rc)) { | |
| 61 die("Failed to return data from open() [process]"); | |
| 62 } | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 if ((open_req.flags & O_ACCMODE) != O_RDONLY || | |
| 67 !g_policy.allow_file_namespace) { | |
| 68 // After locking the mutex, we can no longer abandon the system call. So, | |
| 69 // perform checks before clobbering the securely shared memory. | |
| 70 char tmp[open_req.path_length]; | |
| 71 if (read(sys, sandboxFd, tmp, open_req.path_length) != | |
| 72 (ssize_t)open_req.path_length) { | |
| 73 goto read_parm_failed; | |
| 74 } | |
| 75 Debug::message(("Denying access to \"" + std::string(tmp) + "\"").c_str()); | |
| 76 SecureMem::abandonSystemCall(threadFd, -EACCES); | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 SecureMem::lockSystemCall(parentMapsFd, mem); | |
| 81 if (read(sys, sandboxFd, mem->pathname, open_req.path_length) != | |
| 82 (ssize_t)open_req.path_length) { | |
| 83 goto read_parm_failed; | |
| 84 } | |
| 85 mem->pathname[open_req.path_length] = '\000'; | |
| 86 | |
| 87 // TODO(markus): Implement sandboxing policy. For now, we allow read | |
| 88 // access to everything. That's probably not correct. | |
| 89 Debug::message(("Allowing access to \"" + std::string(mem->pathname) + | |
| 90 "\"").c_str()); | |
| 91 | |
| 92 // Tell trusted thread to open the file. | |
| 93 SecureMem::sendSystemCall(threadFdPub, true, parentMapsFd, mem, __NR_open, | |
| 94 mem->pathname - (char*)mem + (char*)mem->self, | |
| 95 open_req.flags, open_req.mode); | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 } // namespace | |
| OLD | NEW |