This commit is contained in:
parent
5fa33bc11b
commit
8b65fc75c4
@ -24,7 +24,6 @@ import (
|
|||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/beego/beego/v2/core/utils"
|
"github.com/beego/beego/v2/core/utils"
|
||||||
@ -66,7 +65,7 @@ func ProcessInput(input string, w io.Writer) {
|
|||||||
// MemProf record memory profile in pprof
|
// MemProf record memory profile in pprof
|
||||||
func MemProf(w io.Writer) {
|
func MemProf(w io.Writer) {
|
||||||
filename := "mem-" + strconv.Itoa(pid) + ".memprof"
|
filename := "mem-" + strconv.Itoa(pid) + ".memprof"
|
||||||
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600)
|
f, err := utils.OpenFileSecure(filename, os.O_RDWR|os.O_CREATE, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "create file %s error %s\n", filename, err.Error())
|
fmt.Fprintf(w, "create file %s error %s\n", filename, err.Error())
|
||||||
log.Fatal("record heap profile failed: ", err)
|
log.Fatal("record heap profile failed: ", err)
|
||||||
@ -84,7 +83,7 @@ func MemProf(w io.Writer) {
|
|||||||
func GetCPUProfile(w io.Writer) {
|
func GetCPUProfile(w io.Writer) {
|
||||||
sec := 30
|
sec := 30
|
||||||
filename := "cpu-" + strconv.Itoa(pid) + ".pprof"
|
filename := "cpu-" + strconv.Itoa(pid) + ".pprof"
|
||||||
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600)
|
f, err := utils.OpenFileSecure(filename, os.O_RDWR|os.O_CREATE, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
|
fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
|
||||||
log.Fatal("record cpu profile failed: ", err)
|
log.Fatal("record cpu profile failed: ", err)
|
||||||
|
|||||||
@ -20,6 +20,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/beego/beego/v2/core/utils"
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
@ -27,9 +29,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -347,7 +346,7 @@ func (c *IniConfigContainer) GetSection(section string) (map[string]string, erro
|
|||||||
// BUG(env): The environment variable config item will be saved with real value in SaveConfigFile Function.
|
// BUG(env): The environment variable config item will be saved with real value in SaveConfigFile Function.
|
||||||
func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
|
func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||||
// Write configuration file by filename.
|
// Write configuration file by filename.
|
||||||
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600)
|
f, err := utils.OpenFileSecure(filename, os.O_RDWR|os.O_CREATE, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
29
core/utils/secureopen.go
Normal file
29
core/utils/secureopen.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// Copyright 2014 beego Author. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
//go:build !windows
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenFileSecure Secure file opening function
|
||||||
|
// Automatically add O_NOFOLLOW on Unix systems
|
||||||
|
func OpenFileSecure(name string, flag int, perm os.FileMode) (*os.File, error) {
|
||||||
|
return os.OpenFile(name, flag|syscall.O_NOFOLLOW, perm)
|
||||||
|
}
|
||||||
47
core/utils/secureopen_windows.go
Normal file
47
core/utils/secureopen_windows.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// Copyright 2014 beego Author. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
//go:build windows
|
||||||
|
// +build windows
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenFileSecure Secure file opening function
|
||||||
|
// Check whether it is a symbolic connection in Windows
|
||||||
|
// NOTE: This check does not prevent TOCTOU vulnerability on Windows,
|
||||||
|
// due to lack of O_NOFOLLOW or equivalent low-level open options in Go stdlib.
|
||||||
|
func OpenFileSecure(name string, flag int, perm os.FileMode) (*os.File, error) {
|
||||||
|
// Check if it's a symbolic link
|
||||||
|
if fi, err := os.Lstat(name); err == nil {
|
||||||
|
if isSymlink(fi) {
|
||||||
|
return nil, &os.PathError{
|
||||||
|
Op: "open",
|
||||||
|
Path: name,
|
||||||
|
Err: syscall.ERROR_ACCESS_DENIED,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Open the file in the normal way
|
||||||
|
return os.OpenFile(name, flag, perm)
|
||||||
|
}
|
||||||
|
|
||||||
|
// isSymlink Check if the file is a symbolic link
|
||||||
|
func isSymlink(fi os.FileInfo) bool {
|
||||||
|
return fi.Mode()&os.ModeSymlink != 0
|
||||||
|
}
|
||||||
@ -18,13 +18,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/beego/beego/v2/core/utils"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -100,13 +100,13 @@ func (fs *FileSessionStore) releaseSession(_ context.Context, _ http.ResponseWri
|
|||||||
_, err = os.Stat(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
|
_, err = os.Stat(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
|
||||||
var f *os.File
|
var f *os.File
|
||||||
if err == nil {
|
if err == nil {
|
||||||
f, err = os.OpenFile(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR|syscall.O_NOFOLLOW, 0o600)
|
f, err = utils.OpenFileSecure(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR, 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SLogger.Println(err)
|
SLogger.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else if os.IsNotExist(err) && createIfNotExist {
|
} else if os.IsNotExist(err) && createIfNotExist {
|
||||||
f, err = os.OpenFile(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_CREATE|os.O_EXCL|os.O_RDWR|syscall.O_NOFOLLOW, 0o600)
|
f, err = utils.OpenFileSecure(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_CREATE|os.O_EXCL|os.O_RDWR, 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SLogger.Println(err)
|
SLogger.Println(err)
|
||||||
return
|
return
|
||||||
@ -164,7 +164,7 @@ func (fp *FileProvider) SessionRead(ctx context.Context, sid string) (Store, err
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case os.IsNotExist(err):
|
case os.IsNotExist(err):
|
||||||
f, err = os.OpenFile(sidPath, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600)
|
f, err = utils.OpenFileSecure(sidPath, os.O_RDWR|os.O_CREATE, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -289,7 +289,7 @@ func (fp *FileProvider) SessionRegenerate(ctx context.Context, oldsid, sid strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if old sid file not exist, just create new sid file and return
|
// if old sid file not exist, just create new sid file and return
|
||||||
newf, err := os.OpenFile(newSidFile, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600)
|
newf, err := utils.OpenFileSecure(newSidFile, os.O_RDWR|os.O_CREATE, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user