xref: /aosp_15_r20/external/sandboxed-api/contrib/jsonnet/jsonnet_base_sandbox.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 #ifndef CONTRIB_JSONNET_BASE_SANDBOX_H_
16 #define CONTRIB_JSONNET_BASE_SANDBOX_H_
17 
18 #include <libgen.h>
19 #include <syscall.h>
20 
21 #include <memory>
22 #include <string>
23 #include <utility>
24 
25 #include "jsonnet_sapi.sapi.h"  // NOLINT(build/include)
26 #include "sandboxed_api/transaction.h"
27 #include "sandboxed_api/vars.h"
28 
29 class JsonnetBaseSandbox : public JsonnetSandbox {
30  public:
JsonnetBaseSandbox(std::string in_file,std::string out_file)31   explicit JsonnetBaseSandbox(std::string in_file, std::string out_file)
32       : in_file_(std::move(in_file)), out_file_(std::move(out_file)) {}
33 
ModifyPolicy(sandbox2::PolicyBuilder *)34   std::unique_ptr<sandbox2::Policy> ModifyPolicy(
35       sandbox2::PolicyBuilder*) override {
36     return sandbox2::PolicyBuilder()
37         .AllowStaticStartup()
38         .AllowOpen()
39         .AllowRead()
40         .AllowWrite()
41         .AllowStat()
42         .AllowSystemMalloc()
43         .AllowExit()
44         .AllowSyscalls({
45             __NR_futex,
46             __NR_close,
47         })
48         .AddDirectoryAt(dirname(&out_file_[0]), "/output", /*is_ro=*/false)
49         .AddDirectoryAt(dirname(&in_file_[0]), "/input", true)
50         .BuildOrDie();
51   }
52 
53  private:
54   std::string in_file_;
55   std::string out_file_;
56 };
57 
58 #endif  // CONTRIB_JSONNET_BASE_SANDBOX_H_
59