sandboxed-api/oss-internship-2020/gdal/raster_to_gtiff/gdal_sandbox.h
Bohdan Tyshchenko 5442d8c6e0 Updated sandbox construction logic and CMakeLists
More flexible CMake file with variables
Added logic to check whether proj.db exists and fetch it from the environment variable
2020-10-05 11:01:15 -07:00

77 lines
2.4 KiB
C++

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GDAL_SANDBOX_H_
#define GDAL_SANDBOX_H_
#include <iostream>
#include <string>
#include <syscall.h>
#include "gdal_sapi.sapi.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
namespace gdal::sandbox {
class GdalSapiSandbox : public gdalSandbox {
public:
GdalSapiSandbox(std::string out_directory_path,
std::string proj_db_path,
time_t time_limit = 0)
: gdalSandbox(),
out_directory_path_(std::move(out_directory_path)),
proj_db_path_(std::move(proj_db_path))
{
SetWallTimeLimit(time_limit).IgnoreError();
}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
return sandbox2::PolicyBuilder()
.AllowDynamicStartup()
.AllowRead()
.AllowSystemMalloc()
.AllowWrite()
.AllowExit()
.AllowOpen()
.AllowSyscalls({
__NR_futex,
__NR_getdents64, // DriverRegisterAll()
__NR_lseek, // GDALCreate()
__NR_getpid, // GDALCreate()
__NR_sysinfo, // VSI_TIFFOpen_common()
__NR_prlimit64, // CPLGetUsablePhysicalRAM()
__NR_ftruncate, // GTiffDataset::FillEmptyTiles()
__NR_unlink, // GDALDriver::Delete()
})
// TODO: Deal with proj.db so you don't need to specify exact path ih the policy
// Add proj path a an environment variable, check it before calling constructor
// Use default path if there is no variable
// If there is no file on the default path return an error
.AddFile(proj_db_path_) // proj.db is required for some projections
.AddDirectory(out_directory_path_, /*is_ro=*/false)
.BuildOrDie();
}
std::string out_directory_path_;
std::string proj_db_path_;
};
} // namespace gdal::sandbox
#endif // GDAL_SANDBOX_H_