Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

actions/config: ensure config file is created with mode 0644 #152

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion actions/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"golang.org/x/sys/unix"

"github.com/google/fscrypt/crypto"
"github.com/google/fscrypt/filesystem"
"github.com/google/fscrypt/metadata"
"github.com/google/fscrypt/util"
)
Expand Down Expand Up @@ -68,7 +69,8 @@ var (
func CreateConfigFile(target time.Duration, useLegacy bool) error {
// Create the config file before computing the hashing costs, so we fail
// immediately if the program has insufficient permissions.
configFile, err := os.OpenFile(ConfigFileLocation, createFlags, configPermissions)
configFile, err := filesystem.OpenFileOverridingUmask(ConfigFileLocation,
createFlags, configPermissions)
switch {
case os.IsExist(err):
return ErrConfigFileExists
Expand Down
55 changes: 55 additions & 0 deletions actions/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* config_test.go - tests for creating the config file
*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package actions

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

"golang.org/x/sys/unix"
)

// Test that the global config file is created with mode 0644, regardless of the
// current umask.
func TestConfigFileIsCreatedWithCorrectMode(t *testing.T) {
oldMask := unix.Umask(0)
defer unix.Umask(oldMask)
unix.Umask(0077)

tempDir, err := ioutil.TempDir("", "fscrypt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
ConfigFileLocation = filepath.Join(tempDir, "test.conf")

if err = CreateConfigFile(time.Millisecond, false); err != nil {
t.Fatal(err)
}
fileInfo, err := os.Stat(ConfigFileLocation)
if err != nil {
t.Fatal(err)
}
if fileInfo.Mode().Perm() != 0644 {
t.Error("Expected newly created config file to have mode 0644")
}
}
10 changes: 10 additions & 0 deletions filesystem/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@ import (
"os"
"path/filepath"

"golang.org/x/sys/unix"

"github.com/pkg/errors"
)

// OpenFileOverridingUmask calls os.OpenFile but with the umask overridden so
// that no permission bits are masked out if the file is created.
func OpenFileOverridingUmask(name string, flag int, perm os.FileMode) (*os.File, error) {
oldMask := unix.Umask(0)
defer unix.Umask(oldMask)
return os.OpenFile(name, flag, perm)
}

// We only check the unix permissions and the sticky bit
const permMask = os.ModeSticky | os.ModePerm

Expand Down