1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
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.
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strings"
"syscall"
"testing"
"github.com/pkg/xattr"
"github.com/rfjakob/gocryptfs/v2/internal/cryptocore"
"github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
)
func TestMain(m *testing.M) {
if !xattrSupported(test_helpers.TmpDir) {
fmt.Printf("xattrs not supported on %q\n", test_helpers.TmpDir)
os.Exit(1)
}
test_helpers.ResetTmpDir(true)
// 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)
}
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)
}
func setGetRmList(fn string) error {
return setGetRmList3(fn, "user.foo", []byte("123456789"))
}
func setGetRmList3(fn string, attr string, val []byte) error {
// 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)
}
err = xattr.LSet(fn, attr, val)
if err != nil {
return err
}
// Read back
val2, err := xattr.LGet(fn, attr)
if err != nil {
return err
}
if !bytes.Equal(val, val2) {
return fmt.Errorf("wrong readback value: %v != %v", val, val2)
}
// Remove
err = xattr.LRemove(fn, attr)
if err != nil {
return err
}
// Read back
val3, err := xattr.LGet(fn, attr)
if err == nil {
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"
err := ioutil.WriteFile(fn, []byte("12345"), 0700)
if err != nil {
t.Fatalf("creating empty file failed: %v", err)
}
err = setGetRmList(fn)
if err != nil {
t.Error(err)
}
fi, _ := os.Lstat(fn)
if fi.Size() != 5 {
t.Errorf("file size has changed!? size=%d", fi.Size())
}
}
// 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)
}
// We expect to get EPERM, but we should not hang:
// $ setfattr -n user.foo -v XXXXX fifo
// setfattr: fifo: Operation not permitted
setGetRmList(fn)
}
// 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 {
t.Fatalf("creating directory failed: %v", err)
}
setGetRmList(fn)
}
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
_, err = xattr.LGet(fn, attr)
if err == nil {
t.Fatal("we should have got an error here")
}
// Set empty value
err = xattr.LSet(fn, attr, nil)
if err != nil {
t.Fatal(err)
}
// Read back
val, err := xattr.LGet(fn, attr)
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")
err = xattr.LSet(fn, attr, val1)
if err != nil {
t.Fatal(err)
}
// Read back
val2, err := xattr.LGet(fn, attr)
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
err = xattr.LSet(fn, attr, nil)
if err != nil {
t.Fatal(err)
}
// Read back
val, err = xattr.LGet(fn, attr)
if err != nil {
t.Fatal(err)
}
if len(val) != 0 {
t.Errorf("wrong length: want=0 have=%d", len(val2))
}
}
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)
err = xattr.LSet(fn, attr, val)
if err != nil {
t.Fatal(err)
}
}
names, err := xattr.LList(fn)
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)
}
}
}
func xattrSupported(path string) bool {
_, err := xattr.LGet(path, "user.xattrSupported-dummy-value")
if err == nil {
return true
}
err2 := err.(*xattr.Error)
if err2.Err == syscall.EOPNOTSUPP {
return false
}
return true
}
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)
}
xattr.LSet(plainFn, attrName, []byte(attrValue))
encryptedAttrValue, err1 := xattr.LGet(encryptedFn, encryptedAttrName)
if err1 != nil {
t.Fatal(err1)
}
xattr.LSet(encryptedFn, encryptedAttrName2, encryptedAttrValue)
plainValue, err := xattr.LGet(plainFn, attrName2)
if err != nil || string(plainValue) != attrValue {
t.Fatalf("Attribute binary value decryption error: have=%q want=%q err=%v", string(plainValue), attrValue, err)
}
encryptedAttrValue64 := base64.RawURLEncoding.EncodeToString(encryptedAttrValue)
xattr.LSet(encryptedFn, encryptedAttrName2, []byte(encryptedAttrValue64))
plainValue, err = xattr.LGet(plainFn, attrName2)
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 {
xattr.LSet(encryptedFn, encryptedAttrName2, []byte(val))
plainValue, err = xattr.LGet(plainFn, attrName2)
err2, _ := err.(*xattr.Error)
if err == nil || err2.Err != syscall.EIO {
t.Fatalf("Incorrect handling of broken data %s %v", string(plainValue), err)
}
}
}
// 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)
}
}
// 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)
}
}
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
// setfacl:
//
// $ setfacl -m u:root:r file
// $ getfattr -n system.posix_acl_access file
// # file: file
// system.posix_acl_access=0sAgAAAAEABgD/////AgAEAAAAAAAEAAQA/////xAABAD/////IAAEAP////8=
//
// The ACL gives user root additional read rights, in other words, it should
// have no effect at all.
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)
if err != nil {
t.Error(err)
}
}
|