commit e6c2c6a9cc5a3d2dc6e02dfca9be63f9378c05f5
parent 50a090f253b4bf14c02865c5a722a4534236e2a7
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date: Wed, 23 Sep 2020 21:19:29 +1000
add pledge+unveil support
Diffstat:
2 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/bsdiff.c b/bsdiff.c
@@ -42,6 +42,9 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#if HAVE_PLEDGE && HAVE_UNVEIL
+#include <unistd.h>
+#endif
#include "bsdiff.h"
@@ -68,6 +71,18 @@ main(int argc, char **argv)
if (argc != 4)
usage();
+#if HAVE_PLEDGE && HAVE_UNVEIL
+ if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
+ err(1, "pledge");
+ if (unveil(argv[1], "r") == -1)
+ err(1, "unveil(%s, r)", argv[1]);
+ if (unveil(argv[2], "r") == -1)
+ err(1, "unveil(%s, r)", argv[2]);
+ if (unveil(argv[3], "wc") == -1)
+ err(1, "unveil(%s, wc)", argv[3]);
+ if (pledge("stdio rpath wpath cpath", NULL) == -1)
+ err(1, "pledge");
+#endif
old = readfile(argv[1], &oldsize);
if ((I = reallocarray(NULL, oldsize + 1, sizeof(off_t))) == NULL)
@@ -80,6 +95,10 @@ main(int argc, char **argv)
free(V);
new = readfile(argv[2], &newsize);
+#if HAVE_PLEDGE && HAVE_UNVEIL
+ if (pledge("stdio wpath cpath", NULL) == -1)
+ err(1, "pledge");
+#endif
if ((db = reallocarray(NULL, oldsize + 1, sizeof(off_t))) == NULL)
err(1, "reallocarray");
@@ -90,6 +109,10 @@ main(int argc, char **argv)
/* Create the patch file */
if ((pf = fopen(argv[3], "w")) == NULL)
err(1, "fopen(%s)", argv[3]);
+#if HAVE_PLEDGE && HAVE_UNVEIL
+ if (pledge("stdio", NULL) == -1)
+ err(1, "pledge");
+#endif
/* Header is
0 8 "BSDIFF40"
diff --git a/bspatch.c b/bspatch.c
@@ -63,9 +63,28 @@ main(int argc, char **argv)
if (argc != 4)
usage();
+#if HAVE_PLEDGE && HAVE_UNVEIL
+ if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
+ err(1, "pledge");
+ if (unveil(argv[1], "r") == -1)
+ err(1, "unveil(%s, r)", argv[1]);
+ if (unveil(argv[2], "wc") == -1)
+ err(1, "unveil(%s, wc)", argv[2]);
+ if (unveil(argv[3], "r") == -1)
+ err(1, "unveil(%s, r)", argv[3]);
+ if (pledge("stdio rpath wpath cpath", NULL) == -1)
+ err(1, "pledge");
+#endif
/* Open patch file */
if ((f = fopen(argv[3], "r")) == NULL)
err(1, "%s: fopen", argv[3]);
+ /* Open the new file */
+ if ((fd = open(argv[2], O_CREAT|O_TRUNC|O_WRONLY, 0666)) == -1)
+ err(1, "open(%s)", argv[2]);
+#if HAVE_PLEDGE && HAVE_UNVEIL
+ if (pledge("stdio rpath", NULL) == -1)
+ err(1, "pledge");
+#endif
/*
File format:
@@ -126,6 +145,10 @@ main(int argc, char **argv)
old = readfile(argv[1], &oldsize);
if ((new = reallocarray(NULL, newsize + 1, 1)) == NULL)
err(1, "reallocarray");
+#if HAVE_PLEDGE && HAVE_UNVEIL
+ if (pledge("stdio", NULL) == -1)
+ err(1, "pledge");
+#endif
oldpos = newpos = 0;
while (newpos < newsize) {
@@ -184,8 +207,6 @@ main(int argc, char **argv)
err(1, "fclose(%s)", argv[3]);
/* Write the new file */
- if ((fd = open(argv[2], O_CREAT|O_TRUNC|O_WRONLY, 0666)) == -1)
- err(1, "open(%s)", argv[2]);
if (write(fd, new, newsize) != newsize)
err(1, "write(%s, %lld)", argv[2], (long long)newsize);
if (close(fd) == -1)