From c26f01eefdf8c28881db7b6f8b8fd6120a3ad68e Mon Sep 17 00:00:00 2001 From: Jerome Eteve Date: Tue, 6 Jun 2023 15:26:50 +0100 Subject: [PATCH] client checks kernel module in /sys/module for WSL2 bridge networking (#17306) --- .changelog/17306.txt | 3 +++ client/fingerprint/bridge_linux.go | 16 ++++++++++++++++ client/fingerprint/bridge_linux_test.go | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .changelog/17306.txt diff --git a/.changelog/17306.txt b/.changelog/17306.txt new file mode 100644 index 000000000..683c410d3 --- /dev/null +++ b/.changelog/17306.txt @@ -0,0 +1,3 @@ +```release-note:improvement +client: check kernel module in `/sys/module` to help with WSL2 bridge networking +``` diff --git a/client/fingerprint/bridge_linux.go b/client/fingerprint/bridge_linux.go index e351c384b..10f0e78b6 100644 --- a/client/fingerprint/bridge_linux.go +++ b/client/fingerprint/bridge_linux.go @@ -56,6 +56,14 @@ func (f *BridgeFingerprint) detect(module string) error { // accumulate errors from every place we might find the module var errs error + // Check if the module is in /sys/modules + sysfsModulePath := fmt.Sprintf("/sys/module/%s", module) + if err := f.findDir(sysfsModulePath); err != nil { + errs = multierror.Append(errs, err) + } else { + return nil + } + // check if the module has been dynamically loaded dynamicPath := "/proc/modules" if err := f.searchFile(module, dynamicPath, f.regexp(dynamicModuleRe, module)); err != nil { @@ -89,6 +97,14 @@ func (f *BridgeFingerprint) detect(module string) error { return errs } +func (f *BridgeFingerprint) findDir(dirname string) error { + if _, err := os.Stat(dirname); err != nil { + return fmt.Errorf("failed to find %s: %v", dirname, err) + } else { + return nil + } +} + func (f *BridgeFingerprint) searchFile(module, filename string, re *regexp.Regexp) error { file, err := os.Open(filename) if err != nil { diff --git a/client/fingerprint/bridge_linux_test.go b/client/fingerprint/bridge_linux_test.go index 37d0acd5e..16581ba3c 100644 --- a/client/fingerprint/bridge_linux_test.go +++ b/client/fingerprint/bridge_linux_test.go @@ -19,11 +19,11 @@ func TestBridgeFingerprint_detect(t *testing.T) { ci.Parallel(t) f := &BridgeFingerprint{logger: testlog.HCLogger(t)} - require.NoError(t, f.detect("ip_tables")) + require.NoError(t, f.detect("kernel")) // kernel should be there. err := f.detect("nonexistentmodule") require.Error(t, err) - require.Contains(t, err.Error(), "3 errors occurred") + require.Contains(t, err.Error(), "4 errors occurred") } func writeFile(t *testing.T, prefix, content string) string {