Skip to content

Commit

Permalink
cleaned up portability for MingGW compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdavidgraham committed Nov 16, 2023
1 parent cc40823 commit 0e9f811
Show file tree
Hide file tree
Showing 49 changed files with 206 additions and 239 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ probably faster than you want anyway.
A bounty is offered for vulnerabilities, see the VULNINFO.md file for more
information.

This project uses safe functions like `strcpy_s()` instead of unsafe functions
This project uses safe functions like `safe_strcpy()` instead of unsafe functions
like `strcpy()`.

This project has automated unit regression tests (`make regress`).
Expand Down
2 changes: 1 addition & 1 deletion src/crypto-blackrock2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "pixie-timer.h"
#include "unusedparm.h"
#include "util-malloc.h"
#include "string_s.h"
#include "util-safefunc.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion src/crypto-lcg.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "crypto-lcg.h"
#include "crypto-primegen.h" /* DJB's prime factoring code */
#include "string_s.h"
#include "util-safefunc.h"
#include "util-malloc.h"

#include <math.h> /* for 'sqrt()', may need -lm for gcc */
Expand Down
10 changes: 5 additions & 5 deletions src/in-binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "masscan-status.h"
#include "main-globals.h"
#include "output.h"
#include "string_s.h"
#include "util-safefunc.h"
#include "in-filter.h"
#include "in-report.h"
#include "util-malloc.h"
Expand Down Expand Up @@ -478,15 +478,15 @@ _binaryfile_parse(struct Output *out, const char *filename,
unsigned char *buf = 0;
size_t bytes_read;
uint64_t total_records = 0;
int x;

/* Allocate a buffer of up to one megabyte per record */
buf = MALLOC(BUF_MAX);

/* Open the file */
x = fopen_s(&fp, filename, "rb");
if (x != 0 || fp == NULL) {
perror(filename);
fp = fopen(filename, "rb");
if (fp == NULL) {
fprintf(stderr, "[-] FAIL: --readscan\n");
fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno));
goto end;
}

Expand Down
61 changes: 30 additions & 31 deletions src/main-conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "masscan.h"
#include "massip-addr.h"
#include "masscan-version.h"
#include "string_s.h"
#include "util-safefunc.h"
#include "util-logger.h"
#include "proto-banner1.h"
#include "templ-payloads.h"
Expand Down Expand Up @@ -296,7 +296,7 @@ masscan_echo_nic(struct Masscan *masscan, FILE *fp, unsigned i)
if (masscan->nic_count <= 1)
idx_str[0] = '\0';
else
sprintf_s(idx_str, sizeof(idx_str), "[%u]", i);
snprintf(idx_str, sizeof(idx_str), "[%u]", i);

if (masscan->nic[i].ifname[0])
fprintf(fp, "adapter%s = %s\n", idx_str, masscan->nic[i].ifname);
Expand Down Expand Up @@ -391,17 +391,16 @@ masscan_save_state(struct Masscan *masscan)
{
char filename[512];
FILE *fp;
int err;


strcpy_s(filename, sizeof(filename), "paused.conf");
safe_strcpy(filename, sizeof(filename), "paused.conf");
fprintf(stderr, " "
" \r");
fprintf(stderr, "saving resume file to: %s\n", filename);

err = fopen_s(&fp, filename, "wt");
if (err) {
perror(filename);
fp = fopen(filename, "wt");
if (fp == NULL) {
fprintf(stderr, "[-] FAIL: saving resume file\n");
fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno));
return;
}

Expand Down Expand Up @@ -1205,7 +1204,6 @@ static int SET_hello_file(struct Masscan *masscan, const char *name, const char
{
unsigned index;
FILE *fp;
int x;
char buf[16384];
char buf2[16384];
size_t bytes_read;
Expand All @@ -1225,10 +1223,10 @@ static int SET_hello_file(struct Masscan *masscan, const char *name, const char
}

/* When connecting via TCP, send this file */
x = fopen_s(&fp, value, "rb");
if (x != 0) {
LOG(0, "[FAILED] could not read hello file\n");
perror(value);
fp = fopen(value, "rb");
if (fp == NULL) {
LOG(0, "[-] [FAILED] --hello-file\n");
LOG(0, "[-] %s: %s\n", value, strerror(errno));
return CONF_ERR;
}

Expand All @@ -1244,7 +1242,7 @@ static int SET_hello_file(struct Masscan *masscan, const char *name, const char
bytes_encoded = base64_encode(buf2, sizeof(buf2)-1, buf, bytes_read);
buf2[bytes_encoded] = '\0';

sprintf_s(foo, sizeof(foo), "hello-string[%u]", (unsigned)index);
snprintf(foo, sizeof(foo), "hello-string[%u]", (unsigned)index);

masscan_set_parameter(masscan, foo, buf2);

Expand Down Expand Up @@ -1636,7 +1634,7 @@ static int SET_output_filename(struct Masscan *masscan, const char *name, const
}
if (masscan->output.format == 0)
masscan->output.format = Output_XML; /*TODO: Why is the default XML?*/
strcpy_s(masscan->output.filename,
safe_strcpy(masscan->output.filename,
sizeof(masscan->output.filename),
value);
return CONF_OK;
Expand Down Expand Up @@ -1806,7 +1804,7 @@ static int SET_pcap_filename(struct Masscan *masscan, const char *name, const ch
return 0;
}
if (value)
strcpy_s(masscan->pcap_filename, sizeof(masscan->pcap_filename), value);
safe_strcpy(masscan->pcap_filename, sizeof(masscan->pcap_filename), value);
return CONF_OK;
}

Expand Down Expand Up @@ -1960,7 +1958,7 @@ static int SET_rotate_directory(struct Masscan *masscan, const char *name, const
}
return 0;
}
strcpy_s( masscan->output.rotate.directory,
safe_strcpy( masscan->output.rotate.directory,
sizeof(masscan->output.rotate.directory),
value);
/* strip trailing slashes */
Expand Down Expand Up @@ -2087,7 +2085,7 @@ static int SET_output_stylesheet(struct Masscan *masscan, const char *name, cons
if (masscan->output.format == 0)
masscan->output.format = Output_XML;

strcpy_s(masscan->output.stylesheet, sizeof(masscan->output.stylesheet), value);
safe_strcpy(masscan->output.stylesheet, sizeof(masscan->output.stylesheet), value);
return CONF_OK;
}

Expand Down Expand Up @@ -2462,7 +2460,7 @@ masscan_set_parameter(struct Masscan *masscan,
}
if (masscan->nic_count < index + 1)
masscan->nic_count = index + 1;
sprintf_s( masscan->nic[index].ifname,
snprintf( masscan->nic[index].ifname,
sizeof(masscan->nic[index].ifname),
"%s",
value);
Expand Down Expand Up @@ -2751,7 +2749,7 @@ masscan_set_parameter(struct Masscan *masscan,
/* The timeout for banners TCP connections */
masscan->tcp_connection_timeout = (unsigned)parseInt(value);
} else if (EQUALS("datadir", name)) {
strcpy_s(masscan->nmap.datadir, sizeof(masscan->nmap.datadir), value);
safe_strcpy(masscan->nmap.datadir, sizeof(masscan->nmap.datadir), value);
} else if (EQUALS("data-length", name)) {
unsigned x = (unsigned)strtoul(value, 0, 0);
if (x >= 1514 - 14 - 40) {
Expand Down Expand Up @@ -2901,7 +2899,7 @@ masscan_set_parameter(struct Masscan *masscan,

masscan->redis.port = port;
masscan->output.format = Output_Redis;
strcpy_s(masscan->output.filename,
safe_strcpy(masscan->output.filename,
sizeof(masscan->output.filename),
"<redis>");
} else if(EQUALS("redis-pwd", name)) {
Expand Down Expand Up @@ -3156,17 +3154,16 @@ masscan_load_database_files(struct Masscan *masscan)
}

/*
* "nmap-payloads"
* `--nmap-payloads`
*/
filename = masscan->payloads.nmap_payloads_filename;
if (filename) {
FILE *fp;
int err;


err = fopen_s(&fp, filename, "rt");
if (err || fp == NULL) {
perror(filename);
fp = fopen(filename, "rt");
if (fp == NULL) {
fprintf(stderr, "[-] FAIL: --nmap-payloads\n");
fprintf(stderr, "[-] %s:%s\n", filename, strerror(errno));
} else {
if (masscan->payloads.udp == NULL)
masscan->payloads.udp = payloads_udp_create();
Expand Down Expand Up @@ -3869,14 +3866,16 @@ void
masscan_read_config_file(struct Masscan *masscan, const char *filename)
{
FILE *fp;
errno_t err;
char line[65536];

err = fopen_s(&fp, filename, "rt");
if (err) {
fp = fopen(filename, "rt");
if (fp == NULL) {
char dir[512];
char *x;
perror(filename);

fprintf(stderr, "[-] FAIL: reading configuration file\n");
fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno));

x = getcwd(dir, sizeof(dir));
if (x)
fprintf(stderr, "[-] cwd = %s\n", dir);
Expand Down
36 changes: 18 additions & 18 deletions src/main-ptrace.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "main-ptrace.h"
#include "proto-preprocess.h"
#include "pixie-timer.h"
#include "string_s.h"
#include "util-safefunc.h"


/***************************************************************************
Expand Down Expand Up @@ -35,20 +35,20 @@ packet_trace(FILE *fp, double pt_start, const unsigned char *px, size_t length,

/* format the IP addresses into fixed-width fields */
fmt = ipaddress_fmt(parsed.src_ip);
sprintf_s(from, sizeof(from), "[%s]:%u", fmt.string, parsed.port_src);
snprintf(from, sizeof(from), "[%s]:%u", fmt.string, parsed.port_src);

fmt = ipaddress_fmt(parsed.dst_ip);
sprintf_s(to, sizeof(to), "[%s]:%u", fmt.string, parsed.port_dst);
snprintf(to, sizeof(to), "[%s]:%u", fmt.string, parsed.port_dst);

switch (parsed.found) {
case FOUND_ARP:
type = px[offset+6]<<8 | px[offset+7];
*strchr(to, ':') = '\0';
*strchr(from, ':') = '\0';
switch (type) {
case 1:strcpy_s(sz_type, sizeof(sz_type), "request"); break;
case 2:strcpy_s(sz_type, sizeof(sz_type), "response"); break;
default: sprintf_s(sz_type, sizeof(sz_type), "unknown(%u)", type); break;
case 1:safe_strcpy(sz_type, sizeof(sz_type), "request"); break;
case 2:safe_strcpy(sz_type, sizeof(sz_type), "response"); break;
default: snprintf(sz_type, sizeof(sz_type), "unknown(%u)", type); break;
}
fprintf(fp, "%s (%5.4f) ARP %-21s > %-21s %s\n", direction,
timestamp - pt_start, from, to, sz_type);
Expand All @@ -65,19 +65,19 @@ packet_trace(FILE *fp, double pt_start, const unsigned char *px, size_t length,
case FOUND_TCP:
type = px[offset+13];
switch (type) {
case 0x00: strcpy_s(sz_type, sizeof(sz_type), "NULL"); break;
case 0x01: strcpy_s(sz_type, sizeof(sz_type), "FIN"); break;
case 0x11: strcpy_s(sz_type, sizeof(sz_type), "FIN-ACK"); break;
case 0x19: strcpy_s(sz_type, sizeof(sz_type), "FIN-ACK-PSH"); break;
case 0x02: strcpy_s(sz_type, sizeof(sz_type), "SYN"); break;
case 0x12: strcpy_s(sz_type, sizeof(sz_type), "SYN-ACK"); break;
case 0x04: strcpy_s(sz_type, sizeof(sz_type), "RST"); break;
case 0x14: strcpy_s(sz_type, sizeof(sz_type), "RST-ACK"); break;
case 0x15: strcpy_s(sz_type, sizeof(sz_type), "RST-FIN-ACK"); break;
case 0x10: strcpy_s(sz_type, sizeof(sz_type), "ACK"); break;
case 0x18: strcpy_s(sz_type, sizeof(sz_type), "ACK-PSH"); break;
case 0x00: safe_strcpy(sz_type, sizeof(sz_type), "NULL"); break;
case 0x01: safe_strcpy(sz_type, sizeof(sz_type), "FIN"); break;
case 0x11: safe_strcpy(sz_type, sizeof(sz_type), "FIN-ACK"); break;
case 0x19: safe_strcpy(sz_type, sizeof(sz_type), "FIN-ACK-PSH"); break;
case 0x02: safe_strcpy(sz_type, sizeof(sz_type), "SYN"); break;
case 0x12: safe_strcpy(sz_type, sizeof(sz_type), "SYN-ACK"); break;
case 0x04: safe_strcpy(sz_type, sizeof(sz_type), "RST"); break;
case 0x14: safe_strcpy(sz_type, sizeof(sz_type), "RST-ACK"); break;
case 0x15: safe_strcpy(sz_type, sizeof(sz_type), "RST-FIN-ACK"); break;
case 0x10: safe_strcpy(sz_type, sizeof(sz_type), "ACK"); break;
case 0x18: safe_strcpy(sz_type, sizeof(sz_type), "ACK-PSH"); break;
default:
sprintf_s(sz_type, sizeof(sz_type),
snprintf(sz_type, sizeof(sz_type),
"%s%s%s%s%s%s%s%s",
(type&0x01)?"FIN":"",
(type&0x02)?"SYN":"",
Expand Down
2 changes: 1 addition & 1 deletion src/main-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "pixie-timer.h"
#include "unusedparm.h"
#include "main-globals.h"
#include "string_s.h"
#include "util-safefunc.h"
#include "util-bool.h"
#include <stdio.h>

Expand Down
10 changes: 5 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,15 +687,15 @@ receive_thread(void *v)

if (masscan->tcp_connection_timeout) {
char foo[64];
sprintf_s(foo, sizeof(foo), "%u", masscan->tcp_connection_timeout);
snprintf(foo, sizeof(foo), "%u", masscan->tcp_connection_timeout);
tcpcon_set_parameter( tcpcon,
"timeout",
strlen(foo),
foo);
}
if (masscan->tcp_hello_timeout) {
char foo[64];
sprintf_s(foo, sizeof(foo), "%u", masscan->tcp_hello_timeout);
snprintf(foo, sizeof(foo), "%u", masscan->tcp_hello_timeout);
tcpcon_set_parameter( tcpcon,
"hello-timeout",
strlen(foo),
Expand Down Expand Up @@ -726,7 +726,7 @@ receive_thread(void *v)

for (pay = masscan->payloads.tcp; pay; pay = pay->next) {
char name[64];
sprintf_s(name, sizeof(name), "hello-string[%u]", pay->port);
snprintf(name, sizeof(name), "hello-string[%u]", pay->port);
tcpcon_set_parameter( tcpcon,
name,
strlen(pay->payload_base64),
Expand Down Expand Up @@ -1347,7 +1347,7 @@ main_scan(struct Masscan *masscan)
struct tm x;

now = time(0);
gmtime_s(&x, &now);
safe_gmtime(&x, &now);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S GMT", &x);
LOG(0, "Starting masscan " MASSCAN_VERSION " (http://bit.ly/14GZzcT) at %s\n",
buffer);
Expand Down Expand Up @@ -1604,7 +1604,7 @@ int main(int argc, char *argv[])
masscan->redis.password = NULL;
masscan->payloads.udp = payloads_udp_create();
masscan->payloads.oproto = payloads_oproto_create();
strcpy_s( masscan->output.rotate.directory,
safe_strcpy( masscan->output.rotate.directory,
sizeof(masscan->output.rotate.directory),
".");
masscan->is_capture_cert = 1;
Expand Down
4 changes: 2 additions & 2 deletions src/masscan-app.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "masscan-app.h"
#include "string_s.h"
#include "util-safefunc.h"

/******************************************************************************
* When outputting results, we call this function to print out the type of
Expand Down Expand Up @@ -51,7 +51,7 @@ masscan_app_to_string(enum ApplicationProtocol proto)
case PROTO_ERROR: return "error";

default:
sprintf_s(tmp, sizeof(tmp), "(%u)", proto);
snprintf(tmp, sizeof(tmp), "(%u)", proto);
return tmp;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/masscan.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MASSCAN_H
#define MASSCAN_H
#include "massip-addr.h"
#include "string_s.h"
#include "util-safefunc.h"
#include "stack-src.h"
#include "massip.h"
#include "util-bool.h"
Expand Down
Loading

0 comments on commit 0e9f811

Please sign in to comment.