github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/syscalls/linux/sys_pipe.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 linux 16 17 import ( 18 "github.com/nicocha30/gvisor-ligolo/pkg/abi/linux" 19 "github.com/nicocha30/gvisor-ligolo/pkg/errors/linuxerr" 20 "github.com/nicocha30/gvisor-ligolo/pkg/hostarch" 21 "github.com/nicocha30/gvisor-ligolo/pkg/marshal/primitive" 22 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/arch" 23 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/fsimpl/pipefs" 24 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/kernel" 25 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/vfs" 26 ) 27 28 // Pipe implements Linux syscall pipe(2). 29 func Pipe(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 30 addr := args[0].Pointer() 31 return 0, nil, pipe2(t, addr, 0) 32 } 33 34 // Pipe2 implements Linux syscall pipe2(2). 35 func Pipe2(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 36 addr := args[0].Pointer() 37 flags := args[1].Int() 38 return 0, nil, pipe2(t, addr, flags) 39 } 40 41 func pipe2(t *kernel.Task, addr hostarch.Addr, flags int32) error { 42 if flags&^(linux.O_NONBLOCK|linux.O_CLOEXEC) != 0 { 43 return linuxerr.EINVAL 44 } 45 r, w, err := pipefs.NewConnectedPipeFDs(t, t.Kernel().PipeMount(), uint32(flags&linux.O_NONBLOCK)) 46 if err != nil { 47 return err 48 } 49 defer r.DecRef(t) 50 defer w.DecRef(t) 51 52 fds, err := t.NewFDs(0, []*vfs.FileDescription{r, w}, kernel.FDFlags{ 53 CloseOnExec: flags&linux.O_CLOEXEC != 0, 54 }) 55 if err != nil { 56 return err 57 } 58 if _, err := primitive.CopyInt32SliceOut(t, addr, fds); err != nil { 59 for _, fd := range fds { 60 if file := t.FDTable().Remove(t, fd); file != nil { 61 file.DecRef(t) 62 } 63 } 64 return err 65 } 66 return nil 67 }