Skip to content

Commit

Permalink
Fix possible thread-safety issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Apr 26, 2024
1 parent 39602ca commit 8e5ba1a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
9 changes: 5 additions & 4 deletions src/rawsock-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdatomic.h>
#include <pcap.h>

#include "rawsock.h"
Expand All @@ -10,7 +11,7 @@
static pcap_t *handle;
static pcap_dumper_t *dumper;
static int linktype;
static bool want_break;
static atomic_bool want_break;

static void callback_fwd(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);

Expand Down Expand Up @@ -124,13 +125,13 @@ int rawsock_sniff(uint64_t *ts, int *length, const uint8_t **pkt)

int rawsock_loop(rawsock_callback func)
{
want_break = false;
atomic_store(&want_break, false);

// pretend to loop if dead handle (dump mode)
if(dumper) {
do
usleep(150*1000);
while(!want_break);
while(!atomic_load(&want_break));
return 0;
}

Expand All @@ -144,7 +145,7 @@ int rawsock_loop(rawsock_callback func)

void rawsock_breakloop(void)
{
want_break = true;
atomic_store(&want_break, true);
// calling pcap_breakloop on a dead handle should be a a no-op, but
// actually segfaults on libpcap 1.10.1 or older.
if(!dumper) {
Expand Down
9 changes: 5 additions & 4 deletions src/scan-responder.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdbool.h>
#include <string.h>
#include <unistd.h> // usleep()
#include <stdatomic.h>
#include <pthread.h>

#include "scan.h"
Expand All @@ -21,7 +22,7 @@ static struct {
uint8_t _Alignas(uint32_t) buffer[FRAME_ETH_SIZE + FRAME_IP_SIZE + TCP_HEADER_SIZE + BANNER_QUERY_MAX_LENGTH];

pthread_t tcp_thread;
bool tcp_thread_exit;
atomic_bool tcp_thread_exit;
} responder;

static void *tcp_thread(void *unused);
Expand All @@ -38,7 +39,7 @@ int scan_responder_init(FILE *outfile, const struct outputdef *outdef, uint16_t
responder.outdef = outdef;
responder.source_port = source_port;

responder.tcp_thread_exit = 0;
atomic_store(&responder.tcp_thread_exit, false);
if(pthread_create(&responder.tcp_thread, NULL, tcp_thread, NULL) < 0)
return -1;

Expand Down Expand Up @@ -231,12 +232,12 @@ static void *tcp_thread(void *unused)
// destroy tcp session
tcp_state_delete(&p);
}
} while(!responder.tcp_thread_exit);
} while(!atomic_load(&responder.tcp_thread_exit));
return NULL;
}

void scan_responder_finish()
{
responder.tcp_thread_exit = 1;
atomic_store(&responder.tcp_thread_exit, true);
pthread_join(responder.tcp_thread, NULL);
}
38 changes: 18 additions & 20 deletions src/scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#include "banner.h"

enum {
ERROR_SEND_THREAD = (1 << 0),
ERROR_RECV_THREAD = (1 << 1),
SEND_FINISHED = (1 << 0),
ERROR_SEND_THREAD = (1 << 1),
ERROR_RECV_THREAD = (1 << 2),
};

static uint8_t source_addr[16];
Expand All @@ -35,8 +36,7 @@ static FILE *outfile;
static struct outputdef outdef;

static atomic_uint pkts_sent, pkts_recv;
static atomic_uchar error_mask;
static bool send_finished;
static atomic_uchar status_bits;

static inline int source_port_rand(void);
static void *send_thread_tcp(void *unused);
Expand Down Expand Up @@ -82,8 +82,7 @@ int scan_main(const char *interface, int quiet)
return -1;
atomic_store(&pkts_sent, 0);
atomic_store(&pkts_recv, 0);
atomic_store(&error_mask, 0);
send_finished = false;
atomic_store(&status_bits, 0);
if(banners && ip_type == IP_TYPE_TCP) {
if(scan_responder_init(outfile, &outdef, source_port) < 0)
goto err;
Expand Down Expand Up @@ -124,7 +123,7 @@ int scan_main(const char *interface, int quiet)
pthread_detach(ts);

// Stats & progress watching
unsigned char cur_error = 0;
unsigned char cur_status = 0;
while(1) {
unsigned int cur_sent, cur_recv;
cur_sent = atomic_exchange(&pkts_sent, 0);
Expand All @@ -136,18 +135,17 @@ int scan_main(const char *interface, int quiet)
else
fprintf(stderr, "snt:%5u rcv:%5u p:%3d%%\r", cur_sent, cur_recv, (int) (progress*100));
}
if(send_finished)
break;
cur_error = atomic_load(&error_mask);
if(cur_error)
cur_status = atomic_load(&status_bits);
if(cur_status)
break;

usleep(STATS_INTERVAL * 1000);
}
cur_status &= ~SEND_FINISHED; // leave only error bits

// Wait for the last packets to arrive
fputs("", stderr);
if(!cur_error) {
if(!cur_status) {
fprintf(stderr, "Waiting %d more seconds...\n", FINISH_WAIT_TIME);
usleep(FINISH_WAIT_TIME * 1000 * 1000);
} else {
Expand All @@ -157,7 +155,7 @@ int scan_main(const char *interface, int quiet)
rawsock_breakloop();
if(banners && ip_type == IP_TYPE_TCP)
scan_responder_finish();
if(!quiet && !cur_error)
if(!quiet && !cur_status)
fprintf(stderr, "rcv:%5u\n", atomic_exchange(&pkts_recv, 0));

// Write output file footer
Expand Down Expand Up @@ -215,10 +213,10 @@ static void *send_thread_tcp(void *unused)
}
}

send_finished = true;
atomic_fetch_or(&status_bits, SEND_FINISHED);
return NULL;
err:
atomic_fetch_or(&error_mask, ERROR_SEND_THREAD);
atomic_fetch_or(&status_bits, ERROR_SEND_THREAD);
return NULL;
}

Expand Down Expand Up @@ -275,10 +273,10 @@ static void *send_thread_udp(void *unused)
}
}

send_finished = true;
atomic_fetch_or(&status_bits, SEND_FINISHED);
return NULL;
err:
atomic_fetch_or(&error_mask, ERROR_SEND_THREAD);
atomic_fetch_or(&status_bits, ERROR_SEND_THREAD);
return NULL;
}

Expand Down Expand Up @@ -317,10 +315,10 @@ static void *send_thread_icmp(void *unused)
rawsock_ip_modify(IP_FRAME(packet), ICMP_HEADER_SIZE, dstaddr);
}

send_finished = true;
atomic_fetch_or(&status_bits, SEND_FINISHED);
return NULL;
err:
atomic_fetch_or(&error_mask, ERROR_SEND_THREAD);
atomic_fetch_or(&status_bits, ERROR_SEND_THREAD);
return NULL;
}

Expand All @@ -334,7 +332,7 @@ static void *recv_thread(void *unused)

int r = rawsock_loop(recv_handler);
if(r < 0)
atomic_fetch_or(&error_mask, ERROR_RECV_THREAD);
atomic_fetch_or(&status_bits, ERROR_RECV_THREAD);
return NULL;
}

Expand Down

0 comments on commit 8e5ba1a

Please sign in to comment.