Debian Codegraph @

pathrs-lite/internal/fd/at_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 fd
13
14 import (
15 "fmt"
16 "os"
17 "path/filepath"
18 "runtime"
19
20 "golang.org/x/sys/unix"
21
22 "github.com/cyphar/filepath-securejoin/pathrs-lite/internal/gocompat"
23 )
24
25 // prepareAtWith returns -EBADF (an invalid fd) if dir is nil, otherwise using
26 // the dir.Fd(). We use -EBADF because in filepath-securejoin we generally
27 // don't want to allow relative-to-cwd paths. The returned path is an
28 // *informational* string that describes a reasonable pathname for the given
29 // *at(2) arguments. You must not use the full path for any actual filesystem
30 // operations.
31 func prepareAt(dir Fd, path string) (dirFd int, unsafeUnmaskedPath string) {
32 dirFd, dirPath := -int(unix.EBADF), "."
33 if dir != nil {
34 dirFd, dirPath = int(dir.Fd()), dir.Name()
35 }
36 if !filepath.IsAbs(path) {
37 // only prepend the dirfd path for relative paths
38 path = dirPath + "/" + path
39 }
40 // NOTE: If path is "." or "", the returned path won't be filepath.Clean,
41 // but that's okay since this path is either used for errors (in which case
42 // a trailing "/" or "/." is important information) or will be
43 // filepath.Clean'd later (in the case of fd.Openat).
44 return dirFd, path
45 }
46
47 // Openat is an [Fd]-based wrapper around unix.Openat.
48 func Openat(dir Fd, path string, flags int, mode int) (*os.File, error) { //nolint:unparam // wrapper func
49 dirFd, fullPath := prepareAt(dir, path)
50 // Make sure we always set O_CLOEXEC.
51 flags |= unix.O_CLOEXEC
52 fd, err := unix.Openat(dirFd, path, flags, uint32(mode))
53 if err != nil {
54 return nil, &os.PathError{Op: "openat", Path: fullPath, Err: err}
55 }
56 runtime.KeepAlive(dir)
57 // openat is only used with lexically-safe paths so we can use
58 // filepath.Clean here, and also the path itself is not going to be used
59 // for actual path operations.
60 fullPath = filepath.Clean(fullPath)
61 return os.NewFile(uintptr(fd), fullPath), nil
62 }
63
64 // Fstatat is an [Fd]-based wrapper around unix.Fstatat.
65 func Fstatat(dir Fd, path string, flags int) (unix.Stat_t, error) {
66 dirFd, fullPath := prepareAt(dir, path)
67 var stat unix.Stat_t
68 if err := unix.Fstatat(dirFd, path, &stat, flags); err != nil {
69 return stat, &os.PathError{Op: "fstatat", Path: fullPath, Err: err}
70 }
71 runtime.KeepAlive(dir)
72 return stat, nil
73 }
74
75 // Faccessat is an [Fd]-based wrapper around unix.Faccessat.
76 func Faccessat(dir Fd, path string, mode uint32, flags int) error {
77 dirFd, fullPath := prepareAt(dir, path)
78 err := unix.Faccessat(dirFd, path, mode, flags)
79 if err != nil {
80 err = &os.PathError{Op: "faccessat", Path: fullPath, Err: err}
81 }
82 runtime.KeepAlive(dir)
83 return err
84 }
85
86 // Readlinkat is an [Fd]-based wrapper around unix.Readlinkat.
87 func Readlinkat(dir Fd, path string) (string, error) {
88 dirFd, fullPath := prepareAt(dir, path)
89 size := 4096
90 for {
91 linkBuf := make([]byte, size)
92 n, err := unix.Readlinkat(dirFd, path, linkBuf)
93 if err != nil {
94 return "", &os.PathError{Op: "readlinkat", Path: fullPath, Err: err}
95 }
96 runtime.KeepAlive(dir)
97 if n != size {
98 return string(linkBuf[:n]), nil
99 }
100 // Possible truncation, resize the buffer.
101 size *= 2
102 }
103 }
104
105 const (
106 // STATX_MNT_ID_UNIQUE is provided in golang.org/x/[email protected], but in order to
107 // avoid bumping the requirement for a single constant we can just define it
108 // ourselves.
109 _STATX_MNT_ID_UNIQUE = 0x4000 //nolint:revive // unix.* name
110
111 // We don't care which mount ID we get. The kernel will give us the unique
112 // one if it is supported. If the kernel doesn't support
113 // STATX_MNT_ID_UNIQUE, the bit is ignored and the returned request mask
114 // will only contain STATX_MNT_ID (if supported).
115 wantStatxMntMask = _STATX_MNT_ID_UNIQUE | unix.STATX_MNT_ID
116 )
117
118 var hasStatxMountID = gocompat.SyncOnceValue(func() bool {
119 var stx unix.Statx_t
120 err := unix.Statx(-int(unix.EBADF), "/", 0, wantStatxMntMask, &stx)
121 return err == nil && stx.Mask&wantStatxMntMask != 0
122 })
123
124 // GetMountID gets the mount identifier associated with the fd and path
125 // combination. It is effectively a wrapper around fetching
126 // STATX_MNT_ID{,_UNIQUE} with unix.Statx, but with a fallback to 0 if the
127 // kernel doesn't support the feature.
128 func GetMountID(dir Fd, path string) (uint64, error) {
129 // If we don't have statx(STATX_MNT_ID*) support, we can't do anything.
130 if !hasStatxMountID() {
131 return 0, nil
132 }
133
134 dirFd, fullPath := prepareAt(dir, path)
135
136 var stx unix.Statx_t
137 err := unix.Statx(dirFd, path, unix.AT_EMPTY_PATH|unix.AT_SYMLINK_NOFOLLOW, wantStatxMntMask, &stx)
138 if stx.Mask&wantStatxMntMask == 0 {
139 // It's not a kernel limitation, for some reason we couldn't get a
140 // mount ID. Assume it's some kind of attack.
141 err = fmt.Errorf("could not get mount id: %w", err)
142 }
143 if err != nil {
144 return 0, &os.PathError{Op: "statx(STATX_MNT_ID_...)", Path: fullPath, Err: err}
145 }
146 runtime.KeepAlive(dir)
147 return stx.Mnt_id, nil
148 }