diff options
| author | bef | 2016-10-14 13:50:30 +0200 |
|---|---|---|
| committer | GitHub | 2016-10-14 13:50:30 +0200 |
| commit | 7e2d510b9cdd68784fd64bbf73843d4595462d63 (patch) | |
| tree | 3d1a4a968df53787b43741ea4fb4fdc41f49602d /pledge.c | |
| parent | 1a2cb68a847f08f934aba2b256a3098fdfae81df (diff) | |
| parent | 293d017aad39263403cb981af0a81c3a7984c1e3 (diff) | |
Merge pull request #107 from devnexen/master
adding pledge's call wrapper, allowing most of the promises.
Diffstat (limited to 'pledge.c')
| -rw-r--r-- | pledge.c | 127 |
1 files changed, 127 insertions, 0 deletions
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 | |||
| 32 | const 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. */ | ||
| 51 | static 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 **)¤t, &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 | */ | ||
| 108 | static zend_function_entry suhosin_pledge_functions[] = { | ||
| 109 | PHP_NAMED_FE(pledge, PHP_FN(suhosin_pledge), NULL) | ||
| 110 | {NULL, NULL, NULL} | ||
| 111 | }; | ||
| 112 | /* }}} */ | ||
| 113 | |||
| 114 | void 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 | ||
