040b22b
From fee19fa8d4f6f351e5a76f5801884880249d6a45 Mon Sep 17 00:00:00 2001
040b22b
From: Chris Liddell <chris.liddell@artifex.com>
040b22b
Date: Wed, 5 Oct 2016 09:55:55 +0100
040b22b
Subject: [PATCH] Bug 697178: Add a file permissions callback
040b22b
040b22b
For the rare occasions when the graphics library directly opens a file
040b22b
(currently for reading), this allows us to apply any restrictions on
040b22b
file access normally applied in the interpteter.
040b22b
---
040b22b
 base/gsicc_manage.c | 10 ++++++----
040b22b
 base/gslibctx.c     | 12 +++++++++++-
040b22b
 base/gslibctx.h     |  7 +++++++
040b22b
 psi/imain.c         |  2 ++
040b22b
 psi/int.mak         |  2 +-
040b22b
 psi/zfile.c         | 19 +++++++++++++++++++
040b22b
 psi/zfile.h         |  7 +++++++
040b22b
 7 files changed, 53 insertions(+), 6 deletions(-)
040b22b
040b22b
diff --git a/base/gsicc_manage.c b/base/gsicc_manage.c
040b22b
index 931c2a6..e9c09c3 100644
040b22b
--- a/base/gsicc_manage.c
040b22b
+++ b/base/gsicc_manage.c
040b22b
@@ -1124,10 +1124,12 @@ gsicc_open_search(const char* pname, int namelen, gs_memory_t *mem_gc,
040b22b
     }
040b22b
 
040b22b
     /* First just try it like it is */
040b22b
-    str = sfopen(pname, "r", mem_gc);
040b22b
-    if (str != NULL) {
040b22b
-        *strp = str;
040b22b
-        return 0;
040b22b
+    if (gs_check_file_permission(mem_gc, pname, namelen, "r") >= 0) {
040b22b
+        str = sfopen(pname, "r", mem_gc);
040b22b
+        if (str != NULL) {
040b22b
+            *strp = str;
040b22b
+            return 0;
040b22b
+        }
040b22b
     }
040b22b
 
040b22b
     /* If that fails, try %rom% */ /* FIXME: Not sure this is needed or correct */
040b22b
diff --git a/base/gslibctx.c b/base/gslibctx.c
040b22b
index fa4432a..f2c13e3 100644
040b22b
--- a/base/gslibctx.c
040b22b
+++ b/base/gslibctx.c
040b22b
@@ -183,7 +183,7 @@ int gs_lib_ctx_init( gs_memory_t *mem )
040b22b
         mem->gs_lib_ctx = NULL;
040b22b
         return -1;
040b22b
     }
040b22b
-    
040b22b
+    pio->client_check_file_permission = NULL;
040b22b
     gp_get_realtime(pio->real_time_0);
040b22b
 
040b22b
     /* Set scanconverter to 1 (default) */
040b22b
@@ -336,3 +336,13 @@ void errflush(const gs_memory_t *mem)
040b22b
         fflush(mem->gs_lib_ctx->fstderr);
040b22b
     /* else nothing to flush */
040b22b
 }
040b22b
+
040b22b
+int
040b22b
+gs_check_file_permission (gs_memory_t *mem, const char *fname, const int len, const char *permission)
040b22b
+{
040b22b
+    int code = 0;
040b22b
+    if (mem->gs_lib_ctx->client_check_file_permission != NULL) {
040b22b
+        code = mem->gs_lib_ctx->client_check_file_permission(mem, fname, len, permission);
040b22b
+    }
040b22b
+    return code;
040b22b
+}
040b22b
diff --git a/base/gslibctx.h b/base/gslibctx.h
040b22b
index 84ec205..55eb4a6 100644
040b22b
--- a/base/gslibctx.h
040b22b
+++ b/base/gslibctx.h
040b22b
@@ -32,6 +32,9 @@ typedef struct gs_fapi_server_s gs_fapi_server;
040b22b
 #  define gs_font_dir_DEFINED
040b22b
 typedef struct gs_font_dir_s gs_font_dir;
040b22b
 #endif
040b22b
+
040b22b
+typedef int (*client_check_file_permission_t) (gs_memory_t *mem, const char *fname, const int len, const char *permission);
040b22b
+
040b22b
 typedef struct gs_lib_ctx_s
040b22b
 {
040b22b
     gs_memory_t *memory;  /* mem->gs_lib_ctx->memory == mem */
040b22b
@@ -61,6 +64,7 @@ typedef struct gs_lib_ctx_s
040b22b
     struct gx_io_device_s **io_device_table;
040b22b
     int io_device_table_count;
040b22b
     int io_device_table_size;
040b22b
+    client_check_file_permission_t client_check_file_permission;
040b22b
     /* Define the default value of AccurateScreens that affects setscreen
040b22b
        and setcolorscreen. */
040b22b
     bool screen_accurate_screens;
040b22b
@@ -132,6 +136,9 @@ int
040b22b
 gs_lib_ctx_get_default_device_list(const gs_memory_t *mem, char** dev_list_str,
040b22b
                         int *list_str_len);
040b22b
 
040b22b
+int
040b22b
+gs_check_file_permission (gs_memory_t *mem, const char *fname, const int len, const char *permission);
040b22b
+
040b22b
 #define IS_LIBCTX_STDOUT(mem, f) (f == mem->gs_lib_ctx->fstdout)
040b22b
 #define IS_LIBCTX_STDERR(mem, f) (f == mem->gs_lib_ctx->fstderr)
040b22b
 
040b22b
diff --git a/psi/imain.c b/psi/imain.c
040b22b
index 9a9bb5d..6874128 100644
040b22b
--- a/psi/imain.c
040b22b
+++ b/psi/imain.c
040b22b
@@ -57,6 +57,7 @@
040b22b
 #include "ivmspace.h"
040b22b
 #include "idisp.h"              /* for setting display device callback */
040b22b
 #include "iplugin.h"
040b22b
+#include "zfile.h"
040b22b
 
040b22b
 #ifdef PACIFY_VALGRIND
040b22b
 #include "valgrind.h"
040b22b
@@ -212,6 +213,7 @@ gs_main_init1(gs_main_instance * minst)
040b22b
                                            "the_gs_name_table");
040b22b
             if (code < 0)
040b22b
                 return code;
040b22b
+            mem->gs_lib_ctx->client_check_file_permission = z_check_file_permissions;
040b22b
         }
040b22b
         code = obj_init(&minst->i_ctx_p, &idmem);  /* requires name_init */
040b22b
         if (code < 0)
040b22b
diff --git a/psi/int.mak b/psi/int.mak
040b22b
index 4654afc..bb30d51 100644
040b22b
--- a/psi/int.mak
040b22b
+++ b/psi/int.mak
040b22b
@@ -2024,7 +2024,7 @@ $(PSOBJ)imain.$(OBJ) : $(PSSRC)imain.c $(GH) $(memory__h) $(string__h)\
040b22b
  $(ialloc_h) $(iconf_h) $(idebug_h) $(idict_h) $(idisp_h) $(iinit_h)\
040b22b
  $(iname_h) $(interp_h) $(iplugin_h) $(isave_h) $(iscan_h) $(ivmspace_h)\
040b22b
  $(iinit_h) $(main_h) $(oper_h) $(ostack_h)\
040b22b
- $(sfilter_h) $(store_h) $(stream_h) $(strimpl_h)\
040b22b
+ $(sfilter_h) $(store_h) $(stream_h) $(strimpl_h) $(zfile_h)\
040b22b
  $(INT_MAK) $(MAKEDIRS)
040b22b
 	$(PSCC) $(PSO_)imain.$(OBJ) $(C_) $(PSSRC)imain.c
040b22b
 
040b22b
diff --git a/psi/zfile.c b/psi/zfile.c
040b22b
index b6caea2..fd94f67 100644
040b22b
--- a/psi/zfile.c
040b22b
+++ b/psi/zfile.c
040b22b
@@ -197,6 +197,25 @@ check_file_permissions(i_ctx_t *i_ctx_p, const char *fname, int len,
040b22b
     return check_file_permissions_reduced(i_ctx_p, fname_reduced, rlen, permitgroup);
040b22b
 }
040b22b
 
040b22b
+/* z_check_file_permissions: see zfile.h for explanation
040b22b
+ */
040b22b
+int
040b22b
+z_check_file_permissions(gs_memory_t *mem, const char *fname, const int len, const char *permission)
040b22b
+{
040b22b
+    i_ctx_t *i_ctx_p = get_minst_from_memory(mem)->i_ctx_p;
040b22b
+    gs_parsed_file_name_t pname;
040b22b
+    const char *permitgroup = permission[0] == 'r' ? "PermitFileReading" : "PermitFileWriting";
040b22b
+    int code = gs_parse_file_name(&pname, fname, len, imemory);
040b22b
+    if (code < 0)
040b22b
+        return code;
040b22b
+
040b22b
+    if (pname.iodev && i_ctx_p->LockFilePermissions && strcmp(pname.iodev->dname, "%pipe%") == 0)
040b22b
+        return gs_error_invalidfileaccess;
040b22b
+        
040b22b
+    code = check_file_permissions(i_ctx_p, fname, len, permitgroup);
040b22b
+    return code;
040b22b
+}
040b22b
+
040b22b
 /* <name_string> <access_string> file <file> */
040b22b
 int                             /* exported for zsysvm.c */
040b22b
 zfile(i_ctx_t *i_ctx_p)
040b22b
diff --git a/psi/zfile.h b/psi/zfile.h
040b22b
index fdf1373..a9399c7 100644
040b22b
--- a/psi/zfile.h
040b22b
+++ b/psi/zfile.h
040b22b
@@ -22,4 +22,11 @@
040b22b
 int zopen_file(i_ctx_t *i_ctx_p, const gs_parsed_file_name_t *pfn,
040b22b
            const char *file_access, stream **ps, gs_memory_t *mem);
040b22b
 
040b22b
+/* z_check_file_permissions: a callback (via mem->gs_lib_ctx->client_check_file_permission)
040b22b
+ * to allow applying the above permissions checks when opening file(s) from
040b22b
+ * the graphics library
040b22b
+ */
040b22b
+int
040b22b
+z_check_file_permissions(gs_memory_t *mem, const char *fname,
040b22b
+                                 const int len, const char *permission);
040b22b
 #endif
040b22b
-- 
040b22b
2.7.4
040b22b