Debian Codegraph @

contrib/bindings/go/internal/libpathrs/libpathrs_linux.go

raw symbols SCIP
1 //go:build linux
2
3 // SPDX-License-Identifier: MPL-2.0
4 /*
5 * libpathrs: safe path resolution on Linux
6 * Copyright (C) 2019-2025 SUSE LLC
7 * Copyright (C) 2026 Aleksa Sarai <[email protected]>
8 *
9 * This Source Code Form is subject to the terms of the Mozilla Public
10 * License, v. 2.0. If a copy of the MPL was not distributed with this
11 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
12 */
13
14 // Package libpathrs is an internal thin wrapper around the libpathrs C API.
15 package libpathrs
16
17 import (
18 "fmt"
19 "syscall"
20 "unsafe"
21 )
22
23 /*
24 // TODO: Figure out if we need to add support for linking against libpathrs
25 // statically even if in dynamically linked builds in order to make
26 // packaging a bit easier (using "-Wl,-Bstatic -lpathrs -Wl,-Bdynamic" or
27 // "-l:pathrs.a").
28 #cgo pkg-config: pathrs
29 #include <pathrs.h>
30
31 // This is a workaround for unsafe.Pointer() not working for non-void pointers.
32 char *cast_ptr(void *ptr) { return ptr; }
33 */
34 import "C"
35
36 func fetchError(errID C.int) error {
37 if errID >= C.__PATHRS_MAX_ERR_VALUE {
38 return nil
39 }
40 cErr := C.pathrs_errorinfo(errID)
41 defer C.pathrs_errorinfo_free(cErr)
42
43 var err error
44 if cErr != nil {
45 err = &Error{
46 errno: syscall.Errno(cErr.saved_errno),
47 description: C.GoString(cErr.description),
48 }
49 }
50 return err
51 }
52
53 // OpenRoot wraps pathrs_open_root.
54 func OpenRoot(path string) (uintptr, error) {
55 cPath := C.CString(path)
56 defer C.free(unsafe.Pointer(cPath))
57
58 fd := C.pathrs_open_root(cPath)
59 return uintptr(fd), fetchError(fd)
60 }
61
62 // Reopen wraps pathrs_reopen.
63 func Reopen(fd uintptr, flags uint64) (uintptr, error) {
64 newFd := C.pathrs_reopen(C.int(fd), C.uint64_t(flags))
65 return uintptr(newFd), fetchError(newFd)
66 }
67
68 // InRootResolve wraps pathrs_inroot_resolve.
69 func InRootResolve(rootFd uintptr, path string) (uintptr, error) {
70 cPath := C.CString(path)
71 defer C.free(unsafe.Pointer(cPath))
72
73 fd := C.pathrs_inroot_resolve(C.int(rootFd), cPath)
74 return uintptr(fd), fetchError(fd)
75 }
76
77 // InRootResolveNoFollow wraps pathrs_inroot_resolve_nofollow.
78 func InRootResolveNoFollow(rootFd uintptr, path string) (uintptr, error) {
79 cPath := C.CString(path)
80 defer C.free(unsafe.Pointer(cPath))
81
82 fd := C.pathrs_inroot_resolve_nofollow(C.int(rootFd), cPath)
83 return uintptr(fd), fetchError(fd)
84 }
85
86 // InRootOpen wraps pathrs_inroot_open.
87 func InRootOpen(rootFd uintptr, path string, flags uint64) (uintptr, error) {
88 cPath := C.CString(path)
89 defer C.free(unsafe.Pointer(cPath))
90
91 fd := C.pathrs_inroot_open(C.int(rootFd), cPath, C.uint64_t(flags))
92 return uintptr(fd), fetchError(fd)
93 }
94
95 // InRootReadlink wraps pathrs_inroot_readlink.
96 func InRootReadlink(rootFd uintptr, path string) (string, error) {
97 cPath := C.CString(path)
98 defer C.free(unsafe.Pointer(cPath))
99
100 size := 128
101 for {
102 linkBuf := make([]byte, size)
103 n := C.pathrs_inroot_readlink(C.int(rootFd), cPath, C.cast_ptr(unsafe.Pointer(&linkBuf[0])), C.size_t(len(linkBuf)))
104 switch {
105 case int(n) < C.__PATHRS_MAX_ERR_VALUE:
106 return "", fetchError(n)
107 case int(n) <= len(linkBuf):
108 return string(linkBuf[:int(n)]), nil
109 default:
110 // The contents were truncated. Unlike readlinkat, pathrs returns
111 // the size of the link when it checked. So use the returned size
112 // as a basis for the reallocated size (but in order to avoid a DoS
113 // where a magic-link is growing by a single byte each iteration,
114 // make sure we are a fair bit larger).
115 size += int(n)
116 }
117 }
118 }
119
120 // InRootRmdir wraps pathrs_inroot_rmdir.
121 func InRootRmdir(rootFd uintptr, path string) error {
122 cPath := C.CString(path)
123 defer C.free(unsafe.Pointer(cPath))
124
125 err := C.pathrs_inroot_rmdir(C.int(rootFd), cPath)
126 return fetchError(err)
127 }
128
129 // InRootUnlink wraps pathrs_inroot_unlink.
130 func InRootUnlink(rootFd uintptr, path string) error {
131 cPath := C.CString(path)
132 defer C.free(unsafe.Pointer(cPath))
133
134 err := C.pathrs_inroot_unlink(C.int(rootFd), cPath)
135 return fetchError(err)
136 }
137
138 // InRootRemoveAll wraps pathrs_inroot_remove_all.
139 func InRootRemoveAll(rootFd uintptr, path string) error {
140 cPath := C.CString(path)
141 defer C.free(unsafe.Pointer(cPath))
142
143 err := C.pathrs_inroot_remove_all(C.int(rootFd), cPath)
144 return fetchError(err)
145 }
146
147 // InRootCreat wraps pathrs_inroot_creat.
148 func InRootCreat(rootFd uintptr, path string, flags uint64, mode uint32) (uintptr, error) {
149 cPath := C.CString(path)
150 defer C.free(unsafe.Pointer(cPath))
151
152 fd := C.pathrs_inroot_creat(C.int(rootFd), cPath, C.uint64_t(flags), C.uint(mode))
153 return uintptr(fd), fetchError(fd)
154 }
155
156 // InRootRename wraps pathrs_inroot_rename.
157 func InRootRename(oldRootFd uintptr, oldPath string, newRootFd uintptr, newPath string, flags uint64) error {
158 cOldPath := C.CString(oldPath)
159 defer C.free(unsafe.Pointer(cOldPath))
160
161 cNewPath := C.CString(newPath)
162 defer C.free(unsafe.Pointer(cNewPath))
163
164 err := C.pathrs_inroot_rename(C.int(oldRootFd), cOldPath, C.int(newRootFd), cNewPath, C.uint64_t(flags))
165 return fetchError(err)
166 }
167
168 // InRootMkdir wraps pathrs_inroot_mkdir.
169 func InRootMkdir(rootFd uintptr, path string, mode uint32) error {
170 cPath := C.CString(path)
171 defer C.free(unsafe.Pointer(cPath))
172
173 err := C.pathrs_inroot_mkdir(C.int(rootFd), cPath, C.uint(mode))
174 return fetchError(err)
175 }
176
177 // InRootMkdirAll wraps pathrs_inroot_mkdir_all.
178 func InRootMkdirAll(rootFd uintptr, path string, mode uint32) (uintptr, error) {
179 cPath := C.CString(path)
180 defer C.free(unsafe.Pointer(cPath))
181
182 fd := C.pathrs_inroot_mkdir_all(C.int(rootFd), cPath, C.uint(mode))
183 return uintptr(fd), fetchError(fd)
184 }
185
186 // InRootMknod wraps pathrs_inroot_mknod.
187 func InRootMknod(rootFd uintptr, path string, mode uint32, dev uint64) error {
188 cPath := C.CString(path)
189 defer C.free(unsafe.Pointer(cPath))
190
191 err := C.pathrs_inroot_mknod(C.int(rootFd), cPath, C.uint(mode), C.dev_t(dev))
192 return fetchError(err)
193 }
194
195 // InRootSymlink wraps pathrs_inroot_symlink.
196 func InRootSymlink(target string, rootFd uintptr, linkpath string) error {
197 cLinkpath := C.CString(linkpath)
198 defer C.free(unsafe.Pointer(cLinkpath))
199
200 cTarget := C.CString(target)
201 defer C.free(unsafe.Pointer(cTarget))
202
203 err := C.pathrs_inroot_symlink(cTarget, C.int(rootFd), cLinkpath)
204 return fetchError(err)
205 }
206
207 // InRootHardlink wraps pathrs_inroot_hardlink.
208 func InRootHardlink(oldRootFd uintptr, oldPath string, newRootFd uintptr, newPath string, flags uint64) error {
209 cNewPath := C.CString(newPath)
210 defer C.free(unsafe.Pointer(cNewPath))
211
212 cOldPath := C.CString(oldPath)
213 defer C.free(unsafe.Pointer(cOldPath))
214
215 err := C.pathrs_inroot_hardlink(C.int(oldRootFd), cOldPath, C.int(newRootFd), cNewPath, C.uint64_t(flags))
216 return fetchError(err)
217 }
218
219 // ProcBase is pathrs_proc_base_t (uint64_t).
220 type ProcBase C.pathrs_proc_base_t
221
222 // FIXME: We need to open-code the constants because CGo unfortunately will
223 // implicitly convert any non-literal constants (i.e. those resolved using gcc)
224 // to signed integers. See <https://github.com/golang/go/issues/39136> for some
225 // more information on the underlying issue (though.
226 const (
227 // ProcRoot is PATHRS_PROC_ROOT.
228 ProcRoot ProcBase = 0xFFFF_FFFE_7072_6F63 // C.PATHRS_PROC_ROOT
229 // ProcSelf is PATHRS_PROC_SELF.
230 ProcSelf ProcBase = 0xFFFF_FFFE_091D_5E1F // C.PATHRS_PROC_SELF
231 // ProcThreadSelf is PATHRS_PROC_THREAD_SELF.
232 ProcThreadSelf ProcBase = 0xFFFF_FFFE_3EAD_5E1F // C.PATHRS_PROC_THREAD_SELF
233
234 // ProcBaseTypeMask is __PATHRS_PROC_TYPE_MASK.
235 ProcBaseTypeMask ProcBase = 0xFFFF_FFFF_0000_0000 // C.__PATHRS_PROC_TYPE_MASK
236 // ProcBaseTypePid is __PATHRS_PROC_TYPE_PID.
237 ProcBaseTypePid ProcBase = 0x8000_0000_0000_0000 // C.__PATHRS_PROC_TYPE_PID
238
239 // ProcDefaultRootFd is PATHRS_PROC_DEFAULT_ROOTFD.
240 ProcDefaultRootFd = -int(syscall.EBADF) // C.PATHRS_PROC_DEFAULT_ROOTFD
241 )
242
243 func assertEqual[T comparable](a, b T, msg string) {
244 if a != b {
245 panic(fmt.Sprintf("%s ((%T) %#v != (%T) %#v)", msg, a, a, b, b))
246 }
247 }
248
249 // Verify that the values above match the actual C values. Unfortunately, Go
250 // only allows us to forcefully cast int64 to uint64 if you use a temporary
251 // variable, which means we cannot do it in a const context and thus need to do
252 // it at runtime (even though it is a check that fundamentally could be done at
253 // compile-time)...
254 func init() {
255 var (
256 actualProcRoot int64 = C.PATHRS_PROC_ROOT
257 actualProcSelf int64 = C.PATHRS_PROC_SELF
258 actualProcThreadSelf int64 = C.PATHRS_PROC_THREAD_SELF
259 )
260
261 assertEqual(ProcRoot, ProcBase(actualProcRoot), "PATHRS_PROC_ROOT")
262 assertEqual(ProcSelf, ProcBase(actualProcSelf), "PATHRS_PROC_SELF")
263 assertEqual(ProcThreadSelf, ProcBase(actualProcThreadSelf), "PATHRS_PROC_THREAD_SELF")
264
265 var (
266 actualProcBaseTypeMask uint64 = C.__PATHRS_PROC_TYPE_MASK
267 actualProcBaseTypePid uint64 = C.__PATHRS_PROC_TYPE_PID
268 )
269
270 assertEqual(ProcBaseTypeMask, ProcBase(actualProcBaseTypeMask), "__PATHRS_PROC_TYPE_MASK")
271 assertEqual(ProcBaseTypePid, ProcBase(actualProcBaseTypePid), "__PATHRS_PROC_TYPE_PID")
272
273 assertEqual(ProcDefaultRootFd, int(C.PATHRS_PROC_DEFAULT_ROOTFD), "PATHRS_PROC_DEFAULT_ROOTFD")
274 }
275
276 // ProcPid reimplements the PROC_PID(x) conversion.
277 func ProcPid(pid uint32) ProcBase { return ProcBaseTypePid | ProcBase(pid) }
278
279 // ProcOpenat wraps pathrs_proc_openat.
280 func ProcOpenat(procRootFd int, base ProcBase, path string, flags uint64) (uintptr, error) {
281 cBase := C.pathrs_proc_base_t(base)
282
283 cPath := C.CString(path)
284 defer C.free(unsafe.Pointer(cPath))
285
286 fd := C.pathrs_proc_openat(C.int(procRootFd), cBase, cPath, C.uint64_t(flags))
287 return uintptr(fd), fetchError(fd)
288 }
289
290 // ProcReadlinkat wraps pathrs_proc_readlinkat.
291 func ProcReadlinkat(procRootFd int, base ProcBase, path string) (string, error) {
292 // TODO: See if we can unify this code with InRootReadlink.
293
294 cBase := C.pathrs_proc_base_t(base)
295
296 cPath := C.CString(path)
297 defer C.free(unsafe.Pointer(cPath))
298
299 size := 128
300 for {
301 linkBuf := make([]byte, size)
302 n := C.pathrs_proc_readlinkat(
303 C.int(procRootFd), cBase, cPath,
304 C.cast_ptr(unsafe.Pointer(&linkBuf[0])), C.size_t(len(linkBuf)))
305 switch {
306 case int(n) < C.__PATHRS_MAX_ERR_VALUE:
307 return "", fetchError(n)
308 case int(n) <= len(linkBuf):
309 return string(linkBuf[:int(n)]), nil
310 default:
311 // The contents were truncated. Unlike readlinkat, pathrs returns
312 // the size of the link when it checked. So use the returned size
313 // as a basis for the reallocated size (but in order to avoid a DoS
314 // where a magic-link is growing by a single byte each iteration,
315 // make sure we are a fair bit larger).
316 size += int(n)
317 }
318 }
319 }
320
321 // ProcfsOpenHow is pathrs_procfs_open_how (struct).
322 type ProcfsOpenHow C.pathrs_procfs_open_how
323
324 const (
325 // ProcfsNewUnmasked is PATHRS_PROCFS_NEW_UNMASKED.
326 ProcfsNewUnmasked = C.PATHRS_PROCFS_NEW_UNMASKED
327 )
328
329 // Flags returns a pointer to the internal flags field to allow other packages
330 // to modify structure fields that are internal due to Go's visibility model.
331 func (how *ProcfsOpenHow) Flags() *C.uint64_t { return &how.flags }
332
333 // ProcfsOpen is pathrs_procfs_open (sizeof(*how) is passed automatically).
334 func ProcfsOpen(how *ProcfsOpenHow) (uintptr, error) {
335 fd := C.pathrs_procfs_open((*C.pathrs_procfs_open_how)(how), C.size_t(unsafe.Sizeof(*how)))
336 return uintptr(fd), fetchError(fd)
337 }
338
339 // VersionInfo is a Go-friendly form of pathrs_version_info_t (struct).
340 type VersionInfo struct {
341 VersionString string
342 }
343
344 // versionInfo is pathrs_version_info_t (struct).
345 type versionInfo C.pathrs_version_info_t
346
347 // Version is pathrs_version_info_t (sizeof(version) is passed automatically).
348 func Version() (*VersionInfo, error) {
349 var rawVersion versionInfo
350 size := C.pathrs_version((*C.pathrs_version_info_t)(&rawVersion), C.size_t(unsafe.Sizeof(rawVersion)))
351 switch {
352 case size < 0:
353 return nil, fetchError(size)
354 case size > 0:
355 // TODO(log): Logging?
356 fallthrough
357 default:
358 // TODO(log): Add a log statement if sizeof(rawVersion) is bigger than
359 // the number of fields we store in VersionInfo. Otherwise a rebuild
360 // will mask that Go callers cannot see any new fields.
361 return &VersionInfo{
362 VersionString: C.GoString(rawVersion.version_string),
363 }, nil
364 }
365 }