Skip to content

Commit 6296bb3

Browse files
committed
Namestore hardening, workaround containerd#3351
Signed-off-by: apostasie <[email protected]>
1 parent f85bba8 commit 6296bb3

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

pkg/namestore/namestore.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"path/filepath"
2323
"strings"
2424

25+
"github.com/containerd/log"
26+
2527
"github.com/containerd/nerdctl/v2/pkg/identifiers"
2628
"github.com/containerd/nerdctl/v2/pkg/lockutil"
2729
)
@@ -56,12 +58,21 @@ func (x *nameStore) Acquire(name, id string) error {
5658
}
5759
fn := func() error {
5860
fileName := filepath.Join(x.dir, name)
59-
// If containerd was bounced, previously running containers that would get restarted will go again through
60-
// onCreateRuntime (unlike in a "normal" stop/start flow).
61-
// As such, we need to allow reacquiring by the same id
62-
// See: https://github.com/containerd/nerdctl/issues/3354
63-
if b, err := os.ReadFile(fileName); err == nil && string(b) != id {
64-
return fmt.Errorf("name %q is already used by ID %q", name, string(b))
61+
if b, err := os.ReadFile(fileName); err == nil {
62+
if strings.TrimSpace(string(b)) == "" {
63+
// currently acquired for an empty id - this obviously should never happen
64+
// this is recoverable, and we are not hard erroring, but still indicative that something was wrong
65+
// https://github.com/containerd/nerdctl/issues/3351
66+
log.L.Errorf("current name %q is reserved for a an empty id - please report this is as a bug", name)
67+
} else if string(b) != id {
68+
// if acquired by a different container, we error out here
69+
return fmt.Errorf("name %q is already used by ID %q", name, string(b))
70+
}
71+
// Otherwise, this is just re-acquiring after a restart
72+
// For example, if containerd was bounced, previously running containers that would get restarted will go
73+
// again through onCreateRuntime (unlike in a "normal" stop/start flow).
74+
// As such, we are allowing reacquiring by the same id
75+
// See: https://github.com/containerd/nerdctl/issues/3354
6576
}
6677
return os.WriteFile(fileName, []byte(id), 0600)
6778
}

0 commit comments

Comments
 (0)