Fix CVE-2021-27116 CVE-2021-27117

1. Adding O_NOFOLLOW flag to prevent symlink attacks

These changes help protect against various security issues including:

- Symlink attacks where attackers could trick the application into modifying unintended files
- Privilege escalation through improper file permissions

Signed-off-by: chengjingtao <jtcheng0616@gmail.com>
This commit is contained in:
chengjingtao
2025-03-11 18:57:45 +08:00
committed by Ming Deng
parent 5e9c913b47
commit 1f40a88b0c
8 changed files with 21 additions and 12 deletions

View File

@@ -24,6 +24,7 @@ import (
"runtime/debug"
"runtime/pprof"
"strconv"
"syscall"
"time"
"github.com/beego/beego/v2/core/utils"
@@ -65,7 +66,8 @@ func ProcessInput(input string, w io.Writer) {
// MemProf record memory profile in pprof
func MemProf(w io.Writer) {
filename := "mem-" + strconv.Itoa(pid) + ".memprof"
if f, err := os.Create(filename); err != nil {
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600)
if err != nil {
fmt.Fprintf(w, "create file %s error %s\n", filename, err.Error())
log.Fatal("record heap profile failed: ", err)
} else {
@@ -82,7 +84,7 @@ func MemProf(w io.Writer) {
func GetCPUProfile(w io.Writer) {
sec := 30
filename := "cpu-" + strconv.Itoa(pid) + ".pprof"
f, err := os.Create(filename)
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600)
if err != nil {
fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
log.Fatal("record cpu profile failed: ", err)