# git rev-parse -q --verify dbb5afad100a828c97e012c6106566d99f041db6^{commit} dbb5afad100a828c97e012c6106566d99f041db6 already have revision, skipping fetch # git checkout -q -f -B kisskb dbb5afad100a828c97e012c6106566d99f041db6 # git clean -qxdf # < git log -1 # commit dbb5afad100a828c97e012c6106566d99f041db6 # Author: Oleg Nesterov # Date: Wed May 12 15:33:08 2021 +0200 # # ptrace: make ptrace() fail if the tracee changed its pid unexpectedly # # Suppose we have 2 threads, the group-leader L and a sub-theread T, # both parked in ptrace_stop(). Debugger tries to resume both threads # and does # # ptrace(PTRACE_CONT, T); # ptrace(PTRACE_CONT, L); # # If the sub-thread T execs in between, the 2nd PTRACE_CONT doesn not # resume the old leader L, it resumes the post-exec thread T which was # actually now stopped in PTHREAD_EVENT_EXEC. In this case the # PTHREAD_EVENT_EXEC event is lost, and the tracer can't know that the # tracee changed its pid. # # This patch makes ptrace() fail in this case until debugger does wait() # and consumes PTHREAD_EVENT_EXEC which reports old_pid. This affects all # ptrace requests except the "asynchronous" PTRACE_INTERRUPT/KILL. # # The patch doesn't add the new PTRACE_ option to not complicate the API, # and I _hope_ this won't cause any noticeable regression: # # - If debugger uses PTRACE_O_TRACEEXEC and the thread did an exec # and the tracer does a ptrace request without having consumed # the exec event, it's 100% sure that the thread the ptracer # thinks it is targeting does not exist anymore, or isn't the # same as the one it thinks it is targeting. # # - To some degree this patch adds nothing new. In the scenario # above ptrace(L) can fail with -ESRCH if it is called after the # execing sub-thread wakes the leader up and before it "steals" # the leader's pid. # # Test-case: # # #include # #include # #include # #include # #include # #include # #include # #include # # void *tf(void *arg) # { # execve("/usr/bin/true", NULL, NULL); # assert(0); # # return NULL; # } # # int main(void) # { # int leader = fork(); # if (!leader) { # kill(getpid(), SIGSTOP); # # pthread_t th; # pthread_create(&th, NULL, tf, NULL); # for (;;) # pause(); # # return 0; # } # # waitpid(leader, NULL, WSTOPPED); # # ptrace(PTRACE_SEIZE, leader, 0, # PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC); # waitpid(leader, NULL, 0); # # ptrace(PTRACE_CONT, leader, 0,0); # waitpid(leader, NULL, 0); # # int status, thread = waitpid(-1, &status, 0); # assert(thread > 0 && thread != leader); # assert(status == 0x80137f); # # ptrace(PTRACE_CONT, thread, 0,0); # /* # * waitid() because waitpid(leader, &status, WNOWAIT) does not # * report status. Why ???? # * # * Why WEXITED? because we have another kernel problem connected # * to mt-exec. # */ # siginfo_t info; # assert(waitid(P_PID, leader, &info, WSTOPPED|WEXITED|WNOWAIT) == 0); # assert(info.si_pid == leader && info.si_status == 0x0405); # # /* OK, it sleeps in ptrace(PTRACE_EVENT_EXEC == 0x04) */ # assert(ptrace(PTRACE_CONT, leader, 0,0) == -1); # assert(errno == ESRCH); # # assert(leader == waitpid(leader, &status, WNOHANG)); # assert(status == 0x04057f); # # assert(ptrace(PTRACE_CONT, leader, 0,0) == 0); # # return 0; # } # # Signed-off-by: Oleg Nesterov # Reported-by: Simon Marchi # Acked-by: "Eric W. Biederman" # Acked-by: Pedro Alves # Acked-by: Simon Marchi # Acked-by: Jan Kratochvil # Signed-off-by: Linus Torvalds # < /opt/cross/kisskb/korg/gcc-8.1.0-nolibc/m68k-linux/bin/m68k-linux-gcc --version # < /opt/cross/kisskb/korg/gcc-8.1.0-nolibc/m68k-linux/bin/m68k-linux-ld --version # < git log --format=%s --max-count=1 dbb5afad100a828c97e012c6106566d99f041db6 # < make -s -j 120 ARCH=m68k O=/kisskb/build/linus_m68k-defconfig_m68k-gcc8 CROSS_COMPILE=/opt/cross/kisskb/korg/gcc-8.1.0-nolibc/m68k-linux/bin/m68k-linux- defconfig # < make -s -j 120 ARCH=m68k O=/kisskb/build/linus_m68k-defconfig_m68k-gcc8 CROSS_COMPILE=/opt/cross/kisskb/korg/gcc-8.1.0-nolibc/m68k-linux/bin/m68k-linux- help # make -s -j 120 ARCH=m68k O=/kisskb/build/linus_m68k-defconfig_m68k-gcc8 CROSS_COMPILE=/opt/cross/kisskb/korg/gcc-8.1.0-nolibc/m68k-linux/bin/m68k-linux- olddefconfig # make -s -j 120 ARCH=m68k O=/kisskb/build/linus_m68k-defconfig_m68k-gcc8 CROSS_COMPILE=/opt/cross/kisskb/korg/gcc-8.1.0-nolibc/m68k-linux/bin/m68k-linux- /kisskb/src/arch/m68k/mvme16x/config.c: In function 'mvme16x_hwclk': /kisskb/src/arch/m68k/mvme16x/config.c:439:2: warning: #warning check me! [-Wcpp] #warning check me! ^~~~~~~ /kisskb/src/arch/m68k/mvme147/config.c: In function 'mvme147_hwclk': /kisskb/src/arch/m68k/mvme147/config.c:174:2: warning: #warning check me! [-Wcpp] #warning check me! ^~~~~~~ /kisskb/src/drivers/i2c/busses/i2c-icy.c: In function 'icy_probe': /kisskb/src/drivers/i2c/busses/i2c-icy.c:126:24: warning: unused variable 'new_fwnode' [-Wunused-variable] struct fwnode_handle *new_fwnode; ^~~~~~~~~~ In file included from /kisskb/src/drivers/net/ethernet/8390/xsurf100.c:48: /kisskb/src/drivers/net/ethernet/8390/lib8390.c:995:27: warning: '____alloc_ei_netdev' defined but not used [-Wunused-function] static struct net_device *____alloc_ei_netdev(int size) ^~~~~~~~~~~~~~~~~~~ /kisskb/src/drivers/net/ethernet/8390/lib8390.c:957:13: warning: '__ei_set_multicast_list' defined but not used [-Wunused-function] static void __ei_set_multicast_list(struct net_device *dev) ^~~~~~~~~~~~~~~~~~~~~~~ /kisskb/src/drivers/net/ethernet/8390/lib8390.c:857:33: warning: '__ei_get_stats' defined but not used [-Wunused-function] static struct net_device_stats *__ei_get_stats(struct net_device *dev) ^~~~~~~~~~~~~~ /kisskb/src/drivers/net/ethernet/8390/lib8390.c:512:13: warning: '__ei_poll' defined but not used [-Wunused-function] static void __ei_poll(struct net_device *dev) ^~~~~~~~~ /kisskb/src/drivers/net/ethernet/8390/lib8390.c:303:20: warning: '__ei_start_xmit' defined but not used [-Wunused-function] static netdev_tx_t __ei_start_xmit(struct sk_buff *skb, ^~~~~~~~~~~~~~~ /kisskb/src/drivers/net/ethernet/8390/lib8390.c:257:13: warning: '__ei_tx_timeout' defined but not used [-Wunused-function] static void __ei_tx_timeout(struct net_device *dev, unsigned int txqueue) ^~~~~~~~~~~~~~~ /kisskb/src/drivers/net/ethernet/8390/lib8390.c:233:12: warning: '__ei_close' defined but not used [-Wunused-function] static int __ei_close(struct net_device *dev) ^~~~~~~~~~ /kisskb/src/drivers/net/ethernet/8390/lib8390.c:204:12: warning: '__ei_open' defined but not used [-Wunused-function] static int __ei_open(struct net_device *dev) ^~~~~~~~~ Completed OK # rm -rf /kisskb/build/linus_m68k-defconfig_m68k-gcc8 # Build took: 0:01:10.609570