Skip to content

Commit 8a8ab0d

Browse files
authored
Merge pull request #49043 from robmry/backport-27.x/modprobeless
[27.x backport] Try to load kernel modules, without modprobe
2 parents d1201f6 + adb8773 commit 8a8ab0d

File tree

6 files changed

+129
-52
lines changed

6 files changed

+129
-52
lines changed

Jenkinsfile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@ pipeline {
6060
}
6161

6262
stages {
63-
stage("Load kernel modules") {
64-
steps {
65-
sh '''
66-
sudo modprobe ip6table_filter
67-
sudo modprobe -va br_netfilter
68-
sudo systemctl restart docker.service
69-
'''
70-
}
71-
}
7263
stage("Print info") {
7364
steps {
7465
sh 'docker version'

hack/make/run

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,6 @@ if [ -n "$DOCKER_ROOTLESS" ]; then
9191
)
9292
fi
9393

94-
# On a host using nftables, the ip6_tables kernel module may need to be loaded.
95-
# This trick is borrowed from the docker (dind) official image ...
96-
# "modprobe" without modprobe
97-
# https://twitter.com/lucabruno/status/902934379835662336
98-
# This isn't 100% fool-proof, but it'll have a much higher success rate than
99-
# simply using the "real" modprobe (which isn't installed in the dev container).
100-
if ! ip6tables -nL > /dev/null 2>&1; then
101-
ip link show ip6_tables > /dev/null 2>&1 || true
102-
if ! ip6tables -nL > /dev/null 2>&1; then
103-
echo >&2 'ip6tables is not available'
104-
fi
105-
fi
106-
10794
set -x
10895
# shellcheck disable=SC2086
10996
exec "${dockerd[@]}" "${args[@]}"
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Package modprobe attempts to load kernel modules. It may have more success
2+
// than simply running "modprobe", particularly for docker-in-docker.
3+
package modprobe
4+
5+
import (
6+
"context"
7+
"errors"
8+
"fmt"
9+
"os/exec"
10+
"strings"
11+
12+
"github.com/containerd/log"
13+
"golang.org/x/sys/unix"
14+
)
15+
16+
// LoadModules attempts to load kernel modules, if necessary.
17+
//
18+
// isLoaded must be a function that checks whether the modules are loaded. It may
19+
// be called multiple times. isLoaded must return an error to indicate that the
20+
// modules still need to be loaded, otherwise nil.
21+
//
22+
// For each method of loading modules, LoadModules will attempt the load for each
23+
// of modNames, then it will call isLoaded to check the result - moving on to try
24+
// the next method if needed, and there is one.
25+
//
26+
// The returned error is the result of the final call to isLoaded.
27+
func LoadModules(ctx context.Context, isLoaded func() error, modNames ...string) error {
28+
if isLoaded() == nil {
29+
log.G(ctx).WithFields(log.Fields{
30+
"modules": modNames,
31+
}).Debug("Modules already loaded")
32+
return nil
33+
}
34+
35+
if err := tryLoad(ctx, isLoaded, modNames, ioctlLoader{}); err != nil {
36+
return tryLoad(ctx, isLoaded, modNames, modprobeLoader{})
37+
}
38+
return nil
39+
}
40+
41+
type loader interface {
42+
name() string
43+
load(modName string) error
44+
}
45+
46+
func tryLoad(ctx context.Context, isLoaded func() error, modNames []string, loader loader) error {
47+
var loadErrs []error
48+
for _, modName := range modNames {
49+
if err := loader.load(modName); err != nil {
50+
loadErrs = append(loadErrs, err)
51+
}
52+
}
53+
54+
if checkResult := isLoaded(); checkResult != nil {
55+
log.G(ctx).WithFields(log.Fields{
56+
"loader": loader.name(),
57+
"modules": modNames,
58+
"loadErrors": errors.Join(loadErrs...),
59+
"checkResult": checkResult,
60+
}).Debug("Modules not loaded")
61+
return checkResult
62+
}
63+
64+
log.G(ctx).WithFields(log.Fields{
65+
"loader": loader.name(),
66+
"modules": modNames,
67+
"loadErrors": errors.Join(loadErrs...),
68+
}).Debug("Modules loaded")
69+
return nil
70+
}
71+
72+
// ioctlLoader attempts to load the module using an ioctl() to get the interface index
73+
// of a module - it won't have one, but the kernel may load the module. This tends to
74+
// work in docker-in-docker, where the inner-docker may not have "modprobe" or access
75+
// to modules in the host's filesystem.
76+
type ioctlLoader struct{}
77+
78+
func (il ioctlLoader) name() string { return "ioctl" }
79+
80+
func (il ioctlLoader) load(modName string) error {
81+
sd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0)
82+
if err != nil {
83+
return fmt.Errorf("creating socket for ioctl load of %s: %w", modName, err)
84+
}
85+
defer unix.Close(sd)
86+
87+
// This tends to work, if running with CAP_SYS_MODULE, because...
88+
// https://github.com/torvalds/linux/blob/6f7da290413ba713f0cdd9ff1a2a9bb129ef4f6c/net/core/dev_ioctl.c#L457
89+
// https://github.com/torvalds/linux/blob/6f7da290413ba713f0cdd9ff1a2a9bb129ef4f6c/net/core/dev_ioctl.c#L371-L372
90+
ifreq, err := unix.NewIfreq(modName)
91+
if err != nil {
92+
return fmt.Errorf("creating ifreq for %s: %w", modName, err)
93+
}
94+
// An error is returned even if the module load is successful. So, ignore it.
95+
_ = unix.IoctlIfreq(sd, unix.SIOCGIFINDEX, ifreq)
96+
return nil
97+
}
98+
99+
// modprobeLoader attempts to load a kernel module using modprobe.
100+
type modprobeLoader struct{}
101+
102+
func (ml modprobeLoader) name() string { return "modprobe" }
103+
104+
func (ml modprobeLoader) load(modName string) error {
105+
out, err := exec.Command("modprobe", "-va", modName).CombinedOutput()
106+
if err != nil {
107+
return fmt.Errorf("modprobe %s failed with message: %q, error: %w", modName, strings.TrimSpace(string(out)), err)
108+
}
109+
return nil
110+
}

libnetwork/drivers/bridge/bridge_linux.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/containerd/log"
1212
"github.com/docker/docker/errdefs"
13+
"github.com/docker/docker/internal/modprobe"
1314
"github.com/docker/docker/libnetwork/datastore"
1415
"github.com/docker/docker/libnetwork/driverapi"
1516
"github.com/docker/docker/libnetwork/internal/netiputil"
@@ -498,6 +499,14 @@ func (d *driver) configure(option map[string]interface{}) error {
498499
}
499500

500501
if config.EnableIP6Tables {
502+
if err := modprobe.LoadModules(context.TODO(), func() error {
503+
iptable := iptables.GetIptable(iptables.IPv6)
504+
_, err := iptable.Raw("-t", "filter", "-n", "-L", "FORWARD")
505+
return err
506+
}, "ip6_tables"); err != nil {
507+
log.G(context.TODO()).WithError(err).Debug("Loading ip6_tables")
508+
}
509+
501510
removeIPChains(iptables.IPv6)
502511

503512
natChainV6, filterChainV6, isolationChain1V6, isolationChain2V6, err = setupIPChains(config, iptables.IPv6)

libnetwork/drivers/bridge/setup_bridgenetfiltering.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"errors"
88
"fmt"
99
"os"
10-
"os/exec"
1110
"syscall"
1211

1312
"github.com/containerd/log"
13+
"github.com/docker/docker/internal/modprobe"
1414
)
1515

1616
// setupIPv4BridgeNetFiltering checks whether IPv4 forwarding is enabled and, if
@@ -46,21 +46,17 @@ func setupIPv6BridgeNetFiltering(config *networkConfiguration, _ *bridgeInterfac
4646
}
4747

4848
func loadBridgeNetFilterModule(fullPath string) error {
49-
// br_netfilter implictly loads bridge module upon modprobe
50-
modName := "br_netfilter"
51-
if _, err := os.Stat(fullPath); err != nil {
52-
if out, err := exec.Command("modprobe", "-va", modName).CombinedOutput(); err != nil {
53-
log.G(context.TODO()).WithError(err).Errorf("Running modprobe %s failed with message: %s", modName, out)
54-
return fmt.Errorf("cannot restrict inter-container communication: modprobe %s failed: %w", modName, err)
55-
}
56-
}
57-
return nil
49+
// br_netfilter implicitly loads bridge module upon modprobe
50+
return modprobe.LoadModules(context.TODO(), func() error {
51+
_, err := os.Stat(fullPath)
52+
return err
53+
}, "br_netfilter")
5854
}
5955

6056
// Enable bridge net filtering if not already enabled. See GitHub issue #11404
6157
func enableBridgeNetFiltering(nfParam string) error {
6258
if err := loadBridgeNetFilterModule(nfParam); err != nil {
63-
return fmt.Errorf("loadBridgeNetFilterModule failed: %s", err)
59+
return fmt.Errorf("cannot restrict inter-container communication or run without the userland proxy: %w", err)
6460
}
6561
enabled, err := getKernelBoolParam(nfParam)
6662
if err != nil {

libnetwork/ns/init_linux.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package ns
22

33
import (
44
"context"
5-
"fmt"
6-
"os/exec"
7-
"strings"
85
"sync"
96
"syscall"
107
"time"
118

129
"github.com/containerd/log"
10+
"github.com/docker/docker/internal/modprobe"
1311
"github.com/vishvananda/netlink"
1412
"github.com/vishvananda/netns"
1513
)
@@ -65,12 +63,8 @@ func getSupportedNlFamilies() []int {
6563
fams = append(fams, syscall.NETLINK_XFRM)
6664
}
6765
// NETLINK_NETFILTER test
68-
if err := loadNfConntrackModules(); err != nil {
69-
if checkNfSocket() != nil {
70-
log.G(context.TODO()).Warnf("Could not load necessary modules for Conntrack: %v", err)
71-
} else {
72-
fams = append(fams, syscall.NETLINK_NETFILTER)
73-
}
66+
if err := modprobe.LoadModules(context.TODO(), checkNfSocket, "nf_conntrack", "nf_conntrack_netlink"); err != nil {
67+
log.G(context.TODO()).Warnf("Could not load necessary modules for Conntrack: %v", err)
7468
} else {
7569
fams = append(fams, syscall.NETLINK_NETFILTER)
7670
}
@@ -88,16 +82,6 @@ func checkXfrmSocket() error {
8882
return nil
8983
}
9084

91-
func loadNfConntrackModules() error {
92-
if out, err := exec.Command("modprobe", "-va", "nf_conntrack").CombinedOutput(); err != nil {
93-
return fmt.Errorf("Running modprobe nf_conntrack failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
94-
}
95-
if out, err := exec.Command("modprobe", "-va", "nf_conntrack_netlink").CombinedOutput(); err != nil {
96-
return fmt.Errorf("Running modprobe nf_conntrack_netlink failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
97-
}
98-
return nil
99-
}
100-
10185
// API check on required nf_conntrack* modules (nf_conntrack, nf_conntrack_netlink)
10286
func checkNfSocket() error {
10387
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_NETFILTER)

0 commit comments

Comments
 (0)