@@ -25,6 +25,9 @@ import (
2525 "gvisor.dev/gvisor/pkg/sentry/kernel"
2626 "gvisor.dev/gvisor/pkg/sentry/memmap"
2727 "gvisor.dev/gvisor/pkg/sentry/mm"
28+ "gvisor.dev/gvisor/pkg/sentry/seccheck"
29+ pb "gvisor.dev/gvisor/pkg/sentry/seccheck/points/points_go_proto"
30+ "gvisor.dev/gvisor/pkg/sentry/vfs"
2831)
2932
3033// Brk implements linux syscall brk(2).
@@ -81,6 +84,7 @@ func Mmap(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *
8184 }
8285 }()
8386
87+ var mappedFile * vfs.FileDescription
8488 if ! anon {
8589 // Convert the passed FD to a file reference.
8690 file := t .GetFile (fd )
@@ -122,6 +126,7 @@ func Mmap(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *
122126 if err := file .ConfigureMMap (t , & opts ); err != nil {
123127 return 0 , nil , err
124128 }
129+ mappedFile = file
125130 } else if shared {
126131 // Back shared anonymous mappings with an anonymous tmpfs file.
127132 opts .Offset = 0
@@ -133,10 +138,17 @@ func Mmap(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *
133138 if err := file .ConfigureMMap (t , & opts ); err != nil {
134139 return 0 , nil , err
135140 }
141+ mappedFile = file
136142 } else {
137143 opts .NameMut = memmap .NameMutAnon
138144 }
139145
146+ if opts .Perms .Execute {
147+ if err := traceMmap (t , mappedFile ); err != nil {
148+ return 0 , nil , err
149+ }
150+ }
151+
140152 rv , err := t .MemoryManager ().MMap (t , opts )
141153 return uintptr (rv ), nil , err
142154}
@@ -349,3 +361,49 @@ func Munlockall(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uint
349361 Mode : memmap .MLockNone ,
350362 })
351363}
364+
365+ func traceMmap (t * kernel.Task , file * vfs.FileDescription ) error {
366+ if ! seccheck .Global .Enabled (seccheck .PointMmap ) {
367+ return nil
368+ }
369+ fields := seccheck .Global .GetFieldSet (seccheck .PointMmap )
370+ info := & pb.MmapInfo {}
371+ if file != nil {
372+ info .MappedPath = file .MappedName (t )
373+ statOpts := vfs.StatOptions {
374+ Mask : linux .STATX_TYPE | linux .STATX_MODE | linux .STATX_UID | linux .STATX_GID | linux .STATX_INO ,
375+ }
376+ if stat , err := file .Stat (t , statOpts ); err == nil {
377+ if stat .Mask & (linux .STATX_TYPE | linux .STATX_MODE ) == (linux .STATX_TYPE | linux .STATX_MODE ) {
378+ info .MappedMode = uint32 (stat .Mode )
379+ }
380+ if stat .Mask & linux .STATX_UID != 0 {
381+ info .MappedUid = stat .UID
382+ }
383+ if stat .Mask & linux .STATX_GID != 0 {
384+ info .MappedGid = stat .GID
385+ }
386+ if stat .Mask & linux .STATX_INO != 0 {
387+ info .MappedIno = stat .Ino
388+ }
389+ }
390+ }
391+ if exe := t .MemoryManager ().Executable (); exe != nil {
392+ if exe == file {
393+ info .IsInitialMmap = true
394+ } else {
395+ statOpts := vfs.StatOptions {Mask : linux .STATX_INO }
396+ if exeStat , err := exe .Stat (t , statOpts ); err == nil && info .MappedIno != 0 && exeStat .Ino == info .MappedIno {
397+ info .IsInitialMmap = true
398+ }
399+ }
400+ exe .DecRef (t )
401+ }
402+ if ! fields .Context .Empty () {
403+ info .ContextData = & pb.ContextData {}
404+ kernel .LoadSeccheckData (t , fields .Context , info .ContextData )
405+ }
406+ return seccheck .Global .SentToSinks (func (c seccheck.Sink ) error {
407+ return c .Mmap (t , fields , info )
408+ })
409+ }
0 commit comments