Skip to content
Draft
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
4 changes: 4 additions & 0 deletions jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,8 @@ func rpcSetActiveExtension(extensionId string) error {
_ = unmountATXControl()
case "dc-power":
_ = unmountDCControl()
case "serial-console":
_ = unmountSerialConsole()
}
config.ActiveExtension = extensionId
if err := SaveConfig(); err != nil {
Expand All @@ -780,6 +782,8 @@ func rpcSetActiveExtension(extensionId string) error {
_ = mountATXControl()
case "dc-power":
_ = mountDCControl()
case "serial-console":
_ = mountSerialConsole()
}

// Re-publish MQTT HA Discovery for the new extension
Expand Down
19 changes: 19 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,20 @@ func initSerialPort() {
_ = mountATXControl()
case "dc-power":
_ = mountDCControl()
case "serial-console":
_ = mountSerialConsole()
}
}

func reopenSerialPort() error {
if serialMux != nil {
serialMux.Close()
serialMux = nil
}
if consoleBroker != nil {
consoleBroker.Close()
consoleBroker = nil
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

TOCTOU race on nullable globals causes potential panic

Medium Severity

reopenSerialPort now sets serialMux and consoleBroker to nil, making them nullable during normal operation. Previously they were always non-nil after init. The handleSerialChannel OnMessage callback (running on a WebRTC goroutine) reads these unsynchronized globals — the nil check at the top doesn't protect against a concurrent rpcSetActiveExtension call setting them to nil between the check and the subsequent serialMux.Enqueue(...) or consoleBroker.Enqueue(...), causing a nil pointer dereference panic. The same race applies to sendCustomCommand.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 80c5a5e. Configure here.

if port != nil {
port.Close()
}
Expand All @@ -598,6 +608,10 @@ func reopenSerialPort() error {
return err
}

return nil
}
Comment thread
cursor[bot] marked this conversation as resolved.

func mountSerialConsole() error {
// new broker (no sink yet—set it in handleSerialChannel.OnOpen)
norm := NormalizationOptions{
Mode: ModeNames, LineEnding: LineEnding_LF, TabRender: "", PreserveANSI: true,
Expand All @@ -619,6 +633,11 @@ func reopenSerialPort() error {
return nil
}

func unmountSerialConsole() error {
_ = reopenSerialPort()
return nil
}

func handleSerialChannel(dataChannel *webrtc.DataChannel) {
scopedLogger := serialLogger.With().
Uint16("data_channel_id", *dataChannel.ID()).Str("service", "serial terminal channel").Logger()
Expand Down
18 changes: 16 additions & 2 deletions serial_console_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,16 @@ func NewConsoleBroker(s Sink, norm NormalizationOptions) *ConsoleBroker {
}

func (b *ConsoleBroker) Start() { go b.loop() }
func (b *ConsoleBroker) Close() { close(b.done) }
func (b *ConsoleBroker) SetSink(s Sink) { b.sink = s }
func (b *ConsoleBroker) SetNormOptions(norm NormalizationOptions) { b.norm = norm }
func (b *ConsoleBroker) Close() {
select {
case <-b.done:
return
default:
close(b.done)
}
}
func (b *ConsoleBroker) SetTerminalPaused(v bool) {
if b == nil {
return
Expand Down Expand Up @@ -626,7 +633,14 @@ func (m *SerialMux) Start() {
go m.writer()
}

func (m *SerialMux) Close() { close(m.done) }
func (m *SerialMux) Close() {
select {
case <-m.done:
return
default:
close(m.done)
}
}

func (m *SerialMux) SetEchoEnabled(v bool) { m.echoEnabled.Store(v) }

Expand Down
Loading