Skip to content

Commit

Permalink
chore(userspace/libscap): cleanup weird usages of strncmp for engines.
Browse files Browse the repository at this point in the history
Moreover, properly compile-check that bpf and kmod engines are built before
de-referencing their vtable.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP authored and poiana committed Oct 26, 2022
1 parent 1b8cc0b commit 6549aaf
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 66 deletions.
8 changes: 5 additions & 3 deletions userspace/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ add_subdirectory(engine/udig)
target_link_libraries(scap scap_engine_udig)

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
add_definitions(-DHAS_ENGINE_BPF)
add_subdirectory(engine/bpf)
target_link_libraries(scap scap_engine_bpf)
if (NOT MINIMAL_BUILD)
add_definitions(-DHAS_ENGINE_BPF)
add_subdirectory(engine/bpf)
target_link_libraries(scap scap_engine_bpf)
endif()

add_definitions(-DHAS_ENGINE_KMOD)
add_subdirectory(engine/kmod)
Expand Down
1 change: 0 additions & 1 deletion userspace/libscap/engine/bpf/bpf_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#include <stdint.h>

#define BPF_ENGINE "bpf"
#define BPF_ENGINE_LEN 4

#ifdef __cplusplus
extern "C"
Expand Down
37 changes: 1 addition & 36 deletions userspace/libscap/engine/bpf/scap_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct bpf_map_data {

static bool match(scap_open_args* oargs)
{
return strncmp(oargs->engine_name, BPF_ENGINE, BPF_ENGINE_LEN) == 0;
return strcmp(oargs->engine_name, BPF_ENGINE) == 0;
}

static struct bpf_engine* alloc_handle(scap_t* main_handle, char* lasterr_ptr)
Expand All @@ -105,8 +105,6 @@ static void free_handle(struct scap_engine_handle engine)
free(engine.m_handle);
}

#ifndef MINIMAL_BUILD

# define UINT32_MAX (4294967295U)

/* Recommended log buffer size.
Expand Down Expand Up @@ -1781,36 +1779,3 @@ const struct scap_vtable scap_bpf_engine = {
.get_vtid = noop_get_vxid,
.getpid_global = scap_os_getpid_global,
};

#else // MINIMAL_BUILD

static int32_t init(scap_t* handle, scap_open_args *oargs)
{
strlcpy(handle->m_lasterr, "The eBPF probe driver is not supported when using a minimal build", SCAP_LASTERR_SIZE);
return SCAP_NOT_SUPPORTED;
}

const struct scap_vtable scap_bpf_engine = {
.name = "bpf",
.mode = SCAP_MODE_LIVE,
.savefile_ops = NULL,

.match = match,
.alloc_handle = alloc_handle,
.init = init,
.free_handle = free_handle,
.close = noop_close_engine,
.next = noop_next,
.start_capture = noop_start_capture,
.stop_capture = noop_stop_capture,
.configure = noop_configure,
.get_stats = noop_get_stats,
.get_n_tracepoint_hit = noop_get_n_tracepoint_hit,
.get_n_devs = noop_get_n_devs,
.get_max_buf_used = noop_get_max_buf_used,
.get_threadlist = noop_get_threadlist,
.get_vpid = noop_get_vxid,
.get_vtid = noop_get_vxid,
.getpid_global = noop_getpid_global,
};
#endif // MINIMAL_BUILD
2 changes: 1 addition & 1 deletion userspace/libscap/engine/gvisor/gvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static int32_t gvisor_next(struct scap_engine_handle engine, scap_evt **pevent,

static bool gvisor_match(scap_open_args* oargs)
{
return strncmp(oargs->engine_name, GVISOR_ENGINE, GVISOR_ENGINE_LEN) == 0;
return strcmp(oargs->engine_name, GVISOR_ENGINE) == 0;
}

static int32_t gvisor_configure(struct scap_engine_handle engine, enum scap_setting setting, unsigned long arg1, unsigned long arg2)
Expand Down
1 change: 0 additions & 1 deletion userspace/libscap/engine/gvisor/gvisor_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ limitations under the License.
#pragma once

#define GVISOR_ENGINE "gvisor"
#define GVISOR_ENGINE_LEN 7

#ifdef __cplusplus
extern "C"
Expand Down
1 change: 0 additions & 1 deletion userspace/libscap/engine/kmod/kmod_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#include <stdint.h>

#define KMOD_ENGINE "kmod"
#define KMOD_ENGINE_LEN 5

#ifdef __cplusplus
extern "C"
Expand Down
2 changes: 1 addition & 1 deletion userspace/libscap/engine/kmod/scap_kmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ limitations under the License.

static bool match(scap_open_args* oargs)
{
return strncmp(oargs->engine_name, KMOD_ENGINE, KMOD_ENGINE_LEN) == 0;
return strcmp(oargs->engine_name, KMOD_ENGINE) == 0;
}

static struct kmod_engine* alloc_handle(scap_t* main_handle, char* lasterr_ptr)
Expand Down
1 change: 0 additions & 1 deletion userspace/libscap/engine/modern_bpf/modern_bpf_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#include <stdint.h>

#define MODERN_BPF_ENGINE "modern_bpf"
#define MODERN_BPF_ENGINE_LEN 11

#ifdef __cplusplus
extern "C"
Expand Down
2 changes: 1 addition & 1 deletion userspace/libscap/engine/modern_bpf/scap_modern_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static int32_t populate_64bit_interesting_syscalls_table(bool* ppm_sc_array)
/* Right now this is not used */
bool scap_modern_bpf__match(scap_open_args* oargs)
{
return strncmp(oargs->engine_name, MODERN_BPF_ENGINE, MODERN_BPF_ENGINE_LEN) == 0;
return strcmp(oargs->engine_name, MODERN_BPF_ENGINE) == 0;
}

static struct modern_bpf_engine* scap_modern_bpf__alloc_engine(scap_t* main_handle, char* lasterr_ptr)
Expand Down
1 change: 0 additions & 1 deletion userspace/libscap/engine/nodriver/nodriver_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ limitations under the License.
#pragma once

#define NODRIVER_ENGINE "nodriver"
#define NODRIVER_ENGINE_LEN 9
1 change: 0 additions & 1 deletion userspace/libscap/engine/savefile/savefile_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#include <stdint.h>

#define SAVEFILE_ENGINE "savefile"
#define SAVEFILE_ENGINE_LEN 9

#ifdef __cplusplus
extern "C"
Expand Down
2 changes: 1 addition & 1 deletion userspace/libscap/engine/savefile/scap_savefile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ void scap_savefile_fseek(struct scap_engine_handle engine, uint64_t off)

static bool match(struct scap_open_args* oargs)
{
return strncmp(oargs->engine_name, SAVEFILE_ENGINE, SAVEFILE_ENGINE_LEN) == 0;
return strcmp(oargs->engine_name, SAVEFILE_ENGINE) == 0;
}

static struct savefile_engine* alloc_handle(struct scap* main_handle, char* lasterr_ptr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#include "../../plugin_info.h"

#define SOURCE_PLUGIN_ENGINE "source_plugin"
#define SOURCE_PLUGIN_ENGINE_LEN 14

#ifdef __cplusplus
extern "C"
Expand Down
1 change: 0 additions & 1 deletion userspace/libscap/engine/test_input/test_input_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#include "../../scap_test.h"

#define TEST_INPUT_ENGINE "test_input"
#define TEST_INPUT_ENGINE_LEN 11

#ifdef __cplusplus
extern "C"
Expand Down
2 changes: 1 addition & 1 deletion userspace/libscap/engine/udig/scap_udig.c
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ static int32_t configure(struct scap_engine_handle engine, enum scap_setting set

static bool match(scap_open_args* oargs)
{
return strncmp(oargs->engine_name, UDIG_ENGINE, UDIG_ENGINE_LEN) == 0;
return strcmp(oargs->engine_name, UDIG_ENGINE) == 0;
}

static struct udig_engine* alloc_handle(scap_t* main_handle, char* lasterr_ptr)
Expand Down
1 change: 0 additions & 1 deletion userspace/libscap/engine/udig/udig_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#include <stdint.h>

#define UDIG_ENGINE "udig"
#define UDIG_ENGINE_LEN 5

#ifdef __cplusplus
extern "C"
Expand Down
34 changes: 21 additions & 13 deletions userspace/libscap/scap.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,31 @@ scap_t* scap_open_live_int(char *error, int32_t *rc, scap_open_args* oargs)
// Preliminary initializations
//
handle->m_mode = SCAP_MODE_LIVE;
if(strncmp(oargs->engine_name, BPF_ENGINE, BPF_ENGINE_LEN) == 0)
if (false)
{

}
#ifdef HAS_ENGINE_BPF
else if(strcmp(oargs->engine_name, BPF_ENGINE) == 0)
{
handle->m_vtable = &scap_bpf_engine;
}
else if(strncmp(oargs->engine_name, KMOD_ENGINE, KMOD_ENGINE_LEN) == 0)
#endif
#ifdef HAS_ENGINE_KMOD
else if(strcmp(oargs->engine_name, KMOD_ENGINE) == 0)
{
handle->m_vtable = &scap_kmod_engine;
}
#endif
#ifdef HAS_ENGINE_MODERN_BPF
else if(strncmp(oargs->engine_name, MODERN_BPF_ENGINE, MODERN_BPF_ENGINE_LEN) == 0)
else if(strcmp(oargs->engine_name, MODERN_BPF_ENGINE) == 0)
{
handle->m_vtable = &scap_modern_bpf_engine;
}
#endif /* HAS_ENGINE_MODERN_BPF */
else
{
snprintf(error, SCAP_LASTERR_SIZE, "libscap: unknown engine called `scap_open_live_int()`");
snprintf(error, SCAP_LASTERR_SIZE, "libscap: unknown engine '%s' called `scap_open_live_int()`", oargs->engine_name);
*rc = SCAP_FAILURE;
return NULL;
}
Expand Down Expand Up @@ -886,38 +894,38 @@ scap_t* scap_open(scap_open_args* oargs, char *error, int32_t *rc)
* with an internal switch that selects the right vtable! For the moment
* let's keep different functions.
*/
if(strncmp(engine_name, SAVEFILE_ENGINE, SAVEFILE_ENGINE_LEN) == 0)
if(strcmp(engine_name, SAVEFILE_ENGINE) == 0)
{
return scap_open_offline_int(oargs, rc, error);
}
else if(strncmp(engine_name, UDIG_ENGINE, UDIG_ENGINE_LEN) == 0)
else if(strcmp(engine_name, UDIG_ENGINE) == 0)
{
return scap_open_udig_int(error, rc, oargs->proc_callback,
oargs->proc_callback_context,
oargs->import_users,
oargs->suppressed_comms);
}
else if(strncmp(engine_name, GVISOR_ENGINE, GVISOR_ENGINE_LEN) == 0)
else if(strcmp(engine_name, GVISOR_ENGINE) == 0)
{
return scap_open_gvisor_int(error, rc, oargs);
}
else if(strncmp(engine_name, TEST_INPUT_ENGINE, TEST_INPUT_ENGINE_LEN) == 0)
else if(strcmp(engine_name, TEST_INPUT_ENGINE) == 0)
{
return scap_open_test_input_int(error, rc, oargs);
}
else if(strncmp(engine_name, KMOD_ENGINE, KMOD_ENGINE_LEN) == 0 ||
strncmp(engine_name, BPF_ENGINE, BPF_ENGINE_LEN) == 0 ||
strncmp(engine_name, MODERN_BPF_ENGINE, MODERN_BPF_ENGINE_LEN) == 0)
else if(strcmp(engine_name, KMOD_ENGINE) == 0 ||
strcmp(engine_name, BPF_ENGINE) == 0 ||
strcmp(engine_name, MODERN_BPF_ENGINE) == 0)
{
return scap_open_live_int(error, rc, oargs);
}
else if(strncmp(engine_name, NODRIVER_ENGINE, NODRIVER_ENGINE_LEN) == 0)
else if(strcmp(engine_name, NODRIVER_ENGINE) == 0)
{
return scap_open_nodriver_int(error, rc, oargs->proc_callback,
oargs->proc_callback_context,
oargs->import_users);
}
else if(strncmp(engine_name, SOURCE_PLUGIN_ENGINE, SOURCE_PLUGIN_ENGINE_LEN) == 0)
else if(strcmp(engine_name, SOURCE_PLUGIN_ENGINE) == 0)
{
return scap_open_plugin_int(error, rc, oargs);
}
Expand Down

0 comments on commit 6549aaf

Please sign in to comment.