diff --git a/Makefile b/Makefile index 1306283..0315325 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ SHELL := /bin/bash GO ?= $(shell which go) CURL ?= $(shell which curl) +PATCH ?= $(shell which patch) FALCOSECURITY_LIBS_REVISION ?= 0b9ca98fee2453a16f4538db55dcfa34bc8f5aef FALCOSECURITY_LIBS_REPO ?= falcosecurity/libs @@ -34,6 +35,7 @@ pluginlib: $(CURL) -Lso pkg/sdk/plugin_api.h $(PLUGINLIB_URL)/plugin_api.h $(CURL) -Lso pkg/loader/plugin_loader.h $(PLUGINLIB_URL)/plugin_loader.h $(CURL) -Lso pkg/loader/plugin_loader.c $(PLUGINLIB_URL)/plugin_loader.c + $(PATCH) -p1 < pkg/loader/strlcpy.patch clean-pluginlib: rm -f \ diff --git a/pkg/loader/plugin_loader.c b/pkg/loader/plugin_loader.c index aad119f..169f696 100644 --- a/pkg/loader/plugin_loader.c +++ b/pkg/loader/plugin_loader.c @@ -23,23 +23,53 @@ limitations under the License. typedef void* library_handle_t; #endif -#include "strlcpy.h" #include #include #include #include "plugin_loader.h" +// note(jasondellaluce,therealbobo): implementation taken from falcosecurity/libs +// note(leogr): to avoid clashing with `strlcpy` introduced by glibc 2.38, +// the func has been renamed to plugin_loader_strlcpy. +// N.B.: our building system here is not smart enough to detect if the function +// was declared already. +#include +#include +/*! + \brief Copy up to size - 1 characters from the NUL-terminated string src to dst, NUL-terminating the result. + + \return The length of the source string. +*/ + +static inline size_t plugin_loader_strlcpy(char *dst, const char *src, size_t size) { + size_t srcsize = strlen(src); + if (size == 0) { + return srcsize; + } + + size_t copysize = srcsize; + + if (copysize > size - 1) { + copysize = size - 1; + } + + memcpy(dst, src, copysize); + dst[copysize] = '\0'; + + return srcsize; +} + static inline void err_prepend(char* s, const char* prefix, const char* sep) { char tmp[PLUGIN_MAX_ERRLEN]; - size_t prefix_len = strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN); + size_t prefix_len = plugin_loader_strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN); if (*s != '\0') { - strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len); + plugin_loader_strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len); prefix_len += strlen(sep); } - strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len); - strlcpy(s, tmp, PLUGIN_MAX_ERRLEN); + plugin_loader_strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len); + plugin_loader_strlcpy(s, tmp, PLUGIN_MAX_ERRLEN); } static inline void err_append(char* s, const char* suffix, const char* sep) @@ -71,7 +101,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t)); if (!ret) { - strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); + plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); return NULL; } @@ -86,7 +116,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) LPTSTR msg_buf = 0; if (FormatMessageA(flg, 0, GetLastError(), 0, (LPTSTR) &msg_buf, 0, NULL) && msg_buf) { - strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN); + plugin_loader_strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN); LocalFree(msg_buf); } } @@ -94,7 +124,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) ret->handle = dlopen(path, RTLD_LAZY); if (ret->handle == NULL) { - strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN); + plugin_loader_strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN); } #endif @@ -143,14 +173,14 @@ plugin_handle_t* plugin_load_api(const plugin_api* api, char* err) err[0] = '\0'; if (!api) { - strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN); + plugin_loader_strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN); return NULL; } plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t)); if (!ret) { - strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); + plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); return NULL; } ret->api = *api; @@ -203,7 +233,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err) const char *ver, *failmsg; if (h->api.get_required_api_version == NULL) { - strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN); + plugin_loader_strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN); return false; } @@ -243,7 +273,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err) plugin_caps_t plugin_get_capabilities(const plugin_handle_t* h, char* err) { plugin_caps_t caps = CAP_NONE; - strlcpy(err, "", PLUGIN_MAX_ERRLEN); + plugin_loader_strlcpy(err, "", PLUGIN_MAX_ERRLEN); if (h->api.open != NULL && h->api.close != NULL && h->api.next_batch != NULL) { diff --git a/pkg/loader/strlcpy.patch b/pkg/loader/strlcpy.patch new file mode 100644 index 0000000..218677a --- /dev/null +++ b/pkg/loader/strlcpy.patch @@ -0,0 +1,125 @@ +diff --git a/pkg/loader/plugin_loader.c b/pkg/loader/plugin_loader.c +index aad119f..169f696 100644 +--- a/pkg/loader/plugin_loader.c ++++ b/pkg/loader/plugin_loader.c +@@ -23,23 +23,53 @@ limitations under the License. + typedef void* library_handle_t; + #endif + +-#include "strlcpy.h" + #include + #include + #include + #include "plugin_loader.h" + ++// note(jasondellaluce,therealbobo): implementation taken from falcosecurity/libs ++// note(leogr): to avoid clashing with `strlcpy` introduced by glibc 2.38, ++// the func has been renamed to plugin_loader_strlcpy. ++// N.B.: our building system here is not smart enough to detect if the function ++// was declared already. ++#include ++#include ++/*! ++ \brief Copy up to size - 1 characters from the NUL-terminated string src to dst, NUL-terminating the result. ++ ++ \return The length of the source string. ++*/ ++ ++static inline size_t plugin_loader_strlcpy(char *dst, const char *src, size_t size) { ++ size_t srcsize = strlen(src); ++ if (size == 0) { ++ return srcsize; ++ } ++ ++ size_t copysize = srcsize; ++ ++ if (copysize > size - 1) { ++ copysize = size - 1; ++ } ++ ++ memcpy(dst, src, copysize); ++ dst[copysize] = '\0'; ++ ++ return srcsize; ++} ++ + static inline void err_prepend(char* s, const char* prefix, const char* sep) + { + char tmp[PLUGIN_MAX_ERRLEN]; +- size_t prefix_len = strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN); ++ size_t prefix_len = plugin_loader_strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN); + if (*s != '\0') + { +- strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len); ++ plugin_loader_strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len); + prefix_len += strlen(sep); + } +- strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len); +- strlcpy(s, tmp, PLUGIN_MAX_ERRLEN); ++ plugin_loader_strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len); ++ plugin_loader_strlcpy(s, tmp, PLUGIN_MAX_ERRLEN); + } + + static inline void err_append(char* s, const char* suffix, const char* sep) +@@ -71,7 +101,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) + plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t)); + if (!ret) + { +- strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); ++ plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); + return NULL; + } + +@@ -86,7 +116,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) + LPTSTR msg_buf = 0; + if (FormatMessageA(flg, 0, GetLastError(), 0, (LPTSTR) &msg_buf, 0, NULL) && msg_buf) + { +- strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN); ++ plugin_loader_strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN); + LocalFree(msg_buf); + } + } +@@ -94,7 +124,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) + ret->handle = dlopen(path, RTLD_LAZY); + if (ret->handle == NULL) + { +- strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN); ++ plugin_loader_strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN); + } + #endif + +@@ -143,14 +173,14 @@ plugin_handle_t* plugin_load_api(const plugin_api* api, char* err) + err[0] = '\0'; + if (!api) + { +- strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN); ++ plugin_loader_strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN); + return NULL; + } + + plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t)); + if (!ret) + { +- strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); ++ plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); + return NULL; + } + ret->api = *api; +@@ -203,7 +233,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err) + const char *ver, *failmsg; + if (h->api.get_required_api_version == NULL) + { +- strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN); ++ plugin_loader_strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN); + return false; + } + +@@ -243,7 +273,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err) + plugin_caps_t plugin_get_capabilities(const plugin_handle_t* h, char* err) + { + plugin_caps_t caps = CAP_NONE; +- strlcpy(err, "", PLUGIN_MAX_ERRLEN); ++ plugin_loader_strlcpy(err, "", PLUGIN_MAX_ERRLEN); + + if (h->api.open != NULL && h->api.close != NULL && h->api.next_batch != NULL) + {