|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> |
| 3 | +Date: Tue, 24 Nov 2026 21:44:44 +0100 |
| 4 | +Subject: [PATCH] Discard unauthenticated error responses to checks |
| 5 | + |
| 6 | +Connectivity checks use the STUN short-term credential mechanism, and |
| 7 | +RFC 8489 9.1.4 requires a response without valid MESSAGE-INTEGRITY |
| 8 | +received over an unreliable transport to be discarded, as if it had |
| 9 | +never been received, so that retransmissions continue. |
| 10 | + |
| 11 | +The agent applied this to success responses but not to error responses: |
| 12 | +it skipped integrity verification for them and failed the candidate |
| 13 | +pair on any error other than 487. A peer that does not recognize the |
| 14 | +username fragment answers with a 401 it cannot authenticate (RFC 8489 |
| 15 | +9.1.3), for instance when a server that shares one port between |
| 16 | +sessions routes a check to the wrong one. A single such response to a |
| 17 | +consent check on the selected pair ended the connection with "Lost |
| 18 | +connectivity". Anyone able to observe a transaction ID could also send |
| 19 | +one. |
| 20 | + |
| 21 | +Verify error responses to checks like other Binding responses and |
| 22 | +discard them when integrity is missing or invalid. Authenticated error |
| 23 | +responses, such as 487 Role Conflict, are processed as before. |
| 24 | + |
| 25 | +Add a test where the peer answers one consent check on the selected |
| 26 | +pair with an unauthenticated 401, which must not fail the pair, and |
| 27 | +one where it answers with an authenticated 400, which must. |
| 28 | + |
| 29 | +diff --git a/CMakeLists.txt b/CMakeLists.txt |
| 30 | +index 094acbc99f357feac7cb61a3dbfd260184d73de0..129da2cf9ca63a24e091122cd6ad7bb205d94bf9 100644 |
| 31 | +--- a/CMakeLists.txt |
| 32 | ++++ b/CMakeLists.txt |
| 33 | +@@ -84,6 +84,7 @@ set(TESTS_SOURCES |
| 34 | + ${CMAKE_CURRENT_SOURCE_DIR}/test/notrickle.c |
| 35 | + ${CMAKE_CURRENT_SOURCE_DIR}/test/server.c |
| 36 | + ${CMAKE_CURRENT_SOURCE_DIR}/test/conflict.c |
| 37 | ++ ${CMAKE_CURRENT_SOURCE_DIR}/test/error-integrity.c |
| 38 | + ${CMAKE_CURRENT_SOURCE_DIR}/test/bind.c |
| 39 | + ${CMAKE_CURRENT_SOURCE_DIR}/test/ufrag.c |
| 40 | + ${CMAKE_CURRENT_SOURCE_DIR}/test/tcp.c |
| 41 | +diff --git a/src/agent.c b/src/agent.c |
| 42 | +index a90f25603d0ccc52ed55fea7d688f57eb21ebf21..b9e8fe93852ae63e4140ed595c986fe0a2891132 100644 |
| 43 | +--- a/src/agent.c |
| 44 | ++++ b/src/agent.c |
| 45 | +@@ -1346,7 +1346,7 @@ int agent_verify_stun_binding(juice_agent_t *agent, void *buf, size_t size, |
| 46 | + if (msg->msg_method != STUN_METHOD_BINDING) |
| 47 | + return -1; |
| 48 | + |
| 49 | +- if (msg->msg_class == STUN_CLASS_INDICATION || msg->msg_class == STUN_CLASS_RESP_ERROR) |
| 50 | ++ if (msg->msg_class == STUN_CLASS_INDICATION) |
| 51 | + return 0; |
| 52 | + |
| 53 | + if (!msg->has_integrity) { |
| 54 | +@@ -1477,8 +1477,14 @@ int agent_dispatch_stun(juice_agent_t *agent, void *buf, size_t size, stun_messa |
| 55 | + switch (msg->msg_method) { |
| 56 | + case STUN_METHOD_BINDING: |
| 57 | + // Message was verified earlier, no need to re-verify |
| 58 | ++ // RFC 8489 9.1.4. Receiving a Response: |
| 59 | ++ // If [...] both MESSAGE-INTEGRITY and MESSAGE-INTEGRITY-SHA256 are absent, [and] the |
| 60 | ++ // request was sent over an unreliable transport, the response MUST be discarded, as if it |
| 61 | ++ // had never been received. This means that retransmits, if applicable, will continue. |
| 62 | ++ // This covers error responses too: a peer that does not know the username fragment answers |
| 63 | ++ // with an unauthenticated 401, which must not fail the candidate pair. |
| 64 | + if (entry->type == AGENT_STUN_ENTRY_TYPE_CHECK && !msg->has_integrity && |
| 65 | +- (msg->msg_class == STUN_CLASS_REQUEST || msg->msg_class == STUN_CLASS_RESP_SUCCESS)) { |
| 66 | ++ msg->msg_class != STUN_CLASS_INDICATION) { |
| 67 | + JLOG_WARN("Missing integrity in STUN Binding message from remote peer, ignoring"); |
| 68 | + return -1; |
| 69 | + } |
| 70 | +diff --git a/test/error-integrity.c b/test/error-integrity.c |
| 71 | +new file mode 100644 |
| 72 | +index 0000000000000000000000000000000000000000..db89ea2b257c20cfde7dea667117b101b9e6ceea |
| 73 | +--- /dev/null |
| 74 | ++++ b/test/error-integrity.c |
| 75 | +@@ -0,0 +1,192 @@ |
| 76 | ++/** |
| 77 | ++ * Copyright (c) 2026 |
| 78 | ++ * |
| 79 | ++ * This Source Code Form is subject to the terms of the Mozilla Public |
| 80 | ++ * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 81 | ++ * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 82 | ++ */ |
| 83 | ++ |
| 84 | ++#include "juice/juice.h" |
| 85 | ++ |
| 86 | ++#include "socket.h" |
| 87 | ++#include "stun.h" |
| 88 | ++ |
| 89 | ++#include <stdbool.h> |
| 90 | ++#include <stdint.h> |
| 91 | ++#include <stdio.h> |
| 92 | ++#include <string.h> |
| 93 | ++ |
| 94 | ++#ifdef _WIN32 |
| 95 | ++#include <windows.h> |
| 96 | ++static int64_t now_ms(void) { return (int64_t)GetTickCount64(); } |
| 97 | ++#else |
| 98 | ++#include <time.h> |
| 99 | ++static int64_t now_ms(void) { |
| 100 | ++ struct timespec ts; |
| 101 | ++ clock_gettime(CLOCK_MONOTONIC, &ts); |
| 102 | ++ return (int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; |
| 103 | ++} |
| 104 | ++#endif |
| 105 | ++ |
| 106 | ++#define PEER_UFRAG "peer" |
| 107 | ++#define PEER_PWD "peerpeerpeerpeerpeerpeer" |
| 108 | ++#define BUFFER_SIZE 4096 |
| 109 | ++#define CHECKS_AFTER_ERROR 2 |
| 110 | ++#define WAIT_TIME 20000 // msecs |
| 111 | ++ |
| 112 | ++static volatile bool failed; |
| 113 | ++ |
| 114 | ++static void on_state_changed(juice_agent_t *agent, juice_state_t state, void *user_ptr) { |
| 115 | ++ (void)agent; |
| 116 | ++ (void)user_ptr; |
| 117 | ++ if (state == JUICE_STATE_FAILED) |
| 118 | ++ failed = true; |
| 119 | ++} |
| 120 | ++ |
| 121 | ++static void answer(socket_t sock, const stun_message_t *request, const struct sockaddr *source, |
| 122 | ++ socklen_t source_len, unsigned int error_code, bool authenticated) { |
| 123 | ++ stun_message_t response; |
| 124 | ++ memset(&response, 0, sizeof(response)); |
| 125 | ++ response.msg_class = error_code ? STUN_CLASS_RESP_ERROR : STUN_CLASS_RESP_SUCCESS; |
| 126 | ++ response.msg_method = STUN_METHOD_BINDING; |
| 127 | ++ response.error_code = error_code; |
| 128 | ++ memcpy(response.transaction_id, request->transaction_id, STUN_TRANSACTION_ID_SIZE); |
| 129 | ++ if (!error_code) { |
| 130 | ++ memcpy(&response.mapped.addr, source, source_len); |
| 131 | ++ response.mapped.len = source_len; |
| 132 | ++ } |
| 133 | ++ |
| 134 | ++ char buffer[BUFFER_SIZE]; |
| 135 | ++ // Success responses are always authenticated, only the error response may not be |
| 136 | ++ int size = _juice_stun_write(buffer, sizeof(buffer), &response, |
| 137 | ++ error_code && !authenticated ? NULL : PEER_PWD); |
| 138 | ++ if (size > 0) |
| 139 | ++ sendto(sock, buffer, size, 0, source, source_len); |
| 140 | ++} |
| 141 | ++ |
| 142 | ++// The peer is a bare socket that answers every check, except that it answers one consent check on |
| 143 | ++// the selected pair with an error response. Returns whether the agent failed. |
| 144 | ++static int run(unsigned int error_code, bool authenticated, bool *agent_failed) { |
| 145 | ++ failed = false; |
| 146 | ++ |
| 147 | ++ juice_config_t config; |
| 148 | ++ memset(&config, 0, sizeof(config)); |
| 149 | ++ config.cb_state_changed = on_state_changed; |
| 150 | ++ juice_agent_t *agent = juice_create(&config); |
| 151 | ++ if (!agent) |
| 152 | ++ return -1; |
| 153 | ++ |
| 154 | ++ // Asking for the local description first makes the agent the controlling one |
| 155 | ++ char sdp[JUICE_MAX_SDP_STRING_LEN]; |
| 156 | ++ juice_get_local_description(agent, sdp, sizeof(sdp)); |
| 157 | ++ |
| 158 | ++ socket_t sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 159 | ++ struct sockaddr_in bound; |
| 160 | ++ memset(&bound, 0, sizeof(bound)); |
| 161 | ++ bound.sin_family = AF_INET; |
| 162 | ++ inet_pton(AF_INET, "127.0.0.1", &bound.sin_addr); |
| 163 | ++ socklen_t bound_len = sizeof(bound); |
| 164 | ++ if (sock == INVALID_SOCKET || bind(sock, (struct sockaddr *)&bound, sizeof(bound)) != 0 || |
| 165 | ++ getsockname(sock, (struct sockaddr *)&bound, &bound_len) != 0) { |
| 166 | ++ fprintf(stderr, "Could not open the peer socket\n"); |
| 167 | ++ juice_destroy(agent); |
| 168 | ++ return -1; |
| 169 | ++ } |
| 170 | ++ |
| 171 | ++ char remote[JUICE_MAX_SDP_STRING_LEN]; |
| 172 | ++ snprintf(remote, sizeof(remote), "a=ice-ufrag:%s\na=ice-pwd:%s\n", PEER_UFRAG, PEER_PWD); |
| 173 | ++ char candidate[128]; |
| 174 | ++ snprintf(candidate, sizeof(candidate), "a=candidate:1 1 UDP 2130706431 127.0.0.1 %hu typ host", |
| 175 | ++ (unsigned short)ntohs(bound.sin_port)); |
| 176 | ++ if (juice_set_remote_description(agent, remote) != JUICE_ERR_SUCCESS || |
| 177 | ++ juice_add_remote_candidate(agent, candidate) != JUICE_ERR_SUCCESS || |
| 178 | ++ juice_set_remote_gathering_done(agent) != JUICE_ERR_SUCCESS || |
| 179 | ++ juice_gather_candidates(agent) != JUICE_ERR_SUCCESS) { |
| 180 | ++ fprintf(stderr, "Could not set up the agent\n"); |
| 181 | ++ closesocket(sock); |
| 182 | ++ juice_destroy(agent); |
| 183 | ++ return -1; |
| 184 | ++ } |
| 185 | ++ |
| 186 | ++ bool error_sent = false; |
| 187 | ++ int checks_after_error = 0; |
| 188 | ++ int64_t deadline = now_ms() + WAIT_TIME; |
| 189 | ++ while (!failed && checks_after_error < CHECKS_AFTER_ERROR && now_ms() < deadline) { |
| 190 | ++ struct pollfd pfd; |
| 191 | ++ memset(&pfd, 0, sizeof(pfd)); |
| 192 | ++ pfd.fd = sock; |
| 193 | ++ pfd.events = POLLIN; |
| 194 | ++ if (poll(&pfd, 1, 100) <= 0) |
| 195 | ++ continue; |
| 196 | ++ |
| 197 | ++ char buffer[BUFFER_SIZE]; |
| 198 | ++ struct sockaddr_storage source; |
| 199 | ++ socklen_t source_len = sizeof(source); |
| 200 | ++ int size = |
| 201 | ++ (int)recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&source, &source_len); |
| 202 | ++ if (size <= 0) |
| 203 | ++ continue; |
| 204 | ++ |
| 205 | ++ stun_message_t request; |
| 206 | ++ memset(&request, 0, sizeof(request)); |
| 207 | ++ if (_juice_stun_read(buffer, size, &request) < 0 || |
| 208 | ++ request.msg_class != STUN_CLASS_REQUEST || request.msg_method != STUN_METHOD_BINDING || |
| 209 | ++ !_juice_stun_check_integrity(buffer, size, &request, PEER_PWD)) |
| 210 | ++ continue; |
| 211 | ++ |
| 212 | ++ unsigned int code = 0; |
| 213 | ++ if (!error_sent && juice_get_state(agent) == JUICE_STATE_COMPLETED) { |
| 214 | ++ printf("Peer answers a consent check with %s %u\n", |
| 215 | ++ authenticated ? "authenticated" : "unauthenticated", error_code); |
| 216 | ++ error_sent = true; |
| 217 | ++ code = error_code; |
| 218 | ++ } else if (error_sent) { |
| 219 | ++ ++checks_after_error; |
| 220 | ++ } |
| 221 | ++ answer(sock, &request, (struct sockaddr *)&source, source_len, code, authenticated); |
| 222 | ++ } |
| 223 | ++ |
| 224 | ++ // Give a failure caused by the error response time to show |
| 225 | ++ int64_t settle = now_ms() + 1000; |
| 226 | ++ while (!failed && now_ms() < settle) |
| 227 | ++ poll(NULL, 0, 50); |
| 228 | ++ |
| 229 | ++ closesocket(sock); |
| 230 | ++ juice_destroy(agent); |
| 231 | ++ |
| 232 | ++ if (!error_sent) { |
| 233 | ++ fprintf(stderr, "The agent never completed\n"); |
| 234 | ++ return -1; |
| 235 | ++ } |
| 236 | ++ *agent_failed = failed; |
| 237 | ++ return 0; |
| 238 | ++} |
| 239 | ++ |
| 240 | ++int test_error_integrity() { |
| 241 | ++#if defined(JUICE_DISABLE_CONSENT_FRESHNESS) && JUICE_DISABLE_CONSENT_FRESHNESS |
| 242 | ++ printf("Skipped, connected pairs send indications without consent freshness\n"); |
| 243 | ++ return 0; |
| 244 | ++#else |
| 245 | ++ juice_set_log_level(JUICE_LOG_LEVEL_DEBUG); |
| 246 | ++ |
| 247 | ++ // RFC 8489 9.1.3: a peer that does not know the username fragment answers with a 401, which |
| 248 | ++ // it cannot authenticate. RFC 8489 9.1.4: over UDP, the agent must discard such a response. |
| 249 | ++ bool unauthenticated_failed = false; |
| 250 | ++ if (run(401, false, &unauthenticated_failed) || unauthenticated_failed) { |
| 251 | ++ fprintf(stderr, "An unauthenticated 401 failed the selected pair\n"); |
| 252 | ++ printf("Failure\n"); |
| 253 | ++ return -1; |
| 254 | ++ } |
| 255 | ++ |
| 256 | ++ // An authenticated unrecoverable error still fails the pair (RFC 8445 7.2.5.2.4) |
| 257 | ++ bool authenticated_failed = false; |
| 258 | ++ if (run(400, true, &authenticated_failed) || !authenticated_failed) { |
| 259 | ++ fprintf(stderr, "An authenticated 400 did not fail the selected pair\n"); |
| 260 | ++ printf("Failure\n"); |
| 261 | ++ return -1; |
| 262 | ++ } |
| 263 | ++ |
| 264 | ++ printf("Success\n"); |
| 265 | ++ return 0; |
| 266 | ++#endif |
| 267 | ++} |
| 268 | +diff --git a/test/main.c b/test/main.c |
| 269 | +index 4b7748b9de699192974fcc91944a6df209d3e5aa..016c60268309b2bac073612af355a275c54d461a 100644 |
| 270 | +--- a/test/main.c |
| 271 | ++++ b/test/main.c |
| 272 | +@@ -21,6 +21,7 @@ int test_notrickle(void); |
| 273 | + int test_gathering(void); |
| 274 | + int test_turn(void); |
| 275 | + int test_conflict(void); |
| 276 | ++int test_error_integrity(void); |
| 277 | + int test_bind(void); |
| 278 | + int test_ufrag(void); |
| 279 | + int test_stun_unhandled(void); |
| 280 | +@@ -105,6 +106,12 @@ int main(int argc, char **argv) { |
| 281 | + return -1; |
| 282 | + } |
| 283 | + |
| 284 | ++ printf("\nRunning check error response integrity test...\n"); |
| 285 | ++ if (test_error_integrity()) { |
| 286 | ++ fprintf(stderr, "Check error response integrity test failed\n"); |
| 287 | ++ return -1; |
| 288 | ++ } |
| 289 | ++ |
| 290 | + printf("\nRunning connectivity test with bind address...\n"); |
| 291 | + if (test_bind()) { |
| 292 | + fprintf(stderr, "Connectivity test with bind address failed\n"); |
0 commit comments