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
|
#include "php_snuffleupagus.h"
#include <errno.h>
#include <string.h>
ZEND_DECLARE_MODULE_GLOBALS(snuffleupagus)
static void (*orig_execute_ex)(zend_execute_data *execute_data);
static int (*orig_zend_stream_open)(const char *filename,
zend_file_handle *handle);
// FIXME handle symlink
ZEND_COLD static inline void terminate_if_writable(const char *filename) {
if (0 == access(filename, W_OK)) {
if (true == SNUFFLEUPAGUS_G(config).config_readonly_exec->simulation) {
sp_log_msg("readonly_exec", SP_LOG_SIMULATION,
"Attempted execution of a writable file (%s).", filename);
} else {
sp_log_msg("readonly_exec", SP_LOG_DROP,
"Attempted execution of a writable file (%s).", filename);
sp_terminate();
}
} else {
if (EACCES != errno) {
sp_log_err("Writable execution", "Error while accessing %s: %s", filename,
strerror(errno));
}
}
}
static void is_builtin_matching(const char *restrict const filename,
char *restrict function_name,
char *restrict param_name,
sp_list_node *config) {
if (!config || !config->data) {
return;
}
if (true == should_disable(EG(current_execute_data), function_name, filename,
param_name)) {
sp_terminate();
}
}
/* This function gets the filename in which `eval()` is called from,
* since it looks like "foo.php(1) : eval()'d code", so we're starting
* from the end of the string until the second closing parenthesis. */
char *get_eval_filename(const char *filename) {
size_t i = strlen(filename);
int count = 0;
char *clean_filename = estrdup(filename);
while (i--) {
if (clean_filename[i] == '(') {
if (count == 1) {
clean_filename[i] = '\0';
break;
}
count++;
}
}
return clean_filename;
}
static void sp_execute_ex(zend_execute_data *execute_data) {
if (true == should_disable(execute_data, NULL, NULL, NULL)) {
sp_terminate();
}
if (execute_data->func->op_array.type == ZEND_EVAL_CODE) {
SNUFFLEUPAGUS_G(in_eval) = true;
sp_list_node *config =
SNUFFLEUPAGUS_G(config).config_disabled_constructs->construct_eval;
char *filename = get_eval_filename((char *)zend_get_executed_filename());
is_builtin_matching(filename, "eval", NULL, config);
efree(filename);
}
if (NULL != execute_data->func->op_array.filename) {
if (true == SNUFFLEUPAGUS_G(config).config_readonly_exec->enable) {
terminate_if_writable(ZSTR_VAL(execute_data->func->op_array.filename));
}
}
orig_execute_ex(execute_data);
if (true == should_drop_on_ret(execute_data->return_value, execute_data)) {
sp_terminate();
}
SNUFFLEUPAGUS_G(in_eval) = false;
}
static int sp_stream_open(const char *filename, zend_file_handle *handle) {
zend_execute_data const *const data = EG(current_execute_data);
if ((NULL == data) || (NULL == data->opline) ||
(data->func->type != ZEND_USER_FUNCTION)) {
goto end;
}
switch (data->opline->opcode) {
case ZEND_INCLUDE_OR_EVAL:
if (true == SNUFFLEUPAGUS_G(config).config_readonly_exec->enable) {
terminate_if_writable(filename);
}
sp_list_node *config =
SNUFFLEUPAGUS_G(config).config_disabled_constructs->construct_include;
switch (data->opline->extended_value) {
case ZEND_INCLUDE:
is_builtin_matching(filename, "include", "inclusion path", config);
break;
case ZEND_REQUIRE:
is_builtin_matching(filename, "require", "inclusion path", config);
break;
case ZEND_REQUIRE_ONCE:
is_builtin_matching(filename, "require_once", "inclusion path",
config);
break;
case ZEND_INCLUDE_ONCE:
is_builtin_matching(filename, "include_once", "inclusion path",
config);
break;
case ZEND_EVAL:
is_builtin_matching(filename, "eval", NULL, config);
break;
default:
break;
}
}
end:
return orig_zend_stream_open(filename, handle);
}
int hook_execute(void) {
TSRMLS_FETCH();
/* zend_execute_ex is used for "classic" function calls */
orig_execute_ex = zend_execute_ex;
zend_execute_ex = sp_execute_ex;
/* zend_stream_open_function is used FIXME */
orig_zend_stream_open = zend_stream_open_function;
zend_stream_open_function = sp_stream_open;
/* zend_execute_internal is used for "indirect" functions call,
* like array_map or call_user_func. */
return SUCCESS;
}
|