Fixed more serious and less serious issues

Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
This commit is contained in:
Toni Uhlig 2026-05-17 14:14:26 +02:00
parent b5bdcebbce
commit 29e0a3ab24
No known key found for this signature in database
GPG key ID: 22C5333D922537D2
4 changed files with 65 additions and 28 deletions

View file

@ -432,14 +432,14 @@ static inline struct nDPIsrvd_socket * nDPIsrvd_socket_init(size_t global_user_d
instance_cleanup_callback instance_cleanup_cb,
flow_cleanup_callback flow_cleanup_callback_cb)
{
static const UT_icd json_token_icd = {sizeof(struct nDPIsrvd_json_token), NULL, NULL, NULL};
struct nDPIsrvd_socket * sock = (struct nDPIsrvd_socket *)nDPIsrvd_calloc(1, sizeof(*sock) + global_user_data_size);
if (json_cb == NULL)
{
goto error;
return NULL;
}
static const UT_icd json_token_icd = {sizeof(struct nDPIsrvd_json_token), NULL, NULL, NULL};
struct nDPIsrvd_socket * sock = (struct nDPIsrvd_socket *)nDPIsrvd_calloc(1, sizeof(*sock) + global_user_data_size);
if (sock != NULL)
{
sock->fd = -1;
@ -785,9 +785,14 @@ static inline enum nDPIsrvd_conversion_return str_value_to_ull(char const * cons
{
char * endptr = NULL;
errno = 0;
if (value_as_string == NULL)
{
return CONVERSION_NOT_A_NUMBER;
}
*value = strtoull(value_as_string, &endptr, 10);
if (value_as_string == NULL || value_as_string == endptr)
if (value_as_string == endptr)
{
return CONVERSION_NOT_A_NUMBER;
}

26
nDPId.c
View file

@ -796,8 +796,12 @@ static uLong zlib_deflate(void * const src, int srcLen, void * const dst, int ds
{
ret = strm.total_out;
MT_GET_AND_ADD(zlib_compressions, 1);
MT_GET_AND_ADD(zlib_compression_diff, srcLen - ret);
MT_GET_AND_ADD(zlib_compression_bytes, ret);
if (ret < (uLong)srcLen) {
MT_GET_AND_ADD(zlib_compression_diff, srcLen - ret);
MT_GET_AND_ADD(zlib_compression_bytes, ret);
} else {
// TODO: Compression did not improve anything, use a new stats counter?
}
}
else
{
@ -838,7 +842,11 @@ static uLong zlib_inflate(void * const src, int srcLen, void * const dst, int ds
{
ret = strm.total_out;
MT_GET_AND_ADD(zlib_decompressions, 1);
MT_GET_AND_SUB(zlib_compression_diff, ret - srcLen);
if (ret > (uLong)srcLen) {
MT_GET_AND_SUB(zlib_compression_diff, ret - srcLen);
} else {
// TODO: See deflate above.
}
}
else
{
@ -1217,8 +1225,6 @@ static void * ndpi_realloc_wrapper(void * const reallocable, size_t new_size)
void * const new_ptr = realloc(p, sizeof(uint64_t) + new_size);
if (new_ptr == NULL) {
// Do not return the valid pointer as it is old pointer of old size
ndpi_free_wrapper(reallocable);
return NULL;
}
@ -2800,6 +2806,10 @@ static void send_to_collector(struct nDPId_reader_thread * const reader_thread,
}
else if (written < 0)
{
if (saved_errno == EINTR)
{
continue;
}
logger(1,
"[%8llu, %zu] Send data (blocking I/O) to nDPIsrvd Collector at %s failed: %s",
workflow->packets_captured,
@ -4047,9 +4057,13 @@ static uint32_t is_valid_gre_tunnel(struct pcap_pkthdr const * const header,
if (header->caplen < (l4_ptr - packet) + sizeof(struct ndpi_gre_basehdr))
{
return 0; /* Too short for GRE header*/
return 0; /* Too short for GRE header */
}
uint32_t offset = (l4_ptr - packet);
if (offset > UINT16_MAX)
{
return 0; /* Invalid GRE offset */
}
struct ndpi_gre_basehdr const * const grehdr = (struct ndpi_gre_basehdr const *)&packet[offset];
offset += sizeof(struct ndpi_gre_basehdr);

View file

@ -427,6 +427,13 @@ static int drain_write_buffers(struct remote_desc * const remote)
case 0:
return -1;
default:
if ((size_t)written > buf->buf.max)
{
logger_nDPIsrvd(remote, "BUG: Buffer write offset would overshoot",
"Bytes written > buffer size (%zd > %zu)",
written, buf->buf.max);
return -1;
}
buf->written += written;
if (buf->written == buf->buf.max)
{
@ -736,7 +743,7 @@ static void free_remote(struct nio * const io, struct remote_desc * remote)
{
logger_nDPIsrvd(remote,
"Could not delete event from queue for connection",
": %s",
"(%s)",
(errno != 0 ? strerror(errno) : "Internal Error"));
}
errno = 0;
@ -1030,9 +1037,16 @@ static struct remote_desc * accept_remote(int server_fd,
return NULL;
}
if (set_fd_cloexec(client_fd) < 0)
{
logger(1, "Set close on exec() for fd %d failed: %s", client_fd, strerror(errno));
return NULL;
}
struct remote_desc * current = get_remote_descriptor(socktype, client_fd, NETWORK_BUFFER_MAX_SIZE);
if (current == NULL)
{
logger(1, "Could not acquire remote descriptor from the internal descriptor table, too many clients connected?");
return NULL;
}
@ -1077,7 +1091,7 @@ static int new_connection(struct nio * const io, int eventfd)
struct remote_desc * const current = accept_remote(server_fd, stype, (struct sockaddr *)&sockaddr, &peer_addr_len);
if (current == NULL)
{
return 1;
goto error;
}
int sockopt;
@ -1091,7 +1105,7 @@ static int new_connection(struct nio * const io, int eventfd)
if (setsockopt(current->fd, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)) < 0)
{
logger(1, "Error setting socket option SO_RCVBUF: %s", strerror(errno));
return 1;
goto error;
}
#if !defined(__FreeBSD__) && !defined(__APPLE__)
@ -1100,7 +1114,7 @@ static int new_connection(struct nio * const io, int eventfd)
if (getsockopt(current->fd, SOL_SOCKET, SO_PEERCRED, &ucred, &ucred_len) == -1)
{
logger(1, "Error getting credentials from UNIX socket: %s", strerror(errno));
return 1;
goto error;
}
current->event_collector_un.pid = ucred.pid;
#endif
@ -1119,7 +1133,7 @@ static int new_connection(struct nio * const io, int eventfd)
if (getsockopt(current->fd, SOL_SOCKET, SO_PEERCRED, &ucred, &ucred_len) == -1)
{
logger(1, "Error getting credentials from UNIX socket: %s", strerror(errno));
return 1;
goto error;
}
struct passwd pwnam = {};
@ -1133,7 +1147,7 @@ static int new_connection(struct nio * const io, int eventfd)
if (getpwuid_r(ucred.uid, &pwnam, &buf[0], pwsiz, &pwres) != 0 || pwres == NULL)
{
logger(1, "Could not get passwd entry for user id %u", ucred.uid);
return 1;
goto error;
}
current->event_distributor_un.pid = ucred.pid;
@ -1148,7 +1162,7 @@ static int new_connection(struct nio * const io, int eventfd)
if (setsockopt(current->fd, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)) < 0)
{
logger(1, "Error setting socket option SO_RCVBUF: %s", strerror(errno));
return 1;
goto error;
}
if (inet_ntop(current->event_distributor_in.peer.sin_family,
@ -1157,7 +1171,7 @@ static int new_connection(struct nio * const io, int eventfd)
sizeof(current->event_distributor_in.peer_addr)) == NULL)
{
logger(1, "Error converting an internet address: %s", strerror(errno));
return 1;
goto error;
}
}
@ -1165,7 +1179,7 @@ static int new_connection(struct nio * const io, int eventfd)
if (setsockopt(current->fd, SOL_SOCKET, SO_SNDBUF, &sockopt, sizeof(sockopt)) < 0)
{
logger(1, "Error setting socket option SO_SNDBUF: %s", strerror(errno));
return 1;
goto error;
}
{
@ -1184,8 +1198,7 @@ static int new_connection(struct nio * const io, int eventfd)
if (fcntl_add_flags(current->fd, O_NONBLOCK) != 0)
{
logger(1, "Error setting fd flags to non-blocking mode: %s", strerror(errno));
disconnect_client(io, current);
return 1;
goto error;
}
/* shutdown writing end for collector clients */
@ -1200,11 +1213,13 @@ static int new_connection(struct nio * const io, int eventfd)
if (add_in_event(io, current) != NIO_SUCCESS)
{
logger(1, "Error adding input event to %d: %s", current->fd, (errno != 0 ? strerror(errno) : "Internal Error"));
disconnect_client(io, current);
return 1;
goto error;
}
return 0;
error:
disconnect_client(io, current);
return 1;
}
static int handle_collector_protocol(struct nio * const io, struct remote_desc * const current)
@ -1229,14 +1244,13 @@ static int handle_collector_protocol(struct nio * const io, struct remote_desc *
errno = 0;
current->event_collector_un.json_bytes = strtoull(json_read_buffer->buf.ptr.text, &json_msg_start, 10);
current->event_collector_un.json_bytes += json_msg_start - json_read_buffer->buf.ptr.text;
if (errno == ERANGE)
{
logger_nDPIsrvd(current, "BUG: Collector connection", "JSON message length exceeds numceric limits");
disconnect_client(io, current);
return 1;
}
current->event_collector_un.json_bytes += json_msg_start - json_read_buffer->buf.ptr.text;
if (json_msg_start == json_read_buffer->buf.ptr.text)
{

View file

@ -226,7 +226,8 @@ static int is_daemon_running(char const * const pidfile, pid_str ps)
return 0;
}
if (read(pfd, ps, sizeof(pid_str)) <= 0)
memset(ps, 0x00, sizeof(pid_str));
if (read(pfd, ps, sizeof(pid_str) - 1) <= 0)
{
close(pfd);
return 1;
@ -288,7 +289,7 @@ int is_path_absolute(char const * const prefix, char const * const path)
int daemonize_with_pidfile(char const * const pidfile)
{
pid_str ps = {};
pid_str ps;
int nullfd;
if (daemonize != 0)
@ -795,16 +796,19 @@ int parse_config_file(char const * const config_file, config_line_callback cb, v
}
if (fstat(file_fd, &sbuf) != 0)
{
close(file_fd);
return -1;
}
if ((sbuf.st_mode & S_IFMT) != S_IFREG)
{
close(file_fd);
return -ENOENT;
}
file = fdopen(file_fd, "r");
if (file == NULL)
{
close(file_fd);
return -1;
}