|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +/* |
| 3 | + * Copyright (C) 2024-2025 Aleksa Sarai <[email protected]> |
| 4 | + * Copyright (C) 2024-2025 SUSE LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package pathrs |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + |
| 26 | + "github.com/cyphar/filepath-securejoin/pathrs-lite" |
| 27 | + "golang.org/x/sys/unix" |
| 28 | + |
| 29 | + "github.com/opencontainers/runc/internal/linux" |
| 30 | +) |
| 31 | + |
| 32 | +// OpenInRoot opens the given path inside the root with the provided flags. It |
| 33 | +// is effectively shorthand for [securejoin.OpenInRoot] followed by |
| 34 | +// [securejoin.Reopen]. |
| 35 | +func OpenInRoot(root, subpath string, flags int) (*os.File, error) { |
| 36 | + handle, err := pathrs.OpenInRoot(root, subpath) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + defer handle.Close() |
| 41 | + return pathrs.Reopen(handle, flags) |
| 42 | +} |
| 43 | + |
| 44 | +// CreateInRoot creates a new file inside a root (as well as any missing parent |
| 45 | +// directories) and returns a handle to said file. This effectively has |
| 46 | +// open(O_CREAT|O_NOFOLLOW) semantics. If you want the creation to use O_EXCL, |
| 47 | +// include it in the passed flags. The fileMode argument uses unix.* mode bits, |
| 48 | +// *not* os.FileMode. |
| 49 | +func CreateInRoot(root, subpath string, flags int, fileMode uint32) (*os.File, error) { |
| 50 | + dir, filename := filepath.Split(subpath) |
| 51 | + if filepath.Join("/", filename) == "/" { |
| 52 | + return nil, fmt.Errorf("create in root subpath %q has bad trailing component %q", subpath, filename) |
| 53 | + } |
| 54 | + |
| 55 | + dirFd, err := MkdirAllInRootOpen(root, dir, 0o755) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + defer dirFd.Close() |
| 60 | + |
| 61 | + // We know that the filename does not have any "/" components, and that |
| 62 | + // dirFd is inside the root. O_NOFOLLOW will stop us from following |
| 63 | + // trailing symlinks, so this is safe to do. libpathrs's Root::create_file |
| 64 | + // works the same way. |
| 65 | + flags |= unix.O_CREAT | unix.O_NOFOLLOW |
| 66 | + fd, err := linux.Openat(int(dirFd.Fd()), filename, flags, fileMode) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + return os.NewFile(uintptr(fd), root+"/"+subpath), nil |
| 71 | +} |
0 commit comments