1 /*
2 +----------------------------------------------------------------------+
3 | phar php single-file executable PHP extension |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | https://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Gregory Beaver <cellog@php.net> |
16 | Marcus Boerger <helly@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <time.h>
25 #include "php.h"
26 #include "tar.h"
27 #include "pharzip.h"
28 #include "zend_hash.h"
29 #include "ext/spl/spl_directory.h"
30
31 /* PHP_ because this is public information via MINFO */
32 #define PHP_PHAR_API_VERSION "1.1.1"
33 /* x.y.z maps to 0xyz0 */
34 #define PHAR_API_VERSION 0x1110
35 /* if we bump PHAR_API_VERSION, change this from 0x1100 to PHAR_API_VERSION */
36 #define PHAR_API_VERSION_NODIR 0x1100
37 #define PHAR_API_MIN_DIR 0x1110
38 #define PHAR_API_MIN_READ 0x1000
39 #define PHAR_API_MAJORVERSION 0x1000
40 #define PHAR_API_MAJORVER_MASK 0xF000
41 #define PHAR_API_VER_MASK 0xFFF0
42
43 #define PHAR_HDR_COMPRESSION_MASK 0x0000F000
44 #define PHAR_HDR_COMPRESSED_NONE 0x00000000
45 #define PHAR_HDR_COMPRESSED_GZ 0x00001000
46 #define PHAR_HDR_COMPRESSED_BZ2 0x00002000
47 #define PHAR_HDR_SIGNATURE 0x00010000
48
49 /* flags for defining that the entire file should be compressed */
50 #define PHAR_FILE_COMPRESSION_MASK 0x00F00000
51 #define PHAR_FILE_COMPRESSED_NONE 0x00000000
52 #define PHAR_FILE_COMPRESSED_GZ 0x00100000
53 #define PHAR_FILE_COMPRESSED_BZ2 0x00200000
54
55 #define PHAR_SIG_MD5 0x0001
56 #define PHAR_SIG_SHA1 0x0002
57 #define PHAR_SIG_SHA256 0x0003
58 #define PHAR_SIG_SHA512 0x0004
59 #define PHAR_SIG_OPENSSL 0x0010
60 #define PHAR_SIG_OPENSSL_SHA256 0x0011
61 #define PHAR_SIG_OPENSSL_SHA512 0x0012
62
63 /* flags byte for each file adheres to these bitmasks.
64 All unused values are reserved */
65 #define PHAR_ENT_COMPRESSION_MASK 0x0000F000
66 #define PHAR_ENT_COMPRESSED_NONE 0x00000000
67 #define PHAR_ENT_COMPRESSED_GZ 0x00001000
68 #define PHAR_ENT_COMPRESSED_BZ2 0x00002000
69
70 #define PHAR_ENT_PERM_MASK 0x000001FF
71 #define PHAR_ENT_PERM_MASK_USR 0x000001C0
72 #define PHAR_ENT_PERM_SHIFT_USR 6
73 #define PHAR_ENT_PERM_MASK_GRP 0x00000038
74 #define PHAR_ENT_PERM_SHIFT_GRP 3
75 #define PHAR_ENT_PERM_MASK_OTH 0x00000007
76 #define PHAR_ENT_PERM_DEF_FILE 0x000001B6
77 #define PHAR_ENT_PERM_DEF_DIR 0x000001FF
78
79 #define PHAR_FORMAT_SAME 0
80 #define PHAR_FORMAT_PHAR 1
81 #define PHAR_FORMAT_TAR 2
82 #define PHAR_FORMAT_ZIP 3
83
84 #define TAR_FILE '0'
85 #define TAR_LINK '1'
86 #define TAR_SYMLINK '2'
87 #define TAR_DIR '5'
88 #define TAR_NEW '8'
89 #define TAR_GLOBAL_HDR 'g'
90 #define TAR_FILE_HDR 'x'
91
92 #define PHAR_MUNG_PHP_SELF (1<<0)
93 #define PHAR_MUNG_REQUEST_URI (1<<1)
94 #define PHAR_MUNG_SCRIPT_NAME (1<<2)
95 #define PHAR_MUNG_SCRIPT_FILENAME (1<<3)
96
97 typedef struct _phar_entry_fp phar_entry_fp;
98 typedef struct _phar_archive_data phar_archive_data;
99
100 ZEND_BEGIN_MODULE_GLOBALS(phar)
101 /* a list of phar_archive_data objects that reference a cached phar, so
102 that if copy-on-write is performed, we can swap them out for the new value */
103 HashTable phar_persist_map;
104 HashTable phar_fname_map;
105 /* for cached phars, this is a per-process store of fp/ufp */
106 phar_entry_fp *cached_fp;
107 HashTable phar_alias_map;
108 int phar_SERVER_mung_list;
109 char* cache_list;
110 bool readonly;
111 bool manifest_cached;
112 bool persist;
113 bool has_zlib;
114 bool has_bz2;
115 bool readonly_orig;
116 bool require_hash_orig;
117 bool intercepted;
118 bool request_init;
119 bool require_hash;
120 bool request_done;
121 bool request_ends;
122 zif_handler orig_fopen;
123 zif_handler orig_file_get_contents;
124 zif_handler orig_is_file;
125 zif_handler orig_is_link;
126 zif_handler orig_is_dir;
127 zif_handler orig_opendir;
128 zif_handler orig_file_exists;
129 zif_handler orig_fileperms;
130 zif_handler orig_fileinode;
131 zif_handler orig_filesize;
132 zif_handler orig_fileowner;
133 zif_handler orig_filegroup;
134 zif_handler orig_fileatime;
135 zif_handler orig_filemtime;
136 zif_handler orig_filectime;
137 zif_handler orig_filetype;
138 zif_handler orig_is_writable;
139 zif_handler orig_is_readable;
140 zif_handler orig_is_executable;
141 zif_handler orig_lstat;
142 zif_handler orig_readfile;
143 zif_handler orig_stat;
144 /* used for includes with . in them inside front controller */
145 char* cwd;
146 uint32_t cwd_len;
147 bool cwd_init;
148 char *openssl_privatekey;
149 uint32_t openssl_privatekey_len;
150 /* phar_get_archive cache */
151 char* last_phar_name;
152 uint32_t last_phar_name_len;
153 char* last_alias;
154 uint32_t last_alias_len;
155 phar_archive_data* last_phar;
156 HashTable mime_types;
157 ZEND_END_MODULE_GLOBALS(phar)
158
159 ZEND_EXTERN_MODULE_GLOBALS(phar)
160 #define PHAR_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(phar, v)
161
162 #if defined(ZTS) && defined(COMPILE_DL_PHAR)
163 ZEND_TSRMLS_CACHE_EXTERN()
164 #endif
165
166 typedef union _phar_archive_object phar_archive_object;
167 typedef union _phar_entry_object phar_entry_object;
168
169 /*
170 * used in phar_entry_info->fp_type to
171 */
172 enum phar_fp_type {
173 /* regular file pointer phar_archive_data->fp */
174 PHAR_FP,
175 /* uncompressed file pointer phar_archive_data->uncompressed_fp */
176 PHAR_UFP,
177 /* modified file pointer phar_entry_info->fp */
178 PHAR_MOD,
179 /* temporary manifest entry (file outside of the phar mapped to a location inside the phar)
180 this entry stores the stream to open in link (normally used for tars, but we steal it here) */
181 PHAR_TMP
182 };
183
184 /*
185 * Represents the metadata of the phar file or a file entry within the phar.
186 * Can contain any combination of serialized data and the value as needed.
187 */
188 typedef struct _phar_metadata_tracker {
189 /* Can be IS_UNDEF or a regular value */
190 zval val;
191 /* Nullable string with the serialized value, if the serialization was performed or read from a file. */
192 zend_string *str;
193 } phar_metadata_tracker;
194
195 /* entry for one file in a phar file */
196 typedef struct _phar_entry_info {
197 /* first bytes are exactly as in file */
198 uint32_t uncompressed_filesize;
199 uint32_t timestamp;
200 uint32_t compressed_filesize;
201 uint32_t crc32;
202 uint32_t flags;
203 /* remainder */
204 /* when changing compression, save old flags in case fp is NULL */
205 uint32_t old_flags;
206 phar_metadata_tracker metadata_tracker;
207 uint32_t filename_len;
208 char *filename;
209 enum phar_fp_type fp_type;
210 /* offset within original phar file of the file contents */
211 zend_long offset_abs;
212 /* offset within fp of the file contents */
213 zend_long offset;
214 /* offset within original phar file of the file header (for zip-based/tar-based) */
215 zend_long header_offset;
216 php_stream *fp;
217 php_stream *cfp;
218 int fp_refcount;
219 char *tmp;
220 phar_archive_data *phar;
221 char *link; /* symbolic link to another file */
222 char tar_type;
223 /* position in the manifest */
224 uint32_t manifest_pos;
225 /* for stat */
226 unsigned short inode;
227
228 uint32_t is_crc_checked:1;
229 uint32_t is_modified:1;
230 uint32_t is_deleted:1;
231 uint32_t is_dir:1;
232 /* this flag is used for mounted entries (external files mapped to location
233 inside a phar */
234 uint32_t is_mounted:1;
235 /* used when iterating */
236 uint32_t is_temp_dir:1;
237 /* tar-based phar file stuff */
238 uint32_t is_tar:1;
239 /* zip-based phar file stuff */
240 uint32_t is_zip:1;
241 /* for cached phar entries */
242 uint32_t is_persistent:1;
243 } phar_entry_info;
244
245 /* information about a phar file (the archive itself) */
246 struct _phar_archive_data {
247 char *fname;
248 uint32_t fname_len;
249 /* for phar_detect_fname_ext, this stores the location of the file extension within fname */
250 char *ext;
251 uint32_t ext_len;
252 char *alias;
253 uint32_t alias_len;
254 char version[12];
255 size_t halt_offset;
256 HashTable manifest;
257 /* hash of virtual directories, as in path/to/file.txt has path/to and path as virtual directories */
258 HashTable virtual_dirs;
259 /* hash of mounted directory paths */
260 HashTable mounted_dirs;
261 uint32_t flags;
262 uint32_t min_timestamp;
263 uint32_t max_timestamp;
264 php_stream *fp;
265 /* decompressed file contents are stored here */
266 php_stream *ufp;
267 int refcount;
268 uint32_t sig_flags;
269 uint32_t sig_len;
270 char *signature;
271 phar_metadata_tracker metadata_tracker;
272 uint32_t phar_pos;
273 /* if 1, then this alias was manually specified by the user and is not a permanent alias */
274 uint32_t is_temporary_alias:1;
275 uint32_t is_modified:1;
276 uint32_t is_writeable:1;
277 uint32_t is_brandnew:1;
278 /* defer phar creation */
279 uint32_t donotflush:1;
280 /* zip-based phar variables */
281 uint32_t is_zip:1;
282 /* tar-based phar variables */
283 uint32_t is_tar:1;
284 /* PharData variables */
285 uint32_t is_data:1;
286 /* for cached phar manifests */
287 uint32_t is_persistent:1;
288 };
289
290 typedef struct _phar_entry_fp_info {
291 enum phar_fp_type fp_type;
292 /* offset within fp of the file contents */
293 zend_long offset;
294 } phar_entry_fp_info;
295
296 struct _phar_entry_fp {
297 php_stream *fp;
298 php_stream *ufp;
299 phar_entry_fp_info *manifest;
300 };
301
phar_get_entrypfp(const phar_entry_info * entry)302 static inline php_stream *phar_get_entrypfp(const phar_entry_info *entry)
303 {
304 if (!entry->is_persistent) {
305 return entry->phar->fp;
306 }
307 return PHAR_G(cached_fp)[entry->phar->phar_pos].fp;
308 }
309
phar_get_pharfp(const phar_archive_data * phar)310 static inline php_stream *phar_get_pharfp(const phar_archive_data *phar)
311 {
312 if (!phar->is_persistent) {
313 return phar->fp;
314 }
315 return PHAR_G(cached_fp)[phar->phar_pos].fp;
316 }
317
phar_get_fp_type(const phar_entry_info * entry)318 static inline enum phar_fp_type phar_get_fp_type(const phar_entry_info *entry)
319 {
320 if (!entry->is_persistent) {
321 return entry->fp_type;
322 }
323 return PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].fp_type;
324 }
325
326 #define PHAR_MIME_PHP '\0'
327 #define PHAR_MIME_PHPS '\1'
328 #define PHAR_MIME_OTHER '\2'
329
330 typedef struct _phar_mime_type {
331 char *mime;
332 uint32_t len;
333 /* one of PHAR_MIME_* */
334 char type;
335 } phar_mime_type;
336
337 /* stream access data for one file entry in a phar file */
338 typedef struct _phar_entry_data {
339 phar_archive_data *phar;
340 php_stream *fp;
341 /* stream position proxy, allows multiple open streams referring to the same fp */
342 zend_off_t position;
343 /* for copies of the phar fp, defines where 0 is */
344 zend_off_t zero;
345 phar_entry_info *internal_file;
346 } phar_entry_data;
347
348 /* archive php object */
349 union _phar_archive_object {
350 spl_filesystem_object spl;
351 phar_archive_data *archive;
352 };
353
354 /* entry php object */
355 union _phar_entry_object {
356 spl_filesystem_object spl;
357 phar_entry_info *entry;
358 };
359
BEGIN_EXTERN_C()360 BEGIN_EXTERN_C()
361
362 #ifdef PHP_WIN32
363 static inline void phar_unixify_path_separators(char *path, size_t path_len)
364 {
365 char *s;
366
367 /* unixify win paths */
368 for (s = path; (size_t)(s - path) < path_len; ++s) {
369 if (*s == '\\') {
370 *s = '/';
371 }
372 }
373 }
374 #endif
375
phar_validate_alias(const char * alias,size_t alias_len)376 static inline bool phar_validate_alias(const char *alias, size_t alias_len) /* {{{ */
377 {
378 return !(memchr(alias, '/', alias_len) || memchr(alias, '\\', alias_len) || memchr(alias, ':', alias_len) ||
379 memchr(alias, ';', alias_len) || memchr(alias, '\n', alias_len) || memchr(alias, '\r', alias_len));
380 }
381 /* }}} */
382
phar_set_inode(phar_entry_info * entry)383 static inline void phar_set_inode(phar_entry_info *entry) /* {{{ */
384 {
385 char tmp[MAXPATHLEN];
386 size_t tmp_len;
387 size_t len1, len2;
388
389 tmp_len = MIN(MAXPATHLEN, entry->filename_len + entry->phar->fname_len);
390
391 len1 = MIN(entry->phar->fname_len, tmp_len);
392 if (entry->phar->fname) {
393 memcpy(tmp, entry->phar->fname, len1);
394 }
395
396 len2 = MIN(tmp_len - len1, entry->filename_len);
397 memcpy(tmp + len1, entry->filename, len2);
398
399 entry->inode = (unsigned short) zend_hash_func(tmp, tmp_len);
400 }
401 /* }}} */
402
403 void phar_request_initialize(void);
404
405 void phar_object_init(void);
406 void phar_destroy_phar_data(phar_archive_data *phar);
407
408 zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip);
409 zend_result phar_open_from_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, uint32_t options, phar_archive_data** pphar, char **error);
410 zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
411 zend_result phar_create_or_parse_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
412 zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **error);
413 zend_result phar_free_alias(phar_archive_data *phar, char *alias, size_t alias_len);
414 zend_result phar_get_archive(phar_archive_data **archive, char *fname, size_t fname_len, char *alias, size_t alias_len, char **error);
415 zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t sig_type, char *sig, size_t sig_len, char *fname, char **signature, size_t *signature_len, char **error);
416 zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, size_t *signature_length, char **error);
417
418 /* utility functions */
419 zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error);
420 char *phar_decompress_filter(phar_entry_info * entry, int return_unknown);
421 char *phar_compress_filter(phar_entry_info * entry, int return_unknown);
422
423 /* void phar_remove_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len); */
424 void phar_add_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len);
425 zend_result phar_mount_entry(phar_archive_data *phar, char *filename, size_t filename_len, char *path, size_t path_len);
426 zend_string *phar_find_in_include_path(zend_string *file, phar_archive_data **pphar);
427 char *phar_fix_filepath(char *path, size_t *new_len, int use_cwd);
428 phar_entry_info * phar_open_jit(phar_archive_data *phar, phar_entry_info *entry, char **error);
429 void phar_parse_metadata_lazy(const char *buffer, phar_metadata_tracker *tracker, uint32_t zip_metadata_len, bool persistent);
430 bool phar_metadata_tracker_has_data(const phar_metadata_tracker* tracker, bool persistent);
431 /* If this has data, free it and set all values to undefined. */
432 void phar_metadata_tracker_free(phar_metadata_tracker* val, bool persistent);
433 void phar_metadata_tracker_copy(phar_metadata_tracker* dest, const phar_metadata_tracker *source, bool persistent);
434 void phar_metadata_tracker_clone(phar_metadata_tracker* tracker);
435 void phar_metadata_tracker_try_ensure_has_serialized_data(phar_metadata_tracker* tracker, bool persistent);
436 zend_result phar_metadata_tracker_unserialize_or_copy(phar_metadata_tracker* tracker, zval *value, bool persistent, HashTable *unserialize_options, const char* method_name);
437 void destroy_phar_manifest_entry(zval *zv);
438 int phar_seek_efp(phar_entry_info *entry, zend_off_t offset, int whence, zend_off_t position, int follow_links);
439 php_stream *phar_get_efp(phar_entry_info *entry, int follow_links);
440 zend_result phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error);
441 zend_result phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links);
442 phar_entry_info *phar_get_link_source(phar_entry_info *entry);
443 zend_result phar_open_archive_fp(phar_archive_data *phar);
444 zend_result phar_copy_on_write(phar_archive_data **pphar);
445
446 /* tar functions in tar.c */
447 bool phar_is_tar(char *buf, char *fname);
448 zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, uint32_t compression, char **error);
449 zend_result phar_open_or_create_tar(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error);
450 void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error);
451
452 /* zip functions in zip.c */
453 int phar_parse_zipfile(php_stream *fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, char **error);
454 int phar_open_or_create_zip(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error);
455 void phar_zip_flush(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error);
456
457 #ifdef PHAR_MAIN
458 extern const php_stream_wrapper php_stream_phar_wrapper;
459 #else
460 extern HashTable cached_phars;
461 extern HashTable cached_alias;
462 #endif
463
464 bool phar_archive_delref(phar_archive_data *phar);
465 void phar_entry_delref(phar_entry_data *idata);
466
467 phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, int security);
468 phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security);
469 phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security);
470 zend_result phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security);
471 void phar_flush_ex(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error);
472 void phar_flush(phar_archive_data *archive, char **error);
473 zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, int is_complete);
474 zend_result phar_split_fname(const char *filename, size_t filename_len, char **arch, size_t *arch_len, char **entry, size_t *entry_len, int executable, int for_create);
475
476 typedef enum {
477 pcr_use_query,
478 pcr_is_ok,
479 pcr_err_double_slash,
480 pcr_err_up_dir,
481 pcr_err_curr_dir,
482 pcr_err_back_slash,
483 pcr_err_star,
484 pcr_err_illegal_char,
485 pcr_err_empty_entry
486 } phar_path_check_result;
487
488 phar_path_check_result phar_path_check(char **p, size_t *len, const char **error);
489
490 END_EXTERN_C()
491