fixed #5763,fixed #5760 : Add utils.securopen func to fix not syscall.O_NOFOLLOW on Windows (#5764)

This commit is contained in:
lengpucheng 2025-04-04 23:24:26 +08:00 committed by GitHub
parent 5fa33bc11b
commit 8b65fc75c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 86 additions and 12 deletions

View File

@ -24,7 +24,6 @@ import (
"runtime/debug"
"runtime/pprof"
"strconv"
"syscall"
"time"
"github.com/beego/beego/v2/core/utils"
@ -66,7 +65,7 @@ func ProcessInput(input string, w io.Writer) {
// MemProf record memory profile in pprof
func MemProf(w io.Writer) {
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 {
fmt.Fprintf(w, "create file %s error %s\n", filename, err.Error())
log.Fatal("record heap profile failed: ", err)
@ -84,7 +83,7 @@ func MemProf(w io.Writer) {
func GetCPUProfile(w io.Writer) {
sec := 30
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 {
fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
log.Fatal("record cpu profile failed: ", err)

View File

@ -20,6 +20,8 @@ import (
"context"
"errors"
"fmt"
"github.com/beego/beego/v2/core/utils"
"github.com/mitchellh/mapstructure"
"io"
"os"
"os/user"
@ -27,9 +29,6 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"github.com/mitchellh/mapstructure"
)
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.
func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
// 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 {
return err
}

29
core/utils/secureopen.go Normal file
View 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)
}

View 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
}

View File

@ -18,13 +18,13 @@ import (
"context"
"errors"
"fmt"
"github.com/beego/beego/v2/core/utils"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"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))
var f *os.File
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 {
SLogger.Println(err)
return
}
} 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 {
SLogger.Println(err)
return
@ -164,7 +164,7 @@ func (fp *FileProvider) SessionRead(ctx context.Context, sid string) (Store, err
return nil, 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 {
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
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 {
return nil, err
}