Skip to content

Commit f7710a1

Browse files
committed
Add strict pattern matching on response when pattern was provided
Signed-off-by: Singh <[email protected]>
1 parent bffc0e9 commit f7710a1

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

ping.c

+2
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,8 @@ ping4_parse_reply(struct socket_st *sock, struct msghdr *msg, int cc, void *addr
11031103
if (icp->type == ICMP_ECHOREPLY) {
11041104
if (!is_ours(sock, icp->un.echo.id))
11051105
return 1; /* 'Twas not our ECHO */
1106+
if (!contains_pattern_in_payload((__u8*)(icp+1)))
1107+
return 1; /* 'Twas really not our ECHO */
11061108
if (gather_statistics((__u8*)icp, sizeof(*icp), cc,
11071109
ntohs(icp->un.echo.sequence),
11081110
ttl, 0, tv, pr_addr(from, sizeof *from),

ping.h

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ extern ping_func_set_st ping4_func_set;
313313
extern int pinger(ping_func_set_st *fset, socket_st *sock);
314314
extern void sock_setbufs(socket_st*, int alloc);
315315
extern void setup(socket_st *);
316+
extern int contains_pattern_in_payload(__u8 *ptr);
316317
extern void main_loop(ping_func_set_st *fset, socket_st*, __u8 *buf, int buflen) __attribute__((noreturn));
317318
extern void finish(void) __attribute__((noreturn));
318319
extern void status(void);

ping6_common.c

+2
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,8 @@ ping6_parse_reply(socket_st *sock, struct msghdr *msg, int cc, void *addr, struc
14201420
if (icmph->icmp6_type == ICMP6_ECHO_REPLY) {
14211421
if (!is_ours(sock, icmph->icmp6_id))
14221422
return 1;
1423+
if (!contains_pattern_in_payload((__u8*)(icmph+1)))
1424+
return 1; /* 'Twas really not our ECHO */
14231425
if (gather_statistics((__u8*)icmph, sizeof(*icmph), cc,
14241426
ntohs(icmph->icmp6_seq),
14251427
hops, 0, tv, pr_addr(from, sizeof *from),

ping_common.c

+18
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,24 @@ void setup(socket_st *sock)
565565
}
566566
}
567567

568+
/*
569+
* Return 0 if pattern in payload point to be ptr did not match the pattern that was sent
570+
*/
571+
int contains_pattern_in_payload(__u8 *ptr)
572+
{
573+
int i;
574+
__u8 *cp, *dp;
575+
576+
/* check the data */
577+
cp = ((u_char*)ptr) + sizeof(struct timeval);
578+
dp = &outpack[8 + sizeof(struct timeval)];
579+
for (i = sizeof(struct timeval); i < datalen; ++i, ++cp, ++dp) {
580+
if (*cp != *dp)
581+
return 0;
582+
}
583+
return 1;
584+
}
585+
568586
void main_loop(ping_func_set_st *fset, socket_st *sock, __u8 *packet, int packlen)
569587
{
570588
char addrbuf[128];

0 commit comments

Comments
 (0)