fix(sdk): 单调时钟回拨检测与可信时间补偿
- 墙钟回拨超过 60s 时补偿 clockSkew 并告警,trustedNow 不随回拨倒退 - 新增回拨补偿测试
This commit is contained in:
2 files changed
+48
No files matched your search
@@ -185,6 +185,7 @@ type Client struct {
|
|||||||
crlVersion atomic.Int64 // 最近应用/上报的 CRL 版本
|
crlVersion atomic.Int64 // 最近应用/上报的 CRL 版本
|
||||||
lastCheckAt atomic.Int64 // 最近一次在线校验成功时刻(强制校验点计时)
|
lastCheckAt atomic.Int64 // 最近一次在线校验成功时刻(强制校验点计时)
|
||||||
clockSkew atomic.Int64 // 服务端时间 - 本地时间(trustedNow = 本地 + skew)
|
clockSkew atomic.Int64 // 服务端时间 - 本地时间(trustedNow = 本地 + skew)
|
||||||
|
lastWall atomic.Int64 // 最近观测到的本地墙钟(回拨检测)
|
||||||
noCheckWarn sync.Once
|
noCheckWarn sync.Once
|
||||||
fromCache bool
|
fromCache bool
|
||||||
}
|
}
|
||||||
@@ -194,11 +195,30 @@ func (c *Client) trustedNow() int64 {
|
|||||||
return c.opts.clock.Now() + c.clockSkew.Load()
|
return c.opts.clock.Now() + c.clockSkew.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// observeWallClock 检测本地时钟回拨:发现倒退超过 60s 时补偿 skew,
|
||||||
|
// 保证 trustedNow 不随墙钟回拨而倒退(docs/design/12-security.md T7)。
|
||||||
|
func (c *Client) observeWallClock() {
|
||||||
|
now := c.opts.clock.Now()
|
||||||
|
prev := c.lastWall.Load()
|
||||||
|
if prev > 0 && now < prev-60 {
|
||||||
|
delta := prev - now
|
||||||
|
c.clockSkew.Add(delta)
|
||||||
|
c.opts.logger.Warn("检测到系统时钟回拨,已补偿可信时间",
|
||||||
|
"logger", "pkg.sdk.Client.observeWallClock", "rollback_seconds", delta)
|
||||||
|
c.lastWall.Store(prev)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if now > prev {
|
||||||
|
c.lastWall.Store(now)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// applyServerTime 用服务端时间更新本地偏移估计。
|
// applyServerTime 用服务端时间更新本地偏移估计。
|
||||||
func (c *Client) applyServerTime(serverTime int64) {
|
func (c *Client) applyServerTime(serverTime int64) {
|
||||||
if serverTime <= 0 {
|
if serverTime <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
c.observeWallClock()
|
||||||
c.clockSkew.Store(serverTime - c.opts.clock.Now())
|
c.clockSkew.Store(serverTime - c.opts.clock.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,6 +410,7 @@ func (c *Client) installVerified(token string) error {
|
|||||||
old := c.snap.Load()
|
old := c.snap.Load()
|
||||||
ns := &snapshot{payload: p, rawToken: token, state: StateActive}
|
ns := &snapshot{payload: p, rawToken: token, state: StateActive}
|
||||||
c.snap.Store(ns)
|
c.snap.Store(ns)
|
||||||
|
c.observeWallClock()
|
||||||
c.lastCheckAt.Store(c.trustedNow())
|
c.lastCheckAt.Store(c.trustedNow())
|
||||||
if old == nil || old.state != StateActive {
|
if old == nil || old.state != StateActive {
|
||||||
c.opts.logger.Info("授权已装载", "logger", "pkg.sdk.Client.installVerified", "jti", p.JTI)
|
c.opts.logger.Info("授权已装载", "logger", "pkg.sdk.Client.installVerified", "jti", p.JTI)
|
||||||
@@ -453,6 +474,7 @@ func (c *Client) tick() {
|
|||||||
if s == nil || s.payload == nil {
|
if s == nil || s.payload == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
c.observeWallClock()
|
||||||
now := c.trustedNow()
|
now := c.trustedNow()
|
||||||
// 终态不迁移
|
// 终态不迁移
|
||||||
if s.state == StateRevoked || s.state == StateInvalid {
|
if s.state == StateRevoked || s.state == StateInvalid {
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package sdk
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// TestClockRollback_Compensated 本地时钟回拨不得让可信时间倒退。
|
||||||
|
func TestClockRollback_Compensated(t *testing.T) {
|
||||||
|
e := newEnv(t)
|
||||||
|
clock := &fakeClock{now: e.now}
|
||||||
|
path := writeFileLicense(t, e, 0)
|
||||||
|
c, err := New(WithLicenseFile(path), WithProductKey("prod_test"),
|
||||||
|
WithTrustChain(e.trust()), WithClock(clock))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
|
before := c.trustedNow()
|
||||||
|
clock.Advance(-3600) // 墙钟回拨 1h
|
||||||
|
c.tick()
|
||||||
|
if got := c.trustedNow(); got < before {
|
||||||
|
t.Fatalf("可信时间倒退: before=%d after=%d", before, got)
|
||||||
|
}
|
||||||
|
if c.State() != StateActive {
|
||||||
|
t.Fatalf("回拨不应改变有效状态: %s", c.State())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user