-
Notifications
You must be signed in to change notification settings - Fork 2
/
pledge.c
323 lines (271 loc) · 9.43 KB
/
pledge.c
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
/* pledge.c */
/* Needed for OpenBSD pledge/unveil */
#include <unistd.h>
#include <errno.h>
/* Include PHP API */
#include "php.h"
#include "ext/standard/info.h"
#include "ext/spl/spl_exceptions.h"
#include "Zend/zend_exceptions.h"
/* This module's header file */
#include "php_pledge.h"
/* for exceptional circumstances */
zend_class_entry *pledge_exception_ce;
zend_class_entry *unveil_exception_ce;
/* provide info for the reflection API */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pledge, 0, 0, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, promises, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, execpromises, IS_STRING, 1)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_unveil, 0, 0, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, permissions, IS_STRING, 1)
ZEND_END_ARG_INFO();
/* define the function we want to add */
zend_function_entry pledge_functions[] = {
PHP_FE(pledge, arginfo_pledge)
PHP_FE(unveil, arginfo_unveil)
PHP_FE_END
};
/* define the ini settings we want to add */
PHP_INI_BEGIN()
PHP_INI_ENTRY("openbsd.pledge_promises", NULL, PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("openbsd.pledge_execpromises", NULL, PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("openbsd.unveil", "", PHP_INI_SYSTEM, NULL)
PHP_INI_END()
/* create exception classes */
void init_exceptions() {
zend_class_entry ce_pe;
INIT_CLASS_ENTRY(ce_pe, "PledgeException", NULL);
pledge_exception_ce = zend_register_internal_class_ex(&ce_pe, spl_ce_RuntimeException);
zend_class_entry ce_ue;
INIT_CLASS_ENTRY(ce_ue, "UnveilException", NULL);
unveil_exception_ce = zend_register_internal_class_ex(&ce_ue, spl_ce_RuntimeException);
}
/* use the unveil ini value to initialize the runtime */
int init_unveil_ini() {
char *unveil_ini;
unveil_ini = INI_STR("openbsd.unveil");
if (unveil_ini != NULL) {
char *unveil_directive = strtok(unveil_ini, ",");
char *path;
char *permissions;
while (unveil_directive != NULL) {
path = malloc(strlen(unveil_directive) + 1);
permissions = malloc(strlen(unveil_directive) + 1);
if (sscanf(unveil_directive, "%[^:]:%s", path, permissions) != 2) {
zend_error(E_ERROR, "Error parsing unveil directive: \"%s\"\n", unveil_directive);
return 1;
}
/* disallow future unveil calls, also stop parsing the rest of the config */
if (strcmp(path, "null") == 0 && strcmp(permissions, "null") == 0) {
free(path);
free(permissions);
if (unveil(NULL, NULL) != 0) {
zend_error(E_ERROR, "Call to unveil(NULL, NULL) to disallow future unveil calls failed");
return 1;
}
return 0;
}
if (unveil(path, permissions) != 0) {
switch (errno) {
case E2BIG:
zend_error(E_ERROR, "The addition of path would exceed the per-process limit for unveiled paths");
break;
case EFAULT:
zend_error(E_ERROR, "path or permissions points outside the process's allocated address space");
break;
case ENOENT:
zend_error(E_ERROR, "A directory in path did not exist");
break;
case EINVAL:
zend_error(E_ERROR, "An invalid value of permissions was used");
break;
case EPERM:
zend_error(E_ERROR, "An attempt to increase permissions was made, or the path was not accessible, or unveil() was called after locking");
break;
default:
zend_error(E_ERROR, "Unveil error (%d)", errno);
}
free(path);
free(permissions);
return 1;
}
free(path);
free(permissions);
unveil_directive = strtok(NULL, ",");
}
}
return 0;
}
/* use the pledge ini values to initialize the runtime */
int init_pledge_ini() {
char *pledge_promises_ini;
char *pledge_execpromises_ini;
pledge_promises_ini = INI_STR("openbsd.pledge_promises");
pledge_execpromises_ini = INI_STR("openbsd.pledge_execpromises");
if (pledge(pledge_promises_ini, pledge_execpromises_ini) != 0) {
switch (errno) {
case EFAULT:
zend_error(E_ERROR, "promises or execpromises points outside the process's allocated address space");
break;
case EINVAL:
zend_error(E_ERROR, "promises is malformed or contains invalid keywords");
break;
case EPERM:
zend_error(E_ERROR, "This process is attempting to increase permissions");
break;
default:
zend_error(E_ERROR, "Pledge error (%d)", errno);
}
return 1;
}
return 0;
}
/* module init */
PHP_MINIT_FUNCTION(pledge) {
REGISTER_INI_ENTRIES();
init_exceptions();
return SUCCESS;
}
/* runtime init */
PHP_RINIT_FUNCTION(pledge) {
if (init_unveil_ini() != 0) {
return FAILURE;
}
if (init_pledge_ini() != 0) {
return FAILURE;
}
return SUCCESS;
}
/* module info */
PHP_MINFO_FUNCTION(pledge) {
php_info_print_table_start();
php_info_print_table_row(2, "OpenBSD pledge/unveil support", "enabled");
php_info_print_table_row(2, "Version", PHP_PLEDGE_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* module shutdown */
PHP_MSHUTDOWN_FUNCTION(pledge) {
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
zend_module_entry pledge_module_entry = {
STANDARD_MODULE_HEADER,
PHP_PLEDGE_EXTNAME,
pledge_functions, /* Function entries */
PHP_MINIT(pledge), /* Module init */
NULL, /* Module shutdown */
PHP_RINIT(pledge), /* Request init */
NULL, /* Request shutdown */
PHP_MINFO(pledge), /* Module information */
PHP_PLEDGE_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* install module */
ZEND_GET_MODULE(pledge)
/* function pledge(?string $promises = null, ?string $execpromises = null): bool */
PHP_FUNCTION(pledge) {
char *promises = NULL;
size_t promises_len = 0;
char *execpromises = NULL;
size_t execpromises_len = 0;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_STRING_EX(promises, promises_len, 1, 0)
Z_PARAM_STRING_EX(execpromises, execpromises_len, 1, 0)
ZEND_PARSE_PARAMETERS_END();
if (pledge(promises, execpromises) == 0) {
RETURN_TRUE;
}
switch (errno) {
case EFAULT:
zend_throw_exception(
pledge_exception_ce,
"promises or execpromises points outside the process's allocated address space",
errno
);
break;
case EINVAL:
zend_throw_exception(
pledge_exception_ce,
"promises is malformed or contains invalid keywords",
errno
);
break;
case EPERM:
zend_throw_exception(
pledge_exception_ce,
"This process is attempting to increase permissions",
errno
);
break;
default:
zend_throw_exception(
pledge_exception_ce,
"Pledge error",
errno
);
}
RETURN_FALSE;
}
/* function unveil(?string $path = null, ?string $permissions = null): bool */
PHP_FUNCTION(unveil) {
char *path = NULL;
size_t path_len = 0;
char *permissions = NULL;
size_t permissions_len = 0;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_STRING_EX(path, path_len, 1, 0)
Z_PARAM_STRING_EX(permissions, permissions_len, 1, 0)
ZEND_PARSE_PARAMETERS_END();
if (unveil(path, permissions) == 0) {
RETURN_TRUE;
}
switch (errno) {
case E2BIG:
zend_throw_exception(
unveil_exception_ce,
"The addition of path would exceed the per-process limit for unveiled paths",
errno
);
break;
case EFAULT:
zend_throw_exception(
unveil_exception_ce,
"path or permissions points outside the process's allocated address space",
errno
);
break;
case ENOENT:
zend_throw_exception(
unveil_exception_ce,
"A directory in path did not exist",
errno
);
break;
case EINVAL:
zend_throw_exception(
unveil_exception_ce,
"An invalid value of permissions was used",
errno
);
break;
case EPERM:
zend_throw_exception(
unveil_exception_ce,
"An attempt to increase permissions was made, or the path was not accessible, or unveil() was called after locking",
errno
);
break;
default:
zend_throw_exception(
unveil_exception_ce,
"Unveil error",
errno
);
}
RETURN_FALSE;
}