Fri Jan 3 00:37:30 2020 UTC ()
Correct the rule to iterate over LWPs with PT_LWPNEXT and PT_LWPINFO

Correctly exclude dead/dying/zombie and system threads.

Fixes kern/54802 (gdb -p kills the kernel) by <martin>


(kamil)
diff -r1.76 -r1.77 src/sys/kern/sys_ptrace_common.c

cvs diff -r1.76 -r1.77 src/sys/kern/sys_ptrace_common.c (expand / switch to unified diff)

--- src/sys/kern/sys_ptrace_common.c 2019/12/26 08:52:38 1.76
+++ src/sys/kern/sys_ptrace_common.c 2020/01/03 00:37:29 1.77
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: sys_ptrace_common.c,v 1.76 2019/12/26 08:52:38 kamil Exp $ */ 1/* $NetBSD: sys_ptrace_common.c,v 1.77 2020/01/03 00:37:29 kamil Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -108,27 +108,27 @@ @@ -108,27 +108,27 @@
108 108
109/* 109/*
110 * References: 110 * References:
111 * (1) Bach's "The Design of the UNIX Operating System", 111 * (1) Bach's "The Design of the UNIX Operating System",
112 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution, 112 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
113 * (3) the "4.4BSD Programmer's Reference Manual" published 113 * (3) the "4.4BSD Programmer's Reference Manual" published
114 * by USENIX and O'Reilly & Associates. 114 * by USENIX and O'Reilly & Associates.
115 * The 4.4BSD PRM does a reasonably good job of documenting what the various 115 * The 4.4BSD PRM does a reasonably good job of documenting what the various
116 * ptrace() requests should actually do, and its text is quoted several times 116 * ptrace() requests should actually do, and its text is quoted several times
117 * in this file. 117 * in this file.
118 */ 118 */
119 119
120#include <sys/cdefs.h> 120#include <sys/cdefs.h>
121__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.76 2019/12/26 08:52:38 kamil Exp $"); 121__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.77 2020/01/03 00:37:29 kamil Exp $");
122 122
123#ifdef _KERNEL_OPT 123#ifdef _KERNEL_OPT
124#include "opt_ptrace.h" 124#include "opt_ptrace.h"
125#include "opt_ktrace.h" 125#include "opt_ktrace.h"
126#include "opt_pax.h" 126#include "opt_pax.h"
127#include "opt_compat_netbsd32.h" 127#include "opt_compat_netbsd32.h"
128#endif 128#endif
129 129
130#if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \ 130#if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
131 && !defined(_RUMPKERNEL) 131 && !defined(_RUMPKERNEL)
132#define COMPAT_NETBSD32 132#define COMPAT_NETBSD32
133#endif 133#endif
134 134
@@ -747,27 +747,28 @@ ptrace_lwpinfo(struct proc *t, struct lw @@ -747,27 +747,28 @@ ptrace_lwpinfo(struct proc *t, struct lw
747 lwp_delref(*lt); 747 lwp_delref(*lt);
748 mutex_enter(t->p_lock); 748 mutex_enter(t->p_lock);
749 if (tmp == 0) 749 if (tmp == 0)
750 *lt = lwp_find_first(t); 750 *lt = lwp_find_first(t);
751 else { 751 else {
752 *lt = lwp_find(t, tmp); 752 *lt = lwp_find(t, tmp);
753 if (*lt == NULL) { 753 if (*lt == NULL) {
754 mutex_exit(t->p_lock); 754 mutex_exit(t->p_lock);
755 return ESRCH; 755 return ESRCH;
756 } 756 }
757 *lt = LIST_NEXT(*lt, l_sibling); 757 *lt = LIST_NEXT(*lt, l_sibling);
758 } 758 }
759 759
760 while (*lt != NULL && !lwp_alive(*lt)) 760 while (*lt != NULL && (!lwp_alive(*lt) ||
 761 ((*lt)->l_flag & LW_SYSTEM) != 0))
761 *lt = LIST_NEXT(*lt, l_sibling); 762 *lt = LIST_NEXT(*lt, l_sibling);
762 763
763 pl.pl_lwpid = 0; 764 pl.pl_lwpid = 0;
764 pl.pl_event = 0; 765 pl.pl_event = 0;
765 if (*lt) { 766 if (*lt) {
766 lwp_addref(*lt); 767 lwp_addref(*lt);
767 pl.pl_lwpid = (*lt)->l_lid; 768 pl.pl_lwpid = (*lt)->l_lid;
768 769
769 if ((*lt)->l_flag & LW_WSUSPEND) 770 if ((*lt)->l_flag & LW_WSUSPEND)
770 pl.pl_event = PL_EVENT_SUSPENDED; 771 pl.pl_event = PL_EVENT_SUSPENDED;
771 /* 772 /*
772 * If we match the lwp, or it was sent to every lwp, 773 * If we match the lwp, or it was sent to every lwp,
773 * we set PL_EVENT_SIGNAL. 774 * we set PL_EVENT_SIGNAL.
@@ -812,28 +813,28 @@ ptrace_lwpstatus(struct proc *t, struct  @@ -812,28 +813,28 @@ ptrace_lwpstatus(struct proc *t, struct
812 lwpid_t tmp = pls.pl_lwpid; 813 lwpid_t tmp = pls.pl_lwpid;
813 mutex_enter(t->p_lock); 814 mutex_enter(t->p_lock);
814 if (tmp == 0) 815 if (tmp == 0)
815 *lt = lwp_find_first(t); 816 *lt = lwp_find_first(t);
816 else { 817 else {
817 *lt = lwp_find(t, tmp); 818 *lt = lwp_find(t, tmp);
818 if (*lt == NULL) { 819 if (*lt == NULL) {
819 mutex_exit(t->p_lock); 820 mutex_exit(t->p_lock);
820 return ESRCH; 821 return ESRCH;
821 } 822 }
822 *lt = LIST_NEXT(*lt, l_sibling); 823 *lt = LIST_NEXT(*lt, l_sibling);
823 } 824 }
824 825
825 while (*lt != NULL && !lwp_alive(*lt) && 826 while (*lt != NULL && (!lwp_alive(*lt) ||
826 ((*lt)->l_flag & LW_SYSTEM) != 0) 827 ((*lt)->l_flag & LW_SYSTEM) != 0))
827 *lt = LIST_NEXT(*lt, l_sibling); 828 *lt = LIST_NEXT(*lt, l_sibling);
828 829
829 if (*lt == NULL) { 830 if (*lt == NULL) {
830 memset(&pls, 0, sizeof(pls)); 831 memset(&pls, 0, sizeof(pls));
831 mutex_exit(t->p_lock); 832 mutex_exit(t->p_lock);
832 goto out; 833 goto out;
833 } 834 }
834 lwp_addref(*lt); 835 lwp_addref(*lt);
835 mutex_exit(t->p_lock); 836 mutex_exit(t->p_lock);
836 837
837 pls.pl_lwpid = (*lt)->l_lid; 838 pls.pl_lwpid = (*lt)->l_lid;
838 } else { 839 } else {
839 if ((error = ptrace_update_lwp(t, lt, pls.pl_lwpid)) != 0) 840 if ((error = ptrace_update_lwp(t, lt, pls.pl_lwpid)) != 0)