summaryrefslogtreecommitdiff
path: root/src/snuffleupagus.c
blob: f0737b4394137e0f493d0fca6ca6a0485689b08c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifdef PHP_WIN32
#include "win32/glob.h"
#else
#include <glob.h>
#endif

#include "php_snuffleupagus.h"

#ifndef ZEND_EXT_API
#define ZEND_EXT_API ZEND_DLEXPORT
#endif

static PHP_INI_MH(OnUpdateConfiguration);
static inline void sp_op_array_handler(zend_op_array *op);

ZEND_EXTENSION();

// LCOV_EXCL_START
ZEND_DLEXPORT int sp_zend_startup(zend_extension *extension) {
  return zend_startup_module(&snuffleupagus_module_entry);
}
// LCOV_EXCL_END

static inline void sp_op_array_handler(zend_op_array *op) {
  // We need a filename, and strict mode not already enabled on this op
  if (NULL == op->filename || op->fn_flags & ZEND_ACC_STRICT_TYPES) {
    return;
  } else {
    if (true == SNUFFLEUPAGUS_G(config).config_global_strict->enable) {
      op->fn_flags |= ZEND_ACC_STRICT_TYPES;
    }
  }
}

ZEND_DECLARE_MODULE_GLOBALS(snuffleupagus)

static PHP_INI_MH(StrictMode) {
  TSRMLS_FETCH();

  SNUFFLEUPAGUS_G(allow_broken_configuration) = false;
  if (new_value && zend_string_equals_literal(new_value, "1")) {
    SNUFFLEUPAGUS_G(allow_broken_configuration) = true;
  }
  return SUCCESS;
}

PHP_INI_BEGIN()
PHP_INI_ENTRY("sp.configuration_file", "", PHP_INI_SYSTEM,
              OnUpdateConfiguration)
PHP_INI_ENTRY("sp.allow_broken_configuration", "0", PHP_INI_SYSTEM, StrictMode)
PHP_INI_END()

ZEND_DLEXPORT zend_extension zend_extension_entry = {
    PHP_SNUFFLEUPAGUS_EXTNAME,
    PHP_SNUFFLEUPAGUS_VERSION,
    PHP_SNUFFLEUPAGUS_AUTHOR,
    PHP_SNUFFLEUPAGUS_URL,
    PHP_SNUFFLEUPAGUS_COPYRIGHT,
    sp_zend_startup,
    NULL,
    NULL,                /* activate_func_t */
    NULL,                /* deactivate_func_t */
    NULL,                /* message_handler_func_t */
    sp_op_array_handler, /* op_array_handler_func_t */
    NULL,                /* statement_handler_func_t */
    NULL,                /* fcall_begin_handler_func_t */
    NULL,                /* fcall_end_handler_func_t */
    NULL,                /* op_array_ctor_func_t */
    NULL,                /* op_array_dtor_func_t */
    STANDARD_ZEND_EXTENSION_PROPERTIES};

PHP_GINIT_FUNCTION(snuffleupagus) {
  snuffleupagus_globals->is_config_valid = SP_CONFIG_NONE;
  snuffleupagus_globals->in_eval = 0;

#define SP_INIT_HT(F)                                                          \
  snuffleupagus_globals->F = pemalloc(sizeof(*(snuffleupagus_globals->F)), 1); \
  zend_hash_init(snuffleupagus_globals->F, 10, NULL, NULL, 1);
  SP_INIT_HT(disabled_functions_hook);
  SP_INIT_HT(sp_internal_functions_hook);
  SP_INIT_HT(sp_eval_blacklist_functions_hook);
  SP_INIT_HT(config.config_disabled_functions);
  SP_INIT_HT(config.config_disabled_functions_hooked);
  SP_INIT_HT(config.config_disabled_functions_ret);
  SP_INIT_HT(config.config_disabled_functions_ret_hooked);
#undef SP_INIT_HT

#define SP_INIT(F)                  \
  snuffleupagus_globals->config.F = \
      pecalloc(sizeof(*(snuffleupagus_globals->config.F)), 1, 1);
  SP_INIT(config_unserialize);
  SP_INIT(config_random);
  SP_INIT(config_sloppy);
  SP_INIT(config_readonly_exec);
  SP_INIT(config_global_strict);
  SP_INIT(config_auto_cookie_secure);
  SP_INIT(config_snuffleupagus);
  SP_INIT(config_disable_xxe);
  SP_INIT(config_upload_validation);
  SP_INIT(config_disabled_functions_reg);
  SP_INIT(config_disabled_functions_reg_ret);
  SP_INIT(config_cookie);
  SP_INIT(config_session);
  SP_INIT(config_eval);
  SP_INIT(config_wrapper);
#undef SP_INIT

#define SP_INIT_NULL(F) snuffleupagus_globals->config.F = NULL;
  SP_INIT_NULL(config_disabled_functions_reg->disabled_functions);
  SP_INIT_NULL(config_disabled_functions_reg_ret->disabled_functions);
  SP_INIT_NULL(config_cookie->cookies);
  SP_INIT_NULL(config_eval->blacklist);
  SP_INIT_NULL(config_eval->whitelist);
  SP_INIT_NULL(config_wrapper->whitelist);
#undef SP_INIT_NULL
}

PHP_MINIT_FUNCTION(snuffleupagus) {
  REGISTER_INI_ENTRIES();

  return SUCCESS;
}

static void free_disabled_functions_hashtable(HashTable *ht) {
  void *ptr = NULL;
  ZEND_HASH_FOREACH_PTR(ht, ptr) { sp_list_free(ptr); }
  ZEND_HASH_FOREACH_END();
}

PHP_MSHUTDOWN_FUNCTION(snuffleupagus) {
#define FREE_HT(F)                       \
  zend_hash_destroy(SNUFFLEUPAGUS_G(F)); \
  pefree(SNUFFLEUPAGUS_G(F), 1);
  FREE_HT(disabled_functions_hook);
  FREE_HT(sp_eval_blacklist_functions_hook);

#define FREE_HT_LIST(F)                                         \
  free_disabled_functions_hashtable(SNUFFLEUPAGUS_G(config).F); \
  FREE_HT(config.F);
  FREE_HT_LIST(config_disabled_functions);
  FREE_HT_LIST(config_disabled_functions_hooked);
  FREE_HT_LIST(config_disabled_functions_ret);
  FREE_HT_LIST(config_disabled_functions_ret_hooked);
#undef FREE_HT_LIST
#undef FREE_HT

#define FREE_LST_DISABLE(L)                       \
  do {                                            \
    sp_list_node *_n = SNUFFLEUPAGUS_G(config).L; \
    sp_disabled_function_list_free(_n);           \
    sp_list_free(_n);                             \
  } while (0)
  FREE_LST_DISABLE(config_disabled_functions_reg->disabled_functions);
  FREE_LST_DISABLE(config_disabled_functions_reg_ret->disabled_functions);
#undef FREE_LST_DISABLE

  sp_list_node *_n = SNUFFLEUPAGUS_G(config).config_cookie->cookies;
  sp_cookie_list_free(_n);
  sp_list_free(_n);

#define FREE_LST(L) sp_list_free(SNUFFLEUPAGUS_G(config).L);
  FREE_LST(config_eval->blacklist);
  FREE_LST(config_eval->whitelist);
  FREE_LST(config_wrapper->whitelist);
#undef FREE_LST

#define FREE_CFG(C) pefree(SNUFFLEUPAGUS_G(config).C, 1);
  FREE_CFG(config_unserialize);
  FREE_CFG(config_random);
  FREE_CFG(config_readonly_exec);
  FREE_CFG(config_global_strict);
  FREE_CFG(config_auto_cookie_secure);
  FREE_CFG(config_snuffleupagus);
  FREE_CFG(config_disable_xxe);
  FREE_CFG(config_upload_validation);
  FREE_CFG(config_session);
  FREE_CFG(config_disabled_functions_reg);
  FREE_CFG(config_disabled_functions_reg_ret);
  FREE_CFG(config_cookie);
  FREE_CFG(config_wrapper);
#undef FREE_CFG

  UNREGISTER_INI_ENTRIES();

  return SUCCESS;
}

PHP_RINIT_FUNCTION(snuffleupagus) {
  const sp_config_wrapper *config_wrapper =
      SNUFFLEUPAGUS_G(config).config_wrapper;
#if defined(COMPILE_DL_SNUFFLEUPAGUS) && defined(ZTS)
  ZEND_TSRMLS_CACHE_UPDATE();
#endif

  if (!SNUFFLEUPAGUS_G(allow_broken_configuration)) {
    if (SNUFFLEUPAGUS_G(is_config_valid) == SP_CONFIG_INVALID) {
      sp_log_err("config", "Invalid configuration file");
    } else if (SNUFFLEUPAGUS_G(is_config_valid) == SP_CONFIG_NONE) {
      sp_log_warn("config",
                  "No configuration specificed via sp.configuration_file");
    }
  }

  // We need to disable wrappers loaded by extensions loaded after
  // SNUFFLEUPAGUS.
  if (config_wrapper->enabled &&
      zend_hash_num_elements(php_stream_get_url_stream_wrappers_hash()) !=
          config_wrapper->num_wrapper) {
    sp_disable_wrapper();
  }

  if (NULL != SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key) {
    if (NULL != SNUFFLEUPAGUS_G(config).config_cookie->cookies) {
      zend_hash_apply_with_arguments(
          Z_ARRVAL(PG(http_globals)[TRACK_VARS_COOKIE]), decrypt_cookie, 0);
    }
  }
  return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION(snuffleupagus) { return SUCCESS; }

PHP_MINFO_FUNCTION(snuffleupagus) {
  const char *valid_config;
  switch (SNUFFLEUPAGUS_G(is_config_valid)) {
    case SP_CONFIG_VALID:
      valid_config = "yes";
      break;
    case SP_CONFIG_INVALID:
      valid_config = "invalid";
      break;
    case SP_CONFIG_NONE:
    default:
      valid_config = "no";
  }
  php_info_print_table_start();
  php_info_print_table_row(
      2, "snuffleupagus support",
      SNUFFLEUPAGUS_G(is_config_valid) ? "enabled" : "disabled");
  php_info_print_table_row(2, "Version", PHP_SNUFFLEUPAGUS_VERSION);
  php_info_print_table_row(2, "Valid config", valid_config);
  php_info_print_table_end();
  DISPLAY_INI_ENTRIES();
}

static PHP_INI_MH(OnUpdateConfiguration) {
  TSRMLS_FETCH();

  if (!new_value || !new_value->len) {
    return FAILURE;
  }

  char *str = new_value->val;

  while (1) {
    // We don't care about overwriting new_value->val
    char *config_file = strsep(&str, ",");
    if (config_file == NULL) break;

    glob_t globbuf;
    if (0 != glob(config_file, GLOB_NOCHECK, NULL, &globbuf)) {
      SNUFFLEUPAGUS_G(is_config_valid) = SP_CONFIG_INVALID;
      globfree(&globbuf);
      return FAILURE;
    }

    for (size_t i = 0; globbuf.gl_pathv[i]; i++) {
      if (sp_parse_config(globbuf.gl_pathv[i]) != SUCCESS) {
        SNUFFLEUPAGUS_G(is_config_valid) = SP_CONFIG_INVALID;
        globfree(&globbuf);
        return FAILURE;
      }
    }
    globfree(&globbuf);
  }

  SNUFFLEUPAGUS_G(is_config_valid) = SP_CONFIG_VALID;

  if ((SNUFFLEUPAGUS_G(config).config_sloppy->enable)) {
    hook_sloppy();
  }

  if (SNUFFLEUPAGUS_G(config).config_random->enable) {
    hook_rand();
  }

  if (SNUFFLEUPAGUS_G(config).config_upload_validation->enable) {
    hook_upload();
  }

  if (SNUFFLEUPAGUS_G(config).config_disable_xxe->enable == 0) {
    hook_libxml_disable_entity_loader();
  }

  if (SNUFFLEUPAGUS_G(config).config_wrapper->enabled) {
    hook_stream_wrappers();
  }

  if (SNUFFLEUPAGUS_G(config).config_session->encrypt) {
    hook_session();
  }

  if (NULL != SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key) {
    if (SNUFFLEUPAGUS_G(config).config_unserialize->enable) {
      hook_serialize();
    }
  }

  hook_disabled_functions();
  hook_execute();
  hook_cookies();

  if (true == SNUFFLEUPAGUS_G(config).config_global_strict->enable) {
    if (!zend_get_extension(PHP_SNUFFLEUPAGUS_EXTNAME)) {
      zend_extension_entry.startup = NULL;
      zend_register_extension(&zend_extension_entry, NULL);
    }
    // This is needed to implement the global strict mode
    CG(compiler_options) |= ZEND_COMPILE_HANDLE_OP_ARRAY;
  }

  // If `zend_write_default` is not NULL it is already hooked.
  if ((zend_hash_str_find(
           SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked, "echo",
           sizeof("echo") - 1) ||
       zend_hash_str_find(
           SNUFFLEUPAGUS_G(config).config_disabled_functions_ret_hooked, "echo",
           sizeof("echo") - 1)) &&
      NULL == zend_write_default && zend_write != hook_echo) {
    zend_write_default = zend_write;
    zend_write = hook_echo;
  }

  SNUFFLEUPAGUS_G(config).hook_execute =
      SNUFFLEUPAGUS_G(config)
          .config_disabled_functions_reg->disabled_functions ||
      SNUFFLEUPAGUS_G(config)
          .config_disabled_functions_reg_ret->disabled_functions ||
      zend_hash_num_elements(
          SNUFFLEUPAGUS_G(config).config_disabled_functions) ||
      zend_hash_num_elements(
          SNUFFLEUPAGUS_G(config).config_disabled_functions_ret);

  return SUCCESS;
}

const zend_function_entry snuffleupagus_functions[] = {PHP_FE_END};

zend_module_entry snuffleupagus_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_SNUFFLEUPAGUS_EXTNAME,
    snuffleupagus_functions,
    PHP_MINIT(snuffleupagus),
    PHP_MSHUTDOWN(snuffleupagus),
    PHP_RINIT(snuffleupagus),
    PHP_RSHUTDOWN(snuffleupagus),
    PHP_MINFO(snuffleupagus),
    PHP_SNUFFLEUPAGUS_VERSION,
    PHP_MODULE_GLOBALS(snuffleupagus),
    PHP_GINIT(snuffleupagus),
    NULL,
    NULL,
    STANDARD_MODULE_PROPERTIES_EX};

#ifdef COMPILE_DL_SNUFFLEUPAGUS
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(snuffleupagus)
#endif