Debian Codegraph @

pathrs-lite/internal/linux/mount_linux.go

raw
1 // SPDX-License-Identifier: MPL-2.0
2
3 //go:build linux
4
5 // Copyright (C) 2024-2025 Aleksa Sarai <[email protected]>
6 // Copyright (C) 2024-2025 SUSE LLC
7 //
8 // This Source Code Form is subject to the terms of the Mozilla Public
9 // License, v. 2.0. If a copy of the MPL was not distributed with this
10 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
11
12 package linux
13
14 import (
15 "golang.org/x/sys/unix"
16
17 "github.com/cyphar/filepath-securejoin/pathrs-lite/internal/gocompat"
18 "github.com/cyphar/filepath-securejoin/pathrs-lite/internal/kernelversion"
19 )
20
21 // HasNewMountAPI returns whether the new fsopen(2) mount API is supported on
22 // the running kernel.
23 var HasNewMountAPI = gocompat.SyncOnceValue(func() bool {
24 // All of the pieces of the new mount API we use (fsopen, fsconfig,
25 // fsmount, open_tree) were added together in Linux 5.2[1,2], so we can
26 // just check for one of the syscalls and the others should also be
27 // available.
28 //
29 // Just try to use open_tree(2) to open a file without OPEN_TREE_CLONE.
30 // This is equivalent to openat(2), but tells us if open_tree is
31 // available (and thus all of the other basic new mount API syscalls).
32 // open_tree(2) is most light-weight syscall to test here.
33 //
34 // [1]: merge commit 400913252d09
35 // [2]: <https://lore.kernel.org/lkml/153754740781.17872.7869536526927736855.stgit@warthog.procyon.org.uk/>
36 fd, err := unix.OpenTree(-int(unix.EBADF), "/", unix.OPEN_TREE_CLOEXEC)
37 if err != nil {
38 return false
39 }
40 _ = unix.Close(fd)
41
42 // RHEL 8 has a backport of fsopen(2) that appears to have some very
43 // difficult to debug performance pathology. As such, it seems prudent to
44 // simply reject pre-5.2 kernels.
45 isNotBackport, _ := kernelversion.GreaterEqualThan(kernelversion.KernelVersion{5, 2})
46 return isNotBackport
47 })