2018-10-19 23:34:09 +00:00
|
|
|
// Copyright 2018 Google LLC
|
2018-04-27 17:37:02 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2019-03-14 15:11:36 +00:00
|
|
|
"gvisor.googlesource.com/gvisor/pkg/sentry/pgalloc"
|
2018-04-27 17:37:02 +00:00
|
|
|
"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"
|
2019-01-15 04:33:29 +00:00
|
|
|
"gvisor.googlesource.com/gvisor/pkg/waiter"
|
2018-04-27 17:37:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
currentProtocolVersion = 8
|
|
|
|
|
|
|
|
// mmapSizeLimit is the upper limit for mapped memory size in Binder.
|
|
|
|
mmapSizeLimit = 4 * 1024 * 1024 // 4MB
|
|
|
|
)
|
|
|
|
|
|
|
|
// Device implements fs.InodeOperations.
|
2018-08-02 17:41:44 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2018-04-27 17:37:02 +00:00
|
|
|
type Device struct {
|
2019-01-15 04:33:29 +00:00
|
|
|
fsutil.InodeGenericChecker `state:"nosave"`
|
2018-08-02 17:41:44 +00:00
|
|
|
fsutil.InodeNoExtendedAttributes `state:"nosave"`
|
2019-01-15 04:33:29 +00:00
|
|
|
fsutil.InodeNoopRelease `state:"nosave"`
|
|
|
|
fsutil.InodeNoopTruncate `state:"nosave"`
|
|
|
|
fsutil.InodeNoopWriteOut `state:"nosave"`
|
2018-08-02 17:41:44 +00:00
|
|
|
fsutil.InodeNotDirectory `state:"nosave"`
|
2019-01-15 04:33:29 +00:00
|
|
|
fsutil.InodeNotMappable `state:"nosave"`
|
2018-08-02 17:41:44 +00:00
|
|
|
fsutil.InodeNotSocket `state:"nosave"`
|
|
|
|
fsutil.InodeNotSymlink `state:"nosave"`
|
2019-01-15 04:33:29 +00:00
|
|
|
fsutil.InodeVirtual `state:"nosave"`
|
2018-04-27 17:37:02 +00:00
|
|
|
|
2019-01-15 04:33:29 +00:00
|
|
|
fsutil.InodeSimpleAttributes
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 04:33:29 +00:00
|
|
|
var _ fs.InodeOperations = (*Device)(nil)
|
|
|
|
|
2018-04-27 17:37:02 +00:00
|
|
|
// NewDevice creates and intializes a Device structure.
|
|
|
|
func NewDevice(ctx context.Context, owner fs.FileOwner, fp fs.FilePermissions) *Device {
|
|
|
|
return &Device{
|
2019-01-15 04:33:29 +00:00
|
|
|
InodeSimpleAttributes: fsutil.NewInodeSimpleAttributes(ctx, owner, fp, 0),
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2019-03-14 15:11:36 +00:00
|
|
|
bd: bd,
|
|
|
|
task: kernel.TaskFromContext(ctx),
|
|
|
|
mfp: pgalloc.MemoryFileProviderFromContext(ctx),
|
2018-04-27 17:37:02 +00:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proc implements fs.FileOperations and fs.IoctlGetter.
|
2018-08-02 17:41:44 +00:00
|
|
|
//
|
|
|
|
// +stateify savable
|
2018-04-27 17:37:02 +00:00
|
|
|
type Proc struct {
|
2019-01-15 04:33:29 +00:00
|
|
|
waiter.AlwaysReady `state:"nosave"`
|
|
|
|
fsutil.FileNoFsync `state:"nosave"`
|
|
|
|
fsutil.FileNotDirReaddir `state:"nosave"`
|
2018-04-27 17:37:02 +00:00
|
|
|
|
2019-03-14 15:11:36 +00:00
|
|
|
bd *Device
|
|
|
|
task *kernel.Task
|
|
|
|
mfp pgalloc.MemoryFileProvider
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// mu protects fr.
|
|
|
|
mu sync.Mutex `state:"nosave"`
|
|
|
|
|
2019-03-14 15:11:36 +00:00
|
|
|
// mapped is memory allocated from mfp.MemoryFile() by AddMapping.
|
2018-04-27 17:37:02 +00:00
|
|
|
mapped platform.FileRange
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release implements fs.FileOperations.Release.
|
|
|
|
func (bp *Proc) Release() {
|
|
|
|
bp.mu.Lock()
|
|
|
|
defer bp.mu.Unlock()
|
|
|
|
if bp.mapped.Length() != 0 {
|
2019-03-14 15:11:36 +00:00
|
|
|
bp.mfp.MemoryFile().DecRef(bp.mapped)
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-12-12 21:09:10 +00:00
|
|
|
func (bp *Proc) AddMapping(ctx context.Context, ms memmap.MappingSpace, ar usermem.AddrRange, offset uint64, _ bool) error {
|
2018-04-27 17:37:02 +00:00
|
|
|
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()).
|
2019-03-14 15:11:36 +00:00
|
|
|
fr, err := bp.mfp.MemoryFile().Allocate(usermem.PageSize, usage.Anonymous)
|
2018-04-27 17:37:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bp.mapped = fr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveMapping implements memmap.Mappable.RemoveMapping.
|
2018-12-12 21:09:10 +00:00
|
|
|
func (*Proc) RemoveMapping(context.Context, memmap.MappingSpace, usermem.AddrRange, uint64, bool) {
|
2018-04-27 17:37:02 +00:00
|
|
|
// Nothing to do. Notably, we don't free bp.mapped to allow another mmap.
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyMapping implements memmap.Mappable.CopyMapping.
|
2018-12-12 21:09:10 +00:00
|
|
|
func (bp *Proc) CopyMapping(ctx context.Context, ms memmap.MappingSpace, srcAR, dstAR usermem.AddrRange, offset uint64, _ bool) error {
|
2018-04-27 17:37:02 +00:00
|
|
|
// 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},
|
2019-03-14 15:11:36 +00:00
|
|
|
File: bp.mfp.MemoryFile(),
|
2018-04-27 17:37:02 +00:00
|
|
|
Offset: bp.mapped.Start,
|
2019-03-25 19:41:36 +00:00
|
|
|
Perms: usermem.AnyAccess,
|
2018-04-27 17:37:02 +00:00
|
|
|
},
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable.
|
|
|
|
func (bp *Proc) InvalidateUnsavable(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|