Skip to content

Commit

Permalink
fix: directories created via os.MkdirAll are not checked for permissi…
Browse files Browse the repository at this point in the history
…ons (#2613)

Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Aug 9, 2023
1 parent 7f67bc5 commit 05a6680
Show file tree
Hide file tree
Showing 19 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion client/config/dfcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (cfg *CacheOption) checkOutput() error {
}

outputDir, _ := path.Split(cfg.Output)
if err := MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil {
if err := MkdirAll(outputDir, 0700, os.Getuid(), os.Getgid()); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion client/config/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (cfg *ClientOption) checkOutput() error {
return fmt.Errorf("path[%s] is not absolute path", cfg.Output)
}
outputDir, _ := path.Split(cfg.Output)
if err := MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil {
if err := MkdirAll(outputDir, 0700, os.Getuid(), os.Getgid()); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions client/config/dfget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func TestMkdirAllRoot(t *testing.T) {
assert := testifyassert.New(t)
err := MkdirAll("/", 0777, os.Getuid(), os.Getgid())
err := MkdirAll("/", 0700, os.Getuid(), os.Getgid())
assert.Nil(err, "mkdir should not return error")
}

Expand Down Expand Up @@ -114,13 +114,13 @@ func TestMkdirAll(t *testing.T) {
if !ok {
return
}
assert.Nil(os.MkdirAll(tc.parent, 0777))
assert.Nil(os.MkdirAll(tc.parent, 0700))
defer func() {
// remove parent directory
assert.Nil(os.RemoveAll(tc.parent))
}()

err := MkdirAll(tc.dir, 0777, tc.uid, tc.gid)
err := MkdirAll(tc.dir, 0700, tc.uid, tc.gid)
assert.Nil(err, "mkdir should not return error")

// check new directories' permission
Expand Down
2 changes: 1 addition & 1 deletion client/config/peerhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestPeerHostOption_Load(t *testing.T) {
},
Metrics: ":8000",
WorkHome: "/tmp/dragonfly/dfdaemon/",
WorkHomeMode: 0755,
WorkHomeMode: 0700,
CacheDir: "/var/cache/dragonfly/",
CacheDirMode: 0700,
LogDir: "/var/log/dragonfly/",
Expand Down
2 changes: 1 addition & 1 deletion client/config/testdata/config/daemon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metrics: ":8000"
aliveTime: 0s
gcInterval: 1m0s
workHome: /tmp/dragonfly/dfdaemon/
workHomeMode: 0755
workHomeMode: 0700
cacheDir: /var/cache/dragonfly/
cacheDirMode: 0700
logDir: /var/log/dragonfly/
Expand Down
2 changes: 1 addition & 1 deletion client/daemon/peer/peertask_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func setupPeerTaskManagerComponents(ctrl *gomock.Controller, opt componentsOptio
TaskExpireTime: util.Duration{
Duration: -1 * time.Second,
},
}, func(request storage.CommonTaskRequest) {}, os.FileMode(0755))
}, func(request storage.CommonTaskRequest) {}, os.FileMode(0700))
return sched, storageManager
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func setupBackSourcePartialComponents(ctrl *gomock.Controller, testBytes []byte,
TaskExpireTime: util.Duration{
Duration: -1 * time.Second,
},
}, func(request storage.CommonTaskRequest) {}, os.FileMode(0755))
}, func(request storage.CommonTaskRequest) {}, os.FileMode(0700))
return sched, storageManager
}

Expand Down
2 changes: 1 addition & 1 deletion client/daemon/peer/peertask_stream_resume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func setupResumeStreamTaskComponents(ctrl *gomock.Controller, opt componentsOpti
Duration: -1 * time.Second,
},
}, func(request storage.CommonTaskRequest) {},
os.FileMode(0755))
os.FileMode(0700))
return sched, storageManager
}

Expand Down
2 changes: 1 addition & 1 deletion client/daemon/peer/piece_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestPieceManager_DownloadSource(t *testing.T) {
TaskExpireTime: clientutil.Duration{
Duration: -1 * time.Second,
},
}, func(request storage.CommonTaskRequest) {}, os.FileMode(0755))
}, func(request storage.CommonTaskRequest) {}, os.FileMode(0700))

hash := md5.New()
hash.Write(testBytes)
Expand Down
2 changes: 1 addition & 1 deletion client/daemon/peer/traffic_shaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func trafficShaperSetupPeerTaskManagerComponents(ctrl *gomock.Controller, opt tr
TaskExpireTime: util.Duration{
Duration: -1 * time.Second,
},
}, func(request storage.CommonTaskRequest) {}, os.FileMode(0755))
}, func(request storage.CommonTaskRequest) {}, os.FileMode(0700))
return sched, storageManager
}

Expand Down
2 changes: 1 addition & 1 deletion client/daemon/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ func checkOutput(output string) error {
return fmt.Errorf("path[%s] is not absolute path", output)
}
outputDir, _ := path.Split(output)
if err := config.MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil {
if err := config.MkdirAll(outputDir, 0700, os.Getuid(), os.Getgid()); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion client/daemon/storage/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
taskMetadata = "metadata"

defaultFileMode = os.FileMode(0644)
defaultDirectoryMode = os.FileMode(0755) // used unless overridden in config
defaultDirectoryMode = os.FileMode(0700) // used unless overridden in config
)

var (
Expand Down
2 changes: 1 addition & 1 deletion cmd/dependency/doc_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (g *genDocCommand) bindFlags() {
}

func (g *genDocCommand) runDoc() error {
_ = os.MkdirAll(g.path, fs.FileMode(0755))
_ = os.MkdirAll(g.path, fs.FileMode(0700))
file, err := os.Stat(g.path)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/dflog/loginit.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func createConsoleLogger(verbose bool) error {
func createFileLogger(verbose bool, meta []logInitMeta, logDir string) error {
levels = nil
// create parent dir first
_ = os.MkdirAll(logDir, fs.FileMode(0755))
_ = os.MkdirAll(logDir, fs.FileMode(0700))

for _, m := range meta {
log, level, err := CreateLogger(path.Join(logDir, m.fileName), false, false, verbose)
Expand Down
3 changes: 2 additions & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/gob"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -290,7 +291,7 @@ func (c *cache) Save(w io.Writer) (err error) {
// documentation for NewFrom().)
func (c *cache) SaveFile(fname string) error {
dir := filepath.Dir(fname)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
if err := os.MkdirAll(dir, fs.FileMode(0700)); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/dfpath/dfpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ func New(options ...Option) (Dfpath, error) {
}

// Create log directory.
if err := os.MkdirAll(d.logDir, fs.FileMode(0755)); err != nil {
if err := os.MkdirAll(d.logDir, fs.FileMode(0700)); err != nil {
cache.err = multierror.Append(cache.err, err)
}

// Create plugin directory.
if err := os.MkdirAll(d.pluginDir, fs.FileMode(0755)); err != nil {
if err := os.MkdirAll(d.pluginDir, fs.FileMode(0700)); err != nil {
cache.err = multierror.Append(cache.err, err)
}

// Create unix socket directory.
if err := os.MkdirAll(path.Dir(d.daemonSockPath), fs.FileMode(0755)); err != nil {
if err := os.MkdirAll(path.Dir(d.daemonSockPath), fs.FileMode(0700)); err != nil {
cache.err = multierror.Append(cache.err, err)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/dfpath/dfpath_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
)

var DefaultWorkHome = filepath.Join(user.HomeDir(), ".dragonfly")
var DefaultWorkHomeMode = os.FileMode(0755)
var DefaultWorkHomeMode = os.FileMode(0700)
var DefaultCacheDir = filepath.Join(DefaultWorkHome, "cache")
var DefaultCacheDirMode = os.FileMode(0755)
var DefaultCacheDirMode = os.FileMode(0700)
var DefaultConfigDir = filepath.Join(DefaultWorkHome, "config")
var DefaultLogDir = filepath.Join(DefaultWorkHome, "logs")
var DefaultDataDir = filepath.Join(DefaultWorkHome, "data")
var DefaultDataDirMode = os.FileMode(0755)
var DefaultDataDirMode = os.FileMode(0700)
var DefaultPluginDir = filepath.Join(DefaultWorkHome, "plugins")
var DefaultDownloadUnixSocketPath = filepath.Join(DefaultWorkHome, "dfdaemon.sock")
6 changes: 3 additions & 3 deletions pkg/dfpath/dfpath_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ package dfpath
import "os"

var DefaultWorkHome = "/usr/local/dragonfly"
var DefaultWorkHomeMode = os.FileMode(0755)
var DefaultWorkHomeMode = os.FileMode(0700)
var DefaultCacheDir = "/var/cache/dragonfly"
var DefaultCacheDirMode = os.FileMode(0755)
var DefaultCacheDirMode = os.FileMode(0700)
var DefaultConfigDir = "/etc/dragonfly"
var DefaultLogDir = "/var/log/dragonfly"
var DefaultDataDir = "/var/lib/dragonfly"
var DefaultDataDirMode = os.FileMode(0755)
var DefaultDataDirMode = os.FileMode(0700)
var DefaultPluginDir = "/usr/local/dragonfly/plugins"
var DefaultDownloadUnixSocketPath = "/var/run/dfdaemon.sock"
2 changes: 1 addition & 1 deletion trainer/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func TestStorage_Clear(t *testing.T) {
baseDir: os.TempDir(),
mock: func(t *testing.T, s Storage, baseDir string) {
s.(*storage).baseDir = filepath.Join(baseDir, "bae")
if err := os.MkdirAll(s.(*storage).baseDir, os.ModePerm); err != nil {
if err := os.MkdirAll(s.(*storage).baseDir, fs.FileMode(0700)); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit 05a6680

Please sign in to comment.