Skip to content

Commit 9130ff4

Browse files
leogrpoiana
authored andcommitted
build: patch plugin_loader.c to use an internal strlcpy impl
Signed-off-by: Leonardo Grasso <[email protected]>
1 parent c36a0e0 commit 9130ff4

File tree

3 files changed

+169
-12
lines changed

3 files changed

+169
-12
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
SHELL := /bin/bash
1414
GO ?= $(shell which go)
1515
CURL ?= $(shell which curl)
16+
PATCH ?= $(shell which patch)
1617

1718
FALCOSECURITY_LIBS_REVISION ?= 0b9ca98fee2453a16f4538db55dcfa34bc8f5aef
1819
FALCOSECURITY_LIBS_REPO ?= falcosecurity/libs
@@ -34,6 +35,7 @@ pluginlib:
3435
$(CURL) -Lso pkg/sdk/plugin_api.h $(PLUGINLIB_URL)/plugin_api.h
3536
$(CURL) -Lso pkg/loader/plugin_loader.h $(PLUGINLIB_URL)/plugin_loader.h
3637
$(CURL) -Lso pkg/loader/plugin_loader.c $(PLUGINLIB_URL)/plugin_loader.c
38+
$(PATCH) -p1 < pkg/loader/strlcpy.patch
3739

3840
clean-pluginlib:
3941
rm -f \

pkg/loader/plugin_loader.c

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,53 @@ limitations under the License.
2323
typedef void* library_handle_t;
2424
#endif
2525

26-
#include "strlcpy.h"
2726
#include <stdio.h>
2827
#include <stdlib.h>
2928
#include <string.h>
3029
#include "plugin_loader.h"
3130

31+
// note(jasondellaluce,therealbobo): implementation taken from falcosecurity/libs
32+
// note(leogr): to avoid clashing with `strlcpy` introduced by glibc 2.38,
33+
// the func has been renamed to plugin_loader_strlcpy.
34+
// N.B.: our building system here is not smart enough to detect if the function
35+
// was declared already.
36+
#include <stdint.h>
37+
#include <string.h>
38+
/*!
39+
\brief Copy up to size - 1 characters from the NUL-terminated string src to dst, NUL-terminating the result.
40+
41+
\return The length of the source string.
42+
*/
43+
44+
static inline size_t plugin_loader_strlcpy(char *dst, const char *src, size_t size) {
45+
size_t srcsize = strlen(src);
46+
if (size == 0) {
47+
return srcsize;
48+
}
49+
50+
size_t copysize = srcsize;
51+
52+
if (copysize > size - 1) {
53+
copysize = size - 1;
54+
}
55+
56+
memcpy(dst, src, copysize);
57+
dst[copysize] = '\0';
58+
59+
return srcsize;
60+
}
61+
3262
static inline void err_prepend(char* s, const char* prefix, const char* sep)
3363
{
3464
char tmp[PLUGIN_MAX_ERRLEN];
35-
size_t prefix_len = strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN);
65+
size_t prefix_len = plugin_loader_strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN);
3666
if (*s != '\0')
3767
{
38-
strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len);
68+
plugin_loader_strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len);
3969
prefix_len += strlen(sep);
4070
}
41-
strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len);
42-
strlcpy(s, tmp, PLUGIN_MAX_ERRLEN);
71+
plugin_loader_strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len);
72+
plugin_loader_strlcpy(s, tmp, PLUGIN_MAX_ERRLEN);
4373
}
4474

4575
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)
71101
plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t));
72102
if (!ret)
73103
{
74-
strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN);
104+
plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN);
75105
return NULL;
76106
}
77107

@@ -86,15 +116,15 @@ plugin_handle_t* plugin_load(const char* path, char* err)
86116
LPTSTR msg_buf = 0;
87117
if (FormatMessageA(flg, 0, GetLastError(), 0, (LPTSTR) &msg_buf, 0, NULL) && msg_buf)
88118
{
89-
strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN);
119+
plugin_loader_strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN);
90120
LocalFree(msg_buf);
91121
}
92122
}
93123
#else
94124
ret->handle = dlopen(path, RTLD_LAZY);
95125
if (ret->handle == NULL)
96126
{
97-
strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN);
127+
plugin_loader_strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN);
98128
}
99129
#endif
100130

@@ -143,14 +173,14 @@ plugin_handle_t* plugin_load_api(const plugin_api* api, char* err)
143173
err[0] = '\0';
144174
if (!api)
145175
{
146-
strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN);
176+
plugin_loader_strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN);
147177
return NULL;
148178
}
149179

150180
plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t));
151181
if (!ret)
152182
{
153-
strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN);
183+
plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN);
154184
return NULL;
155185
}
156186
ret->api = *api;
@@ -203,7 +233,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err)
203233
const char *ver, *failmsg;
204234
if (h->api.get_required_api_version == NULL)
205235
{
206-
strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN);
236+
plugin_loader_strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN);
207237
return false;
208238
}
209239

@@ -243,7 +273,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err)
243273
plugin_caps_t plugin_get_capabilities(const plugin_handle_t* h, char* err)
244274
{
245275
plugin_caps_t caps = CAP_NONE;
246-
strlcpy(err, "", PLUGIN_MAX_ERRLEN);
276+
plugin_loader_strlcpy(err, "", PLUGIN_MAX_ERRLEN);
247277

248278
if (h->api.open != NULL && h->api.close != NULL && h->api.next_batch != NULL)
249279
{

pkg/loader/strlcpy.patch

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
diff --git a/pkg/loader/plugin_loader.c b/pkg/loader/plugin_loader.c
2+
index aad119f..169f696 100644
3+
--- a/pkg/loader/plugin_loader.c
4+
+++ b/pkg/loader/plugin_loader.c
5+
@@ -23,23 +23,53 @@ limitations under the License.
6+
typedef void* library_handle_t;
7+
#endif
8+
9+
-#include "strlcpy.h"
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include "plugin_loader.h"
14+
15+
+// note(jasondellaluce,therealbobo): implementation taken from falcosecurity/libs
16+
+// note(leogr): to avoid clashing with `strlcpy` introduced by glibc 2.38,
17+
+// the func has been renamed to plugin_loader_strlcpy.
18+
+// N.B.: our building system here is not smart enough to detect if the function
19+
+// was declared already.
20+
+#include <stdint.h>
21+
+#include <string.h>
22+
+/*!
23+
+ \brief Copy up to size - 1 characters from the NUL-terminated string src to dst, NUL-terminating the result.
24+
+
25+
+ \return The length of the source string.
26+
+*/
27+
+
28+
+static inline size_t plugin_loader_strlcpy(char *dst, const char *src, size_t size) {
29+
+ size_t srcsize = strlen(src);
30+
+ if (size == 0) {
31+
+ return srcsize;
32+
+ }
33+
+
34+
+ size_t copysize = srcsize;
35+
+
36+
+ if (copysize > size - 1) {
37+
+ copysize = size - 1;
38+
+ }
39+
+
40+
+ memcpy(dst, src, copysize);
41+
+ dst[copysize] = '\0';
42+
+
43+
+ return srcsize;
44+
+}
45+
+
46+
static inline void err_prepend(char* s, const char* prefix, const char* sep)
47+
{
48+
char tmp[PLUGIN_MAX_ERRLEN];
49+
- size_t prefix_len = strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN);
50+
+ size_t prefix_len = plugin_loader_strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN);
51+
if (*s != '\0')
52+
{
53+
- strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len);
54+
+ plugin_loader_strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len);
55+
prefix_len += strlen(sep);
56+
}
57+
- strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len);
58+
- strlcpy(s, tmp, PLUGIN_MAX_ERRLEN);
59+
+ plugin_loader_strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len);
60+
+ plugin_loader_strlcpy(s, tmp, PLUGIN_MAX_ERRLEN);
61+
}
62+
63+
static inline void err_append(char* s, const char* suffix, const char* sep)
64+
@@ -71,7 +101,7 @@ plugin_handle_t* plugin_load(const char* path, char* err)
65+
plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t));
66+
if (!ret)
67+
{
68+
- strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN);
69+
+ plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN);
70+
return NULL;
71+
}
72+
73+
@@ -86,7 +116,7 @@ plugin_handle_t* plugin_load(const char* path, char* err)
74+
LPTSTR msg_buf = 0;
75+
if (FormatMessageA(flg, 0, GetLastError(), 0, (LPTSTR) &msg_buf, 0, NULL) && msg_buf)
76+
{
77+
- strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN);
78+
+ plugin_loader_strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN);
79+
LocalFree(msg_buf);
80+
}
81+
}
82+
@@ -94,7 +124,7 @@ plugin_handle_t* plugin_load(const char* path, char* err)
83+
ret->handle = dlopen(path, RTLD_LAZY);
84+
if (ret->handle == NULL)
85+
{
86+
- strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN);
87+
+ plugin_loader_strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN);
88+
}
89+
#endif
90+
91+
@@ -143,14 +173,14 @@ plugin_handle_t* plugin_load_api(const plugin_api* api, char* err)
92+
err[0] = '\0';
93+
if (!api)
94+
{
95+
- strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN);
96+
+ plugin_loader_strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN);
97+
return NULL;
98+
}
99+
100+
plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t));
101+
if (!ret)
102+
{
103+
- strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN);
104+
+ plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN);
105+
return NULL;
106+
}
107+
ret->api = *api;
108+
@@ -203,7 +233,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err)
109+
const char *ver, *failmsg;
110+
if (h->api.get_required_api_version == NULL)
111+
{
112+
- strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN);
113+
+ plugin_loader_strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN);
114+
return false;
115+
}
116+
117+
@@ -243,7 +273,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err)
118+
plugin_caps_t plugin_get_capabilities(const plugin_handle_t* h, char* err)
119+
{
120+
plugin_caps_t caps = CAP_NONE;
121+
- strlcpy(err, "", PLUGIN_MAX_ERRLEN);
122+
+ plugin_loader_strlcpy(err, "", PLUGIN_MAX_ERRLEN);
123+
124+
if (h->api.open != NULL && h->api.close != NULL && h->api.next_batch != NULL)
125+
{

0 commit comments

Comments
 (0)