| 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 procfs provides a safe API for operating on /proc on Linux. |
| 13 |
package procfs |
| 14 |
|
| 15 |
import ( |
| 16 |
"os" |
| 17 |
|
| 18 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/procfs" |
| 19 |
) |
| 20 |
|
| 21 |
// This package mostly just wraps internal/procfs APIs. This is necessary |
| 22 |
// because we are forced to export some things from internal/procfs in order to |
| 23 |
// avoid some dependency cycle issues, but we don't want users to see or use |
| 24 |
// them. |
| 25 |
|
| 26 |
// ProcThreadSelfCloser is a callback that needs to be called when you are done |
| 27 |
// operating on an [os.File] fetched using [Handle.OpenThreadSelf]. |
| 28 |
// |
| 29 |
// [os.File]: https://pkg.go.dev/os#File |
| 30 |
type ProcThreadSelfCloser = procfs.ProcThreadSelfCloser |
| 31 |
|
| 32 |
// Handle is a wrapper around an *os.File handle to "/proc", which can be used |
| 33 |
// to do further procfs-related operations in a safe way. |
| 34 |
type Handle struct { |
| 35 |
inner *procfs.Handle |
| 36 |
} |
| 37 |
|
| 38 |
// Close close the resources associated with this [Handle]. Note that if this |
| 39 |
// [Handle] was created with [OpenProcRoot], on some kernels the underlying |
| 40 |
// procfs handle is cached and so this Close operation may be a no-op. However, |
| 41 |
// you should always call Close on [Handle]s once you are done with them. |
| 42 |
func (proc *Handle) Close() error { return proc.inner.Close() } |
| 43 |
|
| 44 |
// OpenProcRoot tries to open a "safer" handle to "/proc" (i.e., one with the |
| 45 |
// "subset=pid" mount option applied, available from Linux 5.8). Unless you |
| 46 |
// plan to do many [Handle.OpenRoot] operations, users should prefer to use |
| 47 |
// this over [OpenUnsafeProcRoot] which is far more dangerous to keep open. |
| 48 |
// |
| 49 |
// If a safe handle cannot be opened, OpenProcRoot will fall back to opening a |
| 50 |
// regular "/proc" handle. |
| 51 |
// |
| 52 |
// Note that using [Handle.OpenRoot] will still work with handles returned by |
| 53 |
// this function. If a subpath cannot be operated on with a safe "/proc" |
| 54 |
// handle, then [OpenUnsafeProcRoot] will be called internally and a temporary |
| 55 |
// unsafe handle will be used. |
| 56 |
func OpenProcRoot() (*Handle, error) { |
| 57 |
proc, err := procfs.OpenProcRoot() |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
return &Handle{inner: proc}, nil |
| 62 |
} |
| 63 |
|
| 64 |
// OpenUnsafeProcRoot opens a handle to "/proc" without any overmounts or |
| 65 |
// masked paths. You must be extremely careful to make sure this handle is |
| 66 |
// never leaked to a container and that you program cannot be tricked into |
| 67 |
// writing to arbitrary paths within it. |
| 68 |
// |
| 69 |
// This is not necessary if you just wish to use [Handle.OpenRoot], as handles |
| 70 |
// returned by [OpenProcRoot] will fall back to using a *temporary* unsafe |
| 71 |
// handle in that case. You should only really use this if you need to do many |
| 72 |
// operations with [Handle.OpenRoot] and the performance overhead of making |
| 73 |
// many procfs handles is an issue. If you do use OpenUnsafeProcRoot, you |
| 74 |
// should make sure to close the handle as soon as possible to avoid |
| 75 |
// known-fd-number attacks. |
| 76 |
func OpenUnsafeProcRoot() (*Handle, error) { |
| 77 |
proc, err := procfs.OpenUnsafeProcRoot() |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
return &Handle{inner: proc}, nil |
| 82 |
} |
| 83 |
|
| 84 |
// OpenThreadSelf returns a handle to "/proc/thread-self/<subpath>" (or an |
| 85 |
// equivalent handle on older kernels where "/proc/thread-self" doesn't exist). |
| 86 |
// Once finished with the handle, you must call the returned closer function |
| 87 |
// ([runtime.UnlockOSThread]). You must not pass the returned *os.File to other |
| 88 |
// Go threads or use the handle after calling the closer. |
| 89 |
// |
| 90 |
// [runtime.UnlockOSThread]: https://pkg.go.dev/runtime#UnlockOSThread |
| 91 |
func (proc *Handle) OpenThreadSelf(subpath string) (*os.File, ProcThreadSelfCloser, error) { |
| 92 |
return proc.inner.OpenThreadSelf(subpath) |
| 93 |
} |
| 94 |
|
| 95 |
// OpenSelf returns a handle to /proc/self/<subpath>. |
| 96 |
// |
| 97 |
// Note that in Go programs with non-homogenous threads, this may result in |
| 98 |
// spurious errors. If you are monkeying around with APIs that are |
| 99 |
// thread-specific, you probably want to use [Handle.OpenThreadSelf] instead |
| 100 |
// which will guarantee that the handle refers to the same thread as the caller |
| 101 |
// is executing on. |
| 102 |
func (proc *Handle) OpenSelf(subpath string) (*os.File, error) { |
| 103 |
return proc.inner.OpenSelf(subpath) |
| 104 |
} |
| 105 |
|
| 106 |
// OpenRoot returns a handle to /proc/<subpath>. |
| 107 |
// |
| 108 |
// You should only use this when you need to operate on global procfs files |
| 109 |
// (such as sysctls in /proc/sys). Unlike [Handle.OpenThreadSelf], |
| 110 |
// [Handle.OpenSelf], and [Handle.OpenPid], the procfs handle used internally |
| 111 |
// for this operation will never use "subset=pid", which makes it a more juicy |
| 112 |
// target for [CVE-2024-21626]-style attacks (and doing something like opening |
| 113 |
// a directory with OpenRoot effectively leaks [OpenUnsafeProcRoot] as long as |
| 114 |
// the file descriptor is open). |
| 115 |
// |
| 116 |
// [CVE-2024-21626]: https://github.com/opencontainers/runc/security/advisories/GHSA-xr7r-f8xq-vfvv |
| 117 |
func (proc *Handle) OpenRoot(subpath string) (*os.File, error) { |
| 118 |
return proc.inner.OpenRoot(subpath) |
| 119 |
} |
| 120 |
|
| 121 |
// OpenPid returns a handle to /proc/$pid/<subpath> (pid can be a pid or tid). |
| 122 |
// This is mainly intended for usage when operating on other processes. |
| 123 |
// |
| 124 |
// You should not use this for the current thread, as special handling is |
| 125 |
// needed for /proc/thread-self (or /proc/self/task/<tid>) when dealing with |
| 126 |
// goroutine scheduling -- use [Handle.OpenThreadSelf] instead. |
| 127 |
// |
| 128 |
// To refer to the current thread-group, you should use prefer |
| 129 |
// [Handle.OpenSelf] to passing os.Getpid as the pid argument. |
| 130 |
func (proc *Handle) OpenPid(pid int, subpath string) (*os.File, error) { |
| 131 |
return proc.inner.OpenPid(pid, subpath) |
| 132 |
} |
| 133 |
|
| 134 |
// ProcSelfFdReadlink gets the real path of the given file by looking at |
| 135 |
// /proc/self/fd/<fd> with [readlink]. It is effectively just shorthand for |
| 136 |
// something along the lines of: |
| 137 |
// |
| 138 |
// proc, err := procfs.OpenProcRoot() |
| 139 |
// if err != nil { |
| 140 |
// return err |
| 141 |
// } |
| 142 |
// link, err := proc.OpenThreadSelf(fmt.Sprintf("fd/%d", f.Fd())) |
| 143 |
// if err != nil { |
| 144 |
// return err |
| 145 |
// } |
| 146 |
// defer link.Close() |
| 147 |
// var buf [4096]byte |
| 148 |
// n, err := unix.Readlinkat(int(link.Fd()), "", buf[:]) |
| 149 |
// if err != nil { |
| 150 |
// return err |
| 151 |
// } |
| 152 |
// pathname := buf[:n] |
| 153 |
// |
| 154 |
// [readlink]: https://pkg.go.dev/golang.org/x/sys/unix#Readlinkat |
| 155 |
func ProcSelfFdReadlink(f *os.File) (string, error) { |
| 156 |
return procfs.ProcSelfFdReadlink(f) |
| 157 |
} |