/* Copyright (C) 2001-2023 Artifex Software, Inc. All Rights Reserved. This software is provided AS-IS with no warranty, either express or implied. This software is distributed under license and may not be copied, modified or distributed except as expressly authorized under the terms of the license contained in the file LICENSE in this distribution. Refer to licensing information at http://www.artifex.com or contact Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, CA 94129, USA, for further information. */ /* Non-I/O file operators */ #include "memory_.h" #include "string_.h" #include "unistd_.h" #include "stat_.h" /* get system header early to avoid name clash on Cygwin */ #include "ghost.h" #include "gscdefs.h" /* for gx_io_device_table */ #include "gsutil.h" /* for bytes_compare */ #include "gp.h" #include "gpmisc.h" #include "gsfname.h" #include "gsstruct.h" /* for registering root */ #include "gxalloc.h" /* for streams */ #include "oper.h" #include "dstack.h" /* for systemdict */ #include "estack.h" /* for filenameforall, .execfile */ #include "ialloc.h" #include "ilevel.h" /* %names only work in Level 2 */ #include "iname.h" #include "isave.h" /* for restore */ #include "idict.h" #include "iddict.h" #include "iutil.h" #include "stream.h" #include "strimpl.h" #include "sfilter.h" #include "gxiodev.h" /* must come after stream.h */ /* and before files.h */ #include "files.h" #include "main.h" /* for gs_lib_paths */ #include "store.h" #include "zfile.h" /* Import the IODevice table. */ extern_gx_io_device_table(); /* Import the dtype of the stdio IODevices. */ extern const char iodev_dtype_stdio[]; /* Forward references: file name parsing. */ static int parse_file_name(const ref * op, gs_parsed_file_name_t * pfn, bool safemode, gs_memory_t *memory); static int parse_real_file_name(const ref * op, gs_parsed_file_name_t * pfn, gs_memory_t *mem, client_name_t cname); static int parse_file_access_string(const ref *op, char file_access[4]); /* Forward references: other. */ static int execfile_finish(i_ctx_t *); static int execfile_cleanup(i_ctx_t *); static iodev_proc_open_file(iodev_os_open_file); stream_proc_report_error(filter_report_error); /* * Since there can be many file objects referring to the same file/stream, * we can't simply free a stream when we close it. On the other hand, * we don't want freed streams to clutter up memory needlessly. * Our solution is to retain the freed streams, and reuse them. * To prevent an old file object from being able to access a reused stream, * we keep a serial number in each stream, and check it against a serial * number stored in the file object (as the "size"); when we close a file, * we increment its serial number. If the serial number ever overflows, * we leave it at zero, and do not reuse the stream. * (This will never happen.) * * Storage management for this scheme is a little tricky. We maintain an * invariant that says that a stream opened at a given save level always * uses a stream structure allocated at that level. By doing this, we don't * need to keep track separately of streams open at a level vs. streams * allocated at a level. To make this interact properly with save and * restore, we maintain a list of all streams allocated at this level, both * open and closed. We store this list in the allocator: this is a hack, * but it simplifies bookkeeping (in particular, it guarantees the list is * restored properly by a restore). * * We want to close streams freed by restore and by garbage collection. We * use the finalization procedure for this. For restore, we don't have to * do anything special to make this happen. For garbage collection, we do * something more drastic: we simply clear the list of known streams (at all * save levels). Any streams open at the time of garbage collection will no * longer participate in the list of known streams, but this does no harm; * it simply means that they won't get reused, and can only be reclaimed by * a future garbage collection or restore. */ /* * Define the default stream buffer sizes. For file streams, * this is arbitrary, since the C library or operating system * does its own buffering in addition. * However, a buffer size of at least 2K bytes is necessary to prevent * JPEG decompression from running very slow. When less than 2K, an * intermediate filter is installed that transfers 1 byte at a time * causing many aborted roundtrips through the JPEG filter code. */ #define DEFAULT_BUFFER_SIZE 2048 extern const uint file_default_buffer_size; /* Make an invalid file object. */ void make_invalid_file(i_ctx_t *i_ctx_p, ref * fp) { make_file(fp, avm_invalid_file_entry, ~0, i_ctx_p->invalid_file_stream); } /* Check a file name for permission by stringmatch on one of the */ /* strings of the permitgroup array. */ static int check_file_permissions_reduced(i_ctx_t *i_ctx_p, const char *fname, int len, gx_io_device *iodev, const char *permitgroup) { long i; ref *permitlist = NULL; /* an empty string (first character == 0) if '\' character is */ /* recognized as a file name separator as on DOS & Windows */ const char *win_sep2 = "\\"; bool use_windows_pathsep = (gs_file_name_check_separator(win_sep2, 1, win_sep2) == 1); uint plen = gp_file_name_parents(fname, len); /* we're protecting arbitrary file system accesses, not Postscript device accesses. * Although, note that %pipe% is explicitly checked for and disallowed elsewhere */ if (iodev && iodev != iodev_default(imemory)) { return 0; } /* Assuming a reduced file name. */ if (dict_find_string(&(i_ctx_p->userparams), permitgroup, &permitlist) <= 0) return 0; /* if Permissions not found, just allow access */ for (i=0; i 0 && gp_file_name_is_absolute(fname, len)) continue; /* * If the permission starts with "./", relative paths * with no "./" are allowed as well as with "./". * 'fname' has no "./" because it is reduced. */ if (string_match( (const unsigned char*) fname, len, permstr + cwd_len, permlen - cwd_len, use_windows_pathsep ? &win_filename_params : NULL)) return 0; /* success */ } /* not found */ return gs_error_invalidfileaccess; } /* Check a file name for permission by stringmatch on one of the */ /* strings of the permitgroup array */ static int check_file_permissions(i_ctx_t *i_ctx_p, const char *fname, int len, gx_io_device *iodev, const char *permitgroup) { char fname_reduced[gp_file_name_sizeof]; uint rlen = sizeof(fname_reduced); if (gp_file_name_reduce(fname, len, fname_reduced, &rlen) != gp_combine_success) return gs_error_invalidaccess; /* fail if we couldn't reduce */ return check_file_permissions_reduced(i_ctx_p, fname_reduced, rlen, iodev, permitgroup); } /* z_check_file_permissions: see zfile.h for explanation */ int z_check_file_permissions(gs_memory_t *mem, const char *fname, const int len, const char *permission) { i_ctx_t *i_ctx_p = get_minst_from_memory(mem)->i_ctx_p; gs_parsed_file_name_t pname; const char *permitgroup = permission[0] == 'r' ? "PermitFileReading" : "PermitFileWriting"; int code = gs_parse_file_name(&pname, fname, len, imemory); if (code < 0) return code; if (pname.iodev && i_ctx_p->LockFilePermissions && strcmp(pname.iodev->dname, "%pipe%") == 0) { code = gs_note_error(gs_error_invalidfileaccess); } else { code = check_file_permissions(i_ctx_p, pname.fname, pname.len, pname.iodev, permitgroup); } return code; } /* file */ int /* exported for zsysvm.c */ zfile(i_ctx_t *i_ctx_p) { os_ptr op = osp; char file_access[4]; gs_parsed_file_name_t pname; int code = parse_file_access_string(op, file_access); stream *s; if (code < 0) return code; code = parse_file_name(op-1, &pname, i_ctx_p->LockFilePermissions, imemory); if (code < 0) return code; /* * HACK: temporarily patch the current context pointer into the * state pointer for stdio-related devices. See ziodev.c for * more information. */ if (pname.iodev && pname.iodev->dtype == iodev_dtype_stdio) { bool statement = (strcmp(pname.iodev->dname, "%statementedit%") == 0); bool lineedit = (strcmp(pname.iodev->dname, "%lineedit%") == 0); if (pname.fname) return_error(gs_error_invalidfileaccess); if (statement || lineedit) { /* These need special code to support callouts */ gx_io_device *indev = gs_findiodevice(imemory, (const byte *)"%stdin", 6); stream *ins; if (strcmp(file_access, "r")) return_error(gs_error_invalidfileaccess); indev->state = i_ctx_p; code = (indev->procs.open_device)(indev, file_access, &ins, imemory); indev->state = 0; if (code < 0) return code; check_ostack(2); push(2); make_stream_file(op - 3, ins, file_access); make_bool(op-2, statement); make_int(op-1, 0); make_string(op, icurrent_space, 0, NULL); return zfilelineedit(i_ctx_p); } pname.iodev->state = i_ctx_p; code = (*pname.iodev->procs.open_device)(pname.iodev, file_access, &s, imemory); pname.iodev->state = NULL; } else { if (pname.iodev == NULL) pname.iodev = iodev_default(imemory); code = zopen_file(i_ctx_p, &pname, file_access, &s, imemory); } if (code < 0) return code; if (s == NULL) return_error(gs_error_undefinedfilename); code = ssetfilename(s, op[-1].value.const_bytes, r_size(op - 1)); if (code < 0) { sclose(s); return_error(gs_error_VMerror); } make_stream_file(op - 1, s, file_access); pop(1); return code; } /* * Files created with .tempfile permit some operations even if the * temp directory is not explicitly named on the PermitFile... path * The names 'SAFETY' and 'tempfiles' are defined by gs_init.ps */ static bool file_is_tempfile(i_ctx_t *i_ctx_p, const uchar *fname, int len) { ref *SAFETY; ref *tempfiles; ref kname; if (dict_find_string(systemdict, "SAFETY", &SAFETY) <= 0 || dict_find_string(SAFETY, "tempfiles", &tempfiles) <= 0) return false; if (name_ref(imemory, fname, len, &kname, -1) < 0 || dict_find(tempfiles, &kname, &SAFETY) <= 0) return false; return true; } static int record_file_is_tempfile(i_ctx_t *i_ctx_p, const uchar *fname, int len, bool add) { ref *SAFETY; ref *tempfiles; ref kname, bref; int code = 0; if (dict_find_string(systemdict, "SAFETY", &SAFETY) <= 0 || dict_find_string(SAFETY, "tempfiles", &tempfiles) <= 0) { return 0; } if ((code = name_ref(imemory, fname, len, &kname, 1)) < 0) { return code; } make_bool(&bref, true); if (add) return idict_put(tempfiles, &kname, &bref); else return idict_undef(tempfiles, &kname); } /* ------ Level 2 extensions ------ */ /* deletefile - */ static int zdeletefile(i_ctx_t *i_ctx_p) { os_ptr op = osp; gs_parsed_file_name_t pname; int code = parse_real_file_name(op, &pname, imemory, "deletefile"); bool is_temp = false; if (code < 0) return code; if (pname.iodev == iodev_default(imemory)) { if ((code = check_file_permissions(i_ctx_p, pname.fname, pname.len, pname.iodev, "PermitFileControl")) < 0 && !(is_temp = file_is_tempfile(i_ctx_p, op->value.bytes, r_size(op)))) { return code; } } code = (*pname.iodev->procs.delete_file)(pname.iodev, pname.fname); if (code >= 0 && is_temp) code = record_file_is_tempfile(i_ctx_p, (unsigned char *)pname.fname, strlen(pname.fname), false); gs_free_file_name(&pname, "deletefile"); if (code < 0) return code; pop(1); return 0; } /*