Skip to content

Commit f02707c

Browse files
committed
Fix permissions for resolv.conf and hosts
WriteFile sets permissions before umask is applied. 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 f02707c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pkg/dnsutil/hostsstore/hostsstore.go

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ 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+
if err = os.Chmod(loc, 0o644); err != nil {
118+
err = errors.Join(store.ErrSystemFailure, err)
119+
}
117120

118121
var content []byte
119122
content, err = json.Marshal(meta)
@@ -175,6 +178,9 @@ func (x *hostsStore) AllocHostsFile(id string, content []byte) (location string,
175178
if err != nil {
176179
err = errors.Join(store.ErrSystemFailure, err)
177180
}
181+
if err = os.Chmod(loc, 0o644); err != nil {
182+
err = errors.Join(store.ErrSystemFailure, err)
183+
}
178184

179185
return err
180186
})
@@ -333,6 +339,7 @@ func (x *hostsStore) updateAllHosts() (err error) {
333339
if err != nil {
334340
log.L.WithError(err).Errorf("failed to write hosts file for %q", entry)
335341
}
342+
_ = os.Chmod(loc, 0o644)
336343
}
337344
return nil
338345
}

pkg/resolvconf/resolvconf.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,12 @@ 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+
return &File{Content: content.Bytes(), Hash: hash}, os.Chmod(path, 0o644)
321326
}
322327

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

0 commit comments

Comments
 (0)