os: respect umask in Mkdir and OpenFile on BSD systems when perm has ModeSticky set

Instead of calling Chmod directly on perm, stat the created file/dir to extract the
actual permission bits which can be different from perm due to umask.

Fixes #23120.

Change-Id: I3e70032451fc254bf48ce9627e98988f84af8d91
Reviewed-on: https://go-review.googlesource.com/84477
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Yuval Pavel Zholkover 2017-12-16 19:06:10 +02:00 committed by Ian Lance Taylor
parent 788464724c
commit a5e8e2d998
3 changed files with 34 additions and 5 deletions

View file

@ -155,10 +155,10 @@ const DevNull = "/dev/null"
// openFileNolog is the Unix implementation of OpenFile.
func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
chmod := false
setSticky := false
if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
if _, err := Stat(name); IsNotExist(err) {
chmod = true
setSticky = true
}
}
@ -181,8 +181,8 @@ func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
}
// open(2) itself won't handle the sticky bit on *BSD and Solaris
if chmod {
Chmod(name, perm)
if setSticky {
setStickyBit(name)
}
// There's a race here with fork/exec, which we are