Debian Codegraph @

test/rist/test_psk_cname_hijack.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC.
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Security regression guard: shared-PSK cname identity takeover.
5 *
6 * The original defect: listener-side cname re-association
7 * (try_listener_reassociate_by_cname) authenticated the migrating
8 * source using only the cname. Under a shared PSK the cname is not a
9 * per-peer secret, so an independent endpoint that held the group PSK
10 * and knew the victim's cname could be absorbed into the victim's peer
11 * record once the victim fell silent -- an identity takeover.
12 *
13 * The fix restricts the migration to peers with an authenticated
14 * per-peer SRP session (eap_is_authenticated()). Under a shared PSK
15 * the gate must now refuse migration, so the attacker must remain a
16 * SEPARATE caller and must never be absorbed into the victim's record.
17 *
18 * Topology (all on 127.0.0.1, MAIN profile, shared PSK):
19 * sender(listener, PSK, streams data)
20 * <-- victim (caller, cname=X, src port A) round 1
21 * <-- attacker(caller, cname=X, src port B) round 2
22 *
23 * The attacker is a SEPARATE rist_ctx with its own socket and its own
24 * receiver thread. It shares ONLY the PSK and the cname with the
25 * victim -- it never touches the victim's socket, keys, or peer object.
26 *
27 * Regression signal: with the fix in place the attacker is NOT
28 * absorbed, so the sender sees it as a new distinct caller
29 * (distinct_callers increments). If the attacker is ever absorbed
30 * (distinct_callers stays at the post-victim count) the PSK identity
31 * takeover has been reintroduced.
32 *
33 * Note: a listener-sender under a shared PSK broadcasts to every
34 * authenticated PSK caller, so the attacker receiving media is NOT by
35 * itself the defect -- absorption into the victim's record is.
36 *
37 * Exit codes:
38 * 0 - FIX HOLDS: PSK attacker was NOT absorbed (stayed a separate
39 * caller); the SRP-only gate refused the migration
40 * 1 - REGRESSION: PSK attacker was absorbed into the victim's peer
41 * record (PSK cname identity takeover reintroduced)
42 * 2 - INDETERMINATE: victim never established / never got data
43 * 99 - setup error
44 */
45
46 #include "librist/librist.h"
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stdbool.h>
51 #include "pthread-shim.h"
52
53 #ifdef _WIN32
54 #include <windows.h>
55 #define usleep(x) Sleep((x)/1000)
56 #else
57 #include <unistd.h>
58 #endif
59
60 #define LISTEN_PORT 19900
61 #define VICTIM_PORT 20011
62 #define ATTACKER_PORT 20012
63 #define CNAME "stream-1"
64 #define PSK "sharedgroupkey4321"
65
66 static struct rist_logging_settings *log_settings = NULL;
67
68 static pthread_mutex_t tracker_lock;
69 #define MAX_TRACKED_PEERS 16
70 static struct {
71 struct rist_peer *seen[MAX_TRACKED_PEERS];
72 int count;
73 } tracker;
74
75 static volatile int victim_pkts = 0;
76 static volatile int attacker_pkts = 0;
77 static volatile bool sender_run = true;
78
79 static int log_cb(void *arg, enum rist_log_level level, const char *msg) {
80 (void)arg;
81 const char *tag = level <= RIST_LOG_ERROR ? "E" :
82 level <= RIST_LOG_WARN ? "W" :
83 level <= RIST_LOG_INFO ? "I" : "D";
84 fprintf(stderr, "%s| %s", tag, msg);
85 return 0;
86 }
87
88 static void sender_status_cb(void *arg, struct rist_peer *peer,
89 enum rist_connection_status status) {
90 (void)arg;
91 if (status == RIST_CONNECTION_ESTABLISHED ||
92 status == RIST_CLIENT_CONNECTED) {
93 pthread_mutex_lock(&tracker_lock);
94 bool seen = false;
95 for (int i = 0; i < tracker.count; i++)
96 if (tracker.seen[i] == peer) { seen = true; break; }
97 if (!seen && tracker.count < MAX_TRACKED_PEERS) {
98 tracker.seen[tracker.count++] = peer;
99 fprintf(stderr, ">> sender CONNECTED (peer=%p, distinct_callers=%d)\n",
100 (void *)peer, tracker.count);
101 }
102 pthread_mutex_unlock(&tracker_lock);
103 }
104 }
105
106 static int victim_data_cb(void *arg, struct rist_data_block *b) {
107 (void)arg;
108 victim_pkts++;
109 rist_receiver_data_block_free2(&b);
110 return 0;
111 }
112 static int attacker_data_cb(void *arg, struct rist_data_block *b) {
113 (void)arg;
114 attacker_pkts++;
115 rist_receiver_data_block_free2(&b);
116 return 0;
117 }
118
119 static struct rist_ctx *start_receiver(const char *url, receiver_data_callback2_t cb) {
120 struct rist_ctx *rx = NULL;
121 if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0)
122 return NULL;
123 struct rist_peer_config *pcfg = NULL;
124 if (rist_parse_address2(url, (void *)&pcfg) != 0) { rist_destroy(rx); return NULL; }
125 struct rist_peer *peer = NULL;
126 if (rist_peer_create(rx, &peer, pcfg) != 0) { free(pcfg); rist_destroy(rx); return NULL; }
127 free(pcfg);
128 if (rist_receiver_data_callback_set2(rx, cb, NULL) != 0) { rist_destroy(rx); return NULL; }
129 if (rist_start(rx) != 0) { rist_destroy(rx); return NULL; }
130 return rx;
131 }
132
133 static PTHREAD_START_FUNC(sender_feed, arg) {
134 struct rist_ctx *tx = arg;
135 uint32_t counter = 0;
136 while (sender_run) {
137 struct rist_data_block blk;
138 memset(&blk, 0, sizeof(blk));
139 blk.payload = &counter;
140 blk.payload_len = sizeof(counter);
141 blk.virt_src_port = 1968;
142 rist_sender_data_write(tx, &blk);
143 counter++;
144 usleep(20000); /* ~50 pkt/s */
145 }
146 return 0;
147 }
148
149 int main(int argc, char *argv[]) {
150 char url[320];
151 /* Negative control: attacker presents a DIFFERENT cname. Expect no
152 * absorption (the cname is what drives the takeover). */
153 bool control = (argc > 1 && strcmp(argv[1], "control") == 0);
154 const char *attacker_cname = control ? "other-stream" : CNAME;
155
156 if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb, NULL, NULL, stderr) != 0)
157 return 99;
158
159 memset(&tracker, 0, sizeof(tracker));
160 pthread_mutex_init(&tracker_lock, NULL);
161
162 /* sender: listener, MAIN, shared PSK, streams data. */
163 struct rist_ctx *tx = NULL;
164 if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) return 99;
165 rist_connection_status_callback_set(tx, sender_status_cb, NULL);
166 snprintf(url, sizeof(url),
167 "rist://@127.0.0.1:%d?session-timeout=30000&keepalive-interval=1000"
168 "&secret=%s&aes-type=128", LISTEN_PORT, PSK);
169 struct rist_peer_config *tx_pcfg = NULL;
170 if (rist_parse_address2(url, (void *)&tx_pcfg) != 0) { rist_destroy(tx); return 99; }
171 struct rist_peer *tx_peer = NULL;
172 if (rist_peer_create(tx, &tx_peer, tx_pcfg) != 0) { free(tx_pcfg); rist_destroy(tx); return 99; }
173 free(tx_pcfg);
174 if (rist_start(tx) != 0) { rist_destroy(tx); return 99; }
175
176 pthread_t feeder;
177 pthread_create(&feeder, NULL, sender_feed, tx);
178 usleep(200000);
179
180 /* round 1: victim caller. */
181 fprintf(stderr, "\n== round 1: VICTIM connects (port %d, cname %s) ==\n",
182 VICTIM_PORT, CNAME);
183 snprintf(url, sizeof(url),
184 "rist://127.0.0.1:%d?local-port=%d&cname=%s"
185 "&session-timeout=30000&keepalive-interval=1000&secret=%s&aes-type=128",
186 LISTEN_PORT, VICTIM_PORT, CNAME, PSK);
187 struct rist_ctx *victim = start_receiver(url, victim_data_cb);
188 if (!victim) { sender_run = false; pthread_join(feeder, NULL); rist_destroy(tx); return 99; }
189 usleep(3000000);
190
191 pthread_mutex_lock(&tracker_lock);
192 int callers_after_victim = tracker.count;
193 pthread_mutex_unlock(&tracker_lock);
194 int victim_before = victim_pkts;
195 fprintf(stderr, "after victim: distinct_callers=%d, victim_pkts=%d\n",
196 callers_after_victim, victim_before);
197
198 /* victim falls silent (modelled by teardown), past 2*keepalive. */
199 fprintf(stderr, "\n== victim goes silent ==\n");
200 rist_destroy(victim);
201 usleep(3000000);
202
203 /* round 2: independent ATTACKER, shares ONLY PSK + cname. */
204 fprintf(stderr, "\n== round 2: ATTACKER connects (port %d, cname=%s, same PSK)%s ==\n",
205 ATTACKER_PORT, attacker_cname, control ? " [CONTROL: different cname]" : "");
206 snprintf(url, sizeof(url),
207 "rist://127.0.0.1:%d?local-port=%d&cname=%s"
208 "&session-timeout=30000&keepalive-interval=1000&secret=%s&aes-type=128",
209 LISTEN_PORT, ATTACKER_PORT, attacker_cname, PSK);
210 int attacker_start = attacker_pkts;
211 struct rist_ctx *attacker = start_receiver(url, attacker_data_cb);
212 if (!attacker) { sender_run = false; pthread_join(feeder, NULL); rist_destroy(tx); return 99; }
213 usleep(4000000);
214
215 pthread_mutex_lock(&tracker_lock);
216 int callers_after_attacker = tracker.count;
217 pthread_mutex_unlock(&tracker_lock);
218 int attacker_got = attacker_pkts - attacker_start;
219 fprintf(stderr, "\n== after attacker: distinct_callers=%d, attacker_pkts=%d ==\n",
220 callers_after_attacker, attacker_got);
221
222 sender_run = false;
223 pthread_join(feeder, NULL);
224 rist_destroy(attacker);
225 rist_destroy(tx);
226 free(log_settings);
227
228 if (victim_before == 0) {
229 fprintf(stderr, "INDETERMINATE: victim never received the stream.\n");
230 return 2;
231 }
232
233 bool absorbed = (callers_after_attacker == callers_after_victim); /* no new distinct caller */
234 bool attacker_received = (attacker_got > 0);
235
236 if (control) {
237 /* Control: a different cname is never a migration candidate.
238 * With the SRP-only fix neither cname is absorbed under PSK, so
239 * this just confirms the different-cname caller stays separate. */
240 fprintf(stderr, "\nCONTROL (different cname): absorbed=%d "
241 "(distinct_callers %d->%d), attacker_pkts=%d\n",
242 absorbed, callers_after_victim, callers_after_attacker, attacker_got);
243 if (!absorbed) {
244 fprintf(stderr, "CONTROL OK: different-cname caller stayed "
245 "separate.\n");
246 return 0;
247 }
248 fprintf(stderr, "CONTROL UNEXPECTED: different cname was absorbed.\n");
249 return 1;
250 }
251
252 if (absorbed) {
253 fprintf(stderr, "\nREGRESSION: independent attacker holding only the "
254 "shared PSK + cname \"%s\" was ABSORBED into the "
255 "victim's peer record (distinct_callers stayed %d). "
256 "Finding 1 (PSK cname identity takeover) has been "
257 "reintroduced.\n",
258 CNAME, callers_after_attacker);
259 return 1;
260 }
261 fprintf(stderr, "\nFIX HOLDS: PSK attacker was NOT absorbed; it became a "
262 "separate caller (distinct_callers %d->%d). The SRP-only "
263 "gate refused the cname migration. (attacker_received=%d is "
264 "expected PSK broadcast behaviour, not the defect.)\n",
265 callers_after_victim, callers_after_attacker, attacker_received);
266 return 0;
267 }