Skip to content

Commit 3971ecb

Browse files
ayushr2gvisor-bot
authored andcommitted
Remove linuxerr.IsValid and use syserr.IsValid instead.
linuxerr.IsValid just checks if the errno is less than the max possible errno value. syserr.IsValid additionally checks if the errno can be translated by the syserr package. Furthermore, drop the usage of linuxerr.TranslateError(), which only checks against a map with 3 entries of sentry-internal errors. Earlier, connError() would always fail at this step and end up returning linuxerr.EINVAL even if the errno being returned is valid. Reported-by: [email protected] PiperOrigin-RevId: 680021716
1 parent 0760a3d commit 3971ecb

File tree

8 files changed

+39
-27
lines changed

8 files changed

+39
-27
lines changed

pkg/errors/linuxerr/internal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func AddErrorUnwrapper(unwrap func(e error) (*errors.Error, bool)) {
5151
errorUnwrappers = append(errorUnwrappers, unwrap)
5252
}
5353

54-
// TranslateError translates errors to errnos, it will return false if
55-
// the error was not registered.
54+
// TranslateError translates errors to errnos for registered internal errors.
55+
// It will return false if the error was not registered.
5656
func TranslateError(from error) (*errors.Error, bool) {
5757
if err, ok := errorMap[from]; ok {
5858
return err, true

pkg/errors/linuxerr/linuxerr.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,3 @@ func Equals(e *errors.Error, err error) bool {
366366
}
367367
return e == err || unixErr == err
368368
}
369-
370-
// IsValid returns whether err is a valid error number.
371-
func IsValid(errno unix.Errno) bool {
372-
return errno < unix.Errno(maxErrno)
373-
}

pkg/sentry/fsimpl/fuse/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ go_library(
8484
"//pkg/sentry/memmap",
8585
"//pkg/sentry/vfs",
8686
"//pkg/sync",
87+
"//pkg/syserr",
8788
"//pkg/usermem",
8889
"//pkg/waiter",
8990
"@org_golang_x_sys//unix:go_default_library",

pkg/sentry/fsimpl/fuse/connection.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import (
1818
goContext "context"
1919
"sync"
2020

21+
"golang.org/x/sys/unix"
2122
"gvisor.dev/gvisor/pkg/abi/linux"
2223
"gvisor.dev/gvisor/pkg/atomicbitops"
2324
"gvisor.dev/gvisor/pkg/context"
2425
"gvisor.dev/gvisor/pkg/errors/linuxerr"
2526
"gvisor.dev/gvisor/pkg/log"
27+
"gvisor.dev/gvisor/pkg/syserr"
2628
"gvisor.dev/gvisor/pkg/waiter"
2729
)
2830

@@ -186,16 +188,17 @@ type connection struct {
186188
}
187189

188190
func connError(err error) error {
189-
errno, ok := linuxerr.TranslateError(err)
190-
if !ok {
191-
log.Warningf("fuse: failed with invalid error: %v", err)
192-
return linuxerr.EINVAL
193-
}
194-
if !linuxerr.IsValid(linuxerr.ToUnix(errno)) {
195-
log.Warningf("fuse: failed with invalid error: %v", err)
196-
return linuxerr.EINVAL
191+
// The error may contain arbitrary errno values that can't be converted.
192+
switch e := err.(type) {
193+
case unix.Errno:
194+
if syserr.IsValid(e) {
195+
return err
196+
}
197+
default:
198+
return err
197199
}
198-
return err
200+
log.Warningf("fusefs: failed with invalid error: %v", err)
201+
return unix.EINVAL
199202
}
200203

201204
func (conn *connection) saveInitializedChan() bool {
@@ -294,8 +297,7 @@ func (conn *connection) Call(ctx context.Context, r *Request) (*Response, error)
294297
return nil, connError(err)
295298
}
296299

297-
var res *Response
298-
res, err = fut.resolve(ctx)
300+
res, err := fut.resolve(ctx)
299301
if err != nil {
300302
return res, connError(err)
301303
}

pkg/sentry/fsimpl/fuse/request_response.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"gvisor.dev/gvisor/pkg/log"
2424
"gvisor.dev/gvisor/pkg/marshal"
2525
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
26+
"gvisor.dev/gvisor/pkg/syserr"
2627
)
2728

2829
// fuseInitRes is a variable-length wrapper of linux.FUSEInitOut. The FUSE
@@ -197,7 +198,7 @@ func (r *Response) Error() error {
197198

198199
// If we get a bad error in the response, warn and convert it to EINVAL.
199200
sysErrNo := unix.Errno(-errno)
200-
if !linuxerr.IsValid(sysErrNo) {
201+
if !syserr.IsValid(sysErrNo) {
201202
log.Warningf("fusefs: invalid response error %d does not correspond to a Linux error", sysErrNo)
202203
sysErrNo = unix.Errno(unix.EINVAL)
203204
}

pkg/syserr/host_darwin.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ const maxErrno = 107
2727

2828
var darwinHostTranslations [maxErrno]*Error
2929

30-
// FromHost translates a unix.Errno to a corresponding Error value.
31-
func FromHost(err unix.Errno) *Error {
32-
if int(err) >= len(darwinHostTranslations) || darwinHostTranslations[err] == nil {
33-
panic(fmt.Sprintf("unknown host errno %q (%d)", err.Error(), err))
30+
func getHostTranslation(err unix.Errno) *Error {
31+
if int(err) >= len(darwinHostTranslations) {
32+
return nil
3433
}
3534
return darwinHostTranslations[err]
3635
}

pkg/syserr/host_linux.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ const maxErrno = 134
2828

2929
var linuxHostTranslations [maxErrno]*Error
3030

31-
// FromHost translates a unix.Errno to a corresponding Error value.
32-
func FromHost(err unix.Errno) *Error {
33-
if int(err) >= len(linuxHostTranslations) || linuxHostTranslations[err] == nil {
34-
panic(fmt.Sprintf("unknown host errno %q (%d)", err.Error(), err))
31+
func getHostTranslation(err unix.Errno) *Error {
32+
if int(err) >= len(linuxHostTranslations) {
33+
return nil
3534
}
3635
return linuxHostTranslations[err]
3736
}

pkg/syserr/syserr.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,21 @@ var (
224224
ErrWouldBlock = New("operation would block", errno.EWOULDBLOCK)
225225
)
226226

227+
// FromHost translates a unix.Errno to a corresponding Error value.
228+
func FromHost(err unix.Errno) *Error {
229+
got := getHostTranslation(err)
230+
if got == nil {
231+
panic(fmt.Sprintf("unknown host errno %q (%d)", err.Error(), err))
232+
}
233+
return got
234+
}
235+
236+
// IsValid checks if the given errno is a valid errno which can be translated
237+
// to an Error.
238+
func IsValid(err unix.Errno) bool {
239+
return getHostTranslation(err) != nil
240+
}
241+
227242
// FromError converts a generic error to an *Error.
228243
//
229244
// TODO(b/34162363): Remove this function.

0 commit comments

Comments
 (0)