Hello,

Forwarding a newer patch that I came up with. This time unveil()s are
done before pledge() so no subsequent pledge() is needed to remove
the unveil promise.

* temporary files are created & unlinked in /tmp, so unveil the directory
* output_file_name is either -o PATH or the default of CWD/y.tab.c;
  open it for write
* code_file_name is CWD/y.code.c if -r option is set, otherwise
  code_file_name==output_file_name; add separate unveil for -r case
* input_file_name is the input path, or empty string for filename of "-";
  open it for read if not an empty string
* verbose_file_name is CWD/y.output if -v option is set, otherwise NULL;
  open it for write if not NULL
* defines_file_name is a path in the same directory as output_file_name
  if option -d is set, otherwise NULL; open it for write if not NULL

getargs() and create_file_names() are responsible for setting the
global variables for the various paths based on argv, so move them to
the top. create_file_names() is being lifted from open_files() which
actually performs fopen().


Index: main.c
===================================================================
RCS file: /cvs/src/usr.bin/yacc/main.c,v
retrieving revision 1.29
diff -u -p -u -r1.29 main.c
--- main.c      25 May 2017 20:11:03 -0000      1.29
+++ main.c      1 Oct 2018 07:11:47 -0000
@@ -34,6 +34,7 @@
  */
 
 #include <sys/types.h>
+#include <err.h>
 #include <fcntl.h>
 #include <paths.h>
 #include <signal.h>
@@ -302,8 +303,6 @@ open_files(void)
 {
        int fd;
 
-       create_file_names();
-
        if (input_file == 0) {
                input_file = fopen(input_file_name, "r");
                if (input_file == 0)
@@ -346,11 +345,34 @@ open_files(void)
 int
 main(int argc, char *argv[])
 {
+       getargs(argc, argv);
+       create_file_names();
+
+       if (unveil(_PATH_TMP, "wrc") == -1)
+               err(1, "unveil");
+       if (unveil(output_file_name, "wc") == -1)
+               err(1, "unveil");
+       if (code_file_name != output_file_name) {
+               if (unveil(code_file_name, "wc") == -1)
+                       err(1, "unveil");
+       }
+       if (verbose_file_name != NULL) {
+               if (unveil(verbose_file_name, "wc") == -1)
+                       err(1, "unveil");
+       }
+       if (defines_file_name != NULL) {
+               if (unveil(defines_file_name, "wc") == -1)
+                       err(1, "unveil");
+       }
+       if (strlen(input_file_name) > 0) {
+               if (unveil(input_file_name, "r") == -1)
+                       err(1, "unveil");
+       }
+
        if (pledge("stdio rpath wpath cpath", NULL) == -1)
                fatal("pledge: invalid arguments");
 
        set_signals();
-       getargs(argc, argv);
        open_files();
        reader();
        lr0();

Reply via email to