Blame SOURCES/ghostscript-9.54.0-include-pipe-handle-in-validation.patch

f226a6
diff --git a/base/gdevpipe.c b/base/gdevpipe.c
f226a6
index 96d71f5d81..752a2f1d94 100644
f226a6
--- a/base/gdevpipe.c
f226a6
+++ b/base/gdevpipe.c
f226a6
@@ -72,8 +72,28 @@ pipe_fopen(gx_io_device * iodev, const char *fname, const char *access,
f226a6
 #else
f226a6
     gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
f226a6
     gs_fs_list_t *fs = ctx->core->fs;
f226a6
+    /* The pipe device can be reached in two ways, explicltly with %pipe%
f226a6
+       or implicitly with "|", so we have to check for both
f226a6
+     */
f226a6
+    char f[gp_file_name_sizeof];
f226a6
+    const char *pipestr = "|";
f226a6
+    const size_t pipestrlen = strlen(pipestr);
f226a6
+    int code1;
f226a6
+    const size_t preflen = strlen(iodev->dname);
f226a6
+    const size_t nlen = strlen(fname);
f226a6
+
f226a6
+    if (preflen + nlen >= gp_file_name_sizeof)
f226a6
+        return_error(gs_error_invalidaccess);
f226a6
+
f226a6
+    memcpy(f, iodev->dname, preflen);
f226a6
+    memcpy(f + preflen, fname, nlen + 1);
f226a6
+
f226a6
+    code1 = gp_validate_path(mem, f, access);
f226a6
+
f226a6
+    memcpy(f, pipestr, pipestrlen);
f226a6
+    memcpy(f + pipestrlen, fname, nlen + 1);
f226a6
 
f226a6
-    if (gp_validate_path(mem, fname, access) != 0)
f226a6
+    if (code1 != 0 && gp_validate_path(mem, f, access) != 0 )
f226a6
         return gs_error_invalidfileaccess;
f226a6
 
f226a6
     /*
f226a6
diff --git a/base/gp_mshdl.c b/base/gp_mshdl.c
f226a6
index 2b964ed749..8d87ceadc0 100644
f226a6
--- a/base/gp_mshdl.c
f226a6
+++ b/base/gp_mshdl.c
f226a6
@@ -95,8 +95,17 @@ mswin_handle_fopen(gx_io_device * iodev, const char *fname, const char *access,
f226a6
     long hfile;	/* Correct for Win32, may be wrong for Win64 */
f226a6
     gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
f226a6
     gs_fs_list_t *fs = ctx->core->fs;
f226a6
+    char f[gp_file_name_sizeof];
f226a6
+    const size_t preflen = strlen(iodev->dname);
f226a6
+    const size_t nlen = strlen(fname);
f226a6
 
f226a6
-    if (gp_validate_path(mem, fname, access) != 0)
f226a6
+    if (preflen + nlen >= gp_file_name_sizeof)
f226a6
+        return_error(gs_error_invalidaccess);
f226a6
+
f226a6
+    memcpy(f, iodev->dname, preflen);
f226a6
+    memcpy(f + preflen, fname, nlen + 1);
f226a6
+
f226a6
+    if (gp_validate_path(mem, f, access) != 0)
f226a6
         return gs_error_invalidfileaccess;
f226a6
 
f226a6
     /* First we try the open_handle method. */
f226a6
diff --git a/base/gp_msprn.c b/base/gp_msprn.c
f226a6
index ed48279685..746a974f78 100644
f226a6
--- a/base/gp_msprn.c
f226a6
+++ b/base/gp_msprn.c
f226a6
@@ -168,8 +168,16 @@ mswin_printer_fopen(gx_io_device * iodev, const char *fname, const char *access,
f226a6
     uintptr_t *ptid = &((tid_t *)(iodev->state))->tid;
f226a6
     gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
f226a6
     gs_fs_list_t *fs = ctx->core->fs;
f226a6
+    const size_t preflen = strlen(iodev->dname);
f226a6
+    const size_t nlen = strlen(fname);
f226a6
 
f226a6
-    if (gp_validate_path(mem, fname, access) != 0)
f226a6
+    if (preflen + nlen >= gp_file_name_sizeof)
f226a6
+        return_error(gs_error_invalidaccess);
f226a6
+
f226a6
+    memcpy(pname, iodev->dname, preflen);
f226a6
+    memcpy(pname + preflen, fname, nlen + 1);
f226a6
+
f226a6
+    if (gp_validate_path(mem, pname, access) != 0)
f226a6
         return gs_error_invalidfileaccess;
f226a6
 
f226a6
     /* First we try the open_printer method. */
f226a6
diff --git a/base/gp_os2pr.c b/base/gp_os2pr.c
f226a6
index f852c71fc8..ba54cde66f 100644
f226a6
--- a/base/gp_os2pr.c
f226a6
+++ b/base/gp_os2pr.c
f226a6
@@ -107,9 +107,20 @@ os2_printer_fopen(gx_io_device * iodev, const char *fname, const char *access,
f226a6
            FILE ** pfile, char *rfname, uint rnamelen)
f226a6
 {
f226a6
     os2_printer_t *pr = (os2_printer_t *)iodev->state;
f226a6
-    char driver_name[256];
f226a6
+    char driver_name[gp_file_name_sizeof];
f226a6
     gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
f226a6
     gs_fs_list_t *fs = ctx->core->fs;
f226a6
+    const size_t preflen = strlen(iodev->dname);
f226a6
+    const int size_t = strlen(fname);
f226a6
+
f226a6
+    if (preflen + nlen >= gp_file_name_sizeof)
f226a6
+        return_error(gs_error_invalidaccess);
f226a6
+
f226a6
+    memcpy(driver_name, iodev->dname, preflen);
f226a6
+    memcpy(driver_name + preflen, fname, nlen + 1);
f226a6
+
f226a6
+    if (gp_validate_path(mem, driver_name, access) != 0)
f226a6
+        return gs_error_invalidfileaccess;
f226a6
 
f226a6
     /* First we try the open_printer method. */
f226a6
     /* Note that the loop condition here ensures we don't
f226a6
diff --git a/base/gslibctx.c b/base/gslibctx.c
f226a6
index 6dfed6cd5a..318039fad0 100644
f226a6
--- a/base/gslibctx.c
f226a6
+++ b/base/gslibctx.c
f226a6
@@ -655,82 +655,39 @@ rewrite_percent_specifiers(char *s)
f226a6
 int
f226a6
 gs_add_outputfile_control_path(gs_memory_t *mem, const char *fname)
f226a6
 {
f226a6
-    char *fp, f[gp_file_name_sizeof];
f226a6
-    const int pipe = 124; /* ASCII code for '|' */
f226a6
-    const int len = strlen(fname);
f226a6
-    int i, code;
f226a6
+    char f[gp_file_name_sizeof];
f226a6
+    int code;
f226a6
 
f226a6
     /* Be sure the string copy will fit */
f226a6
-    if (len >= gp_file_name_sizeof)
f226a6
+    if (strlen(fname) >= gp_file_name_sizeof)
f226a6
         return gs_error_rangecheck;
f226a6
     strcpy(f, fname);
f226a6
-    fp = f;
f226a6
     /* Try to rewrite any %d (or similar) in the string */
f226a6
     rewrite_percent_specifiers(f);
f226a6
-    for (i = 0; i < len; i++) {
f226a6
-        if (f[i] == pipe) {
f226a6
-           fp = &f[i + 1];
f226a6
-           /* Because we potentially have to check file permissions at two levels
f226a6
-              for the output file (gx_device_open_output_file and the low level
f226a6
-              fopen API, if we're using a pipe, we have to add both the full string,
f226a6
-              (including the '|', and just the command to which we pipe - since at
f226a6
-              the pipe_fopen(), the leading '|' has been stripped.
f226a6
-            */
f226a6
-           code = gs_add_control_path(mem, gs_permit_file_writing, f);
f226a6
-           if (code < 0)
f226a6
-               return code;
f226a6
-           code = gs_add_control_path(mem, gs_permit_file_control, f);
f226a6
-           if (code < 0)
f226a6
-               return code;
f226a6
-           break;
f226a6
-        }
f226a6
-        if (!IS_WHITESPACE(f[i]))
f226a6
-            break;
f226a6
-    }
f226a6
-    code = gs_add_control_path(mem, gs_permit_file_control, fp);
f226a6
+
f226a6
+    code = gs_add_control_path(mem, gs_permit_file_control, f);
f226a6
     if (code < 0)
f226a6
         return code;
f226a6
-    return gs_add_control_path(mem, gs_permit_file_writing, fp);
f226a6
+    return gs_add_control_path(mem, gs_permit_file_writing, f);
f226a6
 }
f226a6
 
f226a6
 int
f226a6
 gs_remove_outputfile_control_path(gs_memory_t *mem, const char *fname)
f226a6
 {
f226a6
-    char *fp, f[gp_file_name_sizeof];
f226a6
-    const int pipe = 124; /* ASCII code for '|' */
f226a6
-    const int len = strlen(fname);
f226a6
-    int i, code;
f226a6
+    char f[gp_file_name_sizeof];
f226a6
+    int code;
f226a6
 
f226a6
     /* Be sure the string copy will fit */
f226a6
-    if (len >= gp_file_name_sizeof)
f226a6
+    if (strlen(fname) >= gp_file_name_sizeof)
f226a6
         return gs_error_rangecheck;
f226a6
     strcpy(f, fname);
f226a6
-    fp = f;
f226a6
     /* Try to rewrite any %d (or similar) in the string */
f226a6
-    for (i = 0; i < len; i++) {
f226a6
-        if (f[i] == pipe) {
f226a6
-           fp = &f[i + 1];
f226a6
-           /* Because we potentially have to check file permissions at two levels
f226a6
-              for the output file (gx_device_open_output_file and the low level
f226a6
-              fopen API, if we're using a pipe, we have to add both the full string,
f226a6
-              (including the '|', and just the command to which we pipe - since at
f226a6
-              the pipe_fopen(), the leading '|' has been stripped.
f226a6
-            */
f226a6
-           code = gs_remove_control_path(mem, gs_permit_file_writing, f);
f226a6
-           if (code < 0)
f226a6
-               return code;
f226a6
-           code = gs_remove_control_path(mem, gs_permit_file_control, f);
f226a6
-           if (code < 0)
f226a6
-               return code;
f226a6
-           break;
f226a6
-        }
f226a6
-        if (!IS_WHITESPACE(f[i]))
f226a6
-            break;
f226a6
-    }
f226a6
-    code = gs_remove_control_path(mem, gs_permit_file_control, fp);
f226a6
+    rewrite_percent_specifiers(f);
f226a6
+
f226a6
+    code = gs_remove_control_path(mem, gs_permit_file_control, f);
f226a6
     if (code < 0)
f226a6
         return code;
f226a6
-    return gs_remove_control_path(mem, gs_permit_file_writing, fp);
f226a6
+    return gs_remove_control_path(mem, gs_permit_file_writing, f);
f226a6
 }
f226a6
 
f226a6
 int
f226a6
-- 
f226a6
2.17.1
f226a6