-
Notifications
You must be signed in to change notification settings - Fork 2
/
ffsworm.go
327 lines (264 loc) · 6.32 KB
/
ffsworm.go
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
/* TACIXAT 2020 */
package main
import (
"bazil.org/fuse"
"bazil.org/fuse/fs"
"context"
"encoding/json"
"fmt"
"os"
"sync"
"syscall"
)
// Write once read many file-directory
type FFSWorm struct {
Name string
Written bool
Data [][]byte
Current uint
Index uint64
Interfaces map[string]*FFSInterface
Children map[string]*FFSFile
NextChild uint
Mapping map[int]int
Strategies map[string]Mutator
Mutex *sync.Mutex
}
func NewFFSWorm(name string) *FFSWorm {
ffsw := &FFSWorm{
Name: name,
Written: false,
Data: make([][]byte, 1),
Current: 0,
Index: lidx.Next(),
Interfaces: make(map[string]*FFSInterface),
Children: make(map[string]*FFSFile),
Mapping: make(map[int]int),
Strategies: NewStrats(),
Mutex: new(sync.Mutex),
}
ffsw.Children["0"] = NewFFSFile("0", ffsw)
ffsw.NextChild = 1
ffsw.Interfaces["mutate"] = MutationInterface(ffsw)
ffsw.Interfaces["mask"] = MaskInterface(ffsw)
return ffsw
}
// /mutate
func MutationInterface(ffsw *FFSWorm) *FFSInterface {
ffsiMutate := NewFFSInterface("mutate")
ffsiMutate.AttrHandler = func(a *fuse.Attr) error {
a.Valid = 0
a.Inode = ffsiMutate.Index
a.Mode = 0o444
return nil
}
ffsiMutate.SetAttrHandler = func(req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
if req.Valid.MtimeNow() {
ffsw.Mutex.Lock()
defer ffsw.Mutex.Unlock()
return ffsw.Mutate()
}
return nil
}
return ffsiMutate
}
// /mask
type Range struct {
Offset int `json:"offset"`
Size int `json:"size"`
}
type Mask struct {
Include bool `json:"include"`
Ranges []Range `json:"ranges"`
}
func (ffsw *FFSWorm) include(ranges []Range) error {
old := ffsw.Mapping
ffsw.Mapping = make(map[int]int)
mapping := 0
for _, r := range ranges {
offset := r.Offset
end := offset + r.Size
if end > len(ffsw.Data[ffsw.Current]) {
end = len(ffsw.Data[ffsw.Current])
}
for i := offset; i < end; i++ {
if _, ok := ffsw.Mapping[mapping]; ok {
ffsw.Mapping = old
return syscall.EPERM
}
ffsw.Mapping[mapping] = i
mapping++
}
}
return nil
}
func (ffsw *FFSWorm) exclude(ranges []Range) error {
old := ffsw.Mapping
ffsw.Mapping = make(map[int]int)
mapping := 0
// space before
start := 0
for _, r := range ranges {
for i := start; i < r.Offset; i++ {
ffsw.Mapping[mapping] = i
mapping++
}
start = r.Offset + r.Size
}
// space after
if start < len(ffsw.Data[ffsw.Current]) {
for i := start; i < len(ffsw.Data[ffsw.Current]); i++ {
if _, ok := ffsw.Mapping[mapping]; ok {
ffsw.Mapping = old
return syscall.EPERM
}
ffsw.Mapping[mapping] = i
mapping++
}
}
return nil
}
func MaskInterface(ffsw *FFSWorm) *FFSInterface {
ffsiMask := NewFFSInterface("mask")
ffsiMask.AttrHandler = func(a *fuse.Attr) error {
a.Valid = 0
a.Inode = ffsiMask.Index
a.Mode = 0o644
// out, _ := getMask()
// a.Size = uint64(len(out))
a.Size = 0
return nil
}
ffsiMask.WriteHandler = func(req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
ffsw.Mutex.Lock()
defer ffsw.Mutex.Unlock()
m := Mask{}
err := json.Unmarshal(req.Data, &m)
if err != nil {
return syscall.EPERM
}
resp.Size = len(req.Data)
if len(m.Ranges) == 0 {
ffsw.Mapping = make(map[int]int)
return nil
}
if m.Include {
return ffsw.include(m.Ranges)
} else {
return ffsw.exclude(m.Ranges)
}
return nil
}
ffsiMask.SetAttrHandler = func(req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
return nil
}
return ffsiMask
}
// Caller must lock
func (ffsw *FFSWorm) Mutate() error {
data := ffsw.Data[ffsw.Current]
end := ffsw.NextChild + *batchSize
names := make([]string, 0)
var err error
for i := ffsw.NextChild; i < end; i++ {
name := fmt.Sprintf("%d", i)
err := ffsw.Strategies["bit_flip"].Generate(data, ffsw.Mapping, name)
if err != nil {
break
}
ffsw.Children[name] = NewFFSFile(name, ffsw)
names = append(names, name)
}
if err != nil {
for _, name := range names {
delete(ffsw.Children, name)
}
return err
}
ffsw.NextChild = end
return nil
}
func (ffsw *FFSWorm) Attr(ctx context.Context, a *fuse.Attr) error {
ffsw.Mutex.Lock()
defer ffsw.Mutex.Unlock()
a.Valid = 0
a.Inode = ffsw.Index
if ffsw.Written {
a.Mode = os.ModeDir | 0o644
a.Size = 0
} else {
a.Mode = 0o644
a.Size = uint64(len(ffsw.Data[ffsw.Current]))
}
return nil
}
func (ffsw *FFSWorm) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
ffsw.Mutex.Lock()
defer ffsw.Mutex.Unlock()
return nil, nil, syscall.EPERM
}
func (ffsw *FFSWorm) Flush(ctx context.Context, req *fuse.FlushRequest) error {
ffsw.Mutex.Lock()
defer ffsw.Mutex.Unlock()
if len(ffsw.Data[0]) > 0 {
ffsw.Written = true
}
return nil
}
func (ffsw *FFSWorm) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
return nil
}
func (ffsw *FFSWorm) Lookup(ctx context.Context, name string) (fs.Node, error) {
ffsw.Mutex.Lock()
defer ffsw.Mutex.Unlock()
if f, ok := ffsw.Children[name]; ok {
return f, nil
}
if f, ok := ffsw.Interfaces[name]; ok {
return f, nil
}
return nil, syscall.ENOENT
}
func (ffsw *FFSWorm) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
ffsw.Mutex.Lock()
defer ffsw.Mutex.Unlock()
l := make([]fuse.Dirent, 0)
for n, i := range ffsw.Interfaces {
l = append(l, fuse.Dirent{i.Index, fuse.DT_File, n})
}
for n, c := range ffsw.Children {
l = append(l, fuse.Dirent{c.Index, fuse.DT_File, n})
}
return l, nil
}
func (ffsw *FFSWorm) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
if req.Name == "0" {
return syscall.EPERM
}
if _, ok := ffsw.Children[req.Name]; ok {
delete(ffsw.Children, req.Name)
ffsw.Strategies["bit_flip"].Remove(req.Name)
return nil
}
if _, ok := ffsw.Interfaces[req.Name]; ok {
return syscall.EPERM
}
return syscall.ENOENT
}
func (ffsw *FFSWorm) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
ffsw.Mutex.Lock()
defer ffsw.Mutex.Unlock()
if ffsw.Written {
return syscall.EPERM
}
end := req.Offset + int64(len(req.Data))
if int64(len(ffsw.Data[ffsw.Current])) < end {
n := make([]byte, end)
copy(n, ffsw.Data[ffsw.Current])
ffsw.Data[ffsw.Current] = n
}
start := req.Offset
copy(ffsw.Data[ffsw.Current][start:end], req.Data)
resp.Size = len(req.Data)
return nil
}