gitout

A static git page generator
git clone https://git.bracken.jp/gitout.git
Log | Files | Refs | Submodules | README | LICENSE

security.c (1412B)


      1 #include "security.h"
      2 
      3 #include <err.h>
      4 
      5 #ifdef __OpenBSD__
      6 #include <unistd.h>
      7 #else
      8 static int unveil(const char* path, const char* permissions) {
      9   return (path && permissions) ? 0 : 1;
     10 }
     11 static int pledge(const char* promises, const char* execpromises) {
     12   return (promises && execpromises) ? 0 : 1;
     13 }
     14 #endif  // __OpenBSD__
     15 
     16 void restrict_filesystem_access(const char* readonly_paths[],
     17                                 size_t readonly_paths_count,
     18                                 const char* readwrite_paths[],
     19                                 size_t readwrite_paths_count) {
     20   for (size_t i = 0; i < readonly_paths_count; i++) {
     21     const char* path = readonly_paths[i];
     22     if (unveil(path, "r") == -1) {
     23       err(1, "unveil: %s", path);
     24     }
     25   }
     26   for (size_t i = 0; i < readwrite_paths_count; i++) {
     27     const char* path = readwrite_paths[i];
     28     if (unveil(path, "rwc") == -1) {
     29       err(1, "unveil: %s", path);
     30     }
     31   }
     32 }
     33 
     34 void restrict_system_operations(RestrictionType type) {
     35   const char* promises = NULL;
     36   switch (type) {
     37     case kGitout:
     38       promises = "stdio rpath wpath cpath";
     39       break;
     40     case kGitoutWithCachefile:
     41       promises = "stdio rpath wpath cpath fattr";
     42       break;
     43     case kGitoutIndex:
     44       promises = "stdio rpath";
     45       break;
     46     default:
     47       err(1, "unknown restriction");
     48       break;
     49   }
     50   if (pledge(promises, NULL) == -1) {
     51     err(1, "pledge");
     52   }
     53 }