From 6c9d715a8c64a7c782b8c7b57925e1dc19b29517 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 17 Jul 2017 10:36:46 +0200 Subject: [PATCH 1/3] sysinfo: use Prctl() from x/sys/unix Use unix.Prctl() instead of manually reimplementing it using unix.RawSyscall. Also use unix.SECCOMP_MODE_FILTER instead of locally defining it. Signed-off-by: Tobias Klauser --- pkg/sysinfo/sysinfo_linux.go | 9 ++------- pkg/sysinfo/sysinfo_linux_test.go | 6 +++--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/pkg/sysinfo/sysinfo_linux.go b/pkg/sysinfo/sysinfo_linux.go index 2d33b4dbc3..50ae265bb6 100644 --- a/pkg/sysinfo/sysinfo_linux.go +++ b/pkg/sysinfo/sysinfo_linux.go @@ -12,11 +12,6 @@ import ( "golang.org/x/sys/unix" ) -const ( - // SeccompModeFilter refers to the syscall argument SECCOMP_MODE_FILTER. - SeccompModeFilter = uintptr(2) -) - func findCgroupMountpoints() (map[string]string, error) { cgMounts, err := cgroups.GetCgroupMounts(false) if err != nil { @@ -60,9 +55,9 @@ func New(quiet bool) *SysInfo { } // Check if Seccomp is supported, via CONFIG_SECCOMP. - if _, _, err := unix.RawSyscall(unix.SYS_PRCTL, unix.PR_GET_SECCOMP, 0, 0); err != unix.EINVAL { + if err := unix.Prctl(unix.PR_GET_SECCOMP, 0, 0, 0, 0); err != unix.EINVAL { // Make sure the kernel has CONFIG_SECCOMP_FILTER. - if _, _, err := unix.RawSyscall(unix.SYS_PRCTL, unix.PR_SET_SECCOMP, SeccompModeFilter, 0); err != unix.EINVAL { + if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0); err != unix.EINVAL { sysInfo.Seccomp = true } } diff --git a/pkg/sysinfo/sysinfo_linux_test.go b/pkg/sysinfo/sysinfo_linux_test.go index 77c54f27c9..860784f2ae 100644 --- a/pkg/sysinfo/sysinfo_linux_test.go +++ b/pkg/sysinfo/sysinfo_linux_test.go @@ -5,10 +5,10 @@ import ( "os" "path" "path/filepath" - "syscall" "testing" "github.com/stretchr/testify/require" + "golang.org/x/sys/unix" ) func TestReadProcBool(t *testing.T) { @@ -66,9 +66,9 @@ func TestNew(t *testing.T) { func checkSysInfo(t *testing.T, sysInfo *SysInfo) { // Check if Seccomp is supported, via CONFIG_SECCOMP.then sysInfo.Seccomp must be TRUE , else FALSE - if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_GET_SECCOMP, 0, 0); err != syscall.EINVAL { + if err := unix.Prctl(unix.PR_GET_SECCOMP, 0, 0, 0, 0); err != unix.EINVAL { // Make sure the kernel has CONFIG_SECCOMP_FILTER. - if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_SECCOMP, SeccompModeFilter, 0); err != syscall.EINVAL { + if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0); err != unix.EINVAL { require.True(t, sysInfo.Seccomp) } } else { From 6476504695284fcdc32b5f7621cffca22746e67d Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 17 Jul 2017 10:36:52 +0200 Subject: [PATCH 2/3] [pkg/term] use IoctlGetTermios/IoctlSetTermios from x/sys/unix Use IoctlGetTermios/IoctlSetTermios from golang.org/x/sys/unix instead of manually reimplementing them. Signed-off-by: Tobias Klauser --- pkg/term/termios_linux.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pkg/term/termios_linux.go b/pkg/term/termios_linux.go index 31bfa8419e..3e25eb7a41 100644 --- a/pkg/term/termios_linux.go +++ b/pkg/term/termios_linux.go @@ -1,8 +1,6 @@ package term import ( - "unsafe" - "golang.org/x/sys/unix" ) @@ -18,20 +16,21 @@ type Termios unix.Termios // mode and returns the previous state of the terminal so that it can be // restored. func MakeRaw(fd uintptr) (*State, error) { - var oldState State - if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 { + termios, err := unix.IoctlGetTermios(int(fd), getTermios) + if err != nil { return nil, err } - newState := oldState.termios + var oldState State + oldState.termios = Termios(*termios) - newState.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON) - newState.Oflag &^= unix.OPOST - newState.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN) - newState.Cflag &^= (unix.CSIZE | unix.PARENB) - newState.Cflag |= unix.CS8 + termios.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON) + termios.Oflag &^= unix.OPOST + termios.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN) + termios.Cflag &^= (unix.CSIZE | unix.PARENB) + termios.Cflag |= unix.CS8 - if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 { + if err := unix.IoctlSetTermios(int(fd), setTermios, termios); err != nil { return nil, err } return &oldState, nil From bedf09363cb7f2f59bf2b72fea0704351b9f5c8d Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 17 Jul 2017 10:36:56 +0200 Subject: [PATCH 3/3] loopback: use IoctlGetInt/IoctlSetInt from x/sys/unix Use IoctlGetInt/IoctlSetInt from golang.org/x/sys/unix (where applicable) instead of manually reimplementing them. Signed-off-by: Tobias Klauser --- pkg/loopback/ioctl.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/loopback/ioctl.go b/pkg/loopback/ioctl.go index 534907a023..fa744f0a69 100644 --- a/pkg/loopback/ioctl.go +++ b/pkg/loopback/ioctl.go @@ -9,15 +9,15 @@ import ( ) func ioctlLoopCtlGetFree(fd uintptr) (int, error) { - index, _, err := unix.Syscall(unix.SYS_IOCTL, fd, LoopCtlGetFree, 0) - if err != 0 { + index, err := unix.IoctlGetInt(int(fd), LoopCtlGetFree) + if err != nil { return 0, err } - return int(index), nil + return index, nil } func ioctlLoopSetFd(loopFd, sparseFd uintptr) error { - if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, LoopSetFd, sparseFd); err != 0 { + if err := unix.IoctlSetInt(int(loopFd), LoopSetFd, int(sparseFd)); err != nil { return err } return nil @@ -47,7 +47,7 @@ func ioctlLoopGetStatus64(loopFd uintptr) (*loopInfo64, error) { } func ioctlLoopSetCapacity(loopFd uintptr, value int) error { - if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, LoopSetCapacity, uintptr(value)); err != 0 { + if err := unix.IoctlSetInt(int(loopFd), LoopSetCapacity, value); err != nil { return err } return nil