github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/devices/memdev/random.go (about) 1 // Copyright 2020 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 memdev 16 17 import ( 18 "github.com/nicocha30/gvisor-ligolo/pkg/atomicbitops" 19 "github.com/nicocha30/gvisor-ligolo/pkg/context" 20 "github.com/nicocha30/gvisor-ligolo/pkg/rand" 21 "github.com/nicocha30/gvisor-ligolo/pkg/safemem" 22 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/vfs" 23 "github.com/nicocha30/gvisor-ligolo/pkg/usermem" 24 ) 25 26 const ( 27 randomDevMinor = 8 28 urandomDevMinor = 9 29 ) 30 31 // randomDevice implements vfs.Device for /dev/random and /dev/urandom. 32 // 33 // +stateify savable 34 type randomDevice struct{} 35 36 // Open implements vfs.Device.Open. 37 func (randomDevice) Open(ctx context.Context, mnt *vfs.Mount, vfsd *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) { 38 fd := &randomFD{} 39 if err := fd.vfsfd.Init(fd, opts.Flags, mnt, vfsd, &vfs.FileDescriptionOptions{ 40 UseDentryMetadata: true, 41 }); err != nil { 42 return nil, err 43 } 44 return &fd.vfsfd, nil 45 } 46 47 // randomFD implements vfs.FileDescriptionImpl for /dev/random. 48 // 49 // +stateify savable 50 type randomFD struct { 51 vfsfd vfs.FileDescription 52 vfs.FileDescriptionDefaultImpl 53 vfs.DentryMetadataFileDescriptionImpl 54 vfs.NoLockFD 55 56 // off is the "file offset". off is accessed using atomic memory 57 // operations. 58 off atomicbitops.Int64 59 } 60 61 // Release implements vfs.FileDescriptionImpl.Release. 62 func (fd *randomFD) Release(context.Context) { 63 // noop 64 } 65 66 // PRead implements vfs.FileDescriptionImpl.PRead. 67 func (fd *randomFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) { 68 return dst.CopyOutFrom(ctx, safemem.FromIOReader{rand.Reader}) 69 } 70 71 // Read implements vfs.FileDescriptionImpl.Read. 72 func (fd *randomFD) Read(ctx context.Context, dst usermem.IOSequence, opts vfs.ReadOptions) (int64, error) { 73 n, err := dst.CopyOutFrom(ctx, safemem.FromIOReader{rand.Reader}) 74 fd.off.Add(n) 75 return n, err 76 } 77 78 // PWrite implements vfs.FileDescriptionImpl.PWrite. 79 func (fd *randomFD) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts vfs.WriteOptions) (int64, error) { 80 // In Linux, this mixes the written bytes into the entropy pool; we just 81 // throw them away. 82 return src.NumBytes(), nil 83 } 84 85 // Write implements vfs.FileDescriptionImpl.Write. 86 func (fd *randomFD) Write(ctx context.Context, src usermem.IOSequence, opts vfs.WriteOptions) (int64, error) { 87 fd.off.Add(src.NumBytes()) 88 return src.NumBytes(), nil 89 } 90 91 // Seek implements vfs.FileDescriptionImpl.Seek. 92 func (fd *randomFD) Seek(ctx context.Context, offset int64, whence int32) (int64, error) { 93 // Linux: drivers/char/random.c:random_fops.llseek == urandom_fops.llseek 94 // == noop_llseek 95 return fd.off.Load(), nil 96 }