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
|
// Copyright 2018 Google 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
//
// 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 linux
import (
"bytes"
"io"
"syscall"
"gvisor.googlesource.com/gvisor/pkg/binary"
"gvisor.googlesource.com/gvisor/pkg/sentry/arch"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
"gvisor.googlesource.com/gvisor/pkg/sentry/kernel"
"gvisor.googlesource.com/gvisor/pkg/sentry/kernel/kdefs"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
"gvisor.googlesource.com/gvisor/pkg/syserror"
)
// Getdents implements linux syscall getdents(2) for 64bit systems.
func Getdents(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
fd := kdefs.FD(args[0].Int())
addr := args[1].Pointer()
size := int(args[2].Uint())
minSize := int(smallestDirent(t.Arch()))
if size < minSize {
// size is smaller than smallest possible dirent.
return 0, nil, syserror.EINVAL
}
n, err := getdents(t, fd, addr, size, (*dirent).Serialize)
return n, nil, err
}
// Getdents64 implements linux syscall getdents64(2).
func Getdents64(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
fd := kdefs.FD(args[0].Int())
addr := args[1].Pointer()
size := int(args[2].Uint())
minSize := int(smallestDirent64(t.Arch()))
if size < minSize {
// size is smaller than smallest possible dirent.
return 0, nil, syserror.EINVAL
}
n, err := getdents(t, fd, addr, size, (*dirent).Serialize64)
return n, nil, err
}
// getdents implements the core of getdents(2)/getdents64(2).
// f is the syscall implementation dirent serialization function.
func getdents(t *kernel.Task, fd kdefs.FD, addr usermem.Addr, size int, f func(*dirent, io.Writer) (int, error)) (uintptr, error) {
dir := t.FDMap().GetFile(fd)
if dir == nil {
return 0, syserror.EBADF
}
defer dir.DecRef()
w := &usermem.IOReadWriter{
Ctx: t,
IO: t.MemoryManager(),
Addr: addr,
Opts: usermem.IOOpts{
AddressSpaceActive: true,
},
}
ds := newDirentSerializer(f, w, t.Arch(), size)
rerr := dir.Readdir(t, ds)
switch err := handleIOError(t, ds.Written() > 0, rerr, kernel.ERESTARTSYS, "getdents", dir); err {
case nil:
dir.Dirent.InotifyEvent(syscall.IN_ACCESS, 0)
return uintptr(ds.Written()), nil
case io.EOF:
return 0, nil
default:
return 0, err
}
}
// oldDirentHdr is a fixed sized header matching the fixed size
// fields found in the old linux dirent struct.
type oldDirentHdr struct {
Ino uint64
Off uint64
Reclen uint16
}
// direntHdr is a fixed sized header matching the fixed size
// fields found in the new linux dirent struct.
type direntHdr struct {
OldHdr oldDirentHdr
Typ uint8
}
// dirent contains the data pointed to by a new linux dirent struct.
type dirent struct {
Hdr direntHdr
Name []byte
}
// newDirent returns a dirent from an fs.InodeOperationsInfo.
func newDirent(width uint, name string, attr fs.DentAttr, offset uint64) *dirent {
d := &dirent{
Hdr: direntHdr{
OldHdr: oldDirentHdr{
Ino: attr.InodeID,
Off: offset,
},
Typ: toType(attr.Type),
},
Name: []byte(name),
}
d.Hdr.OldHdr.Reclen = d.padRec(int(width))
return d
}
// smallestDirent returns the size of the smallest possible dirent using
// the old linux dirent format.
func smallestDirent(a arch.Context) uint {
d := dirent{}
return uint(binary.Size(d.Hdr.OldHdr)) + a.Width() + 1
}
// smallestDirent64 returns the size of the smallest possible dirent using
// the new linux dirent format.
func smallestDirent64(a arch.Context) uint {
d := dirent{}
return uint(binary.Size(d.Hdr)) + a.Width()
}
// toType converts an fs.InodeOperationsInfo to a linux dirent typ field.
func toType(nodeType fs.InodeType) uint8 {
switch nodeType {
case fs.RegularFile, fs.SpecialFile:
return syscall.DT_REG
case fs.Symlink:
return syscall.DT_LNK
case fs.Directory:
return syscall.DT_DIR
case fs.Pipe:
return syscall.DT_FIFO
case fs.CharacterDevice:
return syscall.DT_CHR
case fs.BlockDevice:
return syscall.DT_BLK
case fs.Socket:
return syscall.DT_SOCK
default:
return syscall.DT_UNKNOWN
}
}
// padRec pads the name field until the rec length is a multiple of the width,
// which must be a power of 2. It returns the padded rec length.
func (d *dirent) padRec(width int) uint16 {
a := int(binary.Size(d.Hdr)) + len(d.Name)
r := (a + width) &^ (width - 1)
padding := r - a
d.Name = append(d.Name, make([]byte, padding)...)
return uint16(r)
}
// Serialize64 serializes a Dirent struct to a byte slice, keeping the new
// linux dirent format. Returns the number of bytes serialized or an error.
func (d *dirent) Serialize64(w io.Writer) (int, error) {
n1, err := w.Write(binary.Marshal(nil, usermem.ByteOrder, d.Hdr))
if err != nil {
return 0, err
}
n2, err := w.Write(d.Name)
if err != nil {
return 0, err
}
return n1 + n2, nil
}
// Serialize serializes a Dirent struct to a byte slice, using the old linux
// dirent format.
// Returns the number of bytes serialized or an error.
func (d *dirent) Serialize(w io.Writer) (int, error) {
n1, err := w.Write(binary.Marshal(nil, usermem.ByteOrder, d.Hdr.OldHdr))
if err != nil {
return 0, err
}
n2, err := w.Write(d.Name)
if err != nil {
return 0, err
}
n3, err := w.Write([]byte{d.Hdr.Typ})
if err != nil {
return 0, err
}
return n1 + n2 + n3, nil
}
// direntSerializer implements fs.InodeOperationsInfoSerializer, serializing dirents to an
// io.Writer.
type direntSerializer struct {
serialize func(*dirent, io.Writer) (int, error)
w io.Writer
// width is the arch native value width.
width uint
// offset is the current dirent offset.
offset uint64
// written is the total bytes serialized.
written int
// size is the size of the buffer to serialize into.
size int
}
func newDirentSerializer(f func(d *dirent, w io.Writer) (int, error), w io.Writer, ac arch.Context, size int) *direntSerializer {
return &direntSerializer{
serialize: f,
w: w,
width: ac.Width(),
size: size,
}
}
// CopyOut implements fs.InodeOperationsInfoSerializer.CopyOut.
// It serializes and writes the fs.DentAttr to the direntSerializer io.Writer.
func (ds *direntSerializer) CopyOut(name string, attr fs.DentAttr) error {
ds.offset++
d := newDirent(ds.width, name, attr, ds.offset)
// Serialize dirent into a temp buffer.
var b bytes.Buffer
n, err := ds.serialize(d, &b)
if err != nil {
ds.offset--
return err
}
// Check that we have enough room remaining to write the dirent.
if n > (ds.size - ds.written) {
ds.offset--
return io.EOF
}
// Write out the temp buffer.
if _, err := b.WriteTo(ds.w); err != nil {
ds.offset--
return err
}
ds.written += n
return nil
}
// Written returns the total number of bytes written.
func (ds *direntSerializer) Written() int {
return ds.written
}
|