1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // These are needed for the __NR_xxx syscall numbers
16 #include <linux/audit.h>
17 #include <sys/syscall.h>
18
19 #include <iostream>
20 #include <memory>
21
22 #include "absl/memory/memory.h"
23 #include "absl/status/status.h"
24
25 // Generated header
26 #include "hello_sapi.sapi.h" // NOLINT(build/include)
27 #include "sandboxed_api/sandbox2/policy.h"
28 #include "sandboxed_api/sandbox2/policybuilder.h"
29 #include "sandboxed_api/transaction.h"
30 #include "sandboxed_api/util/status_macros.h"
31
32 namespace {
33
34 class CustomHelloSandbox : public HelloSandbox {
35 public:
ModifyPolicy(sandbox2::PolicyBuilder *)36 std::unique_ptr<sandbox2::Policy> ModifyPolicy(
37 sandbox2::PolicyBuilder*) override {
38 // Return a new policy.
39 return sandbox2::PolicyBuilder()
40 .AllowRead()
41 .AllowWrite()
42 .AllowOpen()
43 .AllowSystemMalloc()
44 .AllowHandleSignals()
45 .AllowExit()
46 .AllowStat()
47 .AllowTime()
48 .AllowGetIDs()
49 .AllowGetPIDs()
50 .AllowSyscalls({
51 __NR_tgkill,
52 __NR_recvmsg,
53 __NR_sendmsg,
54 __NR_lseek,
55 __NR_nanosleep,
56 __NR_futex,
57 __NR_close,
58 })
59 .AddFile("/etc/localtime")
60 .BuildOrDie();
61 }
62 };
63
64 } // namespace
65
main()66 int main() {
67 std::cout << "Calling into a sandboxee to add two numbers...\n";
68
69 sapi::BasicTransaction transaction(std::make_unique<CustomHelloSandbox>());
70
71 absl::Status status =
72 transaction.Run([](sapi::Sandbox* sandbox) -> absl::Status {
73 HelloApi api(sandbox);
74 SAPI_ASSIGN_OR_RETURN(int result, api.AddTwoIntegers(1000, 337));
75 std::cout << " 1000 + 337 = " << result << "\n";
76 return absl::OkStatus();
77 });
78 if (!status.ok()) {
79 std::cerr << "Error during sandbox call: " << status.message() << "\n";
80 }
81 }
82