Skip to content

Commit

Permalink
capsicum: disable fs writes
Browse files Browse the repository at this point in the history
Use RLIMIT_FSIZE to disable writes to the filesystem when stdout is not
a regular file.
  • Loading branch information
msantos committed May 11, 2023
1 parent 51852e5 commit 78e5bbb
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions restrict_process_capsicum.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2017-2022, Michael Santos <[email protected]>
/* Copyright (c) 2017-2023, Michael Santos <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,12 +16,13 @@
#include <sys/capsicum.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <stdlib.h>
#include <dirent.h>
#include <stdlib.h>

#include <errno.h>

Expand All @@ -31,6 +32,15 @@ static int fdlimit_range(int lowfd, cap_rights_t *policy);

int restrict_process_init() {
struct rlimit rl = {0};
struct stat sb = {0};

if (fstat(STDOUT_FILENO, &sb) < 0)
return -1;

if (!S_ISREG(sb.st_mode)) {
if (setrlimit(RLIMIT_FSIZE, &rl) < 0)
return -1;
}

return setrlimit(RLIMIT_NPROC, &rl);
}
Expand Down

0 comments on commit 78e5bbb

Please sign in to comment.