avoid malloc in CustomSetters

This commit is contained in:
Christian Grothoff
2024-06-11 14:50:35 +02:00
parent e5d6647003
commit a5dbfbbd69
7 changed files with 46 additions and 55 deletions
+5 -30
View File
@@ -73,24 +73,12 @@ Description1: the size of the socket address pointed by @a sa.
Argument2: const struct sockaddr *sa
Description2: the address to bind to; can be IPv4 (AF_INET), IPv6 (AF_INET6) or even a UNIX domain socket (AF_UNIX)
CustomSetter: /* custom setter */
+ #if FIXME_EG
+ struct sockaddr_storage bind_sa;
+ if (option->val.bind_sa.v_sa_len > sizeof (bind_sa)) return FREAKOUT;
+ memcpy (&settings->bind_sa, option->val.bind_sa.v_sa, option->val.bind_sa.v_sa_len);
+ #endif
+ if (0 != option->val.bind_sa.v_sa_len)
+ {
+ if (NULL != settings->bind_sa.v_sa)
+ free (settings->bind_sa.v_sa);
+ settings->bind_sa.v_sa = malloc (option->val.bind_sa.v_sa_len);
+ if (NULL == settings->bind_sa.v_sa)
+ return MHD_SC_DAEMON_MALLOC_FAILURE;
+ memcpy (settings->bind_sa.v_sa,
+ if (option->val.bind_sa.v_sa_len > sizeof (bind_sa))
+ return MHD_SC_OPTIONS_INVALID;
+ memcpy (&settings->bind_sa.ss,
+ option->val.bind_sa.v_sa,
+ option->val.bind_sa.v_sa_len);
+ settings->bind_sa.v_sa_len = option->val.bind_sa.v_sa_len;
+ settings->bind_sa.v_dual = option->val.bind_sa.v_dual;
+ }
+ settings->bind_sa.ss_len = option->val.bind_sa.v_sa_len;
Name: listen_socket
Value: 82
@@ -373,7 +361,6 @@ Description2: the closure for the callback
Name: random_entropy
Value: 400
Type: struct MHD_DaemonOptionValueRand
Comment: Set strong random data to be used by MHD.
+ Currently the data is only needed for Digest Auth module.
+ The recommended size is between 8 and 32 bytes. Security can be lower for sizes less or equal four.
@@ -382,25 +369,13 @@ Argument1: size_t buf_size
Description1: the size of the buffer
Argument2: const void *buf
Description2: the buffer with strong random data, the content will be copied by MHD
Type: struct MHD_DaemonOptionEntropySeed
CustomSetter: /* custom setter */
+ if (0 != option->val.random_entropy.v_buf_size)
+ {
+ #if FIXME_EG
+ MHD_entropy_hash_ (&settings->random_entropy,
+ option->val.random_entropy.v_buf,
+ option->val.random_entropy.v_buf_size);
+ #endif
+ if (NULL != settings->random_entropy.v_buf)
+ free (settings->random_entropy.v_buf);
+ settings->random_entropy.v_buf
+ = malloc (option->val.random_entropy.v_buf_size);
+ if (NULL == settings->random_entropy.v_buf)
+ return MHD_SC_DAEMON_MALLOC_FAILURE;
+ memcpy (settings->random_entropy.v_buf,
+ option->val.random_entropy.v_buf,
+ option->val.random_entropy.v_buf_size);
+ settings->random_entropy.v_buf_size
+ = option->val.random_entropy.v_buf_size;
+ }
+6
View File
@@ -892,6 +892,11 @@ enum MHD_FIXED_ENUM_MHD_SET_ MHD_StatusCode
* already set.
*/
MHD_SC_OPTIONS_CONFLICT = 60010
,
/**
* Attempted to set an invalid option value.
*/
MHD_SC_OPTIONS_INVALID = 60011
};
@@ -2835,6 +2840,7 @@ typedef void
struct MHD_Request *request,
const struct MHD_String *full_uri);
/**
* The `enum MHD_ConnectionNotificationCode` specifies types
* of connection notifications.
@@ -560,7 +560,7 @@ struct MHD_DaemonOptionValueNotifStreamCB
/**
* Data for #MHD_D_O_RANDOM_ENTROPY
*/
struct MHD_DaemonOptionValueRand
struct MHD_DaemonOptionEntropySeed
{
/**
* the size of the buffer
@@ -784,7 +784,7 @@ union MHD_DaemonOptionValue
* Value for #MHD_D_O_RANDOM_ENTROPY.
* the size of the buffer
*/
struct MHD_DaemonOptionValueRand random_entropy;
struct MHD_DaemonOptionEntropySeed random_entropy;
/**
* Value for #MHD_D_O_DAUTH_MAP_SIZE.
+6
View File
@@ -892,6 +892,11 @@ enum MHD_FIXED_ENUM_MHD_SET_ MHD_StatusCode
* already set.
*/
MHD_SC_OPTIONS_CONFLICT = 60010
,
/**
* Attempted to set an invalid option value.
*/
MHD_SC_OPTIONS_INVALID = 60011
};
@@ -2835,6 +2840,7 @@ typedef void
struct MHD_Request *request,
const struct MHD_String *full_uri);
/**
* The `enum MHD_ConnectionNotificationCode` specifies types
* of connection notifications.
+1 -1
View File
@@ -253,7 +253,7 @@ struct DaemonOptions {
* Value for #MHD_D_O_RANDOM_ENTROPY.
* the size of the buffer
*/
struct MHD_DaemonOptionValueRand random_entropy;
struct MHD_DaemonOptionEntropySeed random_entropy;
/**
+7 -22
View File
@@ -50,19 +50,12 @@ MHD_daemon_set_options (
continue;
case MHD_D_O_BIND_SA:
/* custom setter */
if (0 != option->val.bind_sa.v_sa_len)
{
if (NULL != settings->bind_sa.v_sa)
free (settings->bind_sa.v_sa);
settings->bind_sa.v_sa = malloc (option->val.bind_sa.v_sa_len);
if (NULL == settings->bind_sa.v_sa)
return MHD_SC_DAEMON_MALLOC_FAILURE;
memcpy (settings->bind_sa.v_sa,
if (option->val.bind_sa.v_sa_len > sizeof (bind_sa))
return MHD_SC_OPTIONS_INVALID;
memcpy (&settings->bind_sa.ss,
option->val.bind_sa.v_sa,
option->val.bind_sa.v_sa_len);
settings->bind_sa.v_sa_len = option->val.bind_sa.v_sa_len;
settings->bind_sa.v_dual = option->val.bind_sa.v_dual;
}
settings->bind_sa.ss_len = option->val.bind_sa.v_sa_len;
continue;
case MHD_D_O_LISTEN_SOCKET:
settings->listen_socket = option->val.listen_socket;
@@ -168,17 +161,9 @@ MHD_daemon_set_options (
/* custom setter */
if (0 != option->val.random_entropy.v_buf_size)
{
if (NULL != settings->random_entropy.v_buf)
free (settings->random_entropy.v_buf);
settings->random_entropy.v_buf
= malloc (option->val.random_entropy.v_buf_size);
if (NULL == settings->random_entropy.v_buf)
return MHD_SC_DAEMON_MALLOC_FAILURE;
memcpy (settings->random_entropy.v_buf,
option->val.random_entropy.v_buf,
option->val.random_entropy.v_buf_size);
settings->random_entropy.v_buf_size
= option->val.random_entropy.v_buf_size;
MHD_entropy_hash_ (&settings->random_entropy,
option->val.random_entropy.v_buf,
option->val.random_entropy.v_buf_size);
}
continue;
case MHD_D_O_DAUTH_MAP_SIZE:
+19
View File
@@ -37,4 +37,23 @@
#include "microhttpd2.h"
struct MHD_DaemonOptionValueSA
{
struct sockaddr_storage ss;
size_t ss_len;
};
struct MHD_DaemonOptionEntropySeed
{
uint32_t seed[256 / sizeof (uint32_t)];
};
void
MHD_entropy_hash_ (struct MHD_DaemonOptionEntropySeed *seed,
const void *entropy_input,
size_t entropy_input_size);
#endif /* ! MHD_PUBLIC_API_H */