| 1 | From 505eab7782b429017eb434b2b95120855f2b0e3c Mon Sep 17 00:00:00 2001
|
| 2 | From: Chris Liddell <[email protected]>
|
| 3 | Date: Wed, 7 Jun 2023 10:23:06 +0100
|
| 4 | Subject: [PATCH] Bug 706761: Don't "reduce" %pipe% file names for permission
|
| 5 | validation
|
| 6 |
|
| 7 | For regular file names, we try to simplfy relative paths before we use them.
|
| 8 |
|
| 9 | Because the %pipe% device can, effectively, accept command line calls, we
|
| 10 | shouldn't be simplifying that string, because the command line syntax can end
|
| 11 | up confusing the path simplifying code. That can result in permitting a pipe
|
| 12 | command which does not match what was originally permitted.
|
| 13 |
|
| 14 | Special case "%pipe" in the validation code so we always deal with the entire
|
| 15 | string.
|
| 16 | ---
|
| 17 | base/gpmisc.c | 31 +++++++++++++++++++--------
|
| 18 | base/gslibctx.c | 56 ++++++++++++++++++++++++++++++++++++-------------
|
| 19 | 2 files changed, 64 insertions(+), 23 deletions(-)
|
| 20 |
|
| 21 | diff --git a/base/gpmisc.c b/base/gpmisc.c
|
| 22 | index 5f39ebba7..2fb87f769 100644
|
| 23 | |
| 24 | |
| 25 | @@ -1076,16 +1076,29 @@ gp_validate_path_len(const gs_memory_t *mem,
|
| 26 | && !memcmp(path + cdirstrl, dirsepstr, dirsepstrl)) {
|
| 27 | prefix_len = 0;
|
| 28 | }
|
| 29 | - rlen = len+1;
|
| 30 | - bufferfull = (char *)gs_alloc_bytes(mem->thread_safe_memory, rlen + prefix_len, "gp_validate_path");
|
| 31 | - if (bufferfull == NULL)
|
| 32 | - return gs_error_VMerror;
|
| 33 | -
|
| 34 | - buffer = bufferfull + prefix_len;
|
| 35 | - if (gp_file_name_reduce(path, (uint)len, buffer, &rlen) != gp_combine_success)
|
| 36 | - return gs_error_invalidfileaccess;
|
| 37 | - buffer[rlen] = 0;
|
| 38 |
|
| 39 | + /* "%pipe%" do not follow the normal rules for path definitions, so we
|
| 40 | + don't "reduce" them to avoid unexpected results
|
| 41 | + */
|
| 42 | + if (len > 5 && memcmp(path, "%pipe", 5) != 0) {
|
| 43 | + bufferfull = buffer = (char *)gs_alloc_bytes(mem->thread_safe_memory, len + 1, "gp_validate_path");
|
| 44 | + if (buffer == NULL)
|
| 45 | + return gs_error_VMerror;
|
| 46 | + memcpy(buffer, path, len);
|
| 47 | + buffer[len] = 0;
|
| 48 | + rlen = len;
|
| 49 | + }
|
| 50 | + else {
|
| 51 | + rlen = len+1;
|
| 52 | + bufferfull = (char *)gs_alloc_bytes(mem->thread_safe_memory, rlen + prefix_len, "gp_validate_path");
|
| 53 | + if (bufferfull == NULL)
|
| 54 | + return gs_error_VMerror;
|
| 55 | +
|
| 56 | + buffer = bufferfull + prefix_len;
|
| 57 | + if (gp_file_name_reduce(path, (uint)len, buffer, &rlen) != gp_combine_success)
|
| 58 | + return gs_error_invalidfileaccess;
|
| 59 | + buffer[rlen] = 0;
|
| 60 | + }
|
| 61 | while (1) {
|
| 62 | switch (mode[0])
|
| 63 | {
|
| 64 | diff --git a/base/gslibctx.c b/base/gslibctx.c
|
| 65 | index eb566ed06..d2a1aa91d 100644
|
| 66 | |
| 67 | |
| 68 | @@ -740,14 +740,28 @@ gs_add_control_path_len_flags(const gs_memory_t *mem, gs_path_control_t type, co
|
| 69 | return gs_error_rangecheck;
|
| 70 | }
|
| 71 |
|
| 72 | - rlen = len+1;
|
| 73 | - buffer = (char *)gs_alloc_bytes(core->memory, rlen, "gp_validate_path");
|
| 74 | - if (buffer == NULL)
|
| 75 | - return gs_error_VMerror;
|
| 76 | + /* "%pipe%" do not follow the normal rules for path definitions, so we
|
| 77 | + don't "reduce" them to avoid unexpected results
|
| 78 | + */
|
| 79 | + if (len > 5 && memcmp(path, "%pipe", 5) != 0) {
|
| 80 | + buffer = (char *)gs_alloc_bytes(core->memory, len + 1, "gs_add_control_path_len");
|
| 81 | + if (buffer == NULL)
|
| 82 | + return gs_error_VMerror;
|
| 83 | + memcpy(buffer, path, len);
|
| 84 | + buffer[len] = 0;
|
| 85 | + rlen = len;
|
| 86 | + }
|
| 87 | + else {
|
| 88 | + rlen = len + 1;
|
| 89 |
|
| 90 | - if (gp_file_name_reduce(path, (uint)len, buffer, &rlen) != gp_combine_success)
|
| 91 | - return gs_error_invalidfileaccess;
|
| 92 | - buffer[rlen] = 0;
|
| 93 | + buffer = (char *)gs_alloc_bytes(core->memory, rlen, "gs_add_control_path_len");
|
| 94 | + if (buffer == NULL)
|
| 95 | + return gs_error_VMerror;
|
| 96 | +
|
| 97 | + if (gp_file_name_reduce(path, (uint)len, buffer, &rlen) != gp_combine_success)
|
| 98 | + return gs_error_invalidfileaccess;
|
| 99 | + buffer[rlen] = 0;
|
| 100 | + }
|
| 101 |
|
| 102 | n = control->num;
|
| 103 | for (i = 0; i < n; i++)
|
| 104 | @@ -833,14 +847,28 @@ gs_remove_control_path_len_flags(const gs_memory_t *mem, gs_path_control_t type,
|
| 105 | return gs_error_rangecheck;
|
| 106 | }
|
| 107 |
|
| 108 | - rlen = len+1;
|
| 109 | - buffer = (char *)gs_alloc_bytes(core->memory, rlen, "gp_validate_path");
|
| 110 | - if (buffer == NULL)
|
| 111 | - return gs_error_VMerror;
|
| 112 | + /* "%pipe%" do not follow the normal rules for path definitions, so we
|
| 113 | + don't "reduce" them to avoid unexpected results
|
| 114 | + */
|
| 115 | + if (len > 5 && memcmp(path, "%pipe", 5) != 0) {
|
| 116 | + buffer = (char *)gs_alloc_bytes(core->memory, len + 1, "gs_remove_control_path_len");
|
| 117 | + if (buffer == NULL)
|
| 118 | + return gs_error_VMerror;
|
| 119 | + memcpy(buffer, path, len);
|
| 120 | + buffer[len] = 0;
|
| 121 | + rlen = len;
|
| 122 | + }
|
| 123 | + else {
|
| 124 | + rlen = len+1;
|
| 125 |
|
| 126 | - if (gp_file_name_reduce(path, (uint)len, buffer, &rlen) != gp_combine_success)
|
| 127 | - return gs_error_invalidfileaccess;
|
| 128 | - buffer[rlen] = 0;
|
| 129 | + buffer = (char *)gs_alloc_bytes(core->memory, rlen, "gs_remove_control_path_len");
|
| 130 | + if (buffer == NULL)
|
| 131 | + return gs_error_VMerror;
|
| 132 | +
|
| 133 | + if (gp_file_name_reduce(path, (uint)len, buffer, &rlen) != gp_combine_success)
|
| 134 | + return gs_error_invalidfileaccess;
|
| 135 | + buffer[rlen] = 0;
|
| 136 | + }
|
| 137 |
|
| 138 | n = control->num;
|
| 139 | for (i = 0; i < n; i++) {
|
| 140 | --
|
| 141 | 2.34.1
|
| 142 |
|