This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
chroot.go
134 lines (117 loc) · 3.88 KB
/
chroot.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
// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tar
import (
"archive/tar"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"syscall"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/pkg/multicall"
"github.com/rkt/rkt/pkg/sys"
"github.com/rkt/rkt/pkg/user"
)
const (
multicallName = "extracttar"
fileMapFdNum = 3
)
var mcEntrypoint multicall.Entrypoint
func init() {
mcEntrypoint = multicall.Add(multicallName, extractTarCommand)
}
// Because this function is executed by multicall in a different process, it is not possible to use errwrap to return errors
func extractTarCommand() error {
if len(os.Args) != 5 {
return fmt.Errorf("incorrect number of arguments. Usage: %s DIR {true|false} uidShift uidCount", multicallName)
}
if !sys.HasChrootCapability() {
return fmt.Errorf("chroot capability not available.")
}
dir := os.Args[1]
if !filepath.IsAbs(dir) {
return fmt.Errorf("dir %s must be an absolute path", dir)
}
overwrite, err := strconv.ParseBool(os.Args[2])
if err != nil {
return fmt.Errorf("error parsing overwrite argument: %v", err)
}
us, err := strconv.ParseUint(os.Args[3], 10, 32)
if err != nil {
return fmt.Errorf("error parsing uidShift argument: %v", err)
}
uc, err := strconv.ParseUint(os.Args[4], 10, 32)
if err != nil {
return fmt.Errorf("error parsing uidCount argument: %v", err)
}
uidRange := &user.UidRange{Shift: uint32(us), Count: uint32(uc)}
if err := syscall.Chroot(dir); err != nil {
return fmt.Errorf("failed to chroot in %s: %v", dir, err)
}
if err := syscall.Chdir("/"); err != nil {
return fmt.Errorf("failed to chdir: %v", err)
}
fileMapFile := os.NewFile(uintptr(fileMapFdNum), "fileMap")
fileMap := map[string]struct{}{}
if err := json.NewDecoder(fileMapFile).Decode(&fileMap); err != nil {
return fmt.Errorf("error decoding fileMap: %v", err)
}
editor, err := NewUidShiftingFilePermEditor(uidRange)
if err != nil {
return fmt.Errorf("error determining current user: %v", err)
}
if err := ExtractTarInsecure(tar.NewReader(os.Stdin), "/", overwrite, fileMap, editor); err != nil {
return fmt.Errorf("error extracting tar: %v", err)
}
// flush remaining bytes
io.Copy(ioutil.Discard, os.Stdin)
return nil
}
// ExtractTar extracts a tarball (from a io.Reader) into the given directory
// if pwl is not nil, only the paths in the map are extracted.
// If overwrite is true, existing files will be overwritten.
// The extraction is executed by fork/exec()ing a new process. The new process
// needs the CAP_SYS_CHROOT capability.
func ExtractTar(rs io.Reader, dir string, overwrite bool, uidRange *user.UidRange, pwl PathWhitelistMap) error {
r, w, err := os.Pipe()
if err != nil {
return err
}
defer w.Close()
enc := json.NewEncoder(w)
cmd := mcEntrypoint.Cmd(dir, strconv.FormatBool(overwrite),
strconv.FormatUint(uint64(uidRange.Shift), 10),
strconv.FormatUint(uint64(uidRange.Count), 10))
cmd.ExtraFiles = []*os.File{r}
cmd.Stdin = rs
encodeCh := make(chan error)
go func() {
encodeCh <- enc.Encode(pwl)
}()
out, err := cmd.CombinedOutput()
// read from blocking encodeCh to release the goroutine
encodeErr := <-encodeCh
if err != nil {
return fmt.Errorf("extracttar error: %v, output: %s", err, out)
}
if encodeErr != nil {
return errwrap.Wrap(errors.New("extracttar failed to json encode filemap"), encodeErr)
}
return nil
}