Skip to content

Commit d092561

Browse files
authored
Add the ability to filter network access. (#245)
* Add iptables rule for blocking traffic to 169.254.169.254. * Load iptable rules when initing the sandbox to limit network access.
1 parent 3a15810 commit d092561

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

cmd/analyze/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ RUN curl -fsSL https://gvisor.dev/archive.key | apt-key add - && \
2727
COPY --from=build /src/analyze /usr/local/bin/analyze
2828
COPY --from=build /src/worker /usr/local/bin/worker
2929
COPY --from=build /src/tools/gvisor/runsc_compat.sh /usr/local/bin/runsc_compat.sh
30+
COPY --from=build /src/tools/firewall/iptables.rules /usr/local/etc/iptables.rules
3031
RUN chmod 755 /usr/local/bin/runsc_compat.sh
3132

3233
ARG SANDBOX_IMAGE_TAG

internal/sandbox/sandbox.go

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
const (
1717
podmanBin = "podman"
1818
runtimeBin = "/usr/local/bin/runsc_compat.sh"
19+
iptablesLoadBin = "/usr/sbin/iptables-restore"
20+
iptablesRules = "/usr/local/etc/iptables.rules"
1921
rootDir = "/var/run/runsc"
2022
straceFile = "runsc.log.boot"
2123
hostname = "box"
@@ -165,6 +167,11 @@ func removeAllLogs() error {
165167
return nil
166168
}
167169

170+
func loadIptablesRules() error {
171+
cmd := exec.Command(iptablesLoadBin, iptablesRules)
172+
return cmd.Run()
173+
}
174+
168175
func podman(args ...string) *exec.Cmd {
169176
args = append([]string{
170177
"--cgroup-manager=cgroupfs",
@@ -263,6 +270,10 @@ func (s *podmanSandbox) init() error {
263270
if s.container != "" {
264271
return nil
265272
}
273+
// Load iptables rules to further isolate the sandbox
274+
if err := loadIptablesRules(); err != nil {
275+
return fmt.Errorf("failed restoring iptables rules: %w", err)
276+
}
266277
// Delete existing logs (if any).
267278
if err := removeAllLogs(); err != nil {
268279
return fmt.Errorf("failed removing all logs: %w", err)

tools/firewall/iptables.rules

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Create the chain used by podman networking for user-defined rules
2+
*filter
3+
:CNI-ADMIN - [0:0]
4+
# Block access to metadata.google.internal/AWS metadata
5+
-A CNI-ADMIN -d 169.254.169.254/32 -j DROP
6+
COMMIT

0 commit comments

Comments
 (0)