Skip to content

Commit

Permalink
Namestore hardening, workaround #3354
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Aug 24, 2024
1 parent d885544 commit baf2e52
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pkg/namestore/namestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path/filepath"
"strings"

"github.com/containerd/log"

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

0 comments on commit baf2e52

Please sign in to comment.