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_LIBTIFF_SANDBOXED_H_ 16 #define CONTRIB_LIBTIFF_SANDBOXED_H_ 17 18 #include <linux/futex.h> 19 #include <syscall.h> 20 21 #include <string> 22 #include <memory> 23 #include <utility> 24 25 #include "absl/flags/flag.h" 26 #include "tiff_sapi.sapi.h" // NOLINT(build/include) 27 28 class TiffSapiSandbox : public TiffSandbox { 29 public: 30 explicit TiffSapiSandbox(std::string dir = "", std::string file = "") dir_(std::move (dir))31 : dir_(std::move(dir)), file_(std::move(file)) {} 32 33 private: ModifyPolicy(sandbox2::PolicyBuilder *)34 std::unique_ptr<sandbox2::Policy> ModifyPolicy( 35 sandbox2::PolicyBuilder*) override { 36 auto builder = sandbox2::PolicyBuilder() 37 .AllowRead() 38 .AllowStaticStartup() 39 .AllowWrite() 40 .AllowOpen() 41 .AllowExit() 42 .AllowStat() 43 .AllowSystemMalloc() 44 .AllowSyscalls({ 45 __NR_futex, 46 __NR_close, 47 __NR_lseek, 48 __NR_gettid, 49 __NR_sysinfo, 50 __NR_mmap, 51 __NR_munmap, 52 }); 53 54 if (!dir_.empty()) { 55 builder.AddDirectory(dir_, /*is_ro=*/false); 56 } 57 58 if (!file_.empty()) { 59 builder.AddFile(file_, /*is_ro=*/false); 60 } 61 62 return builder.BuildOrDie(); 63 } 64 65 std::string dir_; 66 std::string file_; 67 }; 68 69 #endif // CONTRIB_LIBTIFF_SANDBOXED_H_ 70