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 LIBPNG_SANDBOXED_H_ 16 #define LIBPNG_SANDBOXED_H_ 17 18 #include <linux/futex.h> 19 #include <syscall.h> 20 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "libpng_sapi.sapi.h" // NOLINT(build/include) 26 27 class LibPNGSapiSandbox : public LibPNGSandbox { 28 public: AddFile(const std::string & file)29 void AddFile(const std::string& file) { files_.push_back(file); } 30 31 private: ModifyPolicy(sandbox2::PolicyBuilder *)32 std::unique_ptr<sandbox2::Policy> ModifyPolicy( 33 sandbox2::PolicyBuilder*) override { 34 sandbox2::PolicyBuilder builder; 35 builder.AllowRead() 36 .AllowStaticStartup() 37 .AllowWrite() 38 .AllowOpen() 39 .AllowExit() 40 .AllowStat() 41 .AllowMmapWithoutExec() 42 .AllowSystemMalloc() 43 .AllowSyscalls({ 44 __NR_futex, 45 __NR_close, 46 __NR_lseek, 47 __NR_gettid, 48 __NR_sysinfo, 49 __NR_munmap, 50 __NR_recvmsg, 51 __NR_fcntl, 52 }); 53 54 for (const auto& file : files_) { 55 builder->AddFile(file, /*is_ro=*/false); 56 } 57 58 return builder.BuildOrDie(); 59 } 60 61 std::vector<std::string> files_; 62 }; 63 64 #endif // LIBPNG_SANDBOXED_H_ 65