@@ -25,6 +25,7 @@ import (
2525 "time"
2626
2727 "gvisor.dev/gvisor/pkg/abi/linux"
28+ "gvisor.dev/gvisor/pkg/cleanup"
2829 "gvisor.dev/gvisor/pkg/fd"
2930 "gvisor.dev/gvisor/pkg/log"
3031 "gvisor.dev/gvisor/pkg/sentry/fdimport"
@@ -217,13 +218,18 @@ func (proc *Proc) execAsync(args *ExecArgs) (*kernel.ThreadGroup, kernel.ThreadI
217218 PIDNamespace : pidns ,
218219 Origin : kernel .OriginExec ,
219220 }
220- if initArgs .MountNamespace != nil {
221- // initArgs must hold a reference on MountNamespace, which will
222- // be donated to the new process in CreateProcess.
223- initArgs .MountNamespace .IncRef ()
224- }
225221 ctx := initArgs .NewContext (proc .Kernel )
226222
223+ if initArgs .MountNamespace == nil {
224+ // Set initArgs so that 'ctx' returns the namespace.
225+ initArgs .MountNamespace = proc .Kernel .GlobalInit ().Leader ().MountNamespace ()
226+ }
227+ // initArgs must hold a reference on MountNamespace, which will
228+ // be donated to the new process in CreateProcess.
229+ initArgs .MountNamespace .IncRef ()
230+ mntnsCu := cleanup .Make (func () { initArgs .MountNamespace .DecRef (ctx ) })
231+ defer mntnsCu .Clean ()
232+
227233 // Import file descriptors.
228234 var fdTable * kernel.FDTable
229235 if args .FDTable != nil {
@@ -235,15 +241,6 @@ func (proc *Proc) execAsync(args *ExecArgs) (*kernel.ThreadGroup, kernel.ThreadI
235241 }
236242 initArgs .FDTable = fdTable
237243
238- // Get the full path to the filename from the PATH env variable.
239- if initArgs .MountNamespace == nil {
240- // Set initArgs so that 'ctx' returns the namespace.
241- //
242- // Add a reference to the namespace, which is transferred to the new process.
243- initArgs .MountNamespace = proc .Kernel .GlobalInit ().Leader ().MountNamespace ()
244- initArgs .MountNamespace .IncRef ()
245- }
246-
247244 fdMap , execFD , err := args .unpackFiles ()
248245 if err != nil {
249246 return nil , 0 , nil , fmt .Errorf ("creating fd map: %w" , err )
@@ -311,6 +308,7 @@ func (proc *Proc) execAsync(args *ExecArgs) (*kernel.ThreadGroup, kernel.ThreadI
311308 initArgs .InitialCgroups = initialCgrps
312309 }
313310
311+ mntnsCu .Release () // mntns ref is transferred to Kernel.CreateProcess()
314312 tg , tid , err := proc .Kernel .CreateProcess (initArgs )
315313 if err != nil {
316314 return nil , 0 , nil , err
0 commit comments