Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: sandbox/mac/seatbelt_exec.cc

Issue 2869203003: Add the SeatbeltExec classes to facilitate the V2 sandbox. (Closed)
Patch Set: Fix the last few nits. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sandbox/mac/seatbelt_exec.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/macros.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/strings/stringprintf.h"
17 #include "sandbox/mac/seatbelt.h"
18
19 namespace sandbox {
20
21 SeatbeltExecClient::SeatbeltExecClient() {
22 PCHECK(pipe(pipe_) == 0) << "pipe";
23 }
24
25 SeatbeltExecClient::~SeatbeltExecClient() {
26 if (pipe_[1] != -1)
27 IGNORE_EINTR(close(pipe_[1]));
28 // If pipe() fails, PCHECK() will be hit in the constructor, so this file
29 // descriptor should always be closed if the proess is alive at this point.
30 IGNORE_EINTR(close(pipe_[0]));
31 }
32
33 bool SeatbeltExecClient::SetBooleanParameter(const base::StringPiece key,
34 bool value) {
35 google::protobuf::MapPair<std::string, std::string> pair(
36 key.as_string(), value ? "TRUE" : "FALSE");
37 return policy_.mutable_params()->insert(pair).second;
38 }
39
40 bool SeatbeltExecClient::SetParameter(const base::StringPiece key,
41 const base::StringPiece value) {
42 google::protobuf::MapPair<std::string, std::string> pair(key.as_string(),
43 value.as_string());
44 return policy_.mutable_params()->insert(pair).second;
45 }
46
47 void SeatbeltExecClient::SetProfile(const base::StringPiece policy) {
48 policy_.set_profile(policy.as_string());
49 }
50
51 int SeatbeltExecClient::SendProfileAndGetFD() {
52 std::string serialized_protobuf;
53 if (!policy_.SerializeToString(&serialized_protobuf))
54 return -1;
55
56 if (!WriteString(&serialized_protobuf))
57 return -1;
58
59 IGNORE_EINTR(close(pipe_[1]));
60 pipe_[1] = -1;
61
62 return pipe_[0];
63 }
64
65 bool SeatbeltExecClient::WriteString(std::string* str) {
66 struct iovec iov[1];
67 iov[0].iov_base = &(*str)[0];
68 iov[0].iov_len = str->size();
69
70 ssize_t written = HANDLE_EINTR(writev(pipe_[1], iov, arraysize(iov)));
71 if (written < 0) {
72 PLOG(ERROR) << "writev";
73 return false;
74 }
75 return static_cast<uint64_t>(written) == str->size();
76 }
77
78 SeatbeltExecServer::SeatbeltExecServer(int fd) : fd_(fd) {}
79
80 SeatbeltExecServer::~SeatbeltExecServer() {}
81
82 bool SeatbeltExecServer::InitializeSandbox() {
83 std::string policy_string;
84 if (!ReadString(&policy_string))
85 return false;
86
87 mac::SandboxPolicy policy;
88 if (!policy.ParseFromString(policy_string)) {
89 LOG(ERROR) << "ParseFromString failed";
90 return false;
91 }
92
93 return ApplySandboxProfile(policy);
94 }
95
96 bool SeatbeltExecServer::ApplySandboxProfile(const mac::SandboxPolicy& policy) {
97 std::vector<const char*> weak_params;
98 for (const auto& pair : policy.params()) {
99 weak_params.push_back(pair.first.c_str());
100 weak_params.push_back(pair.second.c_str());
101 }
102 weak_params.push_back(nullptr);
103
104 char* error = nullptr;
105 int rv = Seatbelt::InitWithParams(policy.profile().c_str(), 0,
106 weak_params.data(), &error);
107 if (error) {
108 LOG(ERROR) << "Failed to initialize sandbox: " << rv << " " << error;
109 Seatbelt::FreeError(error);
110 return false;
111 }
112
113 return rv == 0;
114 }
115
116 bool SeatbeltExecServer::ReadString(std::string* str) {
117 // 4 pages of memory is enough to hold the sandbox profiles.
118 std::vector<char> buffer(4096 * 4, '\0');
119
120 struct iovec iov[1];
121 iov[0].iov_base = buffer.data();
122 iov[0].iov_len = buffer.size();
123
124 ssize_t read_length = HANDLE_EINTR(readv(fd_.get(), iov, arraysize(iov)));
125 if (read_length < 0) {
126 PLOG(ERROR) << "readv";
127 return false;
128 }
129 str->assign(buffer.data());
130 return true;
131 }
132
133 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/mac/seatbelt_exec.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698