summaryrefslogtreecommitdiff
path: root/pledge.c
diff options
context:
space:
mode:
Diffstat (limited to 'pledge.c')
-rw-r--r--pledge.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/pledge.c b/pledge.c
new file mode 100644
index 0000000..b5ceaf8
--- /dev/null
+++ b/pledge.c
@@ -0,0 +1,126 @@
1/*
2 +----------------------------------------------------------------------+
3 | Suhosin Version 1 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2006-2007 The Hardened-PHP Project |
6 | Copyright (c) 2007-2015 SektionEins GmbH |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 3.01 of the PHP license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.php.net/license/3_01.txt |
12 | If you did not receive a copy of the PHP license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@php.net so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
16 | Author: Stefan Esser <sesser@sektioneins.de> |
17 +----------------------------------------------------------------------+
18*/
19
20/* $Id: pledge.c $ */
21
22#include <unistd.h>
23#ifdef __OpenBSD__
24#include <errno.h>
25#include "php.h"
26#include "ext/standard/info.h"
27#include "ext/standard/php_string.h"
28#include "ext/standard/php_smart_str.h"
29
30#include "pledge.h"
31
32const char *promises_defined[] = {
33 "rpath",
34 "wpath",
35 "cpath",
36 "tmppath",
37 "inet",
38 "flock",
39 "unix",
40 "dns",
41 "sendfd",
42 "recvfd",
43 "proc",
44 "exec",
45 NULL
46};
47
48/* {{{ proto string pledge(string str [, bool raw_output])
49 Wrapper around pledge call. Hence subsequent calls are
50 allowed only to diminish the permissions. */
51static PHP_FUNCTION(suhosin_pledge)
52{
53 zval *promises, **current;
54 HashTable *hashp;
55 HashPosition hashpos;
56 const char *pm;
57 int ret;
58 smart_str promisesbuf = { 0 };
59
60 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &promises) == FAILURE) {
61 return;
62 }
63
64 /* PHP needs at least few functions from this promise */
65 smart_str_appends(&promisesbuf, "stdio");
66 hashp = Z_ARRVAL_P(promises);
67 for (zend_hash_internal_pointer_reset_ex(hashp, &hashpos);
68 zend_hash_get_current_data_ex(hashp, (void **)&current, &hashpos) == SUCCESS;
69 zend_hash_move_forward_ex(hashp, &hashpos)) {
70 if (Z_TYPE_PP(current) != IS_STRING)
71 continue;
72 pm = NULL;
73 const char **ptr = promises_defined;
74 char *pp = Z_STRVAL_PP(current);
75 char *p = php_trim(pp, strlen(pp), " ", 1, NULL, 3);
76 while (*ptr) {
77 if (strcmp(*ptr, p) == 0) {
78 pm = *ptr;
79 break;
80 }
81 ptr ++;
82 }
83 if (pm == NULL) {
84 if (strcmp(p, "stdio") != 0)
85 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pledge: %s invalid or forbidden promise", p);
86 continue;
87 }
88 efree(p);
89 smart_str_appends(&promisesbuf, " ");
90 smart_str_appends(&promisesbuf, pm);
91 }
92
93 smart_str_0(&promisesbuf);
94 ret = pledge(promisesbuf.c, NULL);
95 smart_str_free(&promisesbuf);
96
97 if (ret == -1)
98 php_error_docref(NULL TSRMLS_CC, E_ERROR, "pledge failed: %s", strerror(errno));
99
100 RETVAL_LONG(ret);
101}
102
103/* }}} */
104
105/* {{{ suhosin_pledge_functions[]
106 */
107static zend_function_entry suhosin_pledge_functions[] = {
108 PHP_NAMED_FE(pledge, PHP_FN(suhosin_pledge), NULL)
109 {NULL, NULL, NULL}
110};
111/* }}} */
112
113void suhosin_hook_pledge(TSRMLS_D)
114{
115 zend_register_functions(NULL, suhosin_pledge_functions, NULL, MODULE_PERSISTENT TSRMLS_CC);
116}
117
118/*
119 * Local variables:
120 * tab-width: 4
121 * c-basic-offset: 4
122 * End:
123 * vim600: sw=4 ts=4 fdm=marker
124 * vim<600: sw=4 ts=4
125 */
126#endif