mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Fix file mode check in fsgofer Attach
PiperOrigin-RevId: 263189654
This commit is contained in:
committed by
gVisor bot
parent
99bf75a6dc
commit
c386f046c1
@@ -125,7 +125,7 @@ func (a *attachPoint) Attach() (p9.File, error) {
|
||||
return nil, fmt.Errorf("stat file %q, err: %v", a.prefix, err)
|
||||
}
|
||||
mode := syscall.O_RDWR
|
||||
if a.conf.ROMount || stat.Mode&syscall.S_IFDIR != 0 {
|
||||
if a.conf.ROMount || (stat.Mode&syscall.S_IFMT) == syscall.S_IFDIR {
|
||||
mode = syscall.O_RDONLY
|
||||
}
|
||||
|
||||
@@ -141,9 +141,13 @@ func (a *attachPoint) Attach() (p9.File, error) {
|
||||
f.Close()
|
||||
return nil, fmt.Errorf("attach point already attached, prefix: %s", a.prefix)
|
||||
}
|
||||
a.attached = true
|
||||
|
||||
return newLocalFile(a, f, a.prefix, stat)
|
||||
rv, err := newLocalFile(a, f, a.prefix, stat)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.attached = true
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
// makeQID returns a unique QID for the given stat buffer.
|
||||
|
||||
@@ -17,8 +17,10 @@ package fsgofer
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
@@ -621,6 +623,46 @@ func TestAttachFile(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachInvalidType(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "attach-")
|
||||
if err != nil {
|
||||
t.Fatalf("ioutil.TempDir() failed, err: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
fifo := filepath.Join(dir, "fifo")
|
||||
if err := syscall.Mkfifo(fifo, 0755); err != nil {
|
||||
t.Fatalf("Mkfifo(%q): %v", fifo, err)
|
||||
}
|
||||
|
||||
socket := filepath.Join(dir, "socket")
|
||||
l, err := net.Listen("unix", socket)
|
||||
if err != nil {
|
||||
t.Fatalf("net.Listen(unix, %q): %v", socket, err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
path string
|
||||
}{
|
||||
{name: "fifo", path: fifo},
|
||||
{name: "socket", path: socket},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
conf := Config{ROMount: false}
|
||||
a, err := NewAttachPoint(tc.path, conf)
|
||||
if err != nil {
|
||||
t.Fatalf("NewAttachPoint failed: %v", err)
|
||||
}
|
||||
f, err := a.Attach()
|
||||
if f != nil || err == nil {
|
||||
t.Fatalf("Attach should have failed, got (%v, nil)", f)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoubleAttachError(t *testing.T) {
|
||||
conf := Config{ROMount: false}
|
||||
root, err := ioutil.TempDir("", "root-")
|
||||
|
||||
Reference in New Issue
Block a user