| 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 |
"errors" |
| 16 |
"fmt" |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"golang.org/x/sys/unix" |
| 22 |
|
| 23 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/fd" |
| 24 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/procfs" |
| 25 |
) |
| 26 |
|
| 27 |
func openat2(dir fd.Fd, path string, how *unix.OpenHow) (*os.File, error) { |
| 28 |
file, err := fd.Openat2(dir, path, how) |
| 29 |
if err != nil { |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
// If we are using RESOLVE_IN_ROOT, the name we generated may be wrong. |
| 33 |
if how.Resolve&unix.RESOLVE_IN_ROOT == unix.RESOLVE_IN_ROOT { |
| 34 |
if actualPath, err := procfs.ProcSelfFdReadlink(file); err == nil { |
| 35 |
// TODO: Ideally we would not need to dup the fd, but you cannot |
| 36 |
// easily just swap an *os.File with one from the same fd |
| 37 |
// (the GC will close the old one, and you cannot clear the |
| 38 |
// finaliser easily because it is associated with an internal |
| 39 |
// field of *os.File not *os.File itself). |
| 40 |
newFile, err := fd.DupWithName(file, actualPath) |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
_ = file.Close() |
| 45 |
file = newFile |
| 46 |
} |
| 47 |
} |
| 48 |
return file, nil |
| 49 |
} |
| 50 |
|
| 51 |
func lookupOpenat2(root fd.Fd, unsafePath string, partial bool) (*os.File, string, error) { |
| 52 |
if !partial { |
| 53 |
file, err := openat2(root, unsafePath, &unix.OpenHow{ |
| 54 |
Flags: unix.O_PATH | unix.O_CLOEXEC, |
| 55 |
Resolve: unix.RESOLVE_IN_ROOT | unix.RESOLVE_NO_MAGICLINKS, |
| 56 |
}) |
| 57 |
return file, "", err |
| 58 |
} |
| 59 |
return partialLookupOpenat2(root, unsafePath) |
| 60 |
} |
| 61 |
|
| 62 |
// partialLookupOpenat2 is an alternative implementation of |
| 63 |
// partialLookupInRoot, using openat2(RESOLVE_IN_ROOT) to more safely get a |
| 64 |
// handle to the deepest existing child of the requested path within the root. |
| 65 |
func partialLookupOpenat2(root fd.Fd, unsafePath string) (*os.File, string, error) { |
| 66 |
// TODO: Implement this as a git-bisect-like binary search. |
| 67 |
|
| 68 |
unsafePath = filepath.ToSlash(unsafePath) // noop |
| 69 |
endIdx := len(unsafePath) |
| 70 |
var lastError error |
| 71 |
for endIdx > 0 { |
| 72 |
subpath := unsafePath[:endIdx] |
| 73 |
|
| 74 |
handle, err := openat2(root, subpath, &unix.OpenHow{ |
| 75 |
Flags: unix.O_PATH | unix.O_CLOEXEC, |
| 76 |
Resolve: unix.RESOLVE_IN_ROOT | unix.RESOLVE_NO_MAGICLINKS, |
| 77 |
}) |
| 78 |
if err == nil { |
| 79 |
// Jump over the slash if we have a non-"" remainingPath. |
| 80 |
if endIdx < len(unsafePath) { |
| 81 |
endIdx++ |
| 82 |
} |
| 83 |
// We found a subpath! |
| 84 |
return handle, unsafePath[endIdx:], lastError |
| 85 |
} |
| 86 |
if errors.Is(err, unix.ENOENT) || errors.Is(err, unix.ENOTDIR) { |
| 87 |
// That path doesn't exist, let's try the next directory up. |
| 88 |
endIdx = strings.LastIndexByte(subpath, '/') |
| 89 |
lastError = err |
| 90 |
continue |
| 91 |
} |
| 92 |
return nil, "", fmt.Errorf("open subpath: %w", err) |
| 93 |
} |
| 94 |
// If we couldn't open anything, the whole subpath is missing. Return a |
| 95 |
// copy of the root fd so that the caller doesn't close this one by |
| 96 |
// accident. |
| 97 |
rootClone, err := fd.Dup(root) |
| 98 |
if err != nil { |
| 99 |
return nil, "", err |
| 100 |
} |
| 101 |
return rootClone, unsafePath, lastError |
| 102 |
} |