Code to more securely create anonymous temp files on Linux
This commit is contained in:
parent
3a126ffa9d
commit
4185e30d73
39
tools/utils/tmpfile_linux.go
Normal file
39
tools/utils/tmpfile_linux.go
Normal file
@ -0,0 +1,39 @@
|
||||
//go:build linux
|
||||
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func CreateAnonymousTemp(dir string) (*os.File, error) {
|
||||
if dir == "" {
|
||||
dir = os.TempDir()
|
||||
}
|
||||
fd, err := unix.Open(dir, unix.O_RDWR|unix.O_TMPFILE|unix.O_CLOEXEC, 0600)
|
||||
|
||||
if err == nil {
|
||||
path := "/proc/self/fd/" + strconv.FormatUint(uint64(fd), 10)
|
||||
return os.NewFile(uintptr(fd), path), nil
|
||||
}
|
||||
if err == unix.ENOENT {
|
||||
return nil, err
|
||||
}
|
||||
f, err := os.CreateTemp(dir, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = os.Remove(f.Name())
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
return f, err
|
||||
}
|
||||
25
tools/utils/tmpfile_others.go
Normal file
25
tools/utils/tmpfile_others.go
Normal file
@ -0,0 +1,25 @@
|
||||
//go:build !linux
|
||||
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func CreateAnonymousTemp(dir string) (*os.File, error) {
|
||||
f, err := os.CreateTemp(dir, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = os.Remove(f.Name())
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
return f, err
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user