github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/mm/special_mappable.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mm 16 17 import ( 18 "github.com/SagerNet/gvisor/pkg/context" 19 "github.com/SagerNet/gvisor/pkg/errors/linuxerr" 20 "github.com/SagerNet/gvisor/pkg/hostarch" 21 "github.com/SagerNet/gvisor/pkg/sentry/memmap" 22 "github.com/SagerNet/gvisor/pkg/sentry/pgalloc" 23 "github.com/SagerNet/gvisor/pkg/sentry/usage" 24 "github.com/SagerNet/gvisor/pkg/syserror" 25 ) 26 27 // SpecialMappable implements memmap.MappingIdentity and memmap.Mappable with 28 // semantics similar to Linux's mm/mmap.c:_install_special_mapping(), except 29 // that SpecialMappable takes ownership of the memory that it represents 30 // (_install_special_mapping() does not.) 31 // 32 // +stateify savable 33 type SpecialMappable struct { 34 SpecialMappableRefs 35 36 mfp pgalloc.MemoryFileProvider 37 fr memmap.FileRange 38 name string 39 } 40 41 // NewSpecialMappable returns a SpecialMappable that owns fr, which represents 42 // offsets in mfp.MemoryFile() that contain the SpecialMappable's data. The 43 // SpecialMappable will use the given name in /proc/[pid]/maps. 44 // 45 // Preconditions: fr.Length() != 0. 46 func NewSpecialMappable(name string, mfp pgalloc.MemoryFileProvider, fr memmap.FileRange) *SpecialMappable { 47 m := SpecialMappable{mfp: mfp, fr: fr, name: name} 48 m.InitRefs() 49 return &m 50 } 51 52 // DecRef implements refs.RefCounter.DecRef. 53 func (m *SpecialMappable) DecRef(ctx context.Context) { 54 m.SpecialMappableRefs.DecRef(func() { 55 m.mfp.MemoryFile().DecRef(m.fr) 56 }) 57 } 58 59 // MappedName implements memmap.MappingIdentity.MappedName. 60 func (m *SpecialMappable) MappedName(ctx context.Context) string { 61 return m.name 62 } 63 64 // DeviceID implements memmap.MappingIdentity.DeviceID. 65 func (m *SpecialMappable) DeviceID() uint64 { 66 return 0 67 } 68 69 // InodeID implements memmap.MappingIdentity.InodeID. 70 func (m *SpecialMappable) InodeID() uint64 { 71 return 0 72 } 73 74 // Msync implements memmap.MappingIdentity.Msync. 75 func (m *SpecialMappable) Msync(ctx context.Context, mr memmap.MappableRange) error { 76 // Linux: vm_file is NULL, causing msync to skip it entirely. 77 return nil 78 } 79 80 // AddMapping implements memmap.Mappable.AddMapping. 81 func (*SpecialMappable) AddMapping(context.Context, memmap.MappingSpace, hostarch.AddrRange, uint64, bool) error { 82 return nil 83 } 84 85 // RemoveMapping implements memmap.Mappable.RemoveMapping. 86 func (*SpecialMappable) RemoveMapping(context.Context, memmap.MappingSpace, hostarch.AddrRange, uint64, bool) { 87 } 88 89 // CopyMapping implements memmap.Mappable.CopyMapping. 90 func (*SpecialMappable) CopyMapping(context.Context, memmap.MappingSpace, hostarch.AddrRange, hostarch.AddrRange, uint64, bool) error { 91 return nil 92 } 93 94 // Translate implements memmap.Mappable.Translate. 95 func (m *SpecialMappable) Translate(ctx context.Context, required, optional memmap.MappableRange, at hostarch.AccessType) ([]memmap.Translation, error) { 96 var err error 97 if required.End > m.fr.Length() { 98 err = &memmap.BusError{syserror.EFAULT} 99 } 100 if source := optional.Intersect(memmap.MappableRange{0, m.fr.Length()}); source.Length() != 0 { 101 return []memmap.Translation{ 102 { 103 Source: source, 104 File: m.mfp.MemoryFile(), 105 Offset: m.fr.Start + source.Start, 106 Perms: hostarch.AnyAccess, 107 }, 108 }, err 109 } 110 return nil, err 111 } 112 113 // InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable. 114 func (m *SpecialMappable) InvalidateUnsavable(ctx context.Context) error { 115 // Since data is stored in pgalloc.MemoryFile, the contents of which are 116 // preserved across save/restore, we don't need to do anything. 117 return nil 118 } 119 120 // MemoryFileProvider returns the MemoryFileProvider whose MemoryFile stores 121 // the SpecialMappable's contents. 122 func (m *SpecialMappable) MemoryFileProvider() pgalloc.MemoryFileProvider { 123 return m.mfp 124 } 125 126 // FileRange returns the offsets into MemoryFileProvider().MemoryFile() that 127 // store the SpecialMappable's contents. 128 func (m *SpecialMappable) FileRange() memmap.FileRange { 129 return m.fr 130 } 131 132 // Length returns the length of the SpecialMappable. 133 func (m *SpecialMappable) Length() uint64 { 134 return m.fr.Length() 135 } 136 137 // NewSharedAnonMappable returns a SpecialMappable that implements the 138 // semantics of mmap(MAP_SHARED|MAP_ANONYMOUS) and mappings of /dev/zero. 139 // 140 // TODO(github.com/SagerNet/issue/1624): Linux uses an ephemeral file created by 141 // mm/shmem.c:shmem_zero_setup(), and VFS2 does something analogous. VFS1 uses 142 // a SpecialMappable instead, incorrectly getting device and inode IDs of zero 143 // and causing memory for shared anonymous mappings to be allocated up-front 144 // instead of on first touch; this is to avoid exacerbating the fs.MountSource 145 // leak (b/143656263). Delete this function along with VFS1. 146 func NewSharedAnonMappable(length uint64, mfp pgalloc.MemoryFileProvider) (*SpecialMappable, error) { 147 if length == 0 { 148 return nil, linuxerr.EINVAL 149 } 150 alignedLen, ok := hostarch.Addr(length).RoundUp() 151 if !ok { 152 return nil, linuxerr.EINVAL 153 } 154 fr, err := mfp.MemoryFile().Allocate(uint64(alignedLen), usage.Anonymous) 155 if err != nil { 156 return nil, err 157 } 158 return NewSpecialMappable("/dev/zero (deleted)", mfp, fr), nil 159 }