Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update branch 0.10.x #893

Merged
merged 15 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/modules/curl.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ else()
--without-libpsl
--without-nghttp2
--without-libssh2
--with-ca-path=/etc/ssl/certs/
--disable-threaded-resolver
--without-brotli
--without-zstd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ void do___open_by_handle_atX_success(int *open_by_handle_fd, int *dirfd, char *f
* 2. Reallocate file_handle structure with the correct size.
*/
fhsize = sizeof(*fhp) + fhp->handle_bytes;
fhp = (struct file_handle *)realloc(fhp, fhsize);
if(fhp == NULL)
struct file_handle *new_fhp = (struct file_handle *)realloc(fhp, fhsize);
if(new_fhp == NULL)
{
free(fhp);
FAIL() << "Error in allocating the `struct file_handle` with realloc" << std::endl;
}
fhp = new_fhp;

/*
* 3. Get file handle.
Expand Down
5 changes: 5 additions & 0 deletions userspace/chisel/chisel_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,11 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
// If not, skip this thread
//
sinsp_fdtable* fdtable = tinfo.get_fd_table();
if(fdtable == nullptr)
{
// If no fdtable is available we can't do anything
return true;
}

if(filter != NULL)
{
Expand Down
4 changes: 2 additions & 2 deletions userspace/chisel/chisel_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static std::string realpath_ex(const std::string& path)
}
std::string ret = resolved;
free(resolved);
return resolved;
return ret;
}
#endif

Expand Down Expand Up @@ -166,4 +166,4 @@ void chisel_add_dir(string dirname, bool front_add)
{
g_chisel_dirs->push_back(ncdi);
}
}
}
6 changes: 6 additions & 0 deletions userspace/libpman/src/ringbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static bool is_cpu_online(uint16_t cpu_id)
}

fscanf(fp, "%d", &online);
fclose(fp);
return online == 1;
}

Expand All @@ -133,6 +134,11 @@ int pman_finalize_ringbuf_array_after_loading()
int ringubuf_array_fd = -1;
char error_message[MAX_ERROR_MESSAGE_LEN];
int *ringbufs_fds = (int *)calloc(g_state.n_required_buffers, sizeof(int));
if(ringbufs_fds == NULL)
{
pman_print_error("failed to allocate the ringubufs_fds array");
return errno;
}
bool success = false;

/* We don't need anymore the inner map, close it. */
Expand Down
5 changes: 5 additions & 0 deletions userspace/libscap/engine/bpf/scap_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ static int32_t load_elf_maps_section(struct bpf_engine *handle, struct bpf_map_d

*nr_maps = 0;
sym = calloc(BPF_MAPS_MAX + 1, sizeof(GElf_Sym));
if(sym == NULL)
{
return scap_errprintf(handle->m_lasterr, 0, "calloc(BPF_MAPS_MAX + 1) failed");
}

for(i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++)
{
ASSERT(*nr_maps < BPF_MAPS_MAX + 1);
Expand Down
5 changes: 4 additions & 1 deletion userspace/libscap/engine/gvisor/scap_gvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ int32_t sandbox_entry::expand_buffer(size_t size)
{
void* new_buf;

if (m_buf.buf == nullptr) {
if (m_buf.buf == nullptr)
{
new_buf = malloc(size);
} else
{
Expand All @@ -72,6 +73,8 @@ int32_t sandbox_entry::expand_buffer(size_t size)

if (new_buf == nullptr)
{
// no need to clean up existing buffers in case of failed realloc
// since they will be cleaned up by the destructor
return SCAP_FAILURE;
}

Expand Down
16 changes: 12 additions & 4 deletions userspace/libscap/engine/savefile/scap_savefile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,12 +1113,15 @@ static int32_t scap_read_userlist(scap_reader_t* r, uint32_t block_length, uint3
scap_userinfo* puser;

(*userlist_p)->nusers++;
(*userlist_p)->users = (scap_userinfo*)realloc((*userlist_p)->users, (*userlist_p)->nusers * sizeof(scap_userinfo));
if((*userlist_p)->users == NULL)
scap_userinfo *new_userlist = (scap_userinfo*)realloc((*userlist_p)->users, (*userlist_p)->nusers * sizeof(scap_userinfo));
if(new_userlist == NULL)
{
free((*userlist_p)->users);
(*userlist_p)->users = NULL;
snprintf(error, SCAP_LASTERR_SIZE, "memory allocation error in scap_read_userlist(1)");
return SCAP_FAILURE;
}
(*userlist_p)->users = new_userlist;

puser = &(*userlist_p)->users[(*userlist_p)->nusers -1];

Expand Down Expand Up @@ -1218,12 +1221,15 @@ static int32_t scap_read_userlist(scap_reader_t* r, uint32_t block_length, uint3
scap_groupinfo* pgroup;

(*userlist_p)->ngroups++;
(*userlist_p)->groups = (scap_groupinfo*)realloc((*userlist_p)->groups, (*userlist_p)->ngroups * sizeof(scap_groupinfo));
if((*userlist_p)->groups == NULL)
scap_groupinfo *new_grouplist = (scap_groupinfo*)realloc((*userlist_p)->groups, (*userlist_p)->ngroups * sizeof(scap_groupinfo));
if(new_grouplist == NULL)
{
free((*userlist_p)->groups);
(*userlist_p)->groups = NULL;
snprintf(error, SCAP_LASTERR_SIZE, "memory allocation error in scap_read_userlist(2)");
return SCAP_FAILURE;
}
(*userlist_p)->groups = new_grouplist;

pgroup = &(*userlist_p)->groups[(*userlist_p)->ngroups -1];

Expand Down Expand Up @@ -1907,6 +1913,8 @@ static int32_t next(struct scap_engine_handle engine, scap_evt **pevent, uint16_
// Try to allocate a buffer large enough
char *tmp = realloc(handle->m_reader_evt_buf, readlen);
if (!tmp) {
free(handle->m_reader_evt_buf);
handle->m_reader_evt_buf = NULL;
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "event block length %u greater than read buffer size %zu",
readlen,
handle->m_reader_evt_buf_size);
Expand Down
1 change: 1 addition & 0 deletions userspace/libscap/engine/source_plugin/source_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ static int32_t next(struct scap_engine_handle engine, OUT scap_evt** pevent, OUT
}
else
{
free(handle->m_input_plugin_evt_storage);
snprintf(lasterr, SCAP_LASTERR_SIZE, "%s", "failed to alloc space for plugin storage");
ASSERT(false);
return SCAP_FAILURE;
Expand Down
29 changes: 29 additions & 0 deletions userspace/libscap/linux/scap_fds.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ int32_t scap_fd_handle_socket(struct scap_proclist *proclist, char *fname, scap_
if(sockets == NULL)
{
sockets = malloc(sizeof(struct scap_ns_socket_list));
if(sockets == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "sockets allocation error");
return SCAP_FAILURE;
}
sockets->net_ns = net_ns;
sockets->sockets = NULL;
char fd_error[SCAP_LASTERR_SIZE];
Expand Down Expand Up @@ -443,6 +448,12 @@ int32_t scap_fd_read_unix_sockets_from_proc_fs(const char* filename, scap_fdinfo
continue;
}
scap_fdinfo *fdinfo = malloc(sizeof(scap_fdinfo));
if(fdinfo == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "fdinfo allocation error");
fclose(f);
return SCAP_FAILURE;
}
fdinfo->type = SCAP_FD_UNIX_SOCK;


Expand Down Expand Up @@ -570,6 +581,12 @@ int32_t scap_fd_read_netlink_sockets_from_proc_fs(const char* filename, scap_fdi
continue;
}
scap_fdinfo *fdinfo = malloc(sizeof(scap_fdinfo));
if(fdinfo == NULL)
{
snprintf(error, SCAP_LASTERR_SIZE, "fdinfo allocation error");
fclose(f);
return SCAP_FAILURE;
}
memset(fdinfo, 0, sizeof(scap_fdinfo));
fdinfo->type = SCAP_FD_UNIX_SOCK;

Expand Down Expand Up @@ -724,6 +741,12 @@ int32_t scap_fd_read_ipv4_sockets_from_proc_fs(const char *dir, int l4proto, sca
}

scap_fdinfo *fdinfo = malloc(sizeof(scap_fdinfo));
if(fdinfo == NULL)
{
fclose(f);
free(scan_buf);
return scap_errprintf(error, errno, "memory allocation error in scap_fd_read_ipv4_sockets_from_proc_fs");
}

//
// Skip the sl field
Expand Down Expand Up @@ -903,6 +926,12 @@ int32_t scap_fd_read_ipv6_sockets_from_proc_fs(char *dir, int l4proto, scap_fdin
}

scap_fdinfo *fdinfo = malloc(sizeof(scap_fdinfo));
if(fdinfo == NULL)
{
fclose(f);
free(scan_buf);
return scap_errprintf(error, errno, "memory allocation error in scap_fd_read_ipv6_sockets_from_proc_fs");
}

//
// Skip the sl field
Expand Down
29 changes: 27 additions & 2 deletions userspace/libscap/linux/scap_userlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ int32_t scap_create_userlist(scap_t* handle)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "userlist allocation failed(2)");
free(userlist);
handle->m_userlist = NULL;
return SCAP_FAILURE;
}

Expand All @@ -77,6 +78,7 @@ int32_t scap_create_userlist(scap_t* handle)
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "grouplist allocation failed(2)");
free(userlist->users);
free(userlist);
handle->m_userlist = NULL;
return SCAP_FAILURE;
}

Expand Down Expand Up @@ -127,6 +129,7 @@ int32_t scap_create_userlist(scap_t* handle)
free(userlist->users);
free(userlist->groups);
free(userlist);
handle->m_userlist = NULL;
if(file_lookup)
{
fclose(f);
Expand Down Expand Up @@ -192,7 +195,17 @@ int32_t scap_create_userlist(scap_t* handle)
if (useridx < usercnt)
{
// Reduce array size
userlist->users = realloc(userlist->users, useridx * sizeof(scap_userinfo));
scap_userinfo *reduced_userinfos = realloc(userlist->users, useridx * sizeof(scap_userinfo));
if(reduced_userinfos == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "userlist allocation while reducing array size");
free(userlist->users);
free(userlist->groups);
free(userlist);
handle->m_userlist = NULL;
return SCAP_FAILURE;
}
userlist->users = reduced_userinfos;
}

// groups
Expand All @@ -207,6 +220,7 @@ int32_t scap_create_userlist(scap_t* handle)
free(userlist->users);
free(userlist->groups);
free(userlist);
handle->m_userlist = NULL;
return SCAP_FAILURE;
}
}
Expand All @@ -231,6 +245,7 @@ int32_t scap_create_userlist(scap_t* handle)
free(userlist->users);
free(userlist->groups);
free(userlist);
handle->m_userlist = NULL;
if(file_lookup)
{
fclose(f);
Expand Down Expand Up @@ -273,7 +288,17 @@ int32_t scap_create_userlist(scap_t* handle)
if (grpidx < grpcnt)
{
// Reduce array size
userlist->groups = realloc(userlist->groups, grpidx * sizeof(scap_groupinfo));
scap_groupinfo *reduced_groups = realloc(userlist->groups, grpidx * sizeof(scap_groupinfo));
if(reduced_groups == NULL)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "grouplist allocation failed(2)");
free(userlist->users);
free(userlist->groups);
free(userlist);
handle->m_userlist = NULL;
return SCAP_FAILURE;
}
userlist->groups = reduced_groups;
}
return SCAP_SUCCESS;
}
3 changes: 2 additions & 1 deletion userspace/libscap/scap.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ scap_t* scap_open_udig_int(char *error, int32_t *rc, scap_open_args *oargs)
if(*rc != SCAP_SUCCESS)
{
scap_close(handle);
free(handle);
return NULL;
}

Expand Down Expand Up @@ -1346,6 +1345,8 @@ bool scap_alloc_proclist_info(struct ppm_proclist_info **proclist_p, uint32_t n_
struct ppm_proclist_info *procinfo = (struct ppm_proclist_info*) realloc(*proclist_p, memsize);
if(procinfo == NULL)
{
free(*proclist_p);
*proclist_p = NULL;
snprintf(error, SCAP_LASTERR_SIZE, "driver process list allocation error");
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions userspace/libscap/scap_savefile.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ int scap_dump_write(scap_dumper_t *d, void* buf, unsigned len)
targetbufsize);
if(targetbuf == NULL)
{
free(d->m_targetbuf);
return -1;
}

Expand Down Expand Up @@ -550,6 +551,11 @@ static int32_t scap_write_proclist(scap_t *handle, scap_dumper_t *d)
}

scap_dumper_t *proclist_dumper = scap_write_proclist_begin(handle);
if(proclist_dumper == NULL)
{
return SCAP_FAILURE;
}


uint32_t totlen = 0;
struct scap_threadinfo *tinfo;
Expand Down
16 changes: 15 additions & 1 deletion userspace/libscap/scap_suppress.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ int32_t scap_suppress_events_comm_impl(struct scap_suppress *suppress, const cha
}

suppress->m_num_suppressed_comms++;
suppress->m_suppressed_comms = (char **) realloc(suppress->m_suppressed_comms,
char **expanded_suppressed_comms = (char **) realloc(suppress->m_suppressed_comms,
suppress->m_num_suppressed_comms * sizeof(char *));
if(expanded_suppressed_comms == NULL)
{
for(i=0; i<suppress->m_num_suppressed_comms - 1; i++) {
free(suppress->m_suppressed_comms[i]);
}
free(suppress->m_suppressed_comms);
return SCAP_FAILURE;
}
suppress->m_suppressed_comms = expanded_suppressed_comms;

suppress->m_suppressed_comms[suppress->m_num_suppressed_comms-1] = strdup(comm);

Expand Down Expand Up @@ -144,6 +153,11 @@ int32_t scap_update_suppressed(struct scap_suppress *suppress,
if(*suppressed && stid == NULL)
{
stid = (scap_tid *) malloc(sizeof(scap_tid));
if(stid == NULL)
{
return SCAP_FAILURE;
}

stid->tid = tid;
int32_t uth_status = SCAP_SUCCESS;

Expand Down
4 changes: 4 additions & 0 deletions userspace/libsinsp/cyclewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ cycle_writer::conclusion cycle_writer::next_file()
size_t their_size;
char file_name[our_size];
const struct tm *our_time = localtime(&m_last_time);
if(our_time == NULL)
{
throw sinsp_exception("cannot get localtime in cycle_writer::next_file");
}

their_size = strftime(file_name, our_size, m_base_file_name.c_str(), our_time);

Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/examples/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ static bool insert_module()
goto error;

atexit(remove_module);
close(fd);

return true;

Expand Down
Loading