Skip to content

Commit 52762dd

Browse files
dkorunicGopher Bot
authored andcommitted
MAJOR: feature and security improvements for haproxy wrapper
1 parent d8cb855 commit 52762dd

4 files changed

Lines changed: 56 additions & 22 deletions

File tree

build/Dockerfile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,24 @@ WORKDIR /src
3939

4040
COPY hug/protection/block_secrets.c .
4141
COPY hug/protection/haproxy_wrapper.c .
42-
RUN gcc -O3 -Wall -flto -fPIC -shared -s -o libblock_secrets.so block_secrets.c -ldl
43-
RUN gcc -O3 -Wall -g -s -o haproxy_wrapper haproxy_wrapper.c
42+
RUN gcc -O3 -std=c11 -pipe \
43+
-Wall -Wextra -Wformat=2 -Wformat-security \
44+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
45+
-D_FORTIFY_SOURCE=2 \
46+
-fstack-protector-strong -fstack-clash-protection \
47+
-fno-omit-frame-pointer \
48+
-flto -fPIC -shared -s \
49+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed -Wl,-z,defs \
50+
-o libblock_secrets.so block_secrets.c -ldl
51+
RUN gcc -O3 -std=c11 -pipe \
52+
-Wall -Wextra -Wformat=2 -Wformat-security \
53+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
54+
-D_FORTIFY_SOURCE=2 \
55+
-fstack-protector-strong -fstack-clash-protection \
56+
-fno-omit-frame-pointer \
57+
-fPIE -pie -s \
58+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed \
59+
-o haproxy_wrapper haproxy_wrapper.c
4460

4561
FROM haproxytech/haproxy-alpine:3.2
4662

build/Dockerfile.dev

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,24 @@ WORKDIR /src
2626

2727
COPY hug/protection/block_secrets.c .
2828
COPY hug/protection/haproxy_wrapper.c .
29-
RUN gcc -O3 -Wall -flto -fPIC -shared -s -o libblock_secrets.so block_secrets.c -ldl
30-
RUN gcc -O3 -Wall -g -s -o haproxy_wrapper haproxy_wrapper.c
29+
RUN gcc -O3 -std=c11 -pipe \
30+
-Wall -Wextra -Wformat=2 -Wformat-security \
31+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
32+
-D_FORTIFY_SOURCE=2 \
33+
-fstack-protector-strong -fstack-clash-protection \
34+
-fno-omit-frame-pointer \
35+
-flto -fPIC -shared -s \
36+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed -Wl,-z,defs \
37+
-o libblock_secrets.so block_secrets.c -ldl
38+
RUN gcc -O3 -std=c11 -pipe \
39+
-Wall -Wextra -Wformat=2 -Wformat-security \
40+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
41+
-D_FORTIFY_SOURCE=2 \
42+
-fstack-protector-strong -fstack-clash-protection \
43+
-fno-omit-frame-pointer \
44+
-fPIE -pie -s \
45+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed \
46+
-o haproxy_wrapper haproxy_wrapper.c
3147

3248
FROM haproxytech/haproxy-alpine:3.2
3349

hug/protection/block_secrets.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/* See the License for the specific language governing permissions and */
1313
/* limitations under the License. */
1414

15+
#define _XOPEN_SOURCE 700
16+
1517
#include <dirent.h>
1618
#include <dlfcn.h>
1719
#include <errno.h>
@@ -263,7 +265,7 @@ static int (*real_symlinkat)(const char *, int, const char *) = NULL;
263265

264266
/* Priority 101 (0–100 reserved). dlsym before realpath so any internal
265267
hook recursion finds populated pointers. */
266-
__attribute__((constructor(101), cold)) static void block_secrets_init() {
268+
__attribute__((constructor(101), cold)) static void block_secrets_init(void) {
267269
real_open = dlsym(RTLD_NEXT, "open");
268270
real_open64 = dlsym(RTLD_NEXT, "open64");
269271
real_fopen = dlsym(RTLD_NEXT, "fopen");

hug/protection/haproxy_wrapper.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,37 @@
1313
/* limitations under the License. */
1414

1515
#define _GNU_SOURCE
16+
#include <errno.h>
1617
#include <stdio.h>
1718
#include <stdlib.h>
19+
#include <string.h>
1820
#include <unistd.h>
1921

20-
#define LIB_PATH "LD_PRELOAD=/usr/local/lib/libblock_secrets.so"
22+
#define LIB_PATH "/usr/local/lib/libblock_secrets.so"
2123
#define TARGET_PATH "/usr/local/sbin/haproxy"
2224

23-
int main(int argc, char *argv[], char *envp[]) {
24-
char **newargv = malloc((argc + 1) * sizeof(char *));
25+
int main(int argc, char *argv[]) {
26+
int n = argc > 0 ? argc : 1;
27+
char **newargv = malloc((n + 1) * sizeof(char *));
2528
if (!newargv) {
2629
perror("malloc");
27-
return 1;
30+
return EXIT_FAILURE;
2831
}
2932
newargv[0] = (char *)TARGET_PATH;
30-
for (int i = 1; i < argc; i++) {
33+
for (int i = 1; i < n; i++) {
3134
newargv[i] = argv[i];
3235
}
33-
newargv[argc] = NULL;
36+
newargv[n] = NULL;
3437

35-
int envc = 0;
36-
while (environ[envc]) envc++;
37-
38-
char **new_envp = malloc((envc + 2) * sizeof(char *));
39-
for (int i = 0; i < envc; i++) {
40-
new_envp[i] = environ[i];
38+
if (setenv("LD_PRELOAD", LIB_PATH, 1) != 0) {
39+
perror("setenv LD_PRELOAD");
40+
free(newargv);
41+
return EXIT_FAILURE;
4142
}
42-
new_envp[envc] = LIB_PATH;
43-
new_envp[envc + 1] = NULL;
4443

45-
execve(TARGET_PATH, newargv, new_envp);
44+
execv(TARGET_PATH, newargv);
4645

47-
perror("execve");
48-
return 1;
46+
fprintf(stderr, "execv %s: %s\n", TARGET_PATH, strerror(errno));
47+
free(newargv);
48+
return EXIT_FAILURE;
4949
}

0 commit comments

Comments
 (0)