-
Notifications
You must be signed in to change notification settings - Fork 10
/
file_utils.go
450 lines (344 loc) · 9.97 KB
/
file_utils.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Package fileutils contains utilities and helpers to manage and manipulate files.
package fileutils
import (
"crypto/sha256"
"fmt"
"io"
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/89luca89/lilipod/pkg/constants"
"github.com/89luca89/lilipod/pkg/logging"
"github.com/89luca89/lilipod/pkg/procutils"
)
// eadFile will return the content of input file or error.
// This is a linux-only implementation using syscalls for performance benefits.
func ReadFile(path string) ([]byte, error) {
var stat syscall.Stat_t
// ensure that file exists
err := syscall.Stat(path, &stat)
if err != nil {
return nil, err
}
// and that I can open it
fd, err := syscall.Open(path, syscall.O_RDONLY, uint32(os.ModePerm))
if err != nil {
logging.LogDebug("%v", err)
return nil, err
}
defer func() { _ = syscall.Close(fd) }()
fileLenght := 10000
if stat.Size > 0 {
fileLenght = int(stat.Size)
}
filedata := make([]byte, fileLenght)
_, err = syscall.Read(fd, filedata)
if err != nil {
logging.LogError("%v", err)
return nil, err
}
return filedata, nil
}
// WriteFile will write the content in input to file in path or error.
// This is a linux-only implementation using syscalls for performance benefits.
func WriteFile(path string, content []byte, perm uint32) error {
var fd int
var stat syscall.Stat_t
// ensure that file exists
err := syscall.Stat(path, &stat)
if err != nil {
fd, err = syscall.Creat(path, perm)
if err != nil {
logging.LogError("%v", err)
return err
}
} else {
fd, err = syscall.Open(path, syscall.O_RDWR, perm)
if err != nil {
logging.LogError("%v", err)
return err
}
err = syscall.Chmod(path, perm)
if err != nil {
logging.LogError("%v", err)
return err
}
}
defer func() { _ = syscall.Close(fd) }()
_, err = syscall.Write(fd, content)
return err
}
// GetFileDigest will return the sha256sum of input file. Empty if error occurs.
func GetFileDigest(path string) string {
file, err := os.Open(path)
if err != nil {
return ""
}
defer func() { _ = file.Close() }()
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
return ""
}
return fmt.Sprintf("%x", hasher.Sum(nil))
}
// CheckFileDigest will compare input digest to the checksum of input file.
// Returns whether the input digest is equal to the input file's one.
func CheckFileDigest(path string, digest string) bool {
checksum := GetFileDigest(path)
logging.LogDebug("input checksum is: %s", "sha256:"+checksum)
logging.LogDebug("expected checksum is: %s", digest)
return "sha256:"+checksum == digest
}
// Exist returns if a path exists or not.
func Exist(path string) bool {
var stat syscall.Stat_t
err := syscall.Stat(path, &stat)
return err == nil
}
// CopyFileContainer will copy file from src to dest.
func CopyFileContainer(src string, dest string) error {
input, err := ReadFile(src)
if err != nil {
logging.LogError("%v", err)
return err
}
fileinfo, err := os.Stat(src)
if err != nil {
logging.LogError("%v", err)
return err
}
destinfo, err := os.Stat(dest)
if err != nil {
logging.LogDebug("%v", err)
_, err = os.Create(dest)
if err != nil {
logging.LogError("%v", err)
return err
}
destinfo, err = os.Stat(dest)
if err != nil {
logging.LogError("%v", err)
return err
}
}
if destinfo.IsDir() {
dest = filepath.Join(dest, filepath.Base(src))
}
err = WriteFile(dest, input, uint32(fileinfo.Mode()))
if err != nil {
logging.LogError("%v", err)
return err
}
return nil
}
// DiscUsageMegaBytes returns disk usage for input path in MB (rounded).
func DiscUsageMegaBytes(path string) (string, error) {
var discUsage int64
readSize := func(path string, file os.FileInfo, err error) error {
if !file.IsDir() {
discUsage += file.Size()
}
return nil
}
err := filepath.Walk(path, readSize)
if err != nil {
logging.LogError("%v", err)
return "", err
}
size := math.Round(float64(discUsage) / 1024.0 / 1024.0)
return fmt.Sprintf("%.2f MB", size), nil
}
// Umount will force umount a destination path.
func Umount(dest string) error {
for {
if !ismountpoint(dest) {
logging.LogDebug("%s not a mountpoint", dest)
break
}
err := syscall.Unmount(dest, 0)
if err != nil {
logging.LogError("%v", err)
return err
}
time.Sleep(time.Millisecond * 500)
}
return nil
}
// ismountpoint will return whether the input path is a mounpoint or not.
// This function will parse the /proc/mounts file and search for input path.
func ismountpoint(path string) bool {
mounts, err := ReadFile("/proc/mounts")
if err != nil {
return false
}
lines := strings.Split(string(mounts), "\n")
for _, line := range lines {
if len(strings.Split(line, " ")) > 1 {
mountpoint := strings.Split(line, " ")[1]
if mountpoint == path {
return true
}
}
}
return false
}
// Mount will bind-mount src to dest, using input mode.
func Mount(src, dest string, mode uintptr) error {
logging.LogDebug("ensuring source point %s exists", src)
info, err := os.Stat(src)
if err != nil {
return err
}
logging.LogDebug("ensuring destination point %s exists", dest)
if info.IsDir() {
_ = os.MkdirAll(dest, 0o755)
} else {
file, _ := os.Create(dest)
defer func() { _ = file.Close() }()
}
logging.LogDebug("mounting %s on %s as bind, with mode %s", src, dest, mode)
return syscall.Mount(src,
dest,
"bind",
mode,
"")
}
// MountCgroup will mount a new cgroup/cgroup2 fs on dest.
func MountCgroup(dest string) error {
logging.LogDebug("ensuring destination point %s exists", dest)
return syscall.Mount("cgroup2",
dest,
"cgroup2",
0,
"")
}
// MountShm will mount a new shm tmpfs to dest path.
// Said mount will be created with mode: noexec,nosuid,nodev,mode=1777,size=65536k.
func MountShm(dest string) error {
logging.LogDebug("ensuring destination point %s exists", dest)
_ = os.MkdirAll(dest, 0o777)
logging.LogDebug(
"mounting new shm on %s, with mode noexec,nosuid,nodev,mode=1777,size=65536k",
dest,
)
return syscall.Mount("shm",
dest,
"tmpfs",
syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV,
"mode=1777,size=65536k")
}
// MountMqueue will mount a new mqueue tmpfs in dest path.
// Said mount will be created with mode: noexec,nosuid,nodev.
func MountMqueue(dest string) error {
logging.LogDebug("ensuring destination point %s exists", dest)
_ = os.MkdirAll(dest, 0o777)
logging.LogDebug("mounting new mqueue on %s, with mode noexec,nosuid,nodev", dest)
return syscall.Mount("mqueue",
dest,
"mqueue",
syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV,
"")
}
// MountTmpfs will mount a new tmpfs in dest path.
func MountTmpfs(dest string) error {
logging.LogDebug("ensuring destination point %s exists", dest)
_ = os.MkdirAll(dest, 0o777)
logging.LogDebug("mounting new tmpfs on %s", dest)
return syscall.Mount("tmpfs",
dest,
"tmpfs",
uintptr(0),
"")
}
// MountProc will mount a new procfs in dest path.
// Said mount will be created with mode: noexec,nosuid,nodev.
func MountProc(dest string) error {
logging.LogDebug("ensuring destination point %s exists", dest)
_ = os.MkdirAll(dest, 0o755)
return syscall.Mount("proc",
dest,
"proc",
syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV,
"")
}
// MountDevPts will mount a new devpts in dest path.
// Said mount will be created with mode: noexec,nosuid,newinstance,ptmxmode=0666,mode=0620.
func MountDevPts(dest string) error {
logging.LogDebug("ensuring destination point %s exists", dest)
_ = os.MkdirAll(dest, 0o755)
logging.LogDebug(
"mounting new devpts on %s with mode noexec,nosuid,newinstance,ptmxmode=0666,mode=0620",
dest,
)
return syscall.Mount("devpts",
dest,
"devpts",
syscall.MS_NOEXEC|syscall.MS_NOSUID,
"newinstance,ptmxmode=0666,mode=0620")
}
// MountBind will bind-mount src path in dest path.
// Said mount will be created with mode: rbind,rprivate.
func MountBind(src, dest string) error {
logging.LogDebug("performing bind mount %s %s", src, dest)
return Mount(src, dest, syscall.MS_BIND|syscall.MS_REC|syscall.MS_PRIVATE)
}
// MountBindRO will bind-mount read-only src path in dest path.
// Said mount will be created with mode: rbind,rprivate,ro,nosuid,noexec,nodev.
func MountBindRO(src, dest string) error {
logging.LogDebug("performing read-only bind mount %s %s", src, dest)
return Mount(src,
dest,
syscall.MS_BIND|syscall.MS_REC|syscall.MS_RDONLY|syscall.MS_NOSUID|
syscall.MS_NOEXEC|syscall.MS_NODEV|syscall.MS_PRIVATE)
}
// UntarFile will untar target file to target directory.
// If userns is specified and it is keep-id, it will perform the
// untarring in a new user namespace with user id maps set, in order to prevent
// permission errors.
func UntarFile(path string, target string, userns string) error {
// first ensure we can write
err := syscall.Access(path, 2)
if err != nil {
logging.LogError("%v", err)
return err
}
if userns != constants.KeepID {
cmd := exec.Command("tar", "--exclude=dev/*", "-xf", path, "-C", target)
logging.LogDebug("no keep-id specified, simply perform %v", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%w: %s", err, string(out))
}
return nil
}
command := "/bin/sh"
args := []string{
"-c",
"mkdir -p " + target + " &&" +
"chown -R root:root " + target + " &&" +
"tar --exclude=dev/* -xf " + path + " -C " + target,
}
cmd := exec.Command(command, args...)
// we need to unpack using keep-id in order to keep consistency
cloneFlags := syscall.CLONE_NEWUTS | syscall.CLONE_NEWNS
cloneFlags |= syscall.CLONE_NEWUSER
cmd.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{Uid: 0, Gid: 0},
Cloneflags: uintptr(cloneFlags),
GidMappingsEnableSetgroups: true,
Pdeathsig: syscall.SIGTERM,
}
uid := os.Getenv("PARENT_UID_MAP")
gid := os.Getenv("PARENT_GID_MAP")
logging.LogDebug("setting up keep-id %s, %s", uid, gid)
err = procutils.SetProcessKeepIDMaps(cmd, uid, gid)
if err != nil {
logging.LogError("%v", err)
return err
}
return cmd.Run()
}