Skip to content

Commit 74bb2b9

Browse files
dkorunicGopher Bot
authored andcommitted
MAJOR: feature and security improvements for haproxy wrapper
1 parent 606901e commit 74bb2b9

5 files changed

Lines changed: 74 additions & 24 deletions

File tree

build/Dockerfile

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

1919
COPY pkg/protection/block_secrets.c .
2020
COPY pkg/protection/haproxy_wrapper.c .
21-
RUN gcc -O3 -Wall -flto -fPIC -shared -s -o libblock_secrets.so block_secrets.c -ldl
22-
RUN gcc -O3 -Wall -g -s -o haproxy_wrapper haproxy_wrapper.c
21+
RUN gcc -O3 -std=c11 -pipe \
22+
-Wall -Wextra -Wformat=2 -Wformat-security \
23+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
24+
-D_FORTIFY_SOURCE=2 \
25+
-fstack-protector-strong -fstack-clash-protection \
26+
-fno-omit-frame-pointer \
27+
-flto -fPIC -shared -s \
28+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed -Wl,-z,defs \
29+
-o libblock_secrets.so block_secrets.c -ldl
30+
RUN gcc -O3 -std=c11 -pipe \
31+
-Wall -Wextra -Wformat=2 -Wformat-security \
32+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
33+
-D_FORTIFY_SOURCE=2 \
34+
-fstack-protector-strong -fstack-clash-protection \
35+
-fno-omit-frame-pointer \
36+
-fPIE -pie -s \
37+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed \
38+
-o haproxy_wrapper haproxy_wrapper.c
2339

2440
FROM golang:1.25-alpine AS builder
2541
RUN apk --no-cache add git openssh

build/Dockerfile.dev

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,24 @@ RUN apk add --no-cache build-base gcc musl-dev
1616
WORKDIR /src
1717
COPY pkg/protection/block_secrets.c .
1818
COPY pkg/protection/haproxy_wrapper.c .
19-
RUN gcc -O3 -Wall -flto -fPIC -shared -s -o libblock_secrets.so block_secrets.c -ldl
20-
RUN gcc -O3 -Wall -g -s -o haproxy_wrapper haproxy_wrapper.c
19+
RUN gcc -O3 -std=c11 -pipe \
20+
-Wall -Wextra -Wformat=2 -Wformat-security \
21+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
22+
-D_FORTIFY_SOURCE=2 \
23+
-fstack-protector-strong -fstack-clash-protection \
24+
-fno-omit-frame-pointer \
25+
-flto -fPIC -shared -s \
26+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed -Wl,-z,defs \
27+
-o libblock_secrets.so block_secrets.c -ldl
28+
RUN gcc -O3 -std=c11 -pipe \
29+
-Wall -Wextra -Wformat=2 -Wformat-security \
30+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
31+
-D_FORTIFY_SOURCE=2 \
32+
-fstack-protector-strong -fstack-clash-protection \
33+
-fno-omit-frame-pointer \
34+
-fPIE -pie -s \
35+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed \
36+
-o haproxy_wrapper haproxy_wrapper.c
2137

2238
FROM haproxytech/haproxy-alpine:3.2
2339
ARG TARGETPLATFORM

build/Dockerfile.pebble

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

1818
COPY pkg/protection/block_secrets.c .
1919
COPY pkg/protection/haproxy_wrapper.c .
20-
RUN gcc -O3 -Wall -flto -fPIC -shared -s -o libblock_secrets.so block_secrets.c -ldl
21-
RUN gcc -O3 -Wall -g -s -o haproxy_wrapper haproxy_wrapper.c
20+
RUN gcc -O3 -std=c11 -pipe \
21+
-Wall -Wextra -Wformat=2 -Wformat-security \
22+
-Wnull-dereference -Wshadow -Wstrict-prototypes \
23+
-D_FORTIFY_SOURCE=2 \
24+
-fstack-protector-strong -fstack-clash-protection \
25+
-fno-omit-frame-pointer \
26+
-flto -fPIC -shared -s \
27+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed -Wl,-z,defs \
28+
-o libblock_secrets.so block_secrets.c -ldl
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+
-fPIE -pie -s \
36+
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,--as-needed \
37+
-o haproxy_wrapper haproxy_wrapper.c
2238

2339
FROM golang:1.25-alpine AS builder
2440

pkg/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");

pkg/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)