Skip to content

Commit 4b0d808

Browse files
committed
EAP: Modernize packet parsing
Enable ND_LONGJMP_FROM_TCHECK and remove the 'trunc' labels. Report invalid packets as invalid. Use ND_ICHECK_U() for length check. Remove a redundant ND_TCHECK_SIZE(). Add a const qualifier for the eap_print() parameter 'length'. Update a test output accordingly.
1 parent 851d47b commit 4b0d808

File tree

3 files changed

+20
-46
lines changed

3 files changed

+20
-46
lines changed

netdissect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ extern void domain_print(netdissect_options *, const u_char *, u_int, int, int);
631631
extern int dstopt_process(netdissect_options *, const u_char *);
632632
extern void dtp_print(netdissect_options *, const u_char *, u_int);
633633
extern void dvmrp_print(netdissect_options *, const u_char *, u_int);
634-
extern void eap_print(netdissect_options *, const u_char *, u_int);
634+
extern void eap_print(netdissect_options *, const u_char *, const u_int);
635635
extern void eapol_print(netdissect_options *, const u_char *);
636636
extern void egp_print(netdissect_options *, const u_char *, u_int);
637637
extern void eigrp_print(netdissect_options *, const u_char *, u_int);

print-eap.c

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "netdissect-stdinc.h"
2828

29+
#define ND_LONGJMP_FROM_TCHECK
2930
#include "netdissect.h"
3031
#include "extract.h"
3132

@@ -149,7 +150,7 @@ static const struct tok eap_aka_subtype_values[] = {
149150
void
150151
eap_print(netdissect_options *ndo,
151152
const u_char *cp,
152-
u_int length)
153+
const u_int length)
153154
{
154155
u_int type, subtype, len;
155156
u_int count;
@@ -158,6 +159,7 @@ eap_print(netdissect_options *ndo,
158159
ndo->ndo_protocol = "eap";
159160
type = GET_U_1(cp);
160161
len = GET_BE_U_2(cp + 2);
162+
ND_ICHECK_U(len, <, 4);
161163
if (len != length) {
162164
/*
163165
* Probably a fragment; in some cases the fragmentation might
@@ -173,19 +175,12 @@ eap_print(netdissect_options *ndo,
173175
type,
174176
GET_U_1((cp + 1)),
175177
len);
176-
if (len < 4) {
177-
ND_PRINT(" (too short for EAP header)");
178-
return;
179-
}
180178

181179
ND_TCHECK_LEN(cp, len);
182180

183181
if (type == EAP_REQUEST || type == EAP_RESPONSE) {
184182
/* RFC 3748 Section 4.1 */
185-
if (len < 5) {
186-
ND_PRINT(" (too short for EAP request/response)");
187-
return;
188-
}
183+
ND_ICHECK_U(len, <, 5);
189184
subtype = GET_U_1(cp + 4);
190185
ND_PRINT("\n\t\t Type %s (%u)",
191186
tok2str(eap_type_values, "unknown", subtype),
@@ -202,10 +197,7 @@ eap_print(netdissect_options *ndo,
202197

203198
case EAP_TYPE_NOTIFICATION:
204199
/* According to RFC 3748, there must be at least one octet of message */
205-
if (len < 6) {
206-
ND_PRINT(" (too short for EAP Notification request/response)");
207-
return;
208-
}
200+
ND_ICHECK_U(len, <, 6);
209201
ND_PRINT(", Notification: ");
210202
nd_printjnp(ndo, cp + 5, len - 5);
211203
break;
@@ -216,10 +208,7 @@ eap_print(netdissect_options *ndo,
216208
* the desired authentication
217209
* type one octet per type
218210
*/
219-
if (len < 6) {
220-
ND_PRINT(" (too short for EAP Legacy NAK request/response)");
221-
return;
222-
}
211+
ND_ICHECK_U(len, <, 6);
223212
sep = "";
224213
for (count = 5; count < len; count++) {
225214
ND_PRINT("%s %s (%u)", sep,
@@ -231,10 +220,7 @@ eap_print(netdissect_options *ndo,
231220

232221
case EAP_TYPE_TTLS:
233222
case EAP_TYPE_TLS:
234-
if (len < 6) {
235-
ND_PRINT(" (too short for EAP TLS/TTLS request/response)");
236-
return;
237-
}
223+
ND_ICHECK_U(len, <, 6);
238224
if (subtype == EAP_TYPE_TTLS)
239225
ND_PRINT(" TTLSv%u",
240226
EAP_TTLS_VERSION(GET_U_1((cp + 5))));
@@ -243,30 +229,21 @@ eap_print(netdissect_options *ndo,
243229
GET_U_1(cp + 5));
244230

245231
if (EAP_TLS_EXTRACT_BIT_L(GET_U_1(cp + 5))) {
246-
if (len < 10) {
247-
ND_PRINT(" (too short for EAP TLS/TTLS request/response with length)");
248-
return;
249-
}
232+
ND_ICHECK_U(len, <, 10);
250233
ND_PRINT(", len %u", GET_BE_U_4(cp + 6));
251234
}
252235
break;
253236

254237
case EAP_TYPE_FAST:
255-
if (len < 6) {
256-
ND_PRINT(" (too short for EAP FAST request/response)");
257-
return;
258-
}
238+
ND_ICHECK_U(len, <, 6);
259239
ND_PRINT(" FASTv%u",
260240
EAP_TTLS_VERSION(GET_U_1((cp + 5))));
261241
ND_PRINT(" flags [%s] 0x%02x",
262242
bittok2str(eap_tls_flags_values, "none", GET_U_1((cp + 5))),
263243
GET_U_1(cp + 5));
264244

265245
if (EAP_TLS_EXTRACT_BIT_L(GET_U_1(cp + 5))) {
266-
if (len < 10) {
267-
ND_PRINT(" (too short for EAP FAST request/response with length)");
268-
return;
269-
}
246+
ND_ICHECK_U(len, <, 10);
270247
ND_PRINT(", len %u", GET_BE_U_4(cp + 6));
271248
}
272249

@@ -275,10 +252,7 @@ eap_print(netdissect_options *ndo,
275252

276253
case EAP_TYPE_AKA:
277254
case EAP_TYPE_SIM:
278-
if (len < 6) {
279-
ND_PRINT(" (too short for EAP SIM/AKA request/response)");
280-
return;
281-
}
255+
ND_ICHECK_U(len, <, 6);
282256
ND_PRINT(" subtype [%s] 0x%02x",
283257
tok2str(eap_aka_subtype_values, "unknown", GET_U_1((cp + 5))),
284258
GET_U_1(cp + 5));
@@ -296,8 +270,9 @@ eap_print(netdissect_options *ndo,
296270
}
297271
}
298272
return;
299-
trunc:
300-
nd_print_trunc(ndo);
273+
274+
invalid:
275+
nd_print_invalid(ndo);
301276
}
302277

303278
void
@@ -309,7 +284,6 @@ eapol_print(netdissect_options *ndo,
309284

310285
ndo->ndo_protocol = "eap";
311286
eap = (const struct eap_frame_t *)cp;
312-
ND_TCHECK_SIZE(eap);
313287
eap_type = GET_U_1(eap->type);
314288

315289
ND_PRINT("%s (%u) v%u, len %u",
@@ -326,17 +300,17 @@ eapol_print(netdissect_options *ndo,
326300
switch (eap_type) {
327301
case EAP_FRAME_TYPE_PACKET:
328302
if (eap_len == 0)
329-
goto trunc;
303+
goto invalid;
330304
ND_PRINT(", ");
331305
eap_print(ndo, cp, eap_len);
332-
return;
306+
break;
333307
case EAP_FRAME_TYPE_LOGOFF:
334308
case EAP_FRAME_TYPE_ENCAP_ASF_ALERT:
335309
default:
336310
break;
337311
}
338312
return;
339313

340-
trunc:
341-
nd_print_trunc(ndo);
314+
invalid:
315+
nd_print_invalid(ndo);
342316
}

tests/eap_extract_read2_asan.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1 06:58:21.3759079661 EAP packet (0) v155, len 0 [|eap]
1+
1 06:58:21.3759079661 EAP packet (0) v155, len 0 (invalid)

0 commit comments

Comments
 (0)