Skip to content

Commit 8342944

Browse files
dkorunicoktalz
authored andcommitted
MAJOR: haproxy: add wrapper around haproxy on start
this will allow LD_PRELOAD to work on HAProxy
1 parent e07e01d commit 8342944

File tree

7 files changed

+73
-13
lines changed

7 files changed

+73
-13
lines changed

.aspell.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ allowed:
5656
- svc
5757
- frontent
5858
- pprof
59+
- preload

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ bin/check-commit
99
.local/*
1010
__debug_bin*
1111
pkg/protection/libblock_secrets.so
12+
pkg/protection/haproxy_wrapper

build/Dockerfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ RUN apk add --no-cache build-base gcc musl-dev
1717
WORKDIR /src
1818

1919
COPY pkg/protection/block_secrets.c .
20+
COPY pkg/protection/haproxy_wrapper.c .
2021
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
2123

2224
FROM golang:1.24-alpine AS builder
2325
RUN apk --no-cache add git openssh
@@ -46,12 +48,15 @@ ENV S6_USER=haproxy
4648
ENV S6_GROUP=haproxy
4749

4850
COPY /fs /
51+
COPY --from=builder-c /src/libblock_secrets.so /usr/local/lib/libblock_secrets.so
52+
COPY --from=builder-c /src/haproxy_wrapper /usr/local/sbin/haproxy_wrapper
4953

5054
RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
5155
rm -f /usr/local/bin/dataplaneapi /usr/bin/dataplaneapi /etc/haproxy/dataplaneapi.yml && \
5256
rm -f /usr/local/bin/dataplaneapi-v2 /usr/bin/dataplaneapi-v2 && \
53-
chgrp -R haproxy /usr/local/etc/haproxy /run /var && \
54-
chmod -R ug+rwx /usr/local/etc/haproxy /run /var && \
57+
chgrp -R haproxy /usr/local/etc/haproxy /run /var /usr/local/sbin/haproxy_wrapper && \
58+
chmod -R ug+rwx /usr/local/etc/haproxy /run /var /usr/local/sbin/haproxy_wrapper && \
59+
setcap 'cap_net_bind_service=+ep' /usr/local/sbin/haproxy_wrapper && \
5560
case "${TARGETPLATFORM}" in \
5661
"linux/arm64") S6_ARCH=aarch64 ;; \
5762
"linux/amd64") S6_ARCH=x86_64 ;; \
@@ -74,6 +79,4 @@ RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
7479

7580
COPY --from=builder /src/fs/haproxy-ingress-controller .
7681

77-
COPY --from=builder-c /src/libblock_secrets.so /usr/local/lib/libblock_secrets.so
78-
7982
ENTRYPOINT ["/start.sh"]

build/Dockerfile.dev

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ FROM haproxytech/haproxy-alpine:3.2 AS builder-c
1515
RUN apk add --no-cache build-base gcc musl-dev
1616
WORKDIR /src
1717
COPY pkg/protection/block_secrets.c .
18+
COPY pkg/protection/haproxy_wrapper.c .
1819
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
1921

2022
FROM haproxytech/haproxy-alpine:3.2
2123
ARG TARGETPLATFORM
@@ -26,11 +28,14 @@ ENV S6_USER=haproxy
2628
ENV S6_GROUP=haproxy
2729

2830
COPY /fs /
31+
COPY --from=builder-c /src/libblock_secrets.so /usr/local/lib/libblock_secrets.so
32+
COPY --from=builder-c /src/haproxy_wrapper /usr/local/sbin/haproxy_wrapper
2933

3034
RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
3135
rm -f /usr/local/bin/dataplaneapi /usr/bin/dataplaneapi /etc/haproxy/dataplaneapi.yml && \
32-
chgrp -R haproxy /usr/local/etc/haproxy /run /var && \
33-
chmod -R ug+rwx /usr/local/etc/haproxy /run /var && \
36+
chgrp -R haproxy /usr/local/etc/haproxy /run /var /usr/local/sbin/haproxy_wrapper && \
37+
chmod -R ug+rwx /usr/local/etc/haproxy /run /var /usr/local/sbin/haproxy_wrapper && \
38+
setcap 'cap_net_bind_service=+ep' /usr/local/sbin/haproxy_wrapper && \
3439
case "${TARGETPLATFORM}" in \
3540
"linux/arm64") S6_ARCH=aarch64 ;; \
3641
"linux/amd64") S6_ARCH=x86_64 ;; \
@@ -53,6 +58,4 @@ RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
5358

5459
COPY kubernetes-ingress ./haproxy-ingress-controller
5560

56-
COPY --from=builder-c /src/libblock_secrets.so /usr/local/lib/libblock_secrets.so
57-
5861
ENTRYPOINT ["/start.sh"]

build/Dockerfile.pebble

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ RUN apk add --no-cache build-base gcc musl-dev
1616
WORKDIR /src
1717

1818
COPY pkg/protection/block_secrets.c .
19+
COPY pkg/protection/haproxy_wrapper.c .
1920
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
2022

2123
FROM golang:1.24-alpine AS builder
2224

@@ -42,11 +44,14 @@ FROM haproxytech/haproxy-alpine:3.2
4244
ARG TARGETPLATFORM
4345

4446
COPY /fs /
47+
COPY --from=builder-c /src/libblock_secrets.so /usr/local/lib/libblock_secrets.so
48+
COPY --from=builder-c /src/haproxy_wrapper /usr/local/sbin/haproxy_wrapper
4549

4650
RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
4751
rm -f /usr/local/bin/dataplaneapi /usr/bin/dataplaneapi && \
48-
chgrp -R haproxy /usr/local/etc/haproxy /run /var && \
49-
chmod -R ug+rwx /usr/local/etc/haproxy /run /var && \
52+
chgrp -R haproxy /usr/local/etc/haproxy /run /var /usr/local/sbin/haproxy_wrapper && \
53+
chmod -R ug+rwx /usr/local/etc/haproxy /run /var /usr/local/sbin/haproxy_wrapper && \
54+
setcap 'cap_net_bind_service=+ep' /usr/local/sbin/haproxy_wrapper && \
5055
chown -R haproxy:haproxy /var/lib/pebble/default && \
5156
chmod ugo+rwx /var/lib/pebble/default/* && \
5257
rm -rf /etc/services.d/haproxy && \
@@ -57,6 +62,4 @@ RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
5762
COPY --from=builder /go/bin/pebble /usr/local/bin
5863
COPY --from=builder /src/fs/haproxy-ingress-controller .
5964

60-
COPY --from=builder-c /src/libblock_secrets.so /usr/local/lib/libblock_secrets.so
61-
6265
ENTRYPOINT ["/start-pebble.sh"]

fs/etc/s6-overlay/s6-rc.d/haproxy/run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ echo "Memory limit for HAProxy: ${MEMLIMIT}MiB"
2525
export LD_PRELOAD=/usr/local/lib/libblock_secrets.so
2626

2727
# if master socket is changed, that needs to be aligned in pkg/haproxy/process/interface.go
28-
exec /usr/local/sbin/haproxy -W -db -m "${MEMLIMIT}" -S /var/run/haproxy-master.sock,level,admin -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/haproxy-aux.cfg
28+
exec /usr/local/sbin/haproxy_wrapper -W -db -m "${MEMLIMIT}" -S /var/run/haproxy-master.sock,level,admin -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/haproxy-aux.cfg

pkg/protection/haproxy_wrapper.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Copyright 2025 HAProxy Technologies LLC */
2+
/* */
3+
/* Licensed under the Apache License, Version 2.0 (the "License"); */
4+
/* you may not use this file except in compliance with the License. */
5+
/* You may obtain a copy of the License at */
6+
/* */
7+
/* http://www.apache.org/licenses/LICENSE-2.0 */
8+
/* */
9+
/* Unless required by applicable law or agreed to in writing, software */
10+
/* distributed under the License is distributed on an "AS IS" BASIS, */
11+
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
12+
/* See the License for the specific language governing permissions and */
13+
/* limitations under the License. */
14+
15+
#define _GNU_SOURCE
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <unistd.h>
19+
20+
#define LIB_PATH "LD_PRELOAD=/usr/local/lib/libblock_secrets.so"
21+
#define TARGET_PATH "/usr/local/sbin/haproxy"
22+
23+
int main(int argc, char *argv[], char *envp[]) {
24+
char **newargv = malloc((argc + 1) * sizeof(char *));
25+
if (!newargv) {
26+
perror("malloc");
27+
return 1;
28+
}
29+
newargv[0] = (char *)TARGET_PATH;
30+
for (int i = 1; i < argc; i++) {
31+
newargv[i] = argv[i];
32+
}
33+
newargv[argc] = NULL;
34+
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];
41+
}
42+
new_envp[envc] = LIB_PATH;
43+
new_envp[envc + 1] = NULL;
44+
45+
execve(TARGET_PATH, newargv, new_envp);
46+
47+
perror("execve");
48+
return 1;
49+
}

0 commit comments

Comments
 (0)