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
|
// Copyright 2018 Google LLC
//
// 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 binder implements Android Binder IPC module.
package binder
import (
"sync"
"gvisor.googlesource.com/gvisor/pkg/abi/linux"
"gvisor.googlesource.com/gvisor/pkg/sentry/arch"
"gvisor.googlesource.com/gvisor/pkg/sentry/context"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs/fsutil"
"gvisor.googlesource.com/gvisor/pkg/sentry/kernel"
"gvisor.googlesource.com/gvisor/pkg/sentry/memmap"
"gvisor.googlesource.com/gvisor/pkg/sentry/platform"
"gvisor.googlesource.com/gvisor/pkg/sentry/usage"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
"gvisor.googlesource.com/gvisor/pkg/syserror"
"gvisor.googlesource.com/gvisor/pkg/waiter"
)
const (
currentProtocolVersion = 8
// mmapSizeLimit is the upper limit for mapped memory size in Binder.
mmapSizeLimit = 4 * 1024 * 1024 // 4MB
)
// Device implements fs.InodeOperations.
//
// +stateify savable
type Device struct {
fsutil.InodeGenericChecker `state:"nosave"`
fsutil.InodeNoExtendedAttributes `state:"nosave"`
fsutil.InodeNoopRelease `state:"nosave"`
fsutil.InodeNoopTruncate `state:"nosave"`
fsutil.InodeNoopWriteOut `state:"nosave"`
fsutil.InodeNotDirectory `state:"nosave"`
fsutil.InodeNotMappable `state:"nosave"`
fsutil.InodeNotSocket `state:"nosave"`
fsutil.InodeNotSymlink `state:"nosave"`
fsutil.InodeVirtual `state:"nosave"`
fsutil.InodeSimpleAttributes
}
var _ fs.InodeOperations = (*Device)(nil)
// NewDevice creates and intializes a Device structure.
func NewDevice(ctx context.Context, owner fs.FileOwner, fp fs.FilePermissions) *Device {
return &Device{
InodeSimpleAttributes: fsutil.NewInodeSimpleAttributes(ctx, owner, fp, 0),
}
}
// GetFile implements fs.InodeOperations.GetFile.
//
// TODO: Add functionality to GetFile: Additional fields will be
// needed in the Device structure, initialize them here. Also, Device will need
// to keep track of the created Procs in order to implement BINDER_READ_WRITE
// ioctl.
func (bd *Device) GetFile(ctx context.Context, d *fs.Dirent, flags fs.FileFlags) (*fs.File, error) {
return fs.NewFile(ctx, d, flags, &Proc{
bd: bd,
task: kernel.TaskFromContext(ctx),
platform: platform.FromContext(ctx),
}), nil
}
// Proc implements fs.FileOperations and fs.IoctlGetter.
//
// +stateify savable
type Proc struct {
waiter.AlwaysReady `state:"nosave"`
fsutil.FileNoFsync `state:"nosave"`
fsutil.FileNotDirReaddir `state:"nosave"`
bd *Device
task *kernel.Task
platform platform.Platform
// mu protects fr.
mu sync.Mutex `state:"nosave"`
// mapped is memory allocated from platform.Memory() by AddMapping.
mapped platform.FileRange
}
// Release implements fs.FileOperations.Release.
func (bp *Proc) Release() {
bp.mu.Lock()
defer bp.mu.Unlock()
if bp.mapped.Length() != 0 {
bp.platform.Memory().DecRef(bp.mapped)
}
}
// Seek implements fs.FileOperations.Seek.
//
// Binder doesn't support seek operation (unless in debug mode).
func (bp *Proc) Seek(ctx context.Context, file *fs.File, whence fs.SeekWhence, offset int64) (int64, error) {
return offset, syserror.EOPNOTSUPP
}
// Read implements fs.FileOperations.Read.
//
// Binder doesn't support read operation (unless in debug mode).
func (bp *Proc) Read(ctx context.Context, file *fs.File, dst usermem.IOSequence, offset int64) (int64, error) {
return 0, syserror.EOPNOTSUPP
}
// Write implements fs.FileOperations.Write.
//
// Binder doesn't support write operation.
func (bp *Proc) Write(ctx context.Context, file *fs.File, src usermem.IOSequence, offset int64) (int64, error) {
return 0, syserror.EOPNOTSUPP
}
// Flush implements fs.FileOperations.Flush.
//
// TODO: Implement.
func (bp *Proc) Flush(ctx context.Context, file *fs.File) error {
return nil
}
// ConfigureMMap implements fs.FileOperations.ConfigureMMap.
func (bp *Proc) ConfigureMMap(ctx context.Context, file *fs.File, opts *memmap.MMapOpts) error {
// Compare drivers/android/binder.c:binder_mmap().
if caller := kernel.TaskFromContext(ctx); caller != bp.task {
return syserror.EINVAL
}
if opts.Length > mmapSizeLimit {
opts.Length = mmapSizeLimit
}
opts.MaxPerms.Write = false
// TODO: Binder sets VM_DONTCOPY, preventing the created vma
// from being copied across fork(), but we don't support this yet. As
// a result, MMs containing a Binder mapping cannot be forked (MM.Fork will
// fail when AddMapping returns EBUSY).
return fsutil.GenericConfigureMMap(file, bp, opts)
}
// Ioctl implements fs.FileOperations.Ioctl.
//
// TODO: Implement.
func (bp *Proc) Ioctl(ctx context.Context, io usermem.IO, args arch.SyscallArguments) (uintptr, error) {
// Switch on ioctl request.
switch uint32(args[1].Int()) {
case linux.BinderVersionIoctl:
ver := &linux.BinderVersion{
ProtocolVersion: currentProtocolVersion,
}
// Copy result to user-space.
_, err := usermem.CopyObjectOut(ctx, io, args[2].Pointer(), ver, usermem.IOOpts{
AddressSpaceActive: true,
})
return 0, err
case linux.BinderWriteReadIoctl:
// TODO: Implement.
fallthrough
case linux.BinderSetIdleTimeoutIoctl:
// TODO: Implement.
fallthrough
case linux.BinderSetMaxThreadsIoctl:
// TODO: Implement.
fallthrough
case linux.BinderSetIdlePriorityIoctl:
// TODO: Implement.
fallthrough
case linux.BinderSetContextMgrIoctl:
// TODO: Implement.
fallthrough
case linux.BinderThreadExitIoctl:
// TODO: Implement.
return 0, syserror.ENOSYS
default:
// Ioctls irrelevant to Binder.
return 0, syserror.EINVAL
}
}
// AddMapping implements memmap.Mappable.AddMapping.
func (bp *Proc) AddMapping(ctx context.Context, ms memmap.MappingSpace, ar usermem.AddrRange, offset uint64, _ bool) error {
bp.mu.Lock()
defer bp.mu.Unlock()
if bp.mapped.Length() != 0 {
// mmap has been called before, which binder_mmap() doesn't like.
return syserror.EBUSY
}
// Binder only allocates and maps a single page up-front
// (drivers/android/binder.c:binder_mmap() => binder_update_page_range()).
fr, err := bp.platform.Memory().Allocate(usermem.PageSize, usage.Anonymous)
if err != nil {
return err
}
bp.mapped = fr
return nil
}
// RemoveMapping implements memmap.Mappable.RemoveMapping.
func (*Proc) RemoveMapping(context.Context, memmap.MappingSpace, usermem.AddrRange, uint64, bool) {
// Nothing to do. Notably, we don't free bp.mapped to allow another mmap.
}
// CopyMapping implements memmap.Mappable.CopyMapping.
func (bp *Proc) CopyMapping(ctx context.Context, ms memmap.MappingSpace, srcAR, dstAR usermem.AddrRange, offset uint64, _ bool) error {
// Nothing to do. Notably, this is one case where CopyMapping isn't
// equivalent to AddMapping, as AddMapping would return EBUSY.
return nil
}
// Translate implements memmap.Mappable.Translate.
func (bp *Proc) Translate(ctx context.Context, required, optional memmap.MappableRange, at usermem.AccessType) ([]memmap.Translation, error) {
// TODO: In addition to the page initially allocated and mapped
// in AddMapping (Linux: binder_mmap), Binder allocates and maps pages for
// each transaction (Linux: binder_ioctl => binder_ioctl_write_read =>
// binder_thread_write => binder_transaction => binder_alloc_buf =>
// binder_update_page_range). Since we don't actually implement
// BinderWriteReadIoctl (Linux: BINDER_WRITE_READ), we only ever have the
// first page.
var err error
if required.End > usermem.PageSize {
err = &memmap.BusError{syserror.EFAULT}
}
if required.Start == 0 {
return []memmap.Translation{
{
Source: memmap.MappableRange{0, usermem.PageSize},
File: bp.platform.Memory(),
Offset: bp.mapped.Start,
},
}, err
}
return nil, err
}
// InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable.
func (bp *Proc) InvalidateUnsavable(ctx context.Context) error {
return nil
}
|