1*4882a593SmuzhiyunFrom a9bd3dec9fde03327a4a2c69dad1036bf9632e20 Mon Sep 17 00:00:00 2001 2*4882a593SmuzhiyunFrom: Chris Liddell <chris.liddell@artifex.com> 3*4882a593SmuzhiyunDate: Tue, 7 Sep 2021 20:36:12 +0100 4*4882a593SmuzhiyunSubject: [PATCH] Bug 704342: Include device specifier strings in access 5*4882a593Smuzhiyun validation 6*4882a593Smuzhiyun 7*4882a593Smuzhiyunfor the "%pipe%", %handle%" and %printer% io devices. 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunWe previously validated only the part after the "%pipe%" Postscript device 10*4882a593Smuzhiyunspecifier, but this proved insufficient. 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunThis rebuilds the original file name string, and validates it complete. The 13*4882a593Smuzhiyunslight complication for "%pipe%" is it can be reached implicitly using 14*4882a593Smuzhiyun"|" so we have to check both prefixes. 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunAddresses CVE-2021-3781 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunSigned-off-by: Peter Korsgaard <peter@korsgaard.com> 19*4882a593Smuzhiyun--- 20*4882a593Smuzhiyun base/gdevpipe.c | 22 +++++++++++++++- 21*4882a593Smuzhiyun base/gp_mshdl.c | 11 +++++++- 22*4882a593Smuzhiyun base/gp_msprn.c | 10 ++++++- 23*4882a593Smuzhiyun base/gp_os2pr.c | 13 +++++++++- 24*4882a593Smuzhiyun base/gslibctx.c | 69 ++++++++++--------------------------------------- 25*4882a593Smuzhiyun 5 files changed, 65 insertions(+), 60 deletions(-) 26*4882a593Smuzhiyun 27*4882a593Smuzhiyundiff --git a/base/gdevpipe.c b/base/gdevpipe.c 28*4882a593Smuzhiyunindex 96d71f5d8..5bdc485be 100644 29*4882a593Smuzhiyun--- a/base/gdevpipe.c 30*4882a593Smuzhiyun+++ b/base/gdevpipe.c 31*4882a593Smuzhiyun@@ -72,8 +72,28 @@ pipe_fopen(gx_io_device * iodev, const char *fname, const char *access, 32*4882a593Smuzhiyun #else 33*4882a593Smuzhiyun gs_lib_ctx_t *ctx = mem->gs_lib_ctx; 34*4882a593Smuzhiyun gs_fs_list_t *fs = ctx->core->fs; 35*4882a593Smuzhiyun+ /* The pipe device can be reached in two ways, explicltly with %pipe% 36*4882a593Smuzhiyun+ or implicitly with "|", so we have to check for both 37*4882a593Smuzhiyun+ */ 38*4882a593Smuzhiyun+ char f[gp_file_name_sizeof]; 39*4882a593Smuzhiyun+ const char *pipestr = "|"; 40*4882a593Smuzhiyun+ const size_t pipestrlen = strlen(pipestr); 41*4882a593Smuzhiyun+ const size_t preflen = strlen(iodev->dname); 42*4882a593Smuzhiyun+ const size_t nlen = strlen(fname); 43*4882a593Smuzhiyun+ int code1; 44*4882a593Smuzhiyun+ 45*4882a593Smuzhiyun+ if (preflen + nlen >= gp_file_name_sizeof) 46*4882a593Smuzhiyun+ return_error(gs_error_invalidaccess); 47*4882a593Smuzhiyun+ 48*4882a593Smuzhiyun+ memcpy(f, iodev->dname, preflen); 49*4882a593Smuzhiyun+ memcpy(f + preflen, fname, nlen + 1); 50*4882a593Smuzhiyun+ 51*4882a593Smuzhiyun+ code1 = gp_validate_path(mem, f, access); 52*4882a593Smuzhiyun+ 53*4882a593Smuzhiyun+ memcpy(f, pipestr, pipestrlen); 54*4882a593Smuzhiyun+ memcpy(f + pipestrlen, fname, nlen + 1); 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun- if (gp_validate_path(mem, fname, access) != 0) 57*4882a593Smuzhiyun+ if (code1 != 0 && gp_validate_path(mem, f, access) != 0 ) 58*4882a593Smuzhiyun return gs_error_invalidfileaccess; 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun /* 61*4882a593Smuzhiyundiff --git a/base/gp_mshdl.c b/base/gp_mshdl.c 62*4882a593Smuzhiyunindex 2b964ed74..8d87ceadc 100644 63*4882a593Smuzhiyun--- a/base/gp_mshdl.c 64*4882a593Smuzhiyun+++ b/base/gp_mshdl.c 65*4882a593Smuzhiyun@@ -95,8 +95,17 @@ mswin_handle_fopen(gx_io_device * iodev, const char *fname, const char *access, 66*4882a593Smuzhiyun long hfile; /* Correct for Win32, may be wrong for Win64 */ 67*4882a593Smuzhiyun gs_lib_ctx_t *ctx = mem->gs_lib_ctx; 68*4882a593Smuzhiyun gs_fs_list_t *fs = ctx->core->fs; 69*4882a593Smuzhiyun+ char f[gp_file_name_sizeof]; 70*4882a593Smuzhiyun+ const size_t preflen = strlen(iodev->dname); 71*4882a593Smuzhiyun+ const size_t nlen = strlen(fname); 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun- if (gp_validate_path(mem, fname, access) != 0) 74*4882a593Smuzhiyun+ if (preflen + nlen >= gp_file_name_sizeof) 75*4882a593Smuzhiyun+ return_error(gs_error_invalidaccess); 76*4882a593Smuzhiyun+ 77*4882a593Smuzhiyun+ memcpy(f, iodev->dname, preflen); 78*4882a593Smuzhiyun+ memcpy(f + preflen, fname, nlen + 1); 79*4882a593Smuzhiyun+ 80*4882a593Smuzhiyun+ if (gp_validate_path(mem, f, access) != 0) 81*4882a593Smuzhiyun return gs_error_invalidfileaccess; 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun /* First we try the open_handle method. */ 84*4882a593Smuzhiyundiff --git a/base/gp_msprn.c b/base/gp_msprn.c 85*4882a593Smuzhiyunindex ed4827968..746a974f7 100644 86*4882a593Smuzhiyun--- a/base/gp_msprn.c 87*4882a593Smuzhiyun+++ b/base/gp_msprn.c 88*4882a593Smuzhiyun@@ -168,8 +168,16 @@ mswin_printer_fopen(gx_io_device * iodev, const char *fname, const char *access, 89*4882a593Smuzhiyun uintptr_t *ptid = &((tid_t *)(iodev->state))->tid; 90*4882a593Smuzhiyun gs_lib_ctx_t *ctx = mem->gs_lib_ctx; 91*4882a593Smuzhiyun gs_fs_list_t *fs = ctx->core->fs; 92*4882a593Smuzhiyun+ const size_t preflen = strlen(iodev->dname); 93*4882a593Smuzhiyun+ const size_t nlen = strlen(fname); 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun- if (gp_validate_path(mem, fname, access) != 0) 96*4882a593Smuzhiyun+ if (preflen + nlen >= gp_file_name_sizeof) 97*4882a593Smuzhiyun+ return_error(gs_error_invalidaccess); 98*4882a593Smuzhiyun+ 99*4882a593Smuzhiyun+ memcpy(pname, iodev->dname, preflen); 100*4882a593Smuzhiyun+ memcpy(pname + preflen, fname, nlen + 1); 101*4882a593Smuzhiyun+ 102*4882a593Smuzhiyun+ if (gp_validate_path(mem, pname, access) != 0) 103*4882a593Smuzhiyun return gs_error_invalidfileaccess; 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun /* First we try the open_printer method. */ 106*4882a593Smuzhiyundiff --git a/base/gp_os2pr.c b/base/gp_os2pr.c 107*4882a593Smuzhiyunindex f852c71fc..ba54cde66 100644 108*4882a593Smuzhiyun--- a/base/gp_os2pr.c 109*4882a593Smuzhiyun+++ b/base/gp_os2pr.c 110*4882a593Smuzhiyun@@ -107,9 +107,20 @@ os2_printer_fopen(gx_io_device * iodev, const char *fname, const char *access, 111*4882a593Smuzhiyun FILE ** pfile, char *rfname, uint rnamelen) 112*4882a593Smuzhiyun { 113*4882a593Smuzhiyun os2_printer_t *pr = (os2_printer_t *)iodev->state; 114*4882a593Smuzhiyun- char driver_name[256]; 115*4882a593Smuzhiyun+ char driver_name[gp_file_name_sizeof]; 116*4882a593Smuzhiyun gs_lib_ctx_t *ctx = mem->gs_lib_ctx; 117*4882a593Smuzhiyun gs_fs_list_t *fs = ctx->core->fs; 118*4882a593Smuzhiyun+ const size_t preflen = strlen(iodev->dname); 119*4882a593Smuzhiyun+ const int size_t = strlen(fname); 120*4882a593Smuzhiyun+ 121*4882a593Smuzhiyun+ if (preflen + nlen >= gp_file_name_sizeof) 122*4882a593Smuzhiyun+ return_error(gs_error_invalidaccess); 123*4882a593Smuzhiyun+ 124*4882a593Smuzhiyun+ memcpy(driver_name, iodev->dname, preflen); 125*4882a593Smuzhiyun+ memcpy(driver_name + preflen, fname, nlen + 1); 126*4882a593Smuzhiyun+ 127*4882a593Smuzhiyun+ if (gp_validate_path(mem, driver_name, access) != 0) 128*4882a593Smuzhiyun+ return gs_error_invalidfileaccess; 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun /* First we try the open_printer method. */ 131*4882a593Smuzhiyun /* Note that the loop condition here ensures we don't 132*4882a593Smuzhiyundiff --git a/base/gslibctx.c b/base/gslibctx.c 133*4882a593Smuzhiyunindex 6dfed6cd5..318039fad 100644 134*4882a593Smuzhiyun--- a/base/gslibctx.c 135*4882a593Smuzhiyun+++ b/base/gslibctx.c 136*4882a593Smuzhiyun@@ -655,82 +655,39 @@ rewrite_percent_specifiers(char *s) 137*4882a593Smuzhiyun int 138*4882a593Smuzhiyun gs_add_outputfile_control_path(gs_memory_t *mem, const char *fname) 139*4882a593Smuzhiyun { 140*4882a593Smuzhiyun- char *fp, f[gp_file_name_sizeof]; 141*4882a593Smuzhiyun- const int pipe = 124; /* ASCII code for '|' */ 142*4882a593Smuzhiyun- const int len = strlen(fname); 143*4882a593Smuzhiyun- int i, code; 144*4882a593Smuzhiyun+ char f[gp_file_name_sizeof]; 145*4882a593Smuzhiyun+ int code; 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun /* Be sure the string copy will fit */ 148*4882a593Smuzhiyun- if (len >= gp_file_name_sizeof) 149*4882a593Smuzhiyun+ if (strlen(fname) >= gp_file_name_sizeof) 150*4882a593Smuzhiyun return gs_error_rangecheck; 151*4882a593Smuzhiyun strcpy(f, fname); 152*4882a593Smuzhiyun- fp = f; 153*4882a593Smuzhiyun /* Try to rewrite any %d (or similar) in the string */ 154*4882a593Smuzhiyun rewrite_percent_specifiers(f); 155*4882a593Smuzhiyun- for (i = 0; i < len; i++) { 156*4882a593Smuzhiyun- if (f[i] == pipe) { 157*4882a593Smuzhiyun- fp = &f[i + 1]; 158*4882a593Smuzhiyun- /* Because we potentially have to check file permissions at two levels 159*4882a593Smuzhiyun- for the output file (gx_device_open_output_file and the low level 160*4882a593Smuzhiyun- fopen API, if we're using a pipe, we have to add both the full string, 161*4882a593Smuzhiyun- (including the '|', and just the command to which we pipe - since at 162*4882a593Smuzhiyun- the pipe_fopen(), the leading '|' has been stripped. 163*4882a593Smuzhiyun- */ 164*4882a593Smuzhiyun- code = gs_add_control_path(mem, gs_permit_file_writing, f); 165*4882a593Smuzhiyun- if (code < 0) 166*4882a593Smuzhiyun- return code; 167*4882a593Smuzhiyun- code = gs_add_control_path(mem, gs_permit_file_control, f); 168*4882a593Smuzhiyun- if (code < 0) 169*4882a593Smuzhiyun- return code; 170*4882a593Smuzhiyun- break; 171*4882a593Smuzhiyun- } 172*4882a593Smuzhiyun- if (!IS_WHITESPACE(f[i])) 173*4882a593Smuzhiyun- break; 174*4882a593Smuzhiyun- } 175*4882a593Smuzhiyun- code = gs_add_control_path(mem, gs_permit_file_control, fp); 176*4882a593Smuzhiyun+ 177*4882a593Smuzhiyun+ code = gs_add_control_path(mem, gs_permit_file_control, f); 178*4882a593Smuzhiyun if (code < 0) 179*4882a593Smuzhiyun return code; 180*4882a593Smuzhiyun- return gs_add_control_path(mem, gs_permit_file_writing, fp); 181*4882a593Smuzhiyun+ return gs_add_control_path(mem, gs_permit_file_writing, f); 182*4882a593Smuzhiyun } 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun int 185*4882a593Smuzhiyun gs_remove_outputfile_control_path(gs_memory_t *mem, const char *fname) 186*4882a593Smuzhiyun { 187*4882a593Smuzhiyun- char *fp, f[gp_file_name_sizeof]; 188*4882a593Smuzhiyun- const int pipe = 124; /* ASCII code for '|' */ 189*4882a593Smuzhiyun- const int len = strlen(fname); 190*4882a593Smuzhiyun- int i, code; 191*4882a593Smuzhiyun+ char f[gp_file_name_sizeof]; 192*4882a593Smuzhiyun+ int code; 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun /* Be sure the string copy will fit */ 195*4882a593Smuzhiyun- if (len >= gp_file_name_sizeof) 196*4882a593Smuzhiyun+ if (strlen(fname) >= gp_file_name_sizeof) 197*4882a593Smuzhiyun return gs_error_rangecheck; 198*4882a593Smuzhiyun strcpy(f, fname); 199*4882a593Smuzhiyun- fp = f; 200*4882a593Smuzhiyun /* Try to rewrite any %d (or similar) in the string */ 201*4882a593Smuzhiyun- for (i = 0; i < len; i++) { 202*4882a593Smuzhiyun- if (f[i] == pipe) { 203*4882a593Smuzhiyun- fp = &f[i + 1]; 204*4882a593Smuzhiyun- /* Because we potentially have to check file permissions at two levels 205*4882a593Smuzhiyun- for the output file (gx_device_open_output_file and the low level 206*4882a593Smuzhiyun- fopen API, if we're using a pipe, we have to add both the full string, 207*4882a593Smuzhiyun- (including the '|', and just the command to which we pipe - since at 208*4882a593Smuzhiyun- the pipe_fopen(), the leading '|' has been stripped. 209*4882a593Smuzhiyun- */ 210*4882a593Smuzhiyun- code = gs_remove_control_path(mem, gs_permit_file_writing, f); 211*4882a593Smuzhiyun- if (code < 0) 212*4882a593Smuzhiyun- return code; 213*4882a593Smuzhiyun- code = gs_remove_control_path(mem, gs_permit_file_control, f); 214*4882a593Smuzhiyun- if (code < 0) 215*4882a593Smuzhiyun- return code; 216*4882a593Smuzhiyun- break; 217*4882a593Smuzhiyun- } 218*4882a593Smuzhiyun- if (!IS_WHITESPACE(f[i])) 219*4882a593Smuzhiyun- break; 220*4882a593Smuzhiyun- } 221*4882a593Smuzhiyun- code = gs_remove_control_path(mem, gs_permit_file_control, fp); 222*4882a593Smuzhiyun+ rewrite_percent_specifiers(f); 223*4882a593Smuzhiyun+ 224*4882a593Smuzhiyun+ code = gs_remove_control_path(mem, gs_permit_file_control, f); 225*4882a593Smuzhiyun if (code < 0) 226*4882a593Smuzhiyun return code; 227*4882a593Smuzhiyun- return gs_remove_control_path(mem, gs_permit_file_writing, fp); 228*4882a593Smuzhiyun+ return gs_remove_control_path(mem, gs_permit_file_writing, f); 229*4882a593Smuzhiyun } 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun int 232*4882a593Smuzhiyun-- 233*4882a593Smuzhiyun2.20.1 234*4882a593Smuzhiyun 235