Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions internal/localruntime/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func NewService(opts Options) (*Service, error) {
func (s *Service) SocketPath() string { return s.socketPath }

func (s *Service) Start(ctx context.Context) error {
if err := os.Remove(s.socketPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("remove stale local runtime socket: %w", err)
if err := removeStaleSocket(s.socketPath); err != nil {
return err
}
ln, err := net.Listen("unix", s.socketPath)
if err != nil {
Expand All @@ -79,6 +79,23 @@ func (s *Service) Start(ctx context.Context) error {
return nil
}

func removeStaleSocket(path string) error {
info, err := os.Lstat(path)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return fmt.Errorf("inspect local runtime socket path: %w", err)
}
if info.Mode()&os.ModeSocket == 0 {
return fmt.Errorf("local runtime socket path exists and is not a socket: %s", path)
}
if err := os.Remove(path); err != nil {
Comment on lines +83 to +93

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security Non-atomic socket check The new guard checks path with os.Lstat, then later deletes the same pathname with os.Remove. Another local process can replace the verified socket with a regular file between those calls, so Start can still delete a non-socket even though this change is meant to refuse one. This can happen for configured socket paths in user-writable locations, including paths under /tmp.

return fmt.Errorf("remove stale local runtime socket: %w", err)
}
return nil
}

func (s *Service) Stop() {
_ = s.Shutdown(context.Background())
}
Expand Down
36 changes: 36 additions & 0 deletions internal/localruntime/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,42 @@ func TestServiceAllowsNonblockingHookPayloadDecodeFailure(t *testing.T) {
}
}

func TestServiceStartRefusesExistingRegularFile(t *testing.T) {
t.Parallel()

core, err := runtimecore.New(&stubRuntime{})
if err != nil {
t.Fatalf("runtimecore.New() error = %v", err)
}
socketPath := filepath.Join(t.TempDir(), "kontext.sock")
contents := []byte("keep this file")
if err := os.WriteFile(socketPath, contents, 0o600); err != nil {
t.Fatalf("WriteFile() error = %v", err)
}
service, err := NewService(Options{
SocketPath: socketPath,
Core: core,
})
if err != nil {
t.Fatalf("NewService() error = %v", err)
}

err = service.Start(context.Background())
if err == nil {
t.Fatal("Start() error = nil, want non-socket path error")
}
if !strings.Contains(err.Error(), "not a socket") {
t.Fatalf("Start() error = %v, want non-socket path error", err)
}
got, readErr := os.ReadFile(socketPath)
if readErr != nil {
t.Fatalf("ReadFile() error = %v", readErr)
}
if string(got) != string(contents) {
t.Fatalf("file contents = %q, want %q", got, contents)
}
}

func newTestService(t *testing.T, runtime *stubRuntime, asyncIngest bool) *Service {
t.Helper()

Expand Down
Loading