aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2022-04-23 19:18:47 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2022-04-25 16:05:12 +0300
commit314917828af997bcd4abe0b748a254d562f6023a (patch)
treef04aa9db8601b0bbb909dab03eeb6fea7e36b723
parenta4036e668134d1ad9637534bac1e0cb55d0a2b36 (diff)
downloadlibmicrohttpd-314917828af997bcd4abe0b748a254d562f6023a.tar.gz
libmicrohttpd-314917828af997bcd4abe0b748a254d562f6023a.zip
MHD_get_connection_info(): Fixed possible unaligned access
Also: * Reduced number of 'MHD_Connection' members. * Fixed wrong value of returned timeout on some platforms, if timeout is too large.
-rw-r--r--src/include/microhttpd.h2
-rw-r--r--src/microhttpd/connection.c56
-rw-r--r--src/microhttpd/internal.h23
3 files changed, 44 insertions, 37 deletions
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
index f91c1cd0..cf8216d0 100644
--- a/src/include/microhttpd.h
+++ b/src/include/microhttpd.h
@@ -4429,6 +4429,8 @@ MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection,
4429 4429
4430/** 4430/**
4431 * Obtain information about the given connection. 4431 * Obtain information about the given connection.
4432 * The returned pointer is invalidated with the next call of this function or
4433 * when the connection is closed.
4432 * 4434 *
4433 * @param connection what connection to get information about 4435 * @param connection what connection to get information about
4434 * @param info_type what information is desired? 4436 * @param info_type what information is desired?
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index eac75824..f940145e 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -4995,6 +4995,8 @@ MHD_set_http_callbacks_ (struct MHD_Connection *connection)
4995 4995
4996/** 4996/**
4997 * Obtain information about the given connection. 4997 * Obtain information about the given connection.
4998 * The returned pointer is invalidated with the next call of this function or
4999 * when the connection is closed.
4998 * 5000 *
4999 * @param connection what connection to get information about 5001 * @param connection what connection to get information about
5000 * @param info_type what information is desired? 5002 * @param info_type what information is desired?
@@ -5014,44 +5016,62 @@ MHD_get_connection_info (struct MHD_Connection *connection,
5014 case MHD_CONNECTION_INFO_CIPHER_ALGO: 5016 case MHD_CONNECTION_INFO_CIPHER_ALGO:
5015 if (NULL == connection->tls_session) 5017 if (NULL == connection->tls_session)
5016 return NULL; 5018 return NULL;
5017 connection->cipher = gnutls_cipher_get (connection->tls_session); 5019 connection->connection_info_dummy.cipher_algorithm =
5018 return (const union MHD_ConnectionInfo *) &connection->cipher; 5020 gnutls_cipher_get (connection->tls_session);
5021 return &connection->connection_info_dummy;
5019 case MHD_CONNECTION_INFO_PROTOCOL: 5022 case MHD_CONNECTION_INFO_PROTOCOL:
5020 if (NULL == connection->tls_session) 5023 if (NULL == connection->tls_session)
5021 return NULL; 5024 return NULL;
5022 connection->protocol = gnutls_protocol_get_version ( 5025 connection->connection_info_dummy.protocol =
5023 connection->tls_session); 5026 gnutls_protocol_get_version (connection->tls_session);
5024 return (const union MHD_ConnectionInfo *) &connection->protocol; 5027 return &connection->connection_info_dummy;
5025 case MHD_CONNECTION_INFO_GNUTLS_SESSION: 5028 case MHD_CONNECTION_INFO_GNUTLS_SESSION:
5026 if (NULL == connection->tls_session) 5029 if (NULL == connection->tls_session)
5027 return NULL; 5030 return NULL;
5028 return (const union MHD_ConnectionInfo *) &connection->tls_session; 5031 connection->connection_info_dummy.tls_session = connection->tls_session;
5032 return &connection->connection_info_dummy;
5029#endif /* HTTPS_SUPPORT */ 5033#endif /* HTTPS_SUPPORT */
5030 case MHD_CONNECTION_INFO_CLIENT_ADDRESS: 5034 case MHD_CONNECTION_INFO_CLIENT_ADDRESS:
5031 return (const union MHD_ConnectionInfo *) &connection->addr; 5035 memset (&connection->connection_info_dummy.client_addr, 0,
5036 sizeof (connection->connection_info_dummy.client_addr));
5037 memcpy (&connection->connection_info_dummy.client_addr,
5038 &connection->addr,
5039 connection->addr_len);
5040 return &connection->connection_info_dummy;
5032 case MHD_CONNECTION_INFO_DAEMON: 5041 case MHD_CONNECTION_INFO_DAEMON:
5033 return (const union MHD_ConnectionInfo *) &connection->daemon; 5042 connection->connection_info_dummy.daemon = connection->daemon;
5043 return &connection->connection_info_dummy;
5034 case MHD_CONNECTION_INFO_CONNECTION_FD: 5044 case MHD_CONNECTION_INFO_CONNECTION_FD:
5035 return (const union MHD_ConnectionInfo *) &connection->socket_fd; 5045 connection->connection_info_dummy.connect_fd = connection->socket_fd;
5046 return &connection->connection_info_dummy;
5036 case MHD_CONNECTION_INFO_SOCKET_CONTEXT: 5047 case MHD_CONNECTION_INFO_SOCKET_CONTEXT:
5037 return (const union MHD_ConnectionInfo *) &connection->socket_context; 5048 connection->connection_info_dummy.socket_context =
5049 connection->socket_context;
5050 return &connection->connection_info_dummy;
5038 case MHD_CONNECTION_INFO_CONNECTION_SUSPENDED: 5051 case MHD_CONNECTION_INFO_CONNECTION_SUSPENDED:
5039 connection->suspended_dummy = connection->suspended ? MHD_YES : MHD_NO; 5052 connection->connection_info_dummy.suspended =
5040 return (const union MHD_ConnectionInfo *) &connection->suspended_dummy; 5053 connection->suspended ? MHD_YES : MHD_NO;
5054 return &connection->connection_info_dummy;
5041 case MHD_CONNECTION_INFO_CONNECTION_TIMEOUT: 5055 case MHD_CONNECTION_INFO_CONNECTION_TIMEOUT:
5042 connection->connection_timeout_dummy = 5056#if SIZEOF_UNSIGNED_INT <= (SIZEOF_UINT64_T - 2)
5043 (unsigned int) connection->connection_timeout_ms / 1000; 5057 if (UINT_MAX < connection->connection_timeout_ms / 1000)
5044 return (const union MHD_ConnectionInfo *) &connection-> 5058 connection->connection_info_dummy.connection_timeout = UINT_MAX;
5045 connection_timeout_dummy; 5059 else
5060#endif /* SIZEOF_UNSIGNED_INT <=(SIZEOF_UINT64_T - 2) */
5061 connection->connection_info_dummy.connection_timeout =
5062 (unsigned int) (connection->connection_timeout_ms / 1000);
5063 return &connection->connection_info_dummy;
5046 case MHD_CONNECTION_INFO_REQUEST_HEADER_SIZE: 5064 case MHD_CONNECTION_INFO_REQUEST_HEADER_SIZE:
5047 if ( (MHD_CONNECTION_HEADERS_RECEIVED > connection->state) || 5065 if ( (MHD_CONNECTION_HEADERS_RECEIVED > connection->state) ||
5048 (MHD_CONNECTION_CLOSED == connection->state) ) 5066 (MHD_CONNECTION_CLOSED == connection->state) )
5049 return NULL; /* invalid, too early! */ 5067 return NULL; /* invalid, too early! */
5050 return (const union MHD_ConnectionInfo *) &connection->header_size; 5068 connection->connection_info_dummy.header_size = connection->header_size;
5069 return &connection->connection_info_dummy;
5051 case MHD_CONNECTION_INFO_HTTP_STATUS: 5070 case MHD_CONNECTION_INFO_HTTP_STATUS:
5052 if (NULL == connection->response) 5071 if (NULL == connection->response)
5053 return NULL; 5072 return NULL;
5054 return (const union MHD_ConnectionInfo *) &connection->responseCode; 5073 connection->connection_info_dummy.http_status = connection->responseCode;
5074 return &connection->connection_info_dummy;
5055 default: 5075 default:
5056 return NULL; 5076 return NULL;
5057 } 5077 }
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
index 3bedf8ed..094c26ba 100644
--- a/src/microhttpd/internal.h
+++ b/src/microhttpd/internal.h
@@ -1171,11 +1171,6 @@ struct MHD_Connection
1171 uint64_t connection_timeout_ms; 1171 uint64_t connection_timeout_ms;
1172 1172
1173 /** 1173 /**
1174 * Special member to be returned by #MHD_get_connection_info()
1175 */
1176 unsigned int connection_timeout_dummy;
1177
1178 /**
1179 * Did we ever call the "default_handler" on this connection? (this 1174 * Did we ever call the "default_handler" on this connection? (this
1180 * flag will determine if we call the #MHD_OPTION_NOTIFY_COMPLETED 1175 * flag will determine if we call the #MHD_OPTION_NOTIFY_COMPLETED
1181 * handler when the connection closes down). 1176 * handler when the connection closes down).
@@ -1343,16 +1338,6 @@ struct MHD_Connection
1343 gnutls_session_t tls_session; 1338 gnutls_session_t tls_session;
1344 1339
1345 /** 1340 /**
1346 * Memory location to return for protocol session info.
1347 */
1348 int protocol;
1349
1350 /**
1351 * Memory location to return for protocol session info.
1352 */
1353 int cipher;
1354
1355 /**
1356 * State of connection's TLS layer 1341 * State of connection's TLS layer
1357 */ 1342 */
1358 enum MHD_TLS_CONN_STATE tls_state; 1343 enum MHD_TLS_CONN_STATE tls_state;
@@ -1370,14 +1355,14 @@ struct MHD_Connection
1370 bool suspended; 1355 bool suspended;
1371 1356
1372 /** 1357 /**
1373 * Special member to be returned by #MHD_get_connection_info() 1358 * Is the connection wanting to resume?
1374 */ 1359 */
1375 int suspended_dummy; 1360 volatile bool resuming;
1376 1361
1377 /** 1362 /**
1378 * Is the connection wanting to resume? 1363 * Special member to be returned by #MHD_get_connection_info()
1379 */ 1364 */
1380 volatile bool resuming; 1365 union MHD_ConnectionInfo connection_info_dummy;
1381}; 1366};
1382 1367
1383 1368