Skip to content

Commit

Permalink
build: patch plugin_loader.c to use an internal strlcpy impl
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Grasso <me@leonardograsso.com>
  • Loading branch information
leogr authored and poiana committed Aug 30, 2023
1 parent c36a0e0 commit 9130ff4
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand Down
54 changes: 42 additions & 12 deletions pkg/loader/plugin_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,53 @@ limitations under the License.
typedef void* library_handle_t;
#endif

#include "strlcpy.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 <stdint.h>
#include <string.h>
/*!
\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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -86,15 +116,15 @@ 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);
}
}
#else
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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
{
Expand Down
125 changes: 125 additions & 0 deletions pkg/loader/strlcpy.patch
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 <stdint.h>
+#include <string.h>
+/*!
+ \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)
{

0 comments on commit 9130ff4

Please sign in to comment.