Protect regular files from FIFO setup
This commit is contained in:
committed by
Brandon McGinty
parent
dcffa1efa1
commit
43d7addc15
@@ -87,7 +87,16 @@ func setup_fifo(fn string) (chan string, error) {
|
||||
if fn == "" {
|
||||
return t, nil
|
||||
}
|
||||
os.Remove(fn)
|
||||
if info, err := os.Lstat(fn); err == nil {
|
||||
if info.Mode()&os.ModeNamedPipe == 0 {
|
||||
return t, fmt.Errorf("FIFO path %q already exists and is not a FIFO", fn)
|
||||
}
|
||||
if err := os.Remove(fn); err != nil {
|
||||
return t, err
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return t, err
|
||||
}
|
||||
err := syscall.Mkfifo(fn, 0600)
|
||||
if err != nil {
|
||||
return t, err
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetupFIFORefusesToReplaceRegularFile(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "not-a-fifo")
|
||||
if err := os.WriteFile(path, []byte("keep"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := setup_fifo(path); err == nil {
|
||||
t.Fatal("setup_fifo replaced a regular file")
|
||||
}
|
||||
contents, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(contents) != "keep" {
|
||||
t.Fatalf("regular file was modified: %q", contents)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user