Commit b2c7806
committed
fix(stun): harden packet decode and listener shutdown
Fixes three crash paths in STUN discovery, one observed in the wild and
two found while tracing its error path.
ReadPacketData can return an empty buffer with a nil error, so slicing
at payloadOff panicked with "slice bounds out of range [32:0]" on
darwin/bsd. Guard the length before slicing, and guard the equivalent
buf[8:] on Linux.
Parse returns nil when the reply carries no XOR-MAPPED-ADDRESS, which
Connect then dereferenced. An RFC 3489-era server answering with only
MAPPED-ADDRESS would crash the daemon. Return ErrNoMappedAddress instead
so the resolver falls through to the next configured server.
Stop and the listener goroutines shared no shutdown signal. A listener
parked handing off a reply that arrived after Read timed out would race
Stop's close of packetChan and panic on Linux, or block Stop's
waitGroup.Wait until context cancellation on darwin/bsd. Add a done
channel and stop closing packetChan: Read already selects with a
timeout, so the close bought nothing and only opened the race. This also
stops the Linux listener spinning on a closed socket until its timeout.
Not covered by new tests: the short-packet and shutdown paths need a raw
socket or pcap handle, so they are verified by construction and the
existing suite only.
Signed-off-by: Date Huang <tjjh89017@hotmail.com>1 parent 83495ac commit b2c7806
3 files changed
Lines changed: 65 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
14 | 18 | | |
15 | 19 | | |
16 | 20 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
41 | 47 | | |
42 | 48 | | |
43 | 49 | | |
| |||
153 | 159 | | |
154 | 160 | | |
155 | 161 | | |
| 162 | + | |
156 | 163 | | |
157 | 164 | | |
158 | 165 | | |
| |||
198 | 205 | | |
199 | 206 | | |
200 | 207 | | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
201 | 212 | | |
| 213 | + | |
202 | 214 | | |
203 | | - | |
204 | 215 | | |
205 | 216 | | |
206 | 217 | | |
| |||
227 | 238 | | |
228 | 239 | | |
229 | 240 | | |
| 241 | + | |
| 242 | + | |
230 | 243 | | |
231 | 244 | | |
232 | 245 | | |
| |||
239 | 252 | | |
240 | 253 | | |
241 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
242 | 263 | | |
243 | 264 | | |
244 | 265 | | |
| |||
251 | 272 | | |
252 | 273 | | |
253 | 274 | | |
| 275 | + | |
| 276 | + | |
254 | 277 | | |
255 | 278 | | |
256 | 279 | | |
| |||
298 | 321 | | |
299 | 322 | | |
300 | 323 | | |
| 324 | + | |
| 325 | + | |
301 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
302 | 330 | | |
303 | 331 | | |
304 | 332 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
28 | 35 | | |
29 | 36 | | |
30 | 37 | | |
| |||
116 | 123 | | |
117 | 124 | | |
118 | 125 | | |
| 126 | + | |
119 | 127 | | |
120 | 128 | | |
121 | 129 | | |
| |||
125 | 133 | | |
126 | 134 | | |
127 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
128 | 140 | | |
129 | | - | |
| 141 | + | |
130 | 142 | | |
131 | 143 | | |
132 | 144 | | |
| |||
150 | 162 | | |
151 | 163 | | |
152 | 164 | | |
| 165 | + | |
| 166 | + | |
153 | 167 | | |
154 | 168 | | |
155 | 169 | | |
156 | 170 | | |
157 | 171 | | |
158 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
159 | 180 | | |
160 | 181 | | |
161 | 182 | | |
162 | 183 | | |
163 | 184 | | |
164 | 185 | | |
165 | 186 | | |
| 187 | + | |
| 188 | + | |
166 | 189 | | |
167 | 190 | | |
168 | 191 | | |
| |||
218 | 241 | | |
219 | 242 | | |
220 | 243 | | |
| 244 | + | |
| 245 | + | |
221 | 246 | | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
222 | 250 | | |
223 | 251 | | |
224 | 252 | | |
| |||
229 | 257 | | |
230 | 258 | | |
231 | 259 | | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
232 | 263 | | |
233 | 264 | | |
234 | 265 | | |
| |||
0 commit comments