Skip to content

Commit a34de2a

Browse files
committed
Move zend utility function and rename
1 parent 4bfe98e commit a34de2a

12 files changed

+129
-77
lines changed

Zend/zend.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ ZEND_API char *(*zend_getenv)(const char *name, size_t name_len);
9494
ZEND_API zend_string *(*zend_resolve_path)(zend_string *filename);
9595
ZEND_API zend_result (*zend_post_startup_cb)(void) = NULL;
9696
ZEND_API void (*zend_post_shutdown_cb)(void) = NULL;
97-
ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_os_csprng_random_bytes)(void *bytes, size_t size, char *errstr, size_t errstr_size) = NULL;
98-
ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_general_random_bytes)(zend_utility_general_random_state *state, void *bytes, size_t size) = NULL;
97+
ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes)(void *bytes, size_t size, char *errstr, size_t errstr_size) = NULL;
98+
ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes_insecure)(zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size) = NULL;
9999

100100
/* This callback must be signal handler safe! */
101101
void (*zend_on_timeout)(int seconds);
@@ -915,9 +915,9 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
915915
#endif
916916

917917
/* Set up early utility functions. zend_mm depends on
918-
* zend_general_random_bytes */
919-
zend_os_csprng_random_bytes = utility_functions->os_csprng_random_bytes_function;
920-
zend_general_random_bytes = utility_functions->general_random_bytes_function;
918+
* zend_random_bytes_insecure */
919+
zend_random_bytes = utility_functions->random_bytes_function;
920+
zend_random_bytes_insecure = utility_functions->random_bytes_insecure_function;
921921

922922
start_memory_manager();
923923

Zend/zend.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ struct _zend_class_entry {
237237
typedef union {
238238
zend_max_align_t align;
239239
uint64_t opaque[5];
240-
} zend_utility_general_random_state;
240+
} zend_utility_random_bytes_insecure_state;
241241

242242
typedef struct _zend_utility_functions {
243243
void (*error_function)(int type, zend_string *error_filename, const uint32_t error_lineno, zend_string *message);
@@ -253,8 +253,8 @@ typedef struct _zend_utility_functions {
253253
void (*printf_to_smart_str_function)(smart_str *buf, const char *format, va_list ap);
254254
char *(*getenv_function)(const char *name, size_t name_len);
255255
zend_string *(*resolve_path_function)(zend_string *filename);
256-
zend_result (*os_csprng_random_bytes_function)(void *bytes, size_t size, char *errstr, size_t errstr_size);
257-
zend_result (*general_random_bytes_function)(zend_utility_general_random_state *state, void *bytes, size_t size);
256+
zend_result (*random_bytes_function)(void *bytes, size_t size, char *errstr, size_t errstr_size);
257+
zend_result (*random_bytes_insecure_function)(zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size);
258258
} zend_utility_functions;
259259

260260
typedef struct _zend_utility_values {
@@ -348,10 +348,13 @@ extern void (*zend_printf_to_smart_str)(smart_str *buf, const char *format, va_l
348348
extern ZEND_API char *(*zend_getenv)(const char *name, size_t name_len);
349349
extern ZEND_API zend_string *(*zend_resolve_path)(zend_string *filename);
350350
/* Generate 'size' random bytes into 'bytes' with the OS CSPRNG. */
351-
extern ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_os_csprng_random_bytes)(void *bytes, size_t size, char *errstr, size_t errstr_size);
352-
/* Generate 'size' random bytes into 'bytes' with a general purpose PRNG.
353-
* 'state' must be zeroed before the first call and can be reused. */
354-
extern ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_general_random_bytes)(zend_utility_general_random_state *state, void *bytes, size_t size);
351+
extern ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes)(
352+
void *bytes, size_t size, char *errstr, size_t errstr_size);
353+
/* Generate 'size' random bytes into 'bytes' with a general purpose PRNG (not
354+
* crypto safe). 'state' must be zeroed before the first call and can be reused.
355+
*/
356+
extern ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes_insecure)(
357+
zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size);
355358

356359
/* These two callbacks are especially for opcache */
357360
extern ZEND_API zend_result (*zend_post_startup_cb)(void);

Zend/zend_alloc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ struct _zend_mm_heap {
338338
HashTable *tracked_allocs;
339339
#endif
340340
pid_t pid;
341-
zend_utility_general_random_state rand_state;
341+
zend_utility_random_bytes_insecure_state rand_state;
342342
};
343343

344344
struct _zend_mm_chunk {
@@ -2045,7 +2045,7 @@ static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZE
20452045

20462046
static zend_result zend_mm_refresh_key(zend_mm_heap *heap)
20472047
{
2048-
return zend_general_random_bytes(&heap->rand_state, &heap->shadow_key, sizeof(heap->shadow_key));
2048+
return zend_random_bytes_insecure(&heap->rand_state, &heap->shadow_key, sizeof(heap->shadow_key));
20492049
}
20502050

20512051
static zend_result zend_mm_init_key(zend_mm_heap *heap)

Zend/zend_portability.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,12 @@ typedef union {
800800
short s;
801801
int i;
802802
long l;
803+
#if SIZEOF_LONG_LONG
804+
long long ll;
805+
#endif
803806
float f;
804807
double d;
808+
long double ld;
805809
void *p;
806810
void (*fun)();
807811
} zend_max_align_t;

ext/random/config.m4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ PHP_NEW_EXTENSION(random,
2727
engine_secure.c \
2828
engine_user.c \
2929
gammasection.c \
30-
randomizer.c,
30+
randomizer.c \
31+
zend_utils.c,
3132
no,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
3233
PHP_INSTALL_HEADERS([ext/random], [php_random.h php_random_csprng.h php_random_uint128.h])

ext/random/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
EXTENSION("random", "random.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
22
PHP_RANDOM="yes";
3-
ADD_SOURCES(configure_module_dirname, "csprng.c engine_combinedlcg.c engine_mt19937.c engine_pcgoneseq128xslrr64.c engine_xoshiro256starstar.c engine_secure.c engine_user.c gammasection.c randomizer.c", "random");
3+
ADD_SOURCES(configure_module_dirname, "csprng.c engine_combinedlcg.c engine_mt19937.c engine_pcgoneseq128xslrr64.c engine_xoshiro256starstar.c engine_secure.c engine_user.c gammasection.c randomizer.c zend_utils.c", "random");
44
PHP_INSTALL_HEADERS("ext/random", "php_random.h php_random_csprng.h php_random_uint128.h");

ext/random/php_random.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
PHPAPI double php_combined_lcg(void);
3939

4040
typedef struct _php_random_fallback_seed_state php_random_fallback_seed_state;
41-
typedef struct _php_random_state_for_zend php_random_state_for_zend;
4241

4342
PHPAPI uint64_t php_random_generate_fallback_seed(void);
4443
PHPAPI uint64_t php_random_generate_fallback_seed_ex(php_random_fallback_seed_state *state);
45-
PHPAPI zend_result php_general_random_bytes_for_zend(zend_utility_general_random_state *state, void *bytes, size_t size);
4644

4745
static inline zend_long GENERATE_SEED(void)
4846
{
@@ -118,13 +116,6 @@ typedef struct _php_random_fallback_seed_state {
118116
unsigned char seed[20];
119117
} php_random_fallback_seed_state;
120118

121-
typedef struct _php_random_state_for_zend {
122-
bool initialized;
123-
php_random_status_state_xoshiro256starstar xoshiro256starstar_state;
124-
} php_random_state_for_zend;
125-
126-
ZEND_STATIC_ASSERT(sizeof(zend_utility_general_random_state) >= sizeof(php_random_state_for_zend), "");
127-
128119
extern PHPAPI const php_random_algo php_random_algo_combinedlcg;
129120
extern PHPAPI const php_random_algo php_random_algo_mt19937;
130121
extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64;

ext/random/php_random_csprng.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# include "php.h"
2222

2323
ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_random_bytes(void *bytes, size_t size, bool should_throw);
24-
2524
ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_random_bytes_ex(void *bytes, size_t size, char *errstr, size_t errstr_size);
2625

2726
PHPAPI zend_result php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);

ext/random/php_random_zend_utils.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Arnaud Le Blanc <[email protected]> |
14+
| Tim Düsterhus <[email protected]> |
15+
+----------------------------------------------------------------------+
16+
*/
17+
18+
#ifndef PHP_RANDOM_ZEND_UTILS_H
19+
# define PHP_RANDOM_ZEND_UTILS_H
20+
21+
# include "php.h"
22+
# include "php_random.h"
23+
# include "zend.h"
24+
25+
typedef struct _php_random_bytes_insecure_state_for_zend {
26+
bool initialized;
27+
php_random_status_state_xoshiro256starstar xoshiro256starstar_state;
28+
} php_random_bytes_insecure_state_for_zend;
29+
30+
ZEND_STATIC_ASSERT(sizeof(zend_utility_random_bytes_insecure_state) >= sizeof(php_random_bytes_insecure_state_for_zend), "");
31+
32+
ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_random_bytes_insecure_for_zend(
33+
zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size);
34+
35+
#endif /* PHP_RANDOM_ZEND_UTILS_H */

ext/random/random.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -699,55 +699,6 @@ uint64_t php_random_generate_fallback_seed(void)
699699
return php_random_generate_fallback_seed_ex(&RANDOM_G(fallback_seed_state));
700700
}
701701

702-
static void php_general_random_bytes_for_zend_initialize(php_random_state_for_zend *state)
703-
{
704-
uint64_t t[4];
705-
php_random_fallback_seed_state fallback_state = {
706-
.initialized = false,
707-
};
708-
709-
do {
710-
/* Skip the CSPRNG if it has already failed */
711-
if (!fallback_state.initialized) {
712-
char errstr[128];
713-
if (php_random_bytes_ex(&t, sizeof(t), errstr, sizeof(errstr)) == FAILURE) {
714-
#if ZEND_DEBUG
715-
fprintf(stderr, "php_random_bytes_ex: Failed to generate a random seed: %s\n", errstr);
716-
#endif
717-
goto fallback;
718-
}
719-
} else {
720-
fallback:
721-
t[0] = php_random_generate_fallback_seed_ex(&fallback_state);
722-
t[1] = php_random_generate_fallback_seed_ex(&fallback_state);
723-
t[2] = php_random_generate_fallback_seed_ex(&fallback_state);
724-
t[3] = php_random_generate_fallback_seed_ex(&fallback_state);
725-
}
726-
} while (UNEXPECTED(t[0] == 0 && t[1] == 0 && t[2] == 0 && t[3] == 0));
727-
728-
php_random_xoshiro256starstar_seed256(&state->xoshiro256starstar_state, t[0], t[1], t[2], t[3]);
729-
}
730-
731-
PHPAPI zend_result php_general_random_bytes_for_zend(zend_utility_general_random_state *opaque_state, void *bytes, size_t size)
732-
{
733-
php_random_state_for_zend *state = (php_random_state_for_zend*) opaque_state;
734-
735-
if (!state->initialized) {
736-
php_general_random_bytes_for_zend_initialize(state);
737-
state->initialized = true;
738-
}
739-
740-
while (size > 0) {
741-
php_random_result result = php_random_algo_xoshiro256starstar.generate(&state->xoshiro256starstar_state);
742-
ZEND_ASSERT(result.size == 8 && sizeof(result.result) == 8);
743-
size_t chunk_size = MIN(size, 8);
744-
bytes = zend_mempcpy(bytes, &result.result, chunk_size);
745-
size -= chunk_size;
746-
}
747-
748-
return SUCCESS;
749-
}
750-
751702
/* {{{ PHP_GINIT_FUNCTION */
752703
static PHP_GINIT_FUNCTION(random)
753704
{

ext/random/zend_utils.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Arnaud Le Blanc <[email protected]> |
14+
| Tim Düsterhus <[email protected]> |
15+
+----------------------------------------------------------------------+
16+
*/
17+
18+
#ifdef HAVE_CONFIG_H
19+
# include "config.h"
20+
#endif
21+
22+
#include "php_random_zend_utils.h"
23+
24+
ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_random_bytes_insecure_for_zend(
25+
zend_utility_random_bytes_insecure_state *opaque_state, void *bytes, size_t size)
26+
{
27+
php_random_bytes_insecure_state_for_zend *state = (php_random_bytes_insecure_state_for_zend*) opaque_state;
28+
29+
if (!state->initialized) {
30+
uint64_t t[4];
31+
php_random_fallback_seed_state fallback_state = {
32+
.initialized = false,
33+
};
34+
35+
do {
36+
/* Skip the CSPRNG if it has already failed */
37+
if (!fallback_state.initialized) {
38+
char errstr[128];
39+
if (php_random_bytes_ex(&t, sizeof(t), errstr, sizeof(errstr)) == FAILURE) {
40+
#if ZEND_DEBUG
41+
fprintf(stderr, "php_random_bytes_ex: Failed to generate a random seed: %s\n", errstr);
42+
#endif
43+
goto fallback;
44+
}
45+
} else {
46+
fallback:
47+
t[0] = php_random_generate_fallback_seed_ex(&fallback_state);
48+
t[1] = php_random_generate_fallback_seed_ex(&fallback_state);
49+
t[2] = php_random_generate_fallback_seed_ex(&fallback_state);
50+
t[3] = php_random_generate_fallback_seed_ex(&fallback_state);
51+
}
52+
} while (UNEXPECTED(t[0] == 0 && t[1] == 0 && t[2] == 0 && t[3] == 0));
53+
54+
php_random_xoshiro256starstar_seed256(&state->xoshiro256starstar_state, t[0], t[1], t[2], t[3]);
55+
state->initialized = true;
56+
}
57+
58+
while (size > 0) {
59+
php_random_result result = php_random_algo_xoshiro256starstar.generate(&state->xoshiro256starstar_state);
60+
ZEND_ASSERT(result.size == 8 && sizeof(result.result) == 8);
61+
size_t chunk_size = MIN(size, 8);
62+
bytes = zend_mempcpy(bytes, &result.result, chunk_size);
63+
size -= chunk_size;
64+
}
65+
66+
return SUCCESS;
67+
}

main/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "ext/standard/php_standard.h"
5151
#include "ext/date/php_date.h"
5252
#include "ext/random/php_random_csprng.h"
53+
#include "ext/random/php_random_zend_utils.h"
5354
#include "php_variables.h"
5455
#include "ext/standard/credits.h"
5556
#ifdef PHP_WIN32
@@ -2115,8 +2116,8 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
21152116
zuf.printf_to_smart_str_function = php_printf_to_smart_str;
21162117
zuf.getenv_function = sapi_getenv;
21172118
zuf.resolve_path_function = php_resolve_path_for_zend;
2118-
zuf.os_csprng_random_bytes_function = php_random_bytes_ex;
2119-
zuf.general_random_bytes_function = php_general_random_bytes_for_zend;
2119+
zuf.random_bytes_function = php_random_bytes_ex;
2120+
zuf.random_bytes_insecure_function = php_random_bytes_insecure_for_zend;
21202121
zend_startup(&zuf);
21212122
zend_reset_lc_ctype_locale();
21222123
zend_update_current_locale();

0 commit comments

Comments
 (0)