summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/binder/binder.go
blob: 3f87b6b08567b711a5e3ce6c3a3ba4182fbfb1eb (plain)
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
// 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 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/kernel/time"
	"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"
)

const (
	currentProtocolVersion = 8

	// mmapSizeLimit is the upper limit for mapped memory size in Binder.
	mmapSizeLimit = 4 * 1024 * 1024 // 4MB
)

// Device implements fs.InodeOperations.
type Device struct {
	fsutil.InodeNoExtendedAttributes
	fsutil.InodeNotDirectory
	fsutil.InodeNotRenameable
	fsutil.InodeNotSocket
	fsutil.InodeNotSymlink
	fsutil.NoMappable
	fsutil.NoopWriteOut
	fsutil.DeprecatedFileOperations

	// mu protects unstable.
	mu       sync.Mutex `state:"nosave"`
	unstable fs.UnstableAttr
}

// NewDevice creates and intializes a Device structure.
func NewDevice(ctx context.Context, owner fs.FileOwner, fp fs.FilePermissions) *Device {
	return &Device{
		unstable: fs.WithCurrentTime(ctx, fs.UnstableAttr{
			Owner: owner,
			Perms: fp,
			Links: 1,
		}),
	}
}

// Release implements fs.InodeOperations.Release.
func (bd *Device) Release(context.Context) {}

// 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
}

// UnstableAttr implements fs.InodeOperations.UnstableAttr.
func (bd *Device) UnstableAttr(ctx context.Context, inode *fs.Inode) (fs.UnstableAttr, error) {
	bd.mu.Lock()
	defer bd.mu.Unlock()
	return bd.unstable, nil
}

// Check implements fs.InodeOperations.Check.
func (bd *Device) Check(ctx context.Context, inode *fs.Inode, p fs.PermMask) bool {
	return fs.ContextCanAccessFile(ctx, inode, p)
}

// SetPermissions implements fs.InodeOperations.SetPermissions.
func (bd *Device) SetPermissions(ctx context.Context, inode *fs.Inode, fp fs.FilePermissions) bool {
	bd.mu.Lock()
	defer bd.mu.Unlock()
	bd.unstable.Perms = fp
	bd.unstable.StatusChangeTime = time.NowFromContext(ctx)
	return true
}

// SetOwner implements fs.InodeOperations.SetOwner.
func (bd *Device) SetOwner(ctx context.Context, inode *fs.Inode, owner fs.FileOwner) error {
	bd.mu.Lock()
	defer bd.mu.Unlock()
	if owner.UID.Ok() {
		bd.unstable.Owner.UID = owner.UID
	}
	if owner.GID.Ok() {
		bd.unstable.Owner.GID = owner.GID
	}
	return nil
}

// SetTimestamps implements fs.InodeOperations.SetTimestamps.
func (bd *Device) SetTimestamps(ctx context.Context, inode *fs.Inode, ts fs.TimeSpec) error {
	if ts.ATimeOmit && ts.MTimeOmit {
		return nil
	}

	bd.mu.Lock()
	defer bd.mu.Unlock()

	now := time.NowFromContext(ctx)
	if !ts.ATimeOmit {
		if ts.ATimeSetSystemTime {
			bd.unstable.AccessTime = now
		} else {
			bd.unstable.AccessTime = ts.ATime
		}
	}
	if !ts.MTimeOmit {
		if ts.MTimeSetSystemTime {
			bd.unstable.ModificationTime = now
		} else {
			bd.unstable.ModificationTime = ts.MTime
		}
	}
	bd.unstable.StatusChangeTime = now
	return nil
}

// Truncate implements fs.InodeOperations.WriteOut.
//
// Ignored for a character device, such as Binder.
func (bd *Device) Truncate(ctx context.Context, inode *fs.Inode, size int64) error {
	return nil
}

// AddLink implements fs.InodeOperations.AddLink.
//
// Binder doesn't support links, no-op.
func (bd *Device) AddLink() {}

// DropLink implements fs.InodeOperations.DropLink.
//
// Binder doesn't support links, no-op.
func (bd *Device) DropLink() {}

// NotifyStatusChange implements fs.InodeOperations.NotifyStatusChange.
func (bd *Device) NotifyStatusChange(ctx context.Context) {
	bd.mu.Lock()
	defer bd.mu.Unlock()
	now := time.NowFromContext(ctx)
	bd.unstable.ModificationTime = now
	bd.unstable.StatusChangeTime = now
}

// IsVirtual implements fs.InodeOperations.IsVirtual.
//
// Binder is virtual.
func (bd *Device) IsVirtual() bool {
	return true
}

// StatFS implements fs.InodeOperations.StatFS.
//
// Binder doesn't support querying for filesystem info.
func (bd *Device) StatFS(context.Context) (fs.Info, error) {
	return fs.Info{}, syserror.ENOSYS
}

// Proc implements fs.FileOperations and fs.IoctlGetter.
type Proc struct {
	fsutil.NoFsync
	fsutil.DeprecatedFileOperations
	fsutil.NotDirReaddir

	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) 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 (bp *Proc) RemoveMapping(ctx context.Context, ms memmap.MappingSpace, ar usermem.AddrRange, offset uint64) {
	// 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) 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
}