summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.m42
-rw-r--r--php_suhosin.h3
-rw-r--r--pledge.c127
-rw-r--r--pledge.h29
-rw-r--r--suhosin.c3
5 files changed, 163 insertions, 1 deletions
diff --git a/config.m4 b/config.m4
index c908de9..3c4c89d 100644
--- a/config.m4
+++ b/config.m4
@@ -5,7 +5,7 @@ PHP_ARG_ENABLE(suhosin, whether to enable suhosin support,
5[ --enable-suhosin Enable suhosin support]) 5[ --enable-suhosin Enable suhosin support])
6 6
7if test "$PHP_SUHOSIN" != "no"; then 7if test "$PHP_SUHOSIN" != "no"; then
8 PHP_NEW_EXTENSION(suhosin, suhosin.c sha256.c memory_limit.c treat_data.c ifilter.c post_handler.c ufilter.c rfc1867_new.c log.c header.c execute.c ex_imp.c session.c aes.c crypt.c, $ext_shared) 8 PHP_NEW_EXTENSION(suhosin, suhosin.c sha256.c memory_limit.c treat_data.c ifilter.c post_handler.c ufilter.c rfc1867_new.c log.c header.c execute.c ex_imp.c session.c aes.c crypt.c pledge.c, $ext_shared)
9fi 9fi
10 10
11PHP_ARG_ENABLE(suhosin-experimental, whether to enable experimental suhosin features, 11PHP_ARG_ENABLE(suhosin-experimental, whether to enable experimental suhosin features,
diff --git a/php_suhosin.h b/php_suhosin.h
index a3d6de1..bf4d34f 100644
--- a/php_suhosin.h
+++ b/php_suhosin.h
@@ -384,6 +384,9 @@ void suhosin_unhook_header_handler();
384void suhosin_hook_session(TSRMLS_D); 384void suhosin_hook_session(TSRMLS_D);
385void suhosin_unhook_session(TSRMLS_D); 385void suhosin_unhook_session(TSRMLS_D);
386void suhosin_hook_sha256(TSRMLS_D); 386void suhosin_hook_sha256(TSRMLS_D);
387#ifdef __OpenBSD__
388void suhosin_hook_pledge(TSRMLS_D);
389#endif
387void suhosin_hook_ex_imp(TSRMLS_D); 390void suhosin_hook_ex_imp(TSRMLS_D);
388void suhosin_hook_treat_data(); 391void suhosin_hook_treat_data();
389void suhosin_hook_memory_limit(TSRMLS_D); 392void suhosin_hook_memory_limit(TSRMLS_D);
diff --git a/pledge.c b/pledge.c
new file mode 100644
index 0000000..6a9ecdc
--- /dev/null
+++ b/pledge.c
@@ -0,0 +1,127 @@
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: David Carlier <devnexen@gmail.com> |
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 efree(p);
87 continue;
88 }
89 efree(p);
90 smart_str_appends(&promisesbuf, " ");
91 smart_str_appends(&promisesbuf, pm);
92 }
93
94 smart_str_0(&promisesbuf);
95 ret = pledge(promisesbuf.c, NULL);
96 smart_str_free(&promisesbuf);
97
98 if (ret == -1)
99 php_error_docref(NULL TSRMLS_CC, E_ERROR, "pledge failed: %s", strerror(errno));
100
101 RETVAL_LONG(ret);
102}
103
104/* }}} */
105
106/* {{{ suhosin_pledge_functions[]
107 */
108static zend_function_entry suhosin_pledge_functions[] = {
109 PHP_NAMED_FE(pledge, PHP_FN(suhosin_pledge), NULL)
110 {NULL, NULL, NULL}
111};
112/* }}} */
113
114void suhosin_hook_pledge(TSRMLS_D)
115{
116 zend_register_functions(NULL, suhosin_pledge_functions, NULL, MODULE_PERSISTENT TSRMLS_CC);
117}
118
119/*
120 * Local variables:
121 * tab-width: 4
122 * c-basic-offset: 4
123 * End:
124 * vim600: sw=4 ts=4 fdm=marker
125 * vim<600: sw=4 ts=4
126 */
127#endif
diff --git a/pledge.h b/pledge.h
new file mode 100644
index 0000000..24cdccb
--- /dev/null
+++ b/pledge.h
@@ -0,0 +1,29 @@
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: David Carlier <devnexen@gmail.com> |
17 +----------------------------------------------------------------------+
18*/
19
20/* $Id: pledge.h $ */
21
22#ifndef PLEDGE_H
23#define PLEDGE_H
24
25#include "ext/standard/basic_functions.h"
26#ifdef __OpenBSD__
27
28#endif
29#endif
diff --git a/suhosin.c b/suhosin.c
index 11f99f7..32193c6 100644
--- a/suhosin.c
+++ b/suhosin.c
@@ -1050,6 +1050,9 @@ PHP_MINIT_FUNCTION(suhosin)
1050 suhosin_hook_memory_limit(TSRMLS_C); 1050 suhosin_hook_memory_limit(TSRMLS_C);
1051 suhosin_hook_sha256(TSRMLS_C); 1051 suhosin_hook_sha256(TSRMLS_C);
1052 suhosin_hook_ex_imp(TSRMLS_C); 1052 suhosin_hook_ex_imp(TSRMLS_C);
1053#ifdef __OpenBSD__
1054 suhosin_hook_pledge(TSRMLS_C);
1055#endif
1053 1056
1054#if PHP_VERSION_ID < 50500 1057#if PHP_VERSION_ID < 50500
1055 /* register the logo for phpinfo */ 1058 /* register the logo for phpinfo */