Best Syzkaller code snippet using host.isSupportedSyscall
syscalls_linux.go
Source:syscalls_linux.go
...38 if c.Name == "ioctl$EXT4_IOC_SHUTDOWN" && sandbox == "none" {39 // Don't shutdown root filesystem.40 return false, "unsafe with sandbox=none"41 }42 return isSupportedSyscall(c, target)43}44func isSupportedSyscall(c *prog.Syscall, target *prog.Target) (bool, string) {45 // There are 3 possible strategies for detecting supported syscalls:46 // 1. Executes all syscalls with presumably invalid arguments and check for ENOprog.47 // But not all syscalls are safe to execute. For example, pause will hang,48 // while setpgrp will push the process into own process group.49 // 2. Check presence of /sys/kernel/debug/tracing/events/syscalls/sys_enter_* files.50 // This requires root and CONFIG_FTRACE_SYSCALLS. Also it lies for some syscalls.51 // For example, on x86_64 it says that sendfile is not present (only sendfile64).52 // 3. Check sys_syscallname in /proc/kallsyms.53 // Requires CONFIG_KALLSYMS.54 // Kallsyms seems to be the most reliable and fast. That's what we use first.55 // If kallsyms is not present, we fallback to execution of syscalls.56 kallsymsOnce.Do(func() {57 kallsyms, _ := ioutil.ReadFile("/proc/kallsyms")58 if len(kallsyms) == 0 {59 return60 }61 kallsymsSyscallSet = parseKallsyms(kallsyms, target.Arch)62 })63 if !testFallback && len(kallsymsSyscallSet) != 0 {64 r, v := isSupportedKallsyms(c)65 return r, v66 }67 return isSupportedTrial(c)68}69func parseKallsyms(kallsyms []byte, arch string) map[string]bool {70 set := make(map[string]bool)71 var re *regexp.Regexp72 switch arch {73 case "386", "amd64":74 re = regexp.MustCompile(` T (__ia32_|__x64_)?sys_([^\n]+)\n`)75 case "arm", "arm64":76 re = regexp.MustCompile(` T (__arm64_)?sys_([^\n]+)\n`)77 case "ppc64le":78 re = regexp.MustCompile(` T ()?sys_([^\n]+)\n`)79 case "mips64le":80 re = regexp.MustCompile(` T sys_(mips_)?([^\n]+)\n`)81 case "s390x":82 re = regexp.MustCompile(` T (__s390_|__s390x_)?sys_([^\n]+)\n`)83 case "riscv64":84 re = regexp.MustCompile(` T sys_(riscv_)?([^\n]+)\n`)85 default:86 panic("unsupported arch for kallsyms parsing")87 }88 matches := re.FindAllSubmatch(kallsyms, -1)89 for _, m := range matches {90 name := string(m[2])91 log.Logf(2, "found in kallsyms: %v", name)92 set[name] = true93 }94 return set95}96func isSupportedKallsyms(c *prog.Syscall) (bool, string) {97 name := c.CallName98 if newname := kallsymsRenameMap[name]; newname != "" {99 name = newname100 }101 if !kallsymsSyscallSet[name] {102 return false, fmt.Sprintf("sys_%v is not present in /proc/kallsyms", name)103 }104 return true, ""105}106func isSupportedTrial(c *prog.Syscall) (bool, string) {107 switch c.CallName {108 // These known to cause hangs.109 case "exit", "pause":110 return true, ""111 }112 trialMu.Lock()113 defer trialMu.Unlock()114 if res, ok := trialSupported[c.NR]; ok {115 return res, "ENOSYS"116 }117 cmd := osutil.Command(os.Args[0])118 cmd.Env = []string{fmt.Sprintf("SYZ_TRIAL_TEST=%v", c.NR)}119 _, err := osutil.Run(10*time.Second, cmd)120 res := err != nil121 trialSupported[c.NR] = res122 return res, "ENOSYS"123}124func init() {125 str := os.Getenv("SYZ_TRIAL_TEST")126 if str == "" {127 return128 }129 nr, err := strconv.Atoi(str)130 if err != nil {131 panic(err)132 }133 arg := ^uintptr(0) - 1e4 // something as invalid as possible134 _, _, err = syscall.Syscall6(uintptr(nr), arg, arg, arg, arg, arg, arg)135 if err == syscall.ENOSYS {136 os.Exit(0)137 }138 os.Exit(1)139}140// Some syscall names diverge in __NR_* consts and kallsyms.141// umount2 is renamed to umount in arch/x86/entry/syscalls/syscall_64.tbl.142// Where umount is renamed to oldumount is unclear.143var (144 kallsymsOnce sync.Once145 kallsymsSyscallSet map[string]bool146 kallsymsRenameMap = map[string]string{147 "umount": "oldumount",148 "umount2": "umount",149 "stat": "newstat",150 }151 trialMu sync.Mutex152 trialSupported = make(map[uint64]bool)153 filesystems []byte154 filesystemsOnce sync.Once155 lsmOnce sync.Once156 lsmError error157 lsmDisabled map[string]bool158)159// The function is lengthy as it handles all pseudo-syscalls,160// but it does not seem to cause comprehension problems as there is no shared state.161// Splitting this per-syscall will only increase code size.162func isSupportedSyzkall(c *prog.Syscall, target *prog.Target, sandbox string) (bool, string) {163 switch c.CallName {164 case "syz_open_dev":165 return isSupportedSyzOpenDev(sandbox, c)166 case "syz_open_procfs":167 return true, ""168 case "syz_open_pts":169 return true, ""170 case "syz_emit_ethernet", "syz_extract_tcp_res":171 reason := checkNetInjection()172 return reason == "", reason173 case "syz_emit_vhci":174 reason := checkVhciInjection()175 return reason == "", reason176 case "syz_usb_connect", "syz_usb_connect_ath9k", "syz_usb_disconnect",177 "syz_usb_control_io", "syz_usb_ep_write", "syz_usb_ep_read":178 reason := checkUSBEmulation()179 return reason == "", reason180 case "syz_kvm_setup_cpu":181 switch c.Name {182 case "syz_kvm_setup_cpu$x86":183 if runtime.GOARCH == "amd64" || runtime.GOARCH == "386" {184 return true, ""185 }186 case "syz_kvm_setup_cpu$arm64":187 if runtime.GOARCH == "arm64" {188 return true, ""189 }190 }191 return false, "unsupported arch"192 case "syz_init_net_socket":193 // Unfortunately this only works with sandbox none at the moment.194 // The problem is that setns of a network namespace requires CAP_SYS_ADMIN195 // in the target namespace, and we've lost all privs in the init namespace196 // during creation of a user namespace.197 if ok, reason := onlySandboxNone(sandbox); !ok {198 return false, reason199 }200 return isSupportedSocket(c)201 case "syz_genetlink_get_family_id":202 fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_GENERIC)203 if fd == -1 {204 return false, fmt.Sprintf("socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC) failed: %v", err)205 }206 syscall.Close(fd)207 return true, ""208 case "syz_mount_image":209 if ok, reason := onlySandboxNone(sandbox); !ok {210 return ok, reason211 }212 fstype, ok := extractStringConst(c.Args[0].Type)213 if !ok {214 panic("syz_mount_image arg is not string")215 }216 return isSupportedFilesystem(fstype)217 case "syz_read_part_table":218 return onlySandboxNone(sandbox)219 case "syz_execute_func":220 return true, ""221 case "syz_io_uring_submit", "syz_io_uring_complete", "syz_io_uring_setup", "syz_memcpy_off":222 // syz_memcpy_off is only used for io_uring descriptions, thus, enable it223 // only if io_uring syscalls are enabled.224 ioUringSyscallName := "io_uring_setup"225 ioUringSyscall := target.SyscallMap[ioUringSyscallName]226 if ioUringSyscall == nil {227 return false, fmt.Sprintf("sys_%v is not present in the target", ioUringSyscallName)228 }229 return isSupportedSyscall(ioUringSyscall, target)230 }231 panic("unknown syzkall: " + c.Name)232}233func isSupportedSyzOpenDev(sandbox string, c *prog.Syscall) (bool, string) {234 if _, ok := c.Args[0].Type.(*prog.ConstType); ok {235 // This is for syz_open_dev$char/block.236 return true, ""237 }238 fname, ok := extractStringConst(c.Args[0].Type)239 if !ok {240 panic("first open arg is not a pointer to string const")241 }242 if strings.Contains(fname, "/dev/raw/raw#") {243 // For syz_open_dev$char_raw, these files don't exist initially....
isSupportedSyscall
Using AI Code Generation
1import (2func main() {3 if h.IsSupportedSyscall("execve") {4 fmt.Println("syscall is supported")5 } else {6 fmt.Println("syscall is not supported")7 }8}
isSupportedSyscall
Using AI Code Generation
1import (2func main() {3 fmt.Println(syscall.IsSupportedSyscall(1))4}5Related Posts: Golang | os.Hostname() method6Golang | os.Hostname() method Golang | os.Getwd() method7Golang | os.Getwd() method Golang | os.Chdir() method8Golang | os.Chdir() method Golang | os.Chmod() method9Golang | os.Chmod() method Golang | os.Chown() method10Golang | os.Chown() method Golang | os.Chroot() method11Golang | os.Chroot() method Golang | os.Executable() method12Golang | os.Executable() method Golang | os.Exit() method13Golang | os.Exit() method Golang | os.Getegid() method14Golang | os.Getegid() method Golang | os.Geteuid() method15Golang | os.Geteuid() method Golang | os.Getgid() method16Golang | os.Getgid() method Golang | os.Getgroups() method17Golang | os.Getgroups() method Golang | os.Getpagesize() method18Golang | os.Getpagesize() method Golang | os.Getpid() method19Golang | os.Getpid() method Golang | os.Getppid() method20Golang | os.Getppid() method Golang | os.Getuid() method21Golang | os.Getuid() method Golang | os.Hostname() method22Golang | os.Hostname() method Golang | os.IsPathSeparator() method23Golang | os.IsPathSeparator() method Golang | os.LookupEnv() method24Golang | os.LookupEnv() method Golang | os.Mkdir() method25Golang | os.Mkdir() method Golang | os.MkdirAll() method26Golang | os.MkdirAll() method Golang | os.MkdirTemp() method27Golang | os.MkdirTemp() method Golang | os.NewFile() method28Golang | os.NewFile() method Golang | os.Open() method29Golang | os.Open() method Golang | os.OpenFile() method30Golang | os.OpenFile() method Golang | os.Readlink() method31Golang | os.Readlink() method Golang | os.Remove
isSupportedSyscall
Using AI Code Generation
1import (2func main() {3 fmt.Println(host.IsSupportedSyscall(syscall.SYS_GETTIMEOFDAY))4 fmt.Println(host.IsSupportedSyscall(syscall.SYS_SOCKET))5 fmt.Println(host.IsSupportedSyscall(syscall.SYS_GETTIMEOFDAY))6 fmt.Println(host.IsSupportedSyscall(syscall.SYS_GETTIMEOFDAY))7}8Your name to display (optional):9Your name to display (optional):
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!