Skip to content

Commit 750d400

Browse files
committed
Fix permissions for resolv.conf and hosts
WriteFile uses syscall.Open, so permissions are modified by umask, if set. For people using agressive umasks (0077), /etc/resolv.conf will end-up unreadable for non root processes. See #3704 Signed-off-by: apostasie <[email protected]>
1 parent 3c41efe commit 750d400

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pkg/dnsutil/hostsstore/hostsstore.go

+9
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ func (x *hostsStore) Acquire(meta Meta) (err error) {
114114
if err = os.WriteFile(loc, []byte{}, 0o644); err != nil {
115115
return errors.Join(store.ErrSystemFailure, err)
116116
}
117+
// See https://www.man7.org/linux/man-pages/man2/open.2.html
118+
if err = os.Chmod(loc, 0o644); err != nil {
119+
err = errors.Join(store.ErrSystemFailure, err)
120+
}
117121

118122
var content []byte
119123
content, err = json.Marshal(meta)
@@ -175,6 +179,10 @@ func (x *hostsStore) AllocHostsFile(id string, content []byte) (location string,
175179
if err != nil {
176180
err = errors.Join(store.ErrSystemFailure, err)
177181
}
182+
// See https://www.man7.org/linux/man-pages/man2/open.2.html
183+
if err = os.Chmod(loc, 0o644); err != nil {
184+
err = errors.Join(store.ErrSystemFailure, err)
185+
}
178186

179187
return err
180188
})
@@ -333,6 +341,7 @@ func (x *hostsStore) updateAllHosts() (err error) {
333341
if err != nil {
334342
log.L.WithError(err).Errorf("failed to write hosts file for %q", entry)
335343
}
344+
_ = os.Chmod(loc, 0o644)
336345
}
337346
return nil
338347
}

pkg/resolvconf/resolvconf.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,13 @@ func Build(path string, dns, dnsSearch, dnsOptions []string) (*File, error) {
317317
return nil, err
318318
}
319319

320-
return &File{Content: content.Bytes(), Hash: hash}, os.WriteFile(path, content.Bytes(), 0644)
320+
err = os.WriteFile(path, content.Bytes(), 0o644)
321+
if err != nil {
322+
return nil, err
323+
}
324+
325+
// See https://www.man7.org/linux/man-pages/man2/open.2.html
326+
return &File{Content: content.Bytes(), Hash: hash}, os.Chmod(path, 0o644)
321327
}
322328

323329
func hashData(src io.Reader) (string, error) {

0 commit comments

Comments
 (0)