2017-12-06 21:07:24 +01:00
|
|
|
package syscallcompat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-01-07 23:00:05 +01:00
|
|
|
"os"
|
2019-01-14 02:51:50 +01:00
|
|
|
"runtime"
|
2017-12-06 21:07:24 +01:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
2019-01-14 02:38:34 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2017-12-06 21:07:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadlinkat(t *testing.T) {
|
|
|
|
for _, targetLen := range []int{100, 500, 4000} {
|
|
|
|
target := string(bytes.Repeat([]byte("x"), targetLen))
|
2018-10-11 19:45:47 +02:00
|
|
|
err := syscall.Symlink(target, tmpDir+"/readlinkat")
|
2017-12-06 21:07:24 +01:00
|
|
|
if err != nil {
|
2018-10-11 19:45:47 +02:00
|
|
|
if targetLen > 1000 {
|
2018-02-28 20:40:08 +01:00
|
|
|
// Symlinks longer than 1024 (?) bytes are not supported on
|
2018-10-11 19:45:47 +02:00
|
|
|
// MacOS and XFS
|
|
|
|
t.Logf("skipping targetLen=%d: %v", targetLen, err)
|
2018-02-28 20:40:08 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-10-11 19:45:47 +02:00
|
|
|
t.Fatalf("targetLen=%d: %v", targetLen, err)
|
2017-12-06 21:07:24 +01:00
|
|
|
}
|
|
|
|
target2, err := Readlinkat(tmpDirFd, "readlinkat")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if target != target2 {
|
|
|
|
t.Errorf("target=%q != target2=%q", target, target2)
|
|
|
|
}
|
|
|
|
err = syscall.Unlink(tmpDir + "/readlinkat")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 23:00:05 +01:00
|
|
|
|
2019-01-14 02:32:28 +01:00
|
|
|
func TestOpenat(t *testing.T) {
|
2019-01-14 22:11:15 +01:00
|
|
|
// Always pass O_NOFOLLOW to avoid this warning:
|
|
|
|
// Openat: O_NOFOLLOW missing: flags = 0x0"
|
|
|
|
_, err := Openat(tmpDirFd, "testOpenAt", syscall.O_NOFOLLOW, 0)
|
2019-01-14 02:32:28 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("should have failed")
|
|
|
|
}
|
|
|
|
fd, err := os.Create(tmpDir + "/testOpenAt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
2019-01-14 22:11:15 +01:00
|
|
|
rawFd, err := Openat(tmpDirFd, "testOpenAt", syscall.O_NOFOLLOW, 0)
|
2019-01-14 02:32:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer syscall.Close(rawFd)
|
|
|
|
if rawFd < 0 {
|
|
|
|
t.Fatalf("rawFd=%d", rawFd)
|
|
|
|
}
|
|
|
|
// Test with absolute path
|
2019-01-14 22:11:15 +01:00
|
|
|
rawFd, err = Openat(-1, tmpDir+"/testOpenAt", syscall.O_NOFOLLOW, 0)
|
2019-01-14 02:32:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer syscall.Close(rawFd)
|
|
|
|
if rawFd < 0 {
|
|
|
|
t.Fatalf("rawFd=%d", rawFd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 02:35:46 +01:00
|
|
|
func TestRenameat(t *testing.T) {
|
|
|
|
os.Mkdir(tmpDir+"/dir1", 0700)
|
|
|
|
dir1, err := os.Open(tmpDir + "/dir1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dir1.Close()
|
|
|
|
os.Mkdir(tmpDir+"/dir2", 0700)
|
|
|
|
dir2, err := os.Open(tmpDir + "/dir2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dir2.Close()
|
|
|
|
fd, err := os.Create(tmpDir + "/dir1/f1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
err = Renameat(int(dir1.Fd()), "f1", int(dir2.Fd()), "f2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = os.Stat(tmpDir + "/dir2/f2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Test with absolute path
|
|
|
|
err = Renameat(-1, tmpDir+"/dir2/f2", -1, tmpDir+"/dir2/f1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = os.Stat(tmpDir + "/dir2/f1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 02:38:34 +01:00
|
|
|
func TestUnlinkat(t *testing.T) {
|
|
|
|
os.Mkdir(tmpDir+"/unlink1", 0700)
|
|
|
|
dirfd, err := os.Open(tmpDir + "/unlink1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dirfd.Close()
|
|
|
|
// Try to delete file
|
|
|
|
fd, err := os.Create(tmpDir + "/unlink1/f1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
err = Unlinkat(int(dirfd.Fd()), "f1", 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = os.Stat(tmpDir + "/unlink1/f1")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("file not deleted!")
|
|
|
|
}
|
|
|
|
// Try to delete dir
|
|
|
|
err = os.Mkdir(tmpDir+"/unlink1/d1", 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = Unlinkat(int(dirfd.Fd()), "d1", 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("this should fail due to missing AT_REMOVEDIR flag")
|
|
|
|
}
|
|
|
|
err = Unlinkat(int(dirfd.Fd()), "d1", unix.AT_REMOVEDIR)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = os.Stat(tmpDir + "/unlink1/d1")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("dir not deleted!")
|
|
|
|
}
|
|
|
|
// Test with absolute path
|
|
|
|
err = os.Mkdir(tmpDir+"/unlink1/d1", 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = Unlinkat(-1, tmpDir+"/unlink1/d1", unix.AT_REMOVEDIR)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = os.Stat(tmpDir + "/unlink1/d1")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("dir not deleted!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 22:11:15 +01:00
|
|
|
func TestFchmodatNofollow(t *testing.T) {
|
2019-01-07 23:00:05 +01:00
|
|
|
regular := "TestFchmodat_Regular"
|
|
|
|
f, err := os.OpenFile(tmpDir+"/"+regular, os.O_CREATE|os.O_WRONLY, 0000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
symlink := "TestFchmodat_Symlink"
|
|
|
|
err = syscall.Symlink(regular, tmpDir+"/"+symlink)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dirfd, err := syscall.Open(tmpDir, syscall.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer syscall.Close(dirfd)
|
|
|
|
|
|
|
|
// Check that chmod on a regular file works ok
|
2019-01-14 21:57:24 +01:00
|
|
|
err = FchmodatNofollow(dirfd, regular, 0111)
|
2019-01-07 23:00:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var st syscall.Stat_t
|
|
|
|
syscall.Lstat(tmpDir+"/"+regular, &st)
|
|
|
|
st.Mode &= 0777
|
|
|
|
if st.Mode != 0111 {
|
|
|
|
t.Errorf("wrong mode: %#0o", st.Mode)
|
|
|
|
}
|
2019-01-14 21:57:24 +01:00
|
|
|
err = FchmodatNofollow(dirfd, regular, 0000)
|
2019-01-07 23:00:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
syscall.Lstat(tmpDir+"/"+regular, &st)
|
|
|
|
st.Mode &= 0777
|
|
|
|
if st.Mode != 0000 {
|
|
|
|
t.Errorf("wrong mode: %#0o", st.Mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check what happens on a symlink
|
2019-01-14 21:57:24 +01:00
|
|
|
err = FchmodatNofollow(dirfd, symlink, 0333)
|
2019-01-14 22:11:15 +01:00
|
|
|
// On Darwin, permissions on symlinks are significant and can be changed. On
|
|
|
|
// Linux they are ignored, and FchmodatNofollow rejects attempts to change
|
|
|
|
// them.
|
|
|
|
if err == nil && runtime.GOOS == "linux" {
|
2019-01-07 23:00:05 +01:00
|
|
|
syscall.Lstat(tmpDir+"/"+symlink, &st)
|
|
|
|
st.Mode &= 0777
|
|
|
|
t.Errorf("chmod on symlink should have failed, but did not. New mode=%#0o", st.Mode)
|
|
|
|
}
|
|
|
|
syscall.Lstat(tmpDir+"/"+regular, &st)
|
|
|
|
st.Mode &= 0777
|
|
|
|
if st.Mode != 0000 {
|
|
|
|
t.Errorf("chmod on symlink affected symlink target: New mode=%#0o", st.Mode)
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 02:51:50 +01:00
|
|
|
|
|
|
|
// symlinkCheckMode looks if the mode bits in "st" say that this is a symlink.
|
|
|
|
// Calls t.Fatal() if not.
|
|
|
|
func symlinkCheckMode(t *testing.T, st syscall.Stat_t) {
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
// On MacOS, symlinks don't carry their own permissions, so
|
|
|
|
// only check the file type.
|
|
|
|
if st.Mode&syscall.S_IFMT != syscall.S_IFLNK {
|
|
|
|
t.Fatalf("This is not a symlink: mode = 0%o", st.Mode)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if st.Mode != 0120777 {
|
|
|
|
t.Fatalf("Wrong mode, have 0%o, want 0120777", st.Mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymlinkat(t *testing.T) {
|
|
|
|
err := Symlinkat("/foo/bar/baz", tmpDirFd, "symlink1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var st syscall.Stat_t
|
|
|
|
err = syscall.Lstat(tmpDir+"/symlink1", &st)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
symlinkCheckMode(t, st)
|
|
|
|
// Test with absolute path
|
|
|
|
err = Symlinkat("/foo/bar/baz", -1, tmpDir+"/symlink2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = syscall.Lstat(tmpDir+"/symlink2", &st)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
symlinkCheckMode(t, st)
|
|
|
|
}
|
2019-01-14 02:56:06 +01:00
|
|
|
|
|
|
|
func TestMkdirat(t *testing.T) {
|
|
|
|
err := Mkdirat(tmpDirFd, "mkdirat", 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(tmpDir + "/mkdirat")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
t.Fatalf("mkdirat did not create a directory")
|
|
|
|
}
|
|
|
|
// Test with absolute path
|
|
|
|
err = Mkdirat(-1, tmpDir+"/mkdirat2", 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fi, err = os.Stat(tmpDir + "/mkdirat2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
t.Fatalf("mkdirat did not create a directory")
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 03:00:49 +01:00
|
|
|
|
|
|
|
func TestFstatat(t *testing.T) {
|
|
|
|
var st unix.Stat_t
|
|
|
|
// stat a normal file (size 3)
|
|
|
|
f, err := os.Create(tmpDir + "/fstatat")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = f.Write([]byte("foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
err = Fstatat(tmpDirFd, "fstatat", &st, unix.AT_SYMLINK_NOFOLLOW)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if st.Size != 3 {
|
|
|
|
t.Errorf("wrong file size: %d", st.Size)
|
|
|
|
}
|
|
|
|
// stat a symlink and check that the size matches the length of the
|
|
|
|
// symlink target. This proves that we have stat'ed the symlink itself.
|
|
|
|
err = os.Symlink(tmpDir+"/fstatat", tmpDir+"/fstatatSymlink")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = Fstatat(tmpDirFd, "fstatatSymlink", &st, unix.AT_SYMLINK_NOFOLLOW)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedSize := int64(len(tmpDir + "/fstatat"))
|
|
|
|
if st.Size != expectedSize {
|
|
|
|
t.Errorf("symlink size: expected=%d, got=%d", expectedSize, st.Size)
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 19:59:36 +02:00
|
|
|
|
|
|
|
// BenchmarkLgetxattr benchmarks Lgetxattr. Lgetxattr is very hot as the kernel
|
|
|
|
// queries security.capabilities for every file access.
|
|
|
|
func BenchmarkLgetxattr(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
syscallcompat: add getxattr fastpaths
The allocations from Lgetxattr were #1 in the tar extract
allocation profile (caused by security.capability lookups).
No more!
$ benchstat old.txt new.txt
name old time/op new time/op delta
Lgetxattr-4 15.2µs ± 0% 1.8µs ± 0% ~ (p=1.000 n=1+1)
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.H8p: gocryptfs v2.0-beta1-4-g95ea738-dirty; go-fuse v2.0.4-0.20200908172753-0b6cbc515082 => github.com/rfjakob/go-fuse/v2 v2.0.4-0.20201015204057-88b12c99f8af; 2020-10-18 go1.15.3 linux/amd64
/tmp/benchmark.bash.H8p.mnt is a mountpoint
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 0,520109 s, 504 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0,255672 s, 1,0 GB/s
UNTAR: 30,238
MD5: 12,721
LS: 10,038
RM: 16,536
2020-10-18 00:25:42 +02:00
|
|
|
Lgetxattr("/", "user.this.attr.does.not.exist")
|
2020-10-16 19:59:36 +02:00
|
|
|
}
|
|
|
|
}
|