From 9c6f50d59e029d554085cc0e5de516c77db59759 Mon Sep 17 00:00:00 2001 From: Etienne Perot Date: Mon, 20 Nov 2023 13:01:07 -0800 Subject: [PATCH] Systrap: Wrap `initSysmsgThreadPriority` in `sync.Once`. As `sysmsgThreadPriority` is used as a variable in Systrap's seccomp-bpf filter, `systrapSeccomp.Variables` is called concurrently for each variant of the seccomp config that uses Systrap. This causes `initSysmsgThreadPriority` to be called concurrently, which means racing writes to `sysmsgThreadPriority`. This change wraps `initSysmsgThreadPriority` in `sync.Once` to ensure it is only modified once. PiperOrigin-RevId: 584107041 --- pkg/sentry/platform/systrap/subprocess.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/sentry/platform/systrap/subprocess.go b/pkg/sentry/platform/systrap/subprocess.go index aefd865ae..f8e667608 100644 --- a/pkg/sentry/platform/systrap/subprocess.go +++ b/pkg/sentry/platform/systrap/subprocess.go @@ -948,15 +948,22 @@ func (s *subprocess) PullFullState(c *context, ac *arch.Context64) error { return nil } -var sysmsgThreadPriority int +var ( + sysmsgThreadPriorityOnce sync.Once + sysmsgThreadPriority int +) +// initSysmsgThreadPriority looks at the current priority of the process +// and updates `sysmsgThreadPriority` accordingly. func initSysmsgThreadPriority() { - prio, err := unix.Getpriority(unix.PRIO_PROCESS, 0) - if err != nil { - panic("unable to get current scheduling priority") - } - // Sysmsg threads are executed with a priority one lower than the Sentry. - sysmsgThreadPriority = 20 - prio + 1 + sysmsgThreadPriorityOnce.Do(func() { + prio, err := unix.Getpriority(unix.PRIO_PROCESS, 0) + if err != nil { + panic("unable to get current scheduling priority") + } + // Sysmsg threads are executed with a priority one lower than the Sentry. + sysmsgThreadPriority = 20 - prio + 1 + }) } // createSysmsgThread creates a new sysmsg thread.