xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/gdal/raster_to_gtiff/gdal_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 RASTER_TO_GTIFF_GDAL_SANDBOX_H_
16 #define RASTER_TO_GTIFF_GDAL_SANDBOX_H_
17 
18 #include <syscall.h>
19 
20 #include <string>
21 
22 #include "gdal_sapi.sapi.h"  // NOLINT(build/include)
23 
24 namespace gdal::sandbox {
25 
26 class GdalSapiSandbox : public GdalSandbox {
27  public:
28   GdalSapiSandbox(std::string out_directory_path, std::string proj_db_path,
29                   time_t time_limit = 0)
out_directory_path_(std::move (out_directory_path))30       : out_directory_path_(std::move(out_directory_path)),
31         proj_db_path_(std::move(proj_db_path)) {
32     SetWallTimeLimit(time_limit).IgnoreError();
33   }
34 
35  private:
ModifyPolicy(sandbox2::PolicyBuilder *)36   std::unique_ptr<sandbox2::Policy> ModifyPolicy(
37       sandbox2::PolicyBuilder*) override {
38     return sandbox2::PolicyBuilder()
39         .AllowDynamicStartup()
40         .AllowRead()
41         .AllowSystemMalloc()
42         .AllowWrite()
43         .AllowExit()
44         .AllowOpen()
45         .AllowSyscall(__NR_futex)
46         .AllowSyscall(__NR_getdents64)  // DriverRegisterAll()
47         .AllowSyscall(__NR_lseek)       // GDALCreate()
48         .AllowSyscall(__NR_getpid)      // GDALCreate()
49         .AllowSyscall(__NR_sysinfo)     // VSI_TIFFOpen_common()
50         .AllowSyscall(__NR_prlimit64)   // CPLGetUsablePhysicalRAM()
51         .AllowSyscall(__NR_ftruncate)   // GTiffDataset::FillEmptyTiles()
52         .AllowUnlink()                  // GDALDriver::Delete()
53         .AddFile(proj_db_path_)  // proj.db is required for some projections
54         .AddDirectory(out_directory_path_, /*is_ro=*/false)
55         .BuildOrDie();
56   }
57 
58   std::string out_directory_path_;
59   std::string proj_db_path_;
60 };
61 
62 }  // namespace gdal::sandbox
63 
64 #endif  // RASTER_TO_GTIFF_GDAL_SANDBOX_H_
65