Skip to content

os: ModeSetgid has no effect while using with Mkdir() on Linux #25539

Open
@Al2Klimov

Description

@Al2Klimov

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GORACE=""
GOROOT="/usr/lib/golang"
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build203196509=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

package main

import (
	"os"
)

func main() {
	os.Mkdir("test", 0770 | os.ModeSetgid)
}

What did you expect to see?

# ls -la test
insgesamt 4
drwxr-s---.  2 root root    6 24. Mai 04:09 .
dr-xr-x---. 13 root root 4096 24. Mai 04:09 ..
#

What did you see instead?

# ls -la test
insgesamt 4
drwxr-x---.  2 root root    6 24. Mai 04:09 .
dr-xr-x---. 13 root root 4096 24. Mai 04:09 ..
#

Why did this happen?

According to strace -f ./mkdir the Go stdlib behaves as expected...

[pid  6782] mkdirat(AT_FDCWD, "test", 02770 <unfinished ...>
[pid  6782] <... mkdirat resumed> )     = 0

... but on Linux this is not enough, see mkdirat(2):

The mkdirat() system call operates in exactly the same way as mkdir(2), (...)

... and mkdir(2):

The  argument  mode  specifies the permissions to use.
It is modified (...): the permissions of the created directory are (mode & ~umask & 0777).
Other mode bits of the created directory depend on the operating system.
For Linux, see below.
(...)
That is, under Linux the created directory actually gets mode (mode & ~umask & 01777)

How could this be fixed?

Similar to #8383, via Chmod.

Activity

added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on May 24, 2018
changed the title [-]os.ModeSetgid has no effect while using with os.Mkdir() on Linux[/-] [+]os: ModeSetgid has no effect while using with Mkdir() on Linux[/+] on May 24, 2018
tklauser

tklauser commented on May 24, 2018

@tklauser
Member
ianlancetaylor

ianlancetaylor commented on May 24, 2018

@ianlancetaylor
Contributor

Seems that we should fix that along the lines that you point out. Want to send a patch?

added
NeedsFixThe path to resolution is known, but the work has not been done.
on May 24, 2018
removed
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on May 24, 2018
added this to the Unplanned milestone on May 24, 2018
Al2Klimov

Al2Klimov commented on May 25, 2018

@Al2Klimov
Author

I'm afraid I have to correct myself: It's not just Linux, it seems to be all *nix.

Discovered this by running this on my Mac workstation:

#include <sys/stat.h>

int main() {
	mkdir("test", 02770);
}

vs.

#include <sys/stat.h>

int main() {
	mkdir("test", 02770);
	chmod("test", 02770);
}
odeke-em

odeke-em commented on Jun 1, 2018

@odeke-em
Member
jessfraz

jessfraz commented on Jun 1, 2018

@jessfraz
Contributor

Dope, if you don't want to send a patch I can make one, just let me know

dnsmichi

dnsmichi commented on Jun 1, 2018

@dnsmichi

@jessfraz Thanks. Haven't tested the linked PR yet, I'll talk to @Al2Klimov in terms of signing the CLA on Monday :)

Al2Klimov

Al2Klimov commented on Jun 4, 2018

@Al2Klimov
Author

As discussed w/ @dnsmichi (offline): I've just overseen an elephant:

I forgot to do this one.

22 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.OS-Linuxhelp wanted

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @justinfx@dnsmichi@tklauser@jessfraz@paulzhol

      Issue actions

        os: ModeSetgid has no effect while using with Mkdir() on Linux · Issue #25539 · golang/go