Skip to content

Commit c01d0fa

Browse files
authored
Merge pull request containerd#3912 from apostasie/fix-3907
[PRIORITY] fix: prevent acquire from blanking out hosts file
2 parents 1d4cdfa + c7919f9 commit c01d0fa

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pkg/dnsutil/hostsstore/hostsstore.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,21 @@ func (x *hostsStore) Acquire(meta Meta) (err error) {
112112
return err
113113
}
114114

115-
if err = os.WriteFile(loc, []byte{}, 0o644); err != nil {
116-
return errors.Join(store.ErrSystemFailure, err)
117-
}
115+
// See https://github.com/containerd/nerdctl/issues/3907
116+
// Because of the way we call network manager ContainerNetworkingOpts then SetupNetworking in sequence
117+
// we need to make sure we do not overwrite an already allocated hosts file.
118+
if _, err = os.Stat(loc); os.IsNotExist(err) {
119+
if err = os.WriteFile(loc, []byte{}, 0o644); err != nil {
120+
return errors.Join(store.ErrSystemFailure, err)
121+
}
118122

119-
// os.WriteFile relies on syscall.Open. Unless there are ACLs, the effective mode of the file will be matched
120-
// against the current process umask.
121-
// See https://www.man7.org/linux/man-pages/man2/open.2.html for details.
122-
// Since we must make sure that these files are world readable, explicitly chmod them here.
123-
if err = os.Chmod(loc, 0o644); err != nil {
124-
err = errors.Join(store.ErrSystemFailure, err)
123+
// os.WriteFile relies on syscall.Open. Unless there are ACLs, the effective mode of the file will be matched
124+
// against the current process umask.
125+
// See https://www.man7.org/linux/man-pages/man2/open.2.html for details.
126+
// Since we must make sure that these files are world readable, explicitly chmod them here.
127+
if err = os.Chmod(loc, 0o644); err != nil {
128+
err = errors.Join(store.ErrSystemFailure, err)
129+
}
125130
}
126131

127132
var content []byte

0 commit comments

Comments
 (0)