Skip to content

Commit 79fa266

Browse files
committed
runc exec: setupRlimits after syscall.rlimit.init() completed
Issue: opencontainers#4195 Since https://go-review.googlesource.com/c/go/+/476097, there is a get/set race between runc exec and syscall.rlimit.init, so we need to call setupRlimits after syscall.rlimit.init() completed. Signed-off-by: lifubang <[email protected]>
1 parent 0d37745 commit 79fa266

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

libcontainer/process_linux.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -268,20 +268,26 @@ func (p *setnsProcess) start() (retErr error) {
268268
}
269269
}
270270
}
271-
// set rlimits, this has to be done here because we lose permissions
272-
// to raise the limits once we enter a user-namespace
273-
if err := setupRlimits(p.config.Rlimits, p.pid()); err != nil {
274-
return fmt.Errorf("error setting rlimits for process: %w", err)
275-
}
271+
276272
if err := utils.WriteJSON(p.comm.initSockParent, p.config); err != nil {
277273
return fmt.Errorf("error writing config to pipe: %w", err)
278274
}
279275

276+
var seenProcReady bool
280277
ierr := parseSync(p.comm.syncSockParent, func(sync *syncT) error {
281278
switch sync.Type {
282279
case procReady:
283-
// This shouldn't happen.
284-
panic("unexpected procReady in setns")
280+
seenProcReady = true
281+
// Set rlimits, this has to be done here because we lose permissions
282+
// to raise the limits once we enter a user-namespace
283+
if err := setupRlimits(p.config.Rlimits, p.pid()); err != nil {
284+
return fmt.Errorf("error setting rlimits for ready process: %w", err)
285+
}
286+
287+
// Sync with child.
288+
if err := writeSync(p.comm.syncSockParent, procRun); err != nil {
289+
return err
290+
}
285291
case procHooks:
286292
// This shouldn't happen.
287293
panic("unexpected procHooks in setns")
@@ -340,6 +346,9 @@ func (p *setnsProcess) start() (retErr error) {
340346
if err := p.comm.syncSockParent.Shutdown(unix.SHUT_WR); err != nil && ierr == nil {
341347
return err
342348
}
349+
if !seenProcReady && ierr == nil {
350+
ierr = errors.New("procReady not received")
351+
}
343352
// Must be done after Shutdown so the child will exit and we can wait for it.
344353
if ierr != nil {
345354
_, _ = p.wait()
@@ -774,7 +783,7 @@ func (p *initProcess) start() (retErr error) {
774783
}
775784
case procReady:
776785
seenProcReady = true
777-
// set rlimits, this has to be done here because we lose permissions
786+
// Set rlimits, this has to be done here because we lose permissions
778787
// to raise the limits once we enter a user-namespace
779788
if err := setupRlimits(p.config.Rlimits, p.pid()); err != nil {
780789
return fmt.Errorf("error setting rlimits for ready process: %w", err)

libcontainer/setns_init_linux.go

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ func (l *linuxSetnsInit) Init() error {
7777
}
7878
}
7979

80+
// Tell our parent that we're ready to Execv. This must be done before the
81+
// Seccomp rules have been applied, because we need to be able to read and
82+
// write to a socket.
83+
if err := syncParentReady(l.pipe); err != nil {
84+
return fmt.Errorf("sync ready: %w", err)
85+
}
86+
8087
if err := selinux.SetExecLabel(l.config.ProcessLabel); err != nil {
8188
return err
8289
}

0 commit comments

Comments
 (0)