Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "sandbox/mac/seatbelt_exec.h" | |
| 6 | |
| 7 #include <sys/socket.h> | |
| 8 #include <sys/uio.h> | |
| 9 #include <unistd.h> | |
| 10 | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/logging.h" | |
| 14 #include "base/posix/eintr_wrapper.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "sandbox/mac/seatbelt.h" | |
| 17 | |
| 18 namespace sandbox { | |
| 19 | |
| 20 SeatbeltExecClient::SeatbeltExecClient() { | |
| 21 PCHECK(pipe(pipe_) == 0) << "pipe"; | |
| 22 } | |
| 23 | |
| 24 SeatbeltExecClient::~SeatbeltExecClient() { | |
| 25 if (!got_fd_) | |
| 26 close(pipe_[1]); | |
|
Robert Sesek
2017/05/10 15:25:28
Use IGNORE_EINTR.
Greg K
2017/05/11 17:44:15
Done.
| |
| 27 | |
| 28 close(pipe_[0]); | |
|
Robert Sesek
2017/05/10 15:25:29
Same.
Greg K
2017/05/11 17:44:15
Done.
| |
| 29 } | |
| 30 | |
| 31 bool SeatbeltExecClient::SetBooleanParameter(const std::string& key, | |
| 32 bool value) { | |
| 33 google::protobuf::MapPair<std::string, std::string> pair( | |
| 34 key, value ? "TRUE" : "FALSE"); | |
| 35 return params_.mutable_params()->insert(pair).second; | |
| 36 } | |
| 37 | |
| 38 bool SeatbeltExecClient::SetParameter(const std::string& key, | |
| 39 const std::string& value) { | |
| 40 google::protobuf::MapPair<std::string, std::string> pair(key, value); | |
| 41 return params_.mutable_params()->insert(pair).second; | |
| 42 } | |
| 43 | |
| 44 void SeatbeltExecClient::SetPolicy(const char* policy) { | |
| 45 params_.set_policy(policy); | |
| 46 } | |
| 47 | |
| 48 int SeatbeltExecClient::GetSandboxFD() { | |
| 49 std::string serialized_protobuf; | |
| 50 if (!params_.SerializeToString(&serialized_protobuf)) | |
| 51 LOG(FATAL) << "failed to serialize protobuf"; | |
|
Robert Sesek
2017/05/10 15:25:29
Why is this LOG(FATAL) and not a return -1?
Greg K
2017/05/11 17:44:15
Done.
| |
| 52 | |
| 53 if (!WriteString(serialized_protobuf)) | |
| 54 return -1; | |
| 55 | |
| 56 got_fd_ = true; | |
| 57 close(pipe_[1]); | |
| 58 | |
| 59 return pipe_[0]; | |
| 60 } | |
| 61 | |
| 62 bool SeatbeltExecClient::WriteString(const std::string& str) { | |
| 63 // iov takes a non-const pointer. | |
|
Robert Sesek
2017/05/10 15:25:28
Can you use &str[0] ?
Greg K
2017/05/11 17:44:15
The compiler says that &str[0] is also a const cha
| |
| 64 char buffer[str.size() + 1]; | |
| 65 memcpy(buffer, str.c_str(), sizeof(buffer)); | |
| 66 | |
| 67 struct iovec iov[1]; | |
| 68 iov[0].iov_base = buffer; | |
| 69 iov[0].iov_len = sizeof(buffer); | |
| 70 | |
| 71 ssize_t written = | |
| 72 HANDLE_EINTR(writev(pipe_[1], iov, sizeof(iov) / sizeof(iov[0]))); | |
|
Robert Sesek
2017/05/10 15:25:28
Use arraysize().
Greg K
2017/05/11 17:44:15
Done.
| |
| 73 if (written < 0) | |
| 74 return false; | |
|
Robert Sesek
2017/05/10 15:25:29
PLOG(ERROR)?
Greg K
2017/05/11 17:44:15
Done.
| |
| 75 return static_cast<uint64_t>(written) == str.size(); | |
| 76 } | |
| 77 | |
| 78 SeatbeltExecServer::SeatbeltExecServer(int fd) : fd_(fd) {} | |
| 79 | |
| 80 SeatbeltExecServer::~SeatbeltExecServer() { | |
| 81 close(fd_); | |
| 82 } | |
| 83 | |
| 84 void SeatbeltExecServer::AllowProcessExec(const std::string& exec_path) { | |
| 85 exec_path_ = exec_path; | |
| 86 } | |
| 87 | |
| 88 int SeatbeltExecServer::InitializeSandbox() { | |
| 89 std::string params_string; | |
| 90 if (!ReadString(¶ms_string)) { | |
| 91 LOG(ERROR) << "ReadString"; | |
|
Robert Sesek
2017/05/10 15:25:28
Not necessary to log here since ReadString logs in
Greg K
2017/05/11 17:44:15
Done.
| |
| 92 return -1; | |
| 93 } | |
| 94 | |
| 95 mac::SandboxParams params; | |
| 96 if (!params.ParseFromString(params_string)) { | |
| 97 LOG(ERROR) << "ParseFromString failed"; | |
| 98 return -1; | |
| 99 } | |
| 100 | |
| 101 return ApplySandboxProfile(params); | |
| 102 } | |
| 103 | |
| 104 int SeatbeltExecServer::ApplySandboxProfile(const mac::SandboxParams& params) { | |
| 105 std::vector<const char*> weak_params; | |
| 106 for (const auto& pair : params.params()) { | |
| 107 weak_params.push_back(pair.first.c_str()); | |
| 108 weak_params.push_back(pair.second.c_str()); | |
| 109 } | |
| 110 weak_params.push_back("EXECUTABLE_PATH"); | |
| 111 weak_params.push_back(exec_path_.c_str()); | |
| 112 weak_params.push_back("CHROMIUM_PID"); | |
|
Robert Sesek
2017/05/10 15:25:28
What's with this magic variable? //sandbox general
Greg K
2017/05/11 17:44:15
The calling code will just add those as parameters
| |
| 113 weak_params.push_back(std::to_string(getppid()).c_str()); | |
| 114 weak_params.push_back(nullptr); | |
| 115 | |
| 116 char* error = nullptr; | |
| 117 int rv = Seatbelt::InitWithParams(params.policy().c_str(), 0, | |
| 118 weak_params.data(), &error); | |
| 119 if (error) { | |
| 120 LOG(ERROR) << "Failed to initialize sandbox: -" << rv << " " << error; | |
|
Robert Sesek
2017/05/10 17:53:28
No need for the negative sign in the string, appar
Greg K
2017/05/11 17:44:15
Done.
| |
| 121 Seatbelt::FreeError(error); | |
| 122 return rv; | |
| 123 } | |
| 124 | |
| 125 return rv; | |
| 126 } | |
| 127 | |
| 128 bool SeatbeltExecServer::ReadString(std::string* str) { | |
| 129 // 4 pages of memory is enough to hold the sandbox profiles. | |
| 130 char buffer[4096 * 4]; | |
|
Robert Sesek
2017/05/10 15:25:29
Use std::vector<char> instead.
Greg K
2017/05/11 17:44:15
Done.
| |
| 131 memset(buffer, '\0', sizeof(buffer)); | |
| 132 | |
| 133 struct iovec iov[1]; | |
| 134 iov[0].iov_base = buffer; | |
| 135 iov[0].iov_len = sizeof(buffer); | |
| 136 | |
| 137 ssize_t read_length = readv(fd_, iov, sizeof(iov) / sizeof(iov[0])); | |
|
Robert Sesek
2017/05/10 15:25:28
HANDLE_EINTR
Robert Sesek
2017/05/10 15:25:28
Use arraysize().
Greg K
2017/05/11 17:44:14
Done.
Greg K
2017/05/11 17:44:15
Done.
Greg K
2017/05/11 17:44:15
Done.
| |
| 138 if (read_length < 0) { | |
| 139 PLOG(ERROR) << "ReadString"; | |
|
Robert Sesek
2017/05/10 15:25:28
"readv" not "ReadString"
Greg K
2017/05/11 17:44:14
Done.
| |
| 140 return false; | |
| 141 } | |
| 142 str->assign(buffer); | |
| 143 return true; | |
| 144 } | |
| 145 | |
| 146 } // namespace sandbox | |
| OLD | NEW |