forked from nestybox/sysbox-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidShiftUtils.go
279 lines (221 loc) · 6.57 KB
/
idShiftUtils.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
//
// Copyright 2019-2021 Nestybox, Inc.
//
// 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
//
// https://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.
//
// Utilities for shifting user and group IDs on the file system using chown
// (e.g., shifting uids:gids from range [0:65536] to range [165536:231071]).
package idShiftUtils
import (
"fmt"
"os"
"strconv"
"syscall"
"github.com/joshlf/go-acl"
aclLib "github.com/joshlf/go-acl"
"github.com/karrick/godirwalk"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
mapset "github.com/deckarep/golang-set"
)
type IDShiftType int
const (
NoShift IDShiftType = iota
Shiftfs
IDMappedMount
IDMappedMountOrShiftfs
Chown
)
type aclType int
const (
aclTypeAccess aclType = iota
aclTypeDefault
)
type IDMapping struct {
ContainerID uint32
HostID uint32
Size uint32
}
// Shifts the ACL type user and group IDs by the given offset
func shiftAclType(aclT aclType, path string, uidOffset, gidOffset int32) error {
var facl aclLib.ACL
var err error
// Read the ACL
if aclT == aclTypeDefault {
facl, err = acl.GetDefault(path)
} else {
facl, err = acl.Get(path)
}
if err != nil {
return fmt.Errorf("failed to get ACL for %s: %s", path, err)
}
// Shift the user and group ACLs (if any)
newACL := aclLib.ACL{}
aclShifted := false
for _, e := range facl {
// ACL_USER id shifting
if e.Tag == aclLib.TagUser {
uid, err := strconv.ParseUint(e.Qualifier, 10, 32)
if err != nil {
logrus.Warnf("failed to convert ACL qualifier for %v: %s", e, err)
continue
}
targetUid := uint64(int32(uid) + uidOffset)
e.Qualifier = strconv.FormatUint(targetUid, 10)
aclShifted = true
}
// ACL_GROUP id shifting
if e.Tag == aclLib.TagGroup {
gid, err := strconv.ParseUint(e.Qualifier, 10, 32)
if err != nil {
logrus.Warnf("failed to convert ACL qualifier %v: %s", e, err)
continue
}
targetGid := uint64(int32(gid) + gidOffset)
e.Qualifier = strconv.FormatUint(targetGid, 10)
aclShifted = true
}
newACL = append(newACL, e)
}
// Write back the modified ACL
if aclShifted {
if aclT == aclTypeDefault {
err = acl.SetDefault(path, newACL)
} else {
err = acl.Set(path, newACL)
}
if err != nil {
return fmt.Errorf("failed to set ACL %v for %s: %s", newACL, path, err)
}
}
return nil
}
// Shifts the ACL user and group IDs by the given offset, both for access and default ACLs
func shiftAclIds(path string, isDir bool, uidOffset, gidOffset int32) error {
// Access list
err := shiftAclType(aclTypeAccess, path, uidOffset, gidOffset)
if err != nil {
return err
}
// Default list (for directories only)
if isDir {
err = shiftAclType(aclTypeDefault, path, uidOffset, gidOffset)
if err != nil {
return err
}
}
return nil
}
// "Shifts" ownership of user and group IDs on the given directory and files and directories
// below it by the given offset, using chown.
func ShiftIdsWithChown(baseDir string, uidOffset, gidOffset int32) error {
hardLinks := []uint64{}
err := godirwalk.Walk(baseDir, &godirwalk.Options{
Callback: func(path string, de *godirwalk.Dirent) error {
// When doing the chown, we don't follow symlinks as we want to change
// the ownership of the symlinks themselves. We will chown the
// symlink's target during the godirwalk (unless the symlink is
// dangling in which case there is nothing to be done).
fi, err := os.Lstat(path)
if err != nil {
return err
}
st, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("failed to convert to syscall.Stat_t")
}
// If a file has multiple hardlinks, change its ownership once
if st.Nlink >= 2 {
for _, linkInode := range hardLinks {
if linkInode == st.Ino {
return nil
}
}
hardLinks = append(hardLinks, st.Ino)
}
targetUid := int32(st.Uid) + uidOffset
targetGid := int32(st.Gid) + gidOffset
err = unix.Lchown(path, int(targetUid), int(targetGid))
if err != nil {
return fmt.Errorf("chown %s to %d:%d failed: %s", path, targetUid, targetGid, err)
}
// chown will turn-off the set-user-ID and set-group-ID bits on files,
// so we need to restore them.
fMode := fi.Mode()
setuid := fMode&os.ModeSetuid == os.ModeSetuid
setgid := fMode&os.ModeSetgid == os.ModeSetgid
if fMode.IsRegular() && (setuid || setgid) {
if err := os.Chmod(path, fMode); err != nil {
return fmt.Errorf("chmod %s to %s failed: %s", path, fMode, err)
}
}
// Chowning the file is not sufficient; we also need to shift user and group IDs in
// the Linux access control list (ACL) for the file
if fMode&os.ModeSymlink == 0 {
if err := shiftAclIds(path, fi.IsDir(), uidOffset, gidOffset); err != nil {
return fmt.Errorf("failed to shift ACL for %s: %s", path, err)
}
}
return nil
},
ErrorCallback: func(path string, err error) godirwalk.ErrorAction {
fi, err := os.Lstat(path)
if err != nil {
return godirwalk.Halt
}
// Ignore errors due to chown on dangling symlinks (they often occur in container image layers)
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
return godirwalk.SkipNode
}
return godirwalk.Halt
},
Unsorted: true, // Speeds up the directory tree walk
})
return err
}
// Returns the lists of user and group IDs for all files and directories at or
// below the given path.
func GetDirIDs(baseDir string) ([]uint32, []uint32, error) {
uidSet := mapset.NewSet()
gidSet := mapset.NewSet()
err := godirwalk.Walk(baseDir, &godirwalk.Options{
Callback: func(path string, de *godirwalk.Dirent) error {
fi, err := os.Lstat(path)
if err != nil {
return err
}
st, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("failed to convert to syscall.Stat_t")
}
uidSet.Add(st.Uid)
gidSet.Add(st.Gid)
return nil
},
Unsorted: true, // Speeds up the directory tree walk
})
if err != nil {
return nil, nil, err
}
uidList := []uint32{}
for _, id := range uidSet.ToSlice() {
val := id.(uint32)
uidList = append(uidList, val)
}
gidList := []uint32{}
for _, id := range gidSet.ToSlice() {
val := id.(uint32)
gidList = append(gidList, val)
}
return uidList, gidList, nil
}