| 1 |
// SPDX-License-Identifier: BSD-3-Clause |
| 2 |
|
| 3 |
// Copyright (C) 2022 The Go Authors. All rights reserved. |
| 4 |
// Copyright (C) 2025 SUSE LLC. All rights reserved. |
| 5 |
// Use of this source code is governed by a BSD-style |
| 6 |
// license that can be found in the LICENSE.BSD file. |
| 7 |
|
| 8 |
// The parsing logic is very loosely based on the Go stdlib's |
| 9 |
// src/internal/syscall/unix/kernel_version_linux.go but with an API that looks |
| 10 |
// a bit like runc's libcontainer/system/kernelversion. |
| 11 |
// |
| 12 |
// TODO(cyphar): This API has been copied around to a lot of different projects |
| 13 |
// (Docker, containerd, runc, and now filepath-securejoin) -- maybe we should |
| 14 |
// put it in a separate project? |
| 15 |
|
| 16 |
// Package kernelversion provides a simple mechanism for checking whether the |
| 17 |
// running kernel is at least as new as some baseline kernel version. This is |
| 18 |
// often useful when checking for features that would be too complicated to |
| 19 |
// test support for (or in cases where we know that some kernel features in |
| 20 |
// backport-heavy kernels are broken and need to be avoided). |
| 21 |
package kernelversion |
| 22 |
|
| 23 |
import ( |
| 24 |
"bytes" |
| 25 |
"errors" |
| 26 |
"fmt" |
| 27 |
"strconv" |
| 28 |
"strings" |
| 29 |
|
| 30 |
"golang.org/x/sys/unix" |
| 31 |
|
| 32 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/gocompat" |
| 33 |
) |
| 34 |
|
| 35 |
// KernelVersion is a numeric representation of the key numerical elements of a |
| 36 |
// kernel version (for instance, "4.1.2-default-1" would be represented as |
| 37 |
// KernelVersion{4, 1, 2}). |
| 38 |
type KernelVersion []uint64 |
| 39 |
|
| 40 |
func (kver KernelVersion) String() string { |
| 41 |
var str strings.Builder |
| 42 |
for idx, elem := range kver { |
| 43 |
if idx != 0 { |
| 44 |
_, _ = str.WriteRune('.') |
| 45 |
} |
| 46 |
_, _ = str.WriteString(strconv.FormatUint(elem, 10)) |
| 47 |
} |
| 48 |
return str.String() |
| 49 |
} |
| 50 |
|
| 51 |
var errInvalidKernelVersion = errors.New("invalid kernel version") |
| 52 |
|
| 53 |
// parseKernelVersion parses a string and creates a KernelVersion based on it. |
| 54 |
func parseKernelVersion(kverStr string) (KernelVersion, error) { |
| 55 |
kver := make(KernelVersion, 1, 3) |
| 56 |
for idx, ch := range kverStr { |
| 57 |
if '0' <= ch && ch <= '9' { |
| 58 |
v := &kver[len(kver)-1] |
| 59 |
*v = (*v * 10) + uint64(ch-'0') |
| 60 |
} else { |
| 61 |
if idx == 0 || kverStr[idx-1] < '0' || '9' < kverStr[idx-1] { |
| 62 |
// "." must be preceded by a digit while in version section |
| 63 |
return nil, fmt.Errorf("%w %q: kernel version has dot(s) followed by non-digit in version section", errInvalidKernelVersion, kverStr) |
| 64 |
} |
| 65 |
if ch != '.' { |
| 66 |
break |
| 67 |
} |
| 68 |
kver = append(kver, 0) |
| 69 |
} |
| 70 |
} |
| 71 |
if len(kver) < 2 { |
| 72 |
return nil, fmt.Errorf("%w %q: kernel versions must contain at least two components", errInvalidKernelVersion, kverStr) |
| 73 |
} |
| 74 |
return kver, nil |
| 75 |
} |
| 76 |
|
| 77 |
// getKernelVersion gets the current kernel version. |
| 78 |
var getKernelVersion = gocompat.SyncOnceValues(func() (KernelVersion, error) { |
| 79 |
var uts unix.Utsname |
| 80 |
if err := unix.Uname(&uts); err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
// Remove the \x00 from the release. |
| 84 |
release := uts.Release[:] |
| 85 |
return parseKernelVersion(string(release[:bytes.IndexByte(release, 0)])) |
| 86 |
}) |
| 87 |
|
| 88 |
// GreaterEqualThan returns true if the the host kernel version is greater than |
| 89 |
// or equal to the provided [KernelVersion]. When doing this comparison, any |
| 90 |
// non-numerical suffixes of the host kernel version are ignored. |
| 91 |
// |
| 92 |
// If the number of components provided is not equal to the number of numerical |
| 93 |
// components of the host kernel version, any missing components are treated as |
| 94 |
// 0. This means that GreaterEqualThan(KernelVersion{4}) will be treated the |
| 95 |
// same as GreaterEqualThan(KernelVersion{4, 0, 0, ..., 0, 0}), and that if the |
| 96 |
// host kernel version is "4" then GreaterEqualThan(KernelVersion{4, 1}) will |
| 97 |
// return false (because the host version will be treated as "4.0"). |
| 98 |
func GreaterEqualThan(wantKver KernelVersion) (bool, error) { |
| 99 |
hostKver, err := getKernelVersion() |
| 100 |
if err != nil { |
| 101 |
return false, err |
| 102 |
} |
| 103 |
|
| 104 |
// Pad out the kernel version lengths to match one another. |
| 105 |
cmpLen := gocompat.Max2(len(hostKver), len(wantKver)) |
| 106 |
hostKver = append(hostKver, make(KernelVersion, cmpLen-len(hostKver))...) |
| 107 |
wantKver = append(wantKver, make(KernelVersion, cmpLen-len(wantKver))...) |
| 108 |
|
| 109 |
for i := 0; i < cmpLen; i++ { |
| 110 |
switch gocompat.CmpCompare(hostKver[i], wantKver[i]) { |
| 111 |
case -1: |
| 112 |
// host < want |
| 113 |
return false, nil |
| 114 |
case +1: |
| 115 |
// host > want |
| 116 |
return true, nil |
| 117 |
case 0: |
| 118 |
continue |
| 119 |
} |
| 120 |
} |
| 121 |
// equal version values |
| 122 |
return true, nil |
| 123 |
} |