mirror of
https://github.com/hanzoai/visor.git
synced 2026-09-24 17:04:14 +00:00
Go 1.27 includes an enhancement to the printf vet check to flag cases that use a pointer-to-error with %w instead of the value implementing error. See https://go.dev/issue/61342 for details. The core premise is that if a type E implements error for E and *E (because the Error method has a value receiver), then flag uses of *E with %w because doing so will require errors.As users to use *E, which is usually unexpected given that E also implements error. This finding flagged fmt.Errorf calls using *user.ExecutableResolveError with %w. Both ExecutableResolveError and *ExecutableResolveError implement error because the type embeds error as a value, thus the Error method has a value receiver. There is not actually anything fundamentally wrong with the code here. ResolveExecutablePath returns *user.ExecutableResolveError. It is a bit strange to return the error as a pointer instead of a value, but it is fine. In turn, it makes sense for the caller to check for the pointer type, and then pass the pointer type on via fmt.Errorf. In fact, as a result of finding this case, we've filed https://go.dev/issue/80159 and will probably change the analyzer. Still, the format of this error (just a struct with an embedded error) and use as a pointer type are strange and a bit difficult to follow, so I think it is worthwhile to simplify things. The callers don't care about the embedded error (they ultimately just print the string), so switch to storing a raw string is more clear. We could (a) appese the printf analyzer and (b) force errors.As users to consistently use *ExecutableResolveError by implementing Error with a pointer receiver, as then only *ExecutableResolveError. But there isn't actually a compelling reason to return this error as a pointer in the first place, as it contains nothing more than a string header. It's simpler and easier to comprehend to change to returning the error by value. PiperOrigin-RevId: 938746701
146 lines
4.1 KiB
Go
146 lines
4.1 KiB
Go
// Copyright 2020 The gVisor Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package user
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"strings"
|
|
|
|
"gvisor.dev/gvisor/pkg/abi/linux"
|
|
"gvisor.dev/gvisor/pkg/context"
|
|
"gvisor.dev/gvisor/pkg/errors/linuxerr"
|
|
"gvisor.dev/gvisor/pkg/fspath"
|
|
"gvisor.dev/gvisor/pkg/log"
|
|
"gvisor.dev/gvisor/pkg/sentry/kernel"
|
|
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
|
|
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
|
)
|
|
|
|
// ExecutableResolveError represents a failure to resolve the executable
|
|
// in ResolveExecutablePath.
|
|
type ExecutableResolveError struct {
|
|
msg string
|
|
}
|
|
|
|
// Error implements the error interface.
|
|
func (e ExecutableResolveError) Error() string {
|
|
return e.msg
|
|
}
|
|
|
|
// ResolveExecutablePath resolves the given executable name given the working
|
|
// dir and environment.
|
|
// Returns *ExecutableResolveError when the executable cannot be resolved.
|
|
func ResolveExecutablePath(ctx context.Context, args *kernel.CreateProcessArgs) (string, error) {
|
|
name := args.Filename
|
|
if len(name) == 0 {
|
|
if len(args.Argv) == 0 {
|
|
return "", fmt.Errorf("no filename or command provided")
|
|
}
|
|
name = args.Argv[0]
|
|
}
|
|
|
|
// Absolute paths can be used directly.
|
|
if path.IsAbs(name) {
|
|
return name, nil
|
|
}
|
|
|
|
// Paths with '/' in them should be joined to the working directory, or
|
|
// to the root if working directory is not set.
|
|
if strings.IndexByte(name, '/') > 0 {
|
|
wd := args.WorkingDirectory
|
|
if wd == "" {
|
|
wd = "/"
|
|
}
|
|
if !path.IsAbs(wd) {
|
|
return "", fmt.Errorf("working directory %q must be absolute", wd)
|
|
}
|
|
return path.Join(wd, name), nil
|
|
}
|
|
|
|
// Otherwise, We must lookup the name in the paths.
|
|
paths := getPath(args.Envv)
|
|
f, err := resolve(ctx, args.Credentials, args.MountNamespace, paths, name)
|
|
if err != nil {
|
|
return "", ExecutableResolveError{fmt.Sprintf("error finding executable %q in PATH %v: %v", name, paths, err)}
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
// resolve searches for an executable in the given PATH directories.
|
|
// Error handling follows glibc execvpe() (posix/execvpe.c).
|
|
func resolve(ctx context.Context, creds *auth.Credentials, mns *vfs.MountNamespace, paths []string, name string) (string, error) {
|
|
root := mns.Root(ctx)
|
|
defer root.DecRef(ctx)
|
|
gotEACCES := false
|
|
lastErr := error(linuxerr.ENOENT)
|
|
for _, p := range paths {
|
|
if !path.IsAbs(p) {
|
|
// Relative paths aren't safe, no one should be using them.
|
|
log.Warningf("Skipping relative path %q in $PATH", p)
|
|
continue
|
|
}
|
|
|
|
binPath := path.Join(p, name)
|
|
pop := &vfs.PathOperation{
|
|
Root: root,
|
|
Start: root,
|
|
Path: fspath.Parse(binPath),
|
|
FollowFinalSymlink: true,
|
|
}
|
|
opts := &vfs.OpenOptions{
|
|
FileExec: true,
|
|
Flags: linux.O_RDONLY,
|
|
}
|
|
dentry, err := root.Mount().Filesystem().VirtualFilesystem().OpenAt(ctx, creds, pop, opts)
|
|
if err == nil {
|
|
dentry.DecRef(ctx)
|
|
return binPath, nil
|
|
}
|
|
|
|
// Preserve the last error, matching glibc's errno semantics.
|
|
lastErr = err
|
|
switch {
|
|
case linuxerr.Equals(linuxerr.EACCES, err):
|
|
gotEACCES = true
|
|
case linuxerr.Equals(linuxerr.ENOENT, err),
|
|
linuxerr.Equals(linuxerr.ESTALE, err),
|
|
linuxerr.Equals(linuxerr.ENOTDIR, err),
|
|
linuxerr.Equals(linuxerr.ENODEV, err),
|
|
linuxerr.Equals(linuxerr.ETIMEDOUT, err):
|
|
// Skip to next PATH entry.
|
|
default:
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
if gotEACCES {
|
|
return "", linuxerr.EACCES
|
|
}
|
|
return "", lastErr
|
|
}
|
|
|
|
// getPath returns the PATH as a slice of strings given the environment
|
|
// variables.
|
|
func getPath(env []string) []string {
|
|
const prefix = "PATH="
|
|
for _, e := range env {
|
|
if strings.HasPrefix(e, prefix) {
|
|
return strings.Split(strings.TrimPrefix(e, prefix), ":")
|
|
}
|
|
}
|
|
return nil
|
|
}
|