1// Copyright 2019 The ChromiumOS Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5//go:build libc_exec 6// +build libc_exec 7 8package main 9 10// #include <errno.h> 11// #include <stdio.h> 12// #include <stdlib.h> 13// #include <string.h> 14// #include <unistd.h> 15// #include <sys/types.h> 16// #include <sys/wait.h> 17// 18// int libc_exec(const char *pathname, char *const argv[], char *const envp[]) { 19// // Since fork() brings us to one thread, we can only use async-signal-safe funcs below. 20// pid_t pid = fork(); 21// if (pid == 0) { 22// // crbug.com/1166017: we're (very rarely) getting ERESTARTSYS on some builders. 23// // Documentation indicates that this is a bug in the kernel. Work around it by 24// // retrying. 25 is an arbitrary retry number that Should Be Enough For Anyone(TM). 25// int i = 0; 26// for (; i < 25; i++) { 27// execve(pathname, argv, envp); 28// if (errno != 512) { 29// break; 30// } 31// // Sleep a bit. Not sure if this helps, but if the condition we're seeing is 32// // transient, it *hopefully* should. nanosleep isn't async-signal safe, so 33// // we have to live with sleep() 34// sleep(1); 35// } 36// fprintf(stderr, "exec failed (errno: %d)\n", errno); 37// _exit(1); 38// } 39// if (pid < 0) { 40// return errno; 41// } 42// 43// int wstatus; 44// pid_t waited = waitpid(pid, &wstatus, 0); 45// if (waited == -1) { 46// return errno; 47// } 48// exit(WEXITSTATUS(wstatus)); 49//} 50import "C" 51import ( 52 "os/exec" 53 "unsafe" 54) 55 56// Replacement for syscall.Execve that uses the libc. 57// This allows tools that rely on intercepting syscalls via 58// LD_PRELOAD to work properly (e.g. gentoo sandbox). 59// Note that this changes the go binary to be a dynamically linked one. 60// See crbug.com/1000863 for details. 61// To use this version of exec, please add '-tags libc_exec' when building Go binary. 62// Without the tags, libc_exec.go will not be used. 63 64func execCmd(env env, cmd *command) error { 65 freeList := []unsafe.Pointer{} 66 defer func() { 67 for _, ptr := range freeList { 68 C.free(ptr) 69 } 70 }() 71 72 goStrToC := func(goStr string) *C.char { 73 cstr := C.CString(goStr) 74 freeList = append(freeList, unsafe.Pointer(cstr)) 75 return cstr 76 } 77 78 goSliceToC := func(goSlice []string) **C.char { 79 // len(goSlice)+1 as the c array needs to be null terminated. 80 cArray := C.malloc(C.size_t(len(goSlice)+1) * C.size_t(unsafe.Sizeof(uintptr(0)))) 81 freeList = append(freeList, cArray) 82 83 // Convert the C array to a Go Array so we can index it. 84 // Note: Storing pointers to the c heap in go pointer types is ok 85 // (see https://golang.org/cmd/cgo/). 86 cArrayForIndex := (*[1<<30 - 1]*C.char)(cArray) 87 for i, str := range goSlice { 88 cArrayForIndex[i] = goStrToC(str) 89 } 90 cArrayForIndex[len(goSlice)] = nil 91 92 return (**C.char)(cArray) 93 } 94 95 execCmd := exec.Command(cmd.Path, cmd.Args...) 96 mergedEnv := mergeEnvValues(env.environ(), cmd.EnvUpdates) 97 if errno := C.libc_exec(goStrToC(execCmd.Path), goSliceToC(execCmd.Args), goSliceToC(mergedEnv)); errno != 0 { 98 return newErrorwithSourceLocf("exec error: %d", errno) 99 } 100 101 return nil 102} 103