Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsafe access to perf standby status from systemview #17186

Merged
merged 4 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions changelog/17186.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
core: fix race when using SystemView.ReplicationState outside of a request context
```
7 changes: 4 additions & 3 deletions vault/dynamic_system_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func (c ctxKeyForwardedRequestMountAccessor) String() string {
}

type dynamicSystemView struct {
core *Core
mountEntry *MountEntry
core *Core
mountEntry *MountEntry
perfStandby bool
ncabatoff marked this conversation as resolved.
Show resolved Hide resolved
}

type extendedSystemView interface {
Expand Down Expand Up @@ -178,7 +179,7 @@ func (d dynamicSystemView) LocalMount() bool {
// in read mode.
func (d dynamicSystemView) ReplicationState() consts.ReplicationState {
state := d.core.ReplicationState()
if d.core.perfStandby {
if d.perfStandby {
state |= consts.ReplicationPerformanceStandby
}
return state
Expand Down
5 changes: 3 additions & 2 deletions vault/mount_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func verifyNamespace(*Core, *namespace.Namespace, *MountEntry) error { return ni
func (c *Core) mountEntrySysView(entry *MountEntry) extendedSystemView {
return extendedSystemViewImpl{
dynamicSystemView{
core: c,
mountEntry: entry,
core: c,
mountEntry: entry,
perfStandby: c.perfStandby,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the enclosing function here called with whatever lock is appropriate to access c.perfStandby?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. During a mount request, it's a request, so we hold the read stateLock as with most regular requests. During a post-unseal setupMounts, the write stateLock is held.

},
}
}
2 changes: 1 addition & 1 deletion vault/policy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func NewPolicyStore(ctx context.Context, core *Core, baseView *BarrierView, syst
func (c *Core) setupPolicyStore(ctx context.Context) error {
// Create the policy store
var err error
sysView := &dynamicSystemView{core: c}
sysView := &dynamicSystemView{core: c, perfStandby: c.perfStandby}
psLogger := c.baseLogger.Named("policy")
c.AddLogger(psLogger)
c.policyStore, err = NewPolicyStore(ctx, c, c.systemBarrierView, sysView, psLogger)
Expand Down
2 changes: 1 addition & 1 deletion vault/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func TestDynamicSystemView(c *Core, ns *namespace.Namespace) *dynamicSystemView
me.namespace = ns
}

return &dynamicSystemView{c, me}
return &dynamicSystemView{c, me, true}
}

// TestAddTestPlugin registers the testFunc as part of the plugin command to the
Expand Down