Skip to content

Commit f26410a

Browse files
committed
ipn/wg: m tighten mutex on wgconn fields
1 parent 8f6e1f7 commit f26410a

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

intra/ipn/wg/wgconn.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,9 @@ func (s *StdNetBind) Closed() bool {
375375
}
376376

377377
func (s *StdNetBind) Close() error {
378-
if s.closed.Load() {
379-
log.W("wg: bind: %s already closed", s.id)
380-
return nil
381-
}
382-
378+
// Do NOT do a pre-lock s.closed.Load() check here.
379+
// Open() writes s.closed under s.mu; a read outside the lock races with it.
380+
// The CAS below (also under s.mu) is the only correct guard.
383381
s.mu.Lock()
384382
defer s.mu.Unlock()
385383

@@ -610,8 +608,12 @@ func (s *StdNetBind) BatchSize() int {
610608

611609
// from: github.com/WireGuard/wireguard-go/blob/1417a47c8/conn/mark_unix.go
612610
func (s *StdNetBind) SetMark(mark uint32) (err error) {
611+
// s.ipv4 and s.ipv6 are written by Open/Close under s.mu; read them here
612+
// under the same lock to avoid a data race.
613+
s.mu.Lock()
613614
uc4, _ := s.ipv4.(core.ControlConn) // may be nil
614615
uc6, _ := s.ipv6.(core.ControlConn) // may be nil
616+
s.mu.Unlock()
615617
var operr error
616618
var raw4, raw6 syscall.RawConn
617619
fwmarkIoctl := 36 /* unix.SO_MARK */
@@ -641,7 +643,7 @@ func (s *StdNetBind) SetMark(mark uint32) (err error) {
641643
}
642644
} // else: return err
643645
}
644-
log.I("wg: bind: %s set mark; err? %v", err, s.id)
646+
log.I("wg: bind: %s set mark; err? %v", s.id, err)
645647
return nil
646648
}
647649

0 commit comments

Comments
 (0)