2018-11-11 15:48:39 +01:00
|
|
|
package xattr_tests
|
|
|
|
|
|
|
|
// xattr integration tests.
|
|
|
|
//
|
|
|
|
// These tests are not integrated into the "matrix" tests because of the need
|
|
|
|
// to switch TMPDIR to /var/tmp.
|
|
|
|
// TODO: check if it actually causes trouble in the "matrix" tests.
|
2018-03-18 17:43:38 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-05-07 22:01:36 +02:00
|
|
|
"encoding/base64"
|
2018-03-18 17:43:38 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2018-03-26 21:54:17 +02:00
|
|
|
"syscall"
|
2018-03-18 17:43:38 +01:00
|
|
|
"testing"
|
|
|
|
|
2018-03-28 19:19:58 +02:00
|
|
|
"github.com/pkg/xattr"
|
2018-03-18 17:43:38 +01:00
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/cryptocore"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
|
2018-03-18 17:43:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2018-03-26 21:54:17 +02:00
|
|
|
if !xattrSupported(test_helpers.TmpDir) {
|
2018-10-10 22:24:20 +02:00
|
|
|
fmt.Printf("xattrs not supported on %q\n", test_helpers.TmpDir)
|
2018-03-18 17:43:38 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
test_helpers.ResetTmpDir(true)
|
2018-05-07 22:01:36 +02:00
|
|
|
// Write deterministic diriv so encrypted filenames are deterministic.
|
|
|
|
os.Remove(test_helpers.DefaultCipherDir + "/gocryptfs.diriv")
|
|
|
|
diriv := []byte("1234567890123456")
|
|
|
|
err := ioutil.WriteFile(test_helpers.DefaultCipherDir+"/gocryptfs.diriv", diriv, 0400)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-03-18 17:43:38 +01:00
|
|
|
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, "-zerokey")
|
|
|
|
r := m.Run()
|
|
|
|
test_helpers.UnmountPanic(test_helpers.DefaultPlainDir)
|
|
|
|
os.RemoveAll(test_helpers.TmpDir)
|
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:22:10 +01:00
|
|
|
func setGetRmList(fn string) error {
|
2020-02-29 20:38:48 +01:00
|
|
|
return setGetRmList3(fn, "user.foo", []byte("123456789"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func setGetRmList3(fn string, attr string, val []byte) error {
|
2018-11-12 22:22:10 +01:00
|
|
|
// List
|
|
|
|
list, err := xattr.LList(fn)
|
2018-03-18 17:43:38 +01:00
|
|
|
if err != nil {
|
2018-11-12 22:22:10 +01:00
|
|
|
return err
|
2018-03-18 17:43:38 +01:00
|
|
|
}
|
2018-11-12 22:22:10 +01:00
|
|
|
if len(list) > 0 {
|
|
|
|
return fmt.Errorf("Should have gotten empty result, got %v", list)
|
|
|
|
}
|
2020-02-29 20:38:48 +01:00
|
|
|
err = xattr.LSet(fn, attr, val)
|
2018-03-18 17:43:38 +01:00
|
|
|
if err != nil {
|
2018-11-12 22:22:10 +01:00
|
|
|
return err
|
2018-03-18 17:43:38 +01:00
|
|
|
}
|
|
|
|
// Read back
|
2018-05-27 20:09:48 +02:00
|
|
|
val2, err := xattr.LGet(fn, attr)
|
2018-03-18 17:43:38 +01:00
|
|
|
if err != nil {
|
2018-11-12 22:22:10 +01:00
|
|
|
return err
|
2018-03-18 17:43:38 +01:00
|
|
|
}
|
2020-02-29 20:38:48 +01:00
|
|
|
if !bytes.Equal(val, val2) {
|
|
|
|
return fmt.Errorf("wrong readback value: %v != %v", val, val2)
|
2018-03-18 17:43:38 +01:00
|
|
|
}
|
|
|
|
// Remove
|
2018-05-27 20:09:48 +02:00
|
|
|
err = xattr.LRemove(fn, attr)
|
2018-03-18 17:43:38 +01:00
|
|
|
if err != nil {
|
2018-11-12 22:22:10 +01:00
|
|
|
return err
|
2018-03-18 17:43:38 +01:00
|
|
|
}
|
|
|
|
// Read back
|
2018-05-27 20:09:48 +02:00
|
|
|
val3, err := xattr.LGet(fn, attr)
|
2018-03-18 17:43:38 +01:00
|
|
|
if err == nil {
|
2018-11-12 22:22:10 +01:00
|
|
|
return fmt.Errorf("attr is still there after deletion!? val3=%v", val3)
|
|
|
|
}
|
|
|
|
// List
|
|
|
|
list, err = xattr.LList(fn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(list) > 0 {
|
|
|
|
return fmt.Errorf("Should have gotten empty result, got %v", list)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test xattr set, get, rm on a regular file.
|
|
|
|
func TestSetGetRmRegularFile(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestSetGetRmRegularFile"
|
2019-01-02 20:45:55 +01:00
|
|
|
err := ioutil.WriteFile(fn, []byte("12345"), 0700)
|
2018-11-12 22:22:10 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating empty file failed: %v", err)
|
|
|
|
}
|
|
|
|
err = setGetRmList(fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-01-02 20:45:55 +01:00
|
|
|
fi, _ := os.Lstat(fn)
|
|
|
|
if fi.Size() != 5 {
|
|
|
|
t.Errorf("file size has changed!? size=%d", fi.Size())
|
|
|
|
}
|
2018-11-12 22:22:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test xattr set, get, rm on a fifo. This should not hang.
|
|
|
|
func TestSetGetRmFifo(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestSetGetRmFifo"
|
|
|
|
err := syscall.Mkfifo(fn, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating fifo failed: %v", err)
|
2018-03-18 17:43:38 +01:00
|
|
|
}
|
2018-11-12 22:22:10 +01:00
|
|
|
// We expect to get EPERM, but we should not hang:
|
|
|
|
// $ setfattr -n user.foo -v XXXXX fifo
|
|
|
|
// setfattr: fifo: Operation not permitted
|
|
|
|
setGetRmList(fn)
|
2018-03-18 17:43:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-04 22:22:24 +01:00
|
|
|
// Test xattr set, get, rm on a directory. This should not fail with EISDIR.
|
|
|
|
func TestSetGetRmDir(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestSetGetRmDir"
|
|
|
|
err := syscall.Mkdir(fn, 0700)
|
|
|
|
if err != nil {
|
2019-01-05 04:33:50 +01:00
|
|
|
t.Fatalf("creating directory failed: %v", err)
|
2019-01-04 22:22:24 +01:00
|
|
|
}
|
|
|
|
setGetRmList(fn)
|
|
|
|
}
|
|
|
|
|
2018-03-25 21:02:33 +02:00
|
|
|
func TestXattrSetEmpty(t *testing.T) {
|
|
|
|
attr := "user.foo"
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestXattrSetEmpty1"
|
|
|
|
err := ioutil.WriteFile(fn, nil, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating empty file failed: %v", err)
|
|
|
|
}
|
|
|
|
// Make sure it does not exist already
|
2018-05-27 20:09:48 +02:00
|
|
|
_, err = xattr.LGet(fn, attr)
|
2018-03-25 21:02:33 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("we should have got an error here")
|
|
|
|
}
|
|
|
|
// Set empty value
|
2018-05-27 20:09:48 +02:00
|
|
|
err = xattr.LSet(fn, attr, nil)
|
2018-03-25 21:02:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Read back
|
2018-05-27 20:09:48 +02:00
|
|
|
val, err := xattr.LGet(fn, attr)
|
2018-03-25 21:02:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(val) != 0 {
|
|
|
|
t.Errorf("wrong length: want=0 have=%d", len(val))
|
|
|
|
}
|
|
|
|
// Overwrite empty value with something
|
|
|
|
val1 := []byte("xyz123")
|
2018-05-27 20:09:48 +02:00
|
|
|
err = xattr.LSet(fn, attr, val1)
|
2018-03-25 21:02:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Read back
|
2018-05-27 20:09:48 +02:00
|
|
|
val2, err := xattr.LGet(fn, attr)
|
2018-03-25 21:02:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(val1, val2) {
|
|
|
|
t.Fatalf("wrong readback value: %v != %v", val1, val2)
|
|
|
|
}
|
|
|
|
// Overwrite something with empty value
|
2018-05-27 20:09:48 +02:00
|
|
|
err = xattr.LSet(fn, attr, nil)
|
2018-03-25 21:02:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Read back
|
2018-05-27 20:09:48 +02:00
|
|
|
val, err = xattr.LGet(fn, attr)
|
2018-03-25 21:02:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(val) != 0 {
|
|
|
|
t.Errorf("wrong length: want=0 have=%d", len(val2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 17:43:38 +01:00
|
|
|
func TestXattrList(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestXattrList"
|
|
|
|
err := ioutil.WriteFile(fn, nil, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating empty file failed: %v", err)
|
|
|
|
}
|
|
|
|
val := []byte("xxxxxxxxyyyyyyyyyyyyyyyzzzzzzzzzzzzz")
|
|
|
|
num := 20
|
|
|
|
for i := 1; i <= num; i++ {
|
|
|
|
attr := fmt.Sprintf("user.TestXattrList.%02d", i)
|
2018-05-27 20:09:48 +02:00
|
|
|
err = xattr.LSet(fn, attr, val)
|
2018-03-18 17:43:38 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 20:09:48 +02:00
|
|
|
names, err := xattr.LList(fn)
|
2018-03-18 17:43:38 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(names) != num {
|
|
|
|
t.Errorf("wrong number of names, want=%d have=%d", num, len(names))
|
|
|
|
}
|
|
|
|
for _, n := range names {
|
|
|
|
if !strings.HasPrefix(n, "user.TestXattrList.") {
|
|
|
|
t.Errorf("unexpected attr name: %q", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 21:54:17 +02:00
|
|
|
|
|
|
|
func xattrSupported(path string) bool {
|
2018-05-27 20:09:48 +02:00
|
|
|
_, err := xattr.LGet(path, "user.xattrSupported-dummy-value")
|
2018-03-26 21:54:17 +02:00
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
err2 := err.(*xattr.Error)
|
|
|
|
if err2.Err == syscall.EOPNOTSUPP {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-05-07 22:01:36 +02:00
|
|
|
|
|
|
|
func TestBase64XattrRead(t *testing.T) {
|
|
|
|
attrName := "user.test"
|
|
|
|
attrName2 := "user.test2"
|
|
|
|
encryptedAttrName := "user.gocryptfs.LB1kHHVrX1OEBdLmj3LTKw"
|
|
|
|
encryptedAttrName2 := "user.gocryptfs.d2yn5l7-0zUVqviADw-Oyw"
|
|
|
|
attrValue := fmt.Sprintf("test.%d", cryptocore.RandUint64())
|
|
|
|
|
|
|
|
fileName := "TestBase64Xattr"
|
|
|
|
encryptedFileName := "BaGak7jIoqAZQMlP0N5uCw"
|
|
|
|
|
|
|
|
plainFn := test_helpers.DefaultPlainDir + "/" + fileName
|
|
|
|
encryptedFn := test_helpers.DefaultCipherDir + "/" + encryptedFileName
|
|
|
|
err := ioutil.WriteFile(plainFn, nil, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating empty file failed: %v", err)
|
|
|
|
}
|
|
|
|
if _, err2 := os.Stat(encryptedFn); os.IsNotExist(err2) {
|
|
|
|
t.Fatalf("encrypted file does not exist: %v", err2)
|
|
|
|
}
|
2018-05-27 20:09:48 +02:00
|
|
|
xattr.LSet(plainFn, attrName, []byte(attrValue))
|
2018-05-07 22:01:36 +02:00
|
|
|
|
2018-05-27 20:09:48 +02:00
|
|
|
encryptedAttrValue, err1 := xattr.LGet(encryptedFn, encryptedAttrName)
|
2018-05-07 22:01:36 +02:00
|
|
|
if err1 != nil {
|
|
|
|
t.Fatal(err1)
|
|
|
|
}
|
|
|
|
|
2018-05-27 20:09:48 +02:00
|
|
|
xattr.LSet(encryptedFn, encryptedAttrName2, encryptedAttrValue)
|
|
|
|
plainValue, err := xattr.LGet(plainFn, attrName2)
|
2018-05-07 22:01:36 +02:00
|
|
|
|
|
|
|
if err != nil || string(plainValue) != attrValue {
|
2019-01-02 16:55:51 +01:00
|
|
|
t.Fatalf("Attribute binary value decryption error: have=%q want=%q err=%v", string(plainValue), attrValue, err)
|
2018-05-07 22:01:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
encryptedAttrValue64 := base64.RawURLEncoding.EncodeToString(encryptedAttrValue)
|
2018-05-27 20:09:48 +02:00
|
|
|
xattr.LSet(encryptedFn, encryptedAttrName2, []byte(encryptedAttrValue64))
|
2018-05-07 22:01:36 +02:00
|
|
|
|
2018-05-27 20:09:48 +02:00
|
|
|
plainValue, err = xattr.LGet(plainFn, attrName2)
|
2018-05-07 22:01:36 +02:00
|
|
|
if err != nil || string(plainValue) != attrValue {
|
|
|
|
t.Fatalf("Attribute base64-encoded value decryption error %s != %s %v", string(plainValue), attrValue, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remount with -wpanic=false so gocryptfs does not panics when it sees
|
|
|
|
// the broken xattrs
|
|
|
|
test_helpers.UnmountPanic(test_helpers.DefaultPlainDir)
|
|
|
|
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, "-zerokey", "-wpanic=false")
|
|
|
|
|
|
|
|
brokenVals := []string{
|
|
|
|
"111",
|
|
|
|
"raw-test-long-block123",
|
|
|
|
"raw-test-long-block123-xyz11111111111111111111111111111111111111",
|
|
|
|
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
|
|
|
}
|
|
|
|
for _, val := range brokenVals {
|
2018-05-27 20:09:48 +02:00
|
|
|
xattr.LSet(encryptedFn, encryptedAttrName2, []byte(val))
|
|
|
|
plainValue, err = xattr.LGet(plainFn, attrName2)
|
2018-05-07 22:01:36 +02:00
|
|
|
err2, _ := err.(*xattr.Error)
|
|
|
|
if err == nil || err2.Err != syscall.EIO {
|
|
|
|
t.Fatalf("Incorrect handling of broken data %s %v", string(plainValue), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 20:45:55 +01:00
|
|
|
|
|
|
|
// Listing xattrs should work even when we don't have read access
|
|
|
|
func TestList0000File(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestList0000File"
|
|
|
|
err := ioutil.WriteFile(fn, nil, 0000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating empty file failed: %v", err)
|
|
|
|
}
|
|
|
|
_, err = xattr.LList(fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setting xattrs should work even when we don't have read access
|
|
|
|
func TestSet0200File(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestSet0200File"
|
|
|
|
err := ioutil.WriteFile(fn, nil, 0200)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating empty file failed: %v", err)
|
|
|
|
}
|
|
|
|
err = xattr.LSet(fn, "user.foo", []byte("bar"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 04:33:50 +01:00
|
|
|
|
|
|
|
// Listing xattrs should work even when we don't have read access
|
|
|
|
func TestList0000Dir(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestList0000Dir"
|
|
|
|
err := syscall.Mkdir(fn, 0000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating directory failed: %v", err)
|
|
|
|
}
|
|
|
|
_, err = xattr.LList(fn)
|
|
|
|
os.Chmod(fn, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setting xattrs should work even when we don't have read access
|
|
|
|
func TestSet0200Dir(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestSet0200Dir"
|
|
|
|
err := syscall.Mkdir(fn, 0200)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating directory failed: %v", err)
|
|
|
|
}
|
|
|
|
err = xattr.LSet(fn, "user.foo", []byte("bar"))
|
|
|
|
os.Chmod(fn, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2020-02-29 20:38:48 +01:00
|
|
|
|
|
|
|
func TestAcl(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestAcl"
|
|
|
|
err := ioutil.WriteFile(fn, nil, 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating empty file failed: %v", err)
|
|
|
|
}
|
|
|
|
// ACLs are blobs generated in userspace, let's steal a valid ACL from
|
2021-02-07 20:00:33 +01:00
|
|
|
// setfacl:
|
2020-02-29 20:38:48 +01:00
|
|
|
//
|
2021-02-07 20:00:33 +01:00
|
|
|
// $ setfacl -m u:root:r file
|
|
|
|
// $ getfattr -n system.posix_acl_access file
|
|
|
|
// # file: file
|
|
|
|
// system.posix_acl_access=0sAgAAAAEABgD/////AgAEAAAAAAAEAAQA/////xAABAD/////IAAEAP////8=
|
2020-02-29 20:38:48 +01:00
|
|
|
//
|
|
|
|
// The ACL gives user root additional read rights, in other words, it should
|
|
|
|
// have no effect at all.
|
2021-02-07 20:00:33 +01:00
|
|
|
|
|
|
|
acl, err := base64.StdEncoding.DecodeString("AgAAAAEABgD/////AgAEAAAAAAAEAAQA/////xAABAD/////IAAEAP////8=")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(acl) != 44 {
|
|
|
|
t.Fatal(len(acl))
|
|
|
|
}
|
|
|
|
err = setGetRmList3(fn, "system.posix_acl_access", acl)
|
2020-02-29 20:38:48 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|