Debian Codegraph @

pathrs-lite/open_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 pathrs
13
14 import (
15 "os"
16
17 "golang.org/x/sys/unix"
18
19 "github.com/cyphar/filepath-securejoin/pathrs-lite/internal/procfs"
20 )
21
22 // OpenatInRoot is equivalent to [OpenInRoot], except that the root is provided
23 // using an *[os.File] handle, to ensure that the correct root directory is used.
24 func OpenatInRoot(root *os.File, unsafePath string) (*os.File, error) {
25 handle, err := completeLookupInRoot(root, unsafePath)
26 if err != nil {
27 return nil, &os.PathError{Op: "securejoin.OpenInRoot", Path: unsafePath, Err: err}
28 }
29 return handle, nil
30 }
31
32 // OpenInRoot safely opens the provided unsafePath within the root.
33 // Effectively, OpenInRoot(root, unsafePath) is equivalent to
34 //
35 // path, _ := securejoin.SecureJoin(root, unsafePath)
36 // handle, err := os.OpenFile(path, unix.O_PATH|unix.O_CLOEXEC)
37 //
38 // But is much safer. The above implementation is unsafe because if an attacker
39 // can modify the filesystem tree between [SecureJoin] and [os.OpenFile], it is
40 // possible for the returned file to be outside of the root.
41 //
42 // Note that the returned handle is an O_PATH handle, meaning that only a very
43 // limited set of operations will work on the handle. This is done to avoid
44 // accidentally opening an untrusted file that could cause issues (such as a
45 // disconnected TTY that could cause a DoS, or some other issue). In order to
46 // use the returned handle, you can "upgrade" it to a proper handle using
47 // [Reopen].
48 //
49 // [SecureJoin]: https://pkg.go.dev/github.com/cyphar/filepath-securejoin#SecureJoin
50 func OpenInRoot(root, unsafePath string) (*os.File, error) {
51 rootDir, err := os.OpenFile(root, unix.O_PATH|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
52 if err != nil {
53 return nil, err
54 }
55 defer rootDir.Close() //nolint:errcheck // close failures aren't critical here
56 return OpenatInRoot(rootDir, unsafePath)
57 }
58
59 // Reopen takes an *[os.File] handle and re-opens it through /proc/self/fd.
60 // Reopen(file, flags) is effectively equivalent to
61 //
62 // fdPath := fmt.Sprintf("/proc/self/fd/%d", file.Fd())
63 // os.OpenFile(fdPath, flags|unix.O_CLOEXEC)
64 //
65 // But with some extra hardenings to ensure that we are not tricked by a
66 // maliciously-configured /proc mount. While this attack scenario is not
67 // common, in container runtimes it is possible for higher-level runtimes to be
68 // tricked into configuring an unsafe /proc that can be used to attack file
69 // operations. See [CVE-2019-19921] for more details.
70 //
71 // [CVE-2019-19921]: https://github.com/advisories/GHSA-fh74-hm69-rqjw
72 func Reopen(handle *os.File, flags int) (*os.File, error) {
73 return procfs.ReopenFd(handle, flags)
74 }