summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Carlier2016-05-24 07:01:21 +0100
committerDavid Carlier2016-05-24 07:01:21 +0100
commita27df15eff8b9d971e0c2c7730125b5cf99dc683 (patch)
tree5b94c8a151c4105e5bd179cf2e881ba5e3b02504
parent765706eb5a01756542faf5609b303927bd6f43dc (diff)
adding pledge's call wrapper, allowing most of the promises.
this promises list might need to be adjusted over time.
-rw-r--r--config.m42
-rw-r--r--php_suhosin.h1
-rw-r--r--pledge.c126
-rw-r--r--pledge.h29
-rw-r--r--suhosin.c1
5 files changed, 158 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 824ce21..b228d48 100644
--- a/php_suhosin.h
+++ b/php_suhosin.h
@@ -383,6 +383,7 @@ void suhosin_unhook_header_handler();
383void suhosin_hook_session(TSRMLS_D); 383void suhosin_hook_session(TSRMLS_D);
384void suhosin_unhook_session(TSRMLS_D); 384void suhosin_unhook_session(TSRMLS_D);
385void suhosin_hook_sha256(TSRMLS_D); 385void suhosin_hook_sha256(TSRMLS_D);
386void suhosin_hook_pledge(TSRMLS_D);
386void suhosin_hook_ex_imp(TSRMLS_D); 387void suhosin_hook_ex_imp(TSRMLS_D);
387void suhosin_hook_treat_data(); 388void suhosin_hook_treat_data();
388void suhosin_hook_memory_limit(TSRMLS_D); 389void suhosin_hook_memory_limit(TSRMLS_D);
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
diff --git a/pledge.h b/pledge.h
new file mode 100644
index 0000000..2666e41
--- /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: Stefan Esser <sesser@sektioneins.de> |
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 d95b92f..abe79df 100644
--- a/suhosin.c
+++ b/suhosin.c
@@ -1051,6 +1051,7 @@ PHP_MINIT_FUNCTION(suhosin)
1051 suhosin_hook_memory_limit(TSRMLS_C); 1051 suhosin_hook_memory_limit(TSRMLS_C);
1052 suhosin_hook_sha256(TSRMLS_C); 1052 suhosin_hook_sha256(TSRMLS_C);
1053 suhosin_hook_ex_imp(TSRMLS_C); 1053 suhosin_hook_ex_imp(TSRMLS_C);
1054 suhosin_hook_pledge(TSRMLS_C);
1054 1055
1055#if PHP_VERSION_ID < 50500 1056#if PHP_VERSION_ID < 50500
1056 /* register the logo for phpinfo */ 1057 /* register the logo for phpinfo */