@@ -67,7 +67,7 @@ func WithChown(uid, gid int) SockOption {
67
67
}
68
68
}
69
69
70
- // WithChmod modifies socket file's access mode
70
+ // WithChmod modifies socket file's access mode.
71
71
func WithChmod (mask os.FileMode ) SockOption {
72
72
return func (path string ) error {
73
73
if err := os .Chmod (path , mask ); err != nil {
@@ -77,15 +77,35 @@ func WithChmod(mask os.FileMode) SockOption {
77
77
}
78
78
}
79
79
80
- // NewUnixSocketWithOpts creates a unix socket with the specified options
80
+ // NewUnixSocketWithOpts creates a unix socket with the specified options.
81
+ // By default, socket permissions are 0000 (i.e.: no access for anyone); pass
82
+ // WithChmod() and WithChown() to set the desired ownership and permissions.
83
+ //
84
+ // This function temporarily changes the system's "umask" to 0777 to work around
85
+ // a race condition between creating the socket and setting its permissions. While
86
+ // this should only be for a short duration, it may affect other processes that
87
+ // create files/directories during that period.
81
88
func NewUnixSocketWithOpts (path string , opts ... SockOption ) (net.Listener , error ) {
82
89
if err := syscall .Unlink (path ); err != nil && ! os .IsNotExist (err ) {
83
90
return nil , err
84
91
}
85
- mask := syscall .Umask (0777 )
86
- defer syscall .Umask (mask )
87
92
93
+ // net.Listen does not allow for permissions to be set. As a result, when
94
+ // specifying custom permissions ("WithChmod()"), there is a short time
95
+ // between creating the socket and applying the permissions, during which
96
+ // the socket permissions are Less restrictive than desired.
97
+ //
98
+ // To work around this limitation of net.Listen(), we temporarily set the
99
+ // umask to 0777, which forces the socket to be created with 000 permissions
100
+ // (i.e.: no access for anyone). After that, WithChmod() must be used to set
101
+ // the desired permissions.
102
+ //
103
+ // We don't use "defer" here, to reset the umask to its original value as soon
104
+ // as possible. Ideally we'd be able to detect if WithChmod() was passed as
105
+ // an option, and skip changing umask if default permissions are used.
106
+ origUmask := syscall .Umask (0777 )
88
107
l , err := net .Listen ("unix" , path )
108
+ syscall .Umask (origUmask )
89
109
if err != nil {
90
110
return nil , err
91
111
}
0 commit comments