Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FreeBSD version parsing #5296

Merged
merged 1 commit into from Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 38 additions & 24 deletions pkg/jail/jail.go
Expand Up @@ -4,6 +4,7 @@
package jail

import (
"fmt"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -187,43 +188,56 @@ func (j *jail) Set(jconf *config) error {
return err
}

func parseVersion(version string) (string, int, int, int, error) {
// Expected formats:
// <major>.<minor>-RELEASE optionally followed by -p<patchlevel>
// <major>-STABLE
// <major>-CURRENT
parts := strings.Split(string(version), "-")
if len(parts) < 2 || len(parts) > 3 {
return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
}
ver := strings.Split(parts[0], ".")

if len(ver) != 2 {
return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
}
major, err := strconv.Atoi(ver[0])
if err != nil {
return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
}
minor, err := strconv.Atoi(ver[1])
if err != nil {
return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
}
patchlevel := 0
if len(parts) == 3 {
if parts[1] != "RELEASE" || !strings.HasPrefix(parts[2], "p") {
return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
}
patchlevel, err = strconv.Atoi(strings.TrimPrefix(parts[2], "p"))
if err != nil {
return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
}
}
return parts[1], major, minor, patchlevel, nil
}

// Return true if its necessary to have a separate jail to own the vnet. For
// FreeBSD 13.3 and later, we don't need a separate vnet jail since it is
// possible to configure the network without either attaching to the container's
// jail or trusting the ifconfig and route utilities in the container. If for
// any reason, we fail to parse the OS version, we default to returning true.
func NeedVnetJail() bool {
needVnetJailOnce.Do(func() {
// FreeBSD 13.3 and later have support for 'ifconfig -j' and 'route -j'
needVnetJail = true
version, err := util.ReadKernelVersion()
if err != nil {
logrus.Errorf("failed to determine OS version: %v", err)
return
}
// Expected formats "<major>.<minor>-<RELEASE|STABLE|CURRENT>" optionally
// followed by "-<patchlevel>"
parts := strings.Split(string(version), "-")
if len(parts) < 2 {
logrus.Errorf("unexpected OS version: %s", version)
return
}
ver := strings.Split(parts[0], ".")
if len(parts) != 2 {
logrus.Errorf("unexpected OS version: %s", version)
return
}

// FreeBSD 13.3 and later have support for 'ifconfig -j' and 'route -j'
major, err := strconv.Atoi(ver[0])
if err != nil {
logrus.Errorf("unexpected OS version: %s", version)
return
}
minor, err := strconv.Atoi(ver[1])
if err != nil {
logrus.Errorf("unexpected OS version: %s", version)
return
}
_, major, minor, _, err := parseVersion(version)
if major > 13 || (major == 13 && minor > 2) {
needVnetJail = false
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/jail/jail_test.go
@@ -0,0 +1,42 @@
//go:build freebsd
// +build freebsd

package jail

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseVersion(t *testing.T) {
tt := []struct {
version string
shouldFail bool
kind string
major, minor, patchlevel int
}{
{"14.0-RELEASE", false, "RELEASE", 14, 0, 0},
{"14.0-RELEASE-p3", false, "RELEASE", 14, 0, 3},
{"13.2-STABLE", false, "STABLE", 13, 2, 0},
{"14.0-STABLE", false, "STABLE", 14, 0, 0},
{"15.0-CURRENT", false, "CURRENT", 15, 0, 0},

{"14-RELEASE", true, "", -1, -1, -1},
{"14.1-STABLE-p1", true, "", -1, -1, -1},
{"14-RELEASE-p3", true, "", -1, -1, -1},
}
for _, tc := range tt {
kind, major, minor, patchlevel, err := parseVersion(tc.version)
if tc.shouldFail {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, kind, tc.kind)
assert.Equal(t, major, tc.major)
assert.Equal(t, minor, tc.minor)
assert.Equal(t, patchlevel, tc.patchlevel)
}

}
}