forked from elbandi/php-prctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprctl.c
More file actions
531 lines (490 loc) · 15.4 KB
/
Copy pathprctl.c
File metadata and controls
531 lines (490 loc) · 15.4 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id: header 252479 2008-02-07 19:39:50Z iliaa $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_prctl.h"
#include "securebits.h"
#include <sys/capability.h>
#include <sys/prctl.h>
#include <sys/signal.h>
#define PRCTL_CONST(tag) REGISTER_LONG_CONSTANT("PRCTL_" #tag, PR_##tag, CONST_CS | CONST_PERSISTENT)
/* If you declare any globals in php_prctl.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(prctl)
*/
/* True global resources - no need for thread safety here */
static int le_prctl;
/* {{{ prctl_functions[]
*
* Every user visible function must have an entry in prctl_functions[].
*/
const zend_function_entry prctl_functions[] = {
PHP_FE(confirm_prctl_compiled, NULL) /* For testing, remove later. */
PHP_FE(prctl_capbset_read, NULL)
PHP_FE(prctl_capbset_drop, NULL)
PHP_FE(prctl_set_dumpable, NULL)
PHP_FE(prctl_get_dumpable, NULL)
PHP_FE(prctl_set_endian, NULL)
PHP_FE(prctl_get_endian, NULL)
PHP_FE(prctl_set_fpemu, NULL)
PHP_FE(prctl_get_fpemu, NULL)
PHP_FE(prctl_set_fpexc, NULL)
PHP_FE(prctl_get_fpexc, NULL)
PHP_FE(prctl_set_keepcaps, NULL)
PHP_FE(prctl_get_keepcaps, NULL)
PHP_FE(prctl_set_name, NULL)
PHP_FE(prctl_get_name, NULL)
PHP_FE(prctl_set_pdeathsig, NULL)
PHP_FE(prctl_get_pdeathsig, NULL)
PHP_FE(prctl_set_seccomp, NULL)
PHP_FE(prctl_get_seccomp, NULL)
PHP_FE(prctl_set_securebits, NULL)
PHP_FE(prctl_get_securebits, NULL)
PHP_FE(prctl_set_tsc, NULL)
PHP_FE(prctl_get_tsc, NULL)
PHP_FE(prctl_set_unalign, NULL)
PHP_FE(prctl_get_unalign, NULL)
{NULL, NULL, NULL} /* Must be the last line in prctl_functions[] */
};
/* }}} */
/* {{{ prctl_module_entry
*/
zend_module_entry prctl_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"prctl",
prctl_functions,
PHP_MINIT(prctl),
PHP_MSHUTDOWN(prctl),
PHP_RINIT(prctl), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(prctl), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(prctl),
#if ZEND_MODULE_API_NO >= 20010901
PHP_PRCTL_VERSION, /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_PRCTL
ZEND_GET_MODULE(prctl)
#endif
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("prctl.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_prctl_globals, prctl_globals)
STD_PHP_INI_ENTRY("prctl.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_prctl_globals, prctl_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ php_prctl_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_prctl_init_globals(zend_prctl_globals *prctl_globals)
{
prctl_globals->global_value = 0;
prctl_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(prctl)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
PRCTL_CONST(ENDIAN_LITTLE);
PRCTL_CONST(ENDIAN_BIG);
PRCTL_CONST(ENDIAN_PPC_LITTLE);
PRCTL_CONST(FPEMU_NOPRINT);
PRCTL_CONST(FPEMU_SIGFPE);
PRCTL_CONST(FP_EXC_SW_ENABLE);
PRCTL_CONST(FP_EXC_DIV);
PRCTL_CONST(FP_EXC_OVF);
PRCTL_CONST(FP_EXC_UND);
PRCTL_CONST(FP_EXC_RES);
PRCTL_CONST(FP_EXC_INV);
PRCTL_CONST(FP_EXC_DISABLED);
PRCTL_CONST(FP_EXC_NONRECOV);
PRCTL_CONST(FP_EXC_ASYNC);
PRCTL_CONST(FP_EXC_PRECISE);
PRCTL_CONST(TIMING_STATISTICAL);
PRCTL_CONST(TIMING_TIMESTAMP);
PRCTL_CONST(TSC_ENABLE);
PRCTL_CONST(TSC_SIGSEGV);
PRCTL_CONST(UNALIGN_NOPRINT);
PRCTL_CONST(UNALIGN_SIGBUS);
PRCTL_CONST(MCE_KILL_DEFAULT);
PRCTL_CONST(MCE_KILL_EARLY);
PRCTL_CONST(MCE_KILL_LATE);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(prctl)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(prctl)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(prctl)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(prctl)
{
php_info_print_table_start();
php_info_print_table_header(2, "prctl support", "enabled");
php_info_print_table_header(2, "version", PHP_PRCTL_VERSION);
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* Remove the following function when you have succesfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_prctl_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_prctl_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "prctl", arg);
RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* Code get from: https://github.com/seveas/python-prctl */
/* New in 2.6.32, but named and implemented inconsistently. The linux
* implementation has two ways of setting the policy to the default, and thus
* needs an extra argument. We ignore the first argument and always all
* PR_MCE_KILL_SET. This makes our implementation simpler and keeps the prctl
* interface more consistent
*/
#ifdef PR_MCE_KILL
#define PR_GET_MCE_KILL PR_MCE_KILL_GET
#define PR_SET_MCE_KILL PR_MCE_KILL
#endif
/* New in 2.6.XX (Ubuntu 10.10) */
#define NOT_SET (-1)
#ifdef PR_SET_PTRACER
/* This one has no getter for some reason, but guard agains that being fixed */
#ifndef PR_GET_PTRACER
#define PR_GET_PTRACER NOT_SET
/* Icky global variable to cache ptracer */
static int __cached_ptracer = NOT_SET;
#endif
#endif
/* {{{ prctl_prctl */
static void prctl_prctl(INTERNAL_FUNCTION_PARAMETERS, int option) /* {{{ */
{
long arg = 0;
char *argstr = NULL;
int argstr_len = 0;
char name[17] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
int result;
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "|l", &arg) == FAILURE) {
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "s", &argstr, &argstr_len) == FAILURE) {
return;
}
if(option != PR_SET_NAME) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "a integer is required");
RETURN_FALSE;
}
} else {
if(option == PR_SET_NAME) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "a string is required");
RETURN_FALSE;
}
}
switch (option) {
case (PR_CAPBSET_READ):
case (PR_CAPBSET_DROP):
if (!cap_valid(arg)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown capability");
RETURN_FALSE;
}
break;
case (PR_SET_DUMPABLE):
case (PR_SET_KEEPCAPS):
arg = arg ? 1 : 0;
break;
case (PR_SET_ENDIAN):
if(arg != PR_ENDIAN_LITTLE && arg != PR_ENDIAN_BIG && arg != PR_ENDIAN_PPC_LITTLE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown endianness");
RETURN_FALSE;
}
break;
case (PR_SET_FPEMU):
if(arg != PR_FPEMU_NOPRINT && arg != PR_FPEMU_SIGFPE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown floating-point emulation setting");
RETURN_FALSE;
}
case (PR_SET_FPEXC):
if(arg & ~(PR_FP_EXC_SW_ENABLE | PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND | PR_FP_EXC_RES |
PR_FP_EXC_INV | PR_FP_EXC_DISABLED | PR_FP_EXC_NONRECOV | PR_FP_EXC_ASYNC | PR_FP_EXC_PRECISE)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown floating-point exception mode");
RETURN_FALSE;
}
#if PR_MCE_KILL
case (PR_SET_MCE_KILL):
if(arg != PR_MCE_KILL_DEFAULT && arg != PR_MCE_KILL_EARLY && arg != PR_MCE_KILL_LATE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown memory corruption kill policy");
RETURN_FALSE;
}
break;
#endif
case (PR_SET_NAME):
if(strlen(argstr) > 16) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Name is truncated to 16 length");
}
strncpy(name, argstr, 16);
break;
case (PR_SET_PDEATHSIG):
if(arg < 0 || arg > SIGRTMAX) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signal");
RETURN_FALSE;
}
break;
case (PR_SET_SECCOMP):
if(!arg) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument must be 1");
RETURN_FALSE;
}
arg = 1;
break;
case (PR_SET_SECUREBITS):
if(arg & ~ ((1 << SECURE_NOROOT) | (1 << SECURE_NOROOT_LOCKED) | (1 << SECURE_NO_SETUID_FIXUP) |
(1 << SECURE_NO_SETUID_FIXUP_LOCKED) | (1 << SECURE_KEEP_CAPS) | (1 << SECURE_KEEP_CAPS_LOCKED))) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid securebits set");
RETURN_FALSE;
}
break;
case (PR_SET_TIMING):
if(arg != PR_TIMING_STATISTICAL && arg != PR_TIMING_TIMESTAMP) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid timing constant");
RETURN_FALSE;
}
break;
case (PR_SET_TSC):
if(arg != PR_TSC_ENABLE && arg != PR_TSC_SIGSEGV) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid TSC setting");
RETURN_FALSE;
}
break;
case (PR_SET_UNALIGN):
if(arg != PR_UNALIGN_NOPRINT && arg != PR_UNALIGN_SIGBUS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UNALIGN setting");
RETURN_FALSE;
}
break;
}
/*
* Calling prctl
* There are 3 basic call modes:
* - Setters and getters for which the return value is the result
* - Getters for which the result is placed in arg2
* - Getters and setters that deal with strings.
*
* This function takes care of all that and always returns Py_None for
* settings or the result of a getter call as a PyInt or PyString.
*/
switch(option) {
case(PR_CAPBSET_READ):
case(PR_CAPBSET_DROP):
case(PR_SET_DUMPABLE):
case(PR_GET_DUMPABLE):
case(PR_SET_ENDIAN):
case(PR_SET_FPEMU):
case(PR_SET_FPEXC):
case(PR_SET_KEEPCAPS):
case(PR_GET_KEEPCAPS):
#ifdef PR_MCE_KILL
case(PR_GET_MCE_KILL):
#endif
case(PR_SET_PDEATHSIG):
#if defined(PR_GET_PTRACER) && (PR_GET_PTRACER != NOT_SET)
case(PR_GET_PTRACER):
#endif
#ifdef PR_SET_PTRACER
case(PR_SET_PTRACER):
#endif
case(PR_SET_SECCOMP):
case(PR_GET_SECCOMP):
case(PR_SET_SECUREBITS):
case(PR_GET_SECUREBITS):
#ifdef PR_GET_TIMERSLACK
case(PR_GET_TIMERSLACK):
case(PR_SET_TIMERSLACK):
#endif
case(PR_SET_TIMING):
case(PR_GET_TIMING):
case(PR_SET_TSC):
case(PR_SET_UNALIGN):
result = prctl(option, arg, 0, 0, 0);
if(result < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
switch(option) {
case(PR_CAPBSET_READ):
case(PR_GET_DUMPABLE):
case(PR_GET_KEEPCAPS):
case(PR_GET_SECCOMP):
case(PR_GET_TIMING):
RETURN_LONG(result);
#ifdef PR_MCE_KILL
case(PR_GET_MCE_KILL):
#endif
#if defined(PR_GET_PTRACER) && (PR_GET_PTRACER != NOT_SET)
case(PR_GET_PTRACER):
#endif
case(PR_GET_SECUREBITS):
#ifdef PR_GET_TIMERSLACK
case(PR_GET_TIMERSLACK):
#endif
RETURN_LONG(result);
#if defined(PR_GET_PTRACER) && (PR_GET_PTRACER == NOT_SET)
case(PR_SET_PTRACER):
__cached_ptracer = arg;
break;
#endif
}
break;
case(PR_GET_ENDIAN):
case(PR_GET_FPEMU):
case(PR_GET_FPEXC):
case(PR_GET_PDEATHSIG):
case(PR_GET_TSC):
case(PR_GET_UNALIGN):
result = prctl(option, &arg, 0, 0, 0);
if(result < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
RETURN_LONG(arg);
case(PR_SET_NAME):
case(PR_GET_NAME):
result = prctl(option, name, 0, 0, 0);
if(result < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
if(option == PR_GET_NAME) {
RETURN_STRING(name, 1);
}
break;
#if defined(PR_GET_PTRACER) && (PR_GET_PTRACER == NOT_SET)
case(PR_GET_PTRACER):
if(__cached_ptracer == NOT_SET)
RETURN_LONG(getppid());
RETURN_LONG(__cached_ptracer);
#endif
#ifdef PR_MCE_KILL
case(PR_SET_MCE_KILL):
result = prctl(option, PR_MCE_KILL_SET, arg, 0, 0);
if(result < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
break;
#endif
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING,"Unkown prctl option");
RETURN_FALSE;
}
/* None is returned by default */
RETURN_TRUE;
}
/* }}} */
/* {{{ prctl_functions */
#define PRCTL_FUNCTION(x,y) PHP_FUNCTION(x) \
{ \
prctl_prctl(INTERNAL_FUNCTION_PARAM_PASSTHRU, y); \
}
PRCTL_FUNCTION(prctl_capbset_read, PR_CAPBSET_READ);
PRCTL_FUNCTION(prctl_capbset_drop, PR_CAPBSET_DROP);
PRCTL_FUNCTION(prctl_set_dumpable, PR_SET_DUMPABLE);
PRCTL_FUNCTION(prctl_get_dumpable, PR_GET_DUMPABLE);
PRCTL_FUNCTION(prctl_set_endian, PR_SET_ENDIAN);
PRCTL_FUNCTION(prctl_get_endian, PR_GET_ENDIAN);
PRCTL_FUNCTION(prctl_set_fpemu, PR_SET_FPEMU);
PRCTL_FUNCTION(prctl_get_fpemu, PR_GET_FPEMU);
PRCTL_FUNCTION(prctl_set_fpexc, PR_SET_FPEXC);
PRCTL_FUNCTION(prctl_get_fpexc, PR_GET_FPEXC);
PRCTL_FUNCTION(prctl_set_keepcaps, PR_SET_KEEPCAPS);
PRCTL_FUNCTION(prctl_get_keepcaps, PR_GET_KEEPCAPS);
PRCTL_FUNCTION(prctl_set_name, PR_SET_NAME);
PRCTL_FUNCTION(prctl_get_name, PR_GET_NAME);
PRCTL_FUNCTION(prctl_set_pdeathsig, PR_SET_PDEATHSIG);
PRCTL_FUNCTION(prctl_get_pdeathsig, PR_GET_PDEATHSIG);
PRCTL_FUNCTION(prctl_set_seccomp, PR_SET_SECCOMP);
PRCTL_FUNCTION(prctl_get_seccomp, PR_GET_SECCOMP);
PRCTL_FUNCTION(prctl_set_securebits, PR_SET_SECUREBITS);
PRCTL_FUNCTION(prctl_get_securebits, PR_GET_SECUREBITS);
PRCTL_FUNCTION(prctl_set_tsc, PR_SET_TSC);
PRCTL_FUNCTION(prctl_get_tsc, PR_GET_TSC);
PRCTL_FUNCTION(prctl_set_unalign, PR_SET_UNALIGN);
PRCTL_FUNCTION(prctl_get_unalign, PR_GET_UNALIGN);
#ifdef PR_MCE_KILL
PRCTL_FUNCTION(prctl_set_mce_kill, PR_SET_MCE_KILL);
PRCTL_FUNCTION(prctl_get_mce_kill, PR_GET_MCE_KILL);
#endif
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/