From owner-dev-commits-src-all@freebsd.org Tue Jun 1 13:20:05 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 532866540DD; Tue, 1 Jun 2021 13:20:05 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: from mail-qv1-xf36.google.com (mail-qv1-xf36.google.com [IPv6:2607:f8b0:4864:20::f36]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FvXnx1g77z3Dwm; Tue, 1 Jun 2021 13:20:05 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: by mail-qv1-xf36.google.com with SMTP id a7so7088805qvf.11; Tue, 01 Jun 2021 06:20:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=0l6TOTW+sIc64CnwE7NMyNPD005s9lo9/WooTzDmgr0=; b=IiQ6iR54bTSEdXarzi+UIG+wujr91gyB61tc+QuTHQr/56omR5UUf9vO6RqYJJx/gK 0xMKozK1IYYTfDFar3W65vw+mlVxgVpxymfsPleSrCKryaCfHfqZBy5C/3t65VkoND7p NerlYSr8635beHcS8ruj4ZdSZo1sQBXUXIw1K5aWZuhU2DfOLn33uV1XdTeCBMrftQ9k 53JNBX+NK1I2Q0/ZNBy6dKTYNlrkeRmDZ/eUcFxLhNBwINH9l3yk7x2IBZJnS3LfuHHR olZKslp4C8GtTMzip6RNMt1VxbwjNs/QoMoz1Sbme2rFCsRaBsxcyMapqU9zA1j/ebWO eCUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=0l6TOTW+sIc64CnwE7NMyNPD005s9lo9/WooTzDmgr0=; b=ccG59RWf4BIHVsaP9p3KF/Uf1zAtRaA9Adv0tDf2e1OHD8Zw2UZ8TAlHsrB1nxYdW4 XiLX/IDCPf4ueX1gyZHpNBpVAL59QAachwclUB+yS3QqKA5ogk6/bumBNC2ILXmuwYXX Qcl7Kri3GTs4HO6iC8cVRiByRLHo22gpUnV0Ayk4mJA7/Z2UiRB+d8TKRJBO6rOEMKdD 63oAiqeambqibajl3+SAvNdEQTdHLzhGUxnUbc+KqGZp6fYMy0bT0+f1rT3iccDdlx5g yS4VfNBIgj4CzKFK76QoUZvMoJ+gnvzoUORQUI0wwjWEIoWYX4m5hnZbjpbNCrr0fgWi 2neA== X-Gm-Message-State: AOAM532IJRS/Lk2Pp5468vORQasCod1edU92V/GnUoSqZvvov5ONX9MB 6xgjyAmLj8AlkYvuRxWnK7h8BzgTzPY= X-Google-Smtp-Source: ABdhPJwTGCy7Zog1SSOI9e3durmi0tqghkF9WxugZKRX0vCh1x/JH8G1vLQYH7/mWuF342ksX1qiZw== X-Received: by 2002:a0c:9e50:: with SMTP id z16mr20605696qve.31.1622553603687; Tue, 01 Jun 2021 06:20:03 -0700 (PDT) Received: from nuc ([142.126.172.178]) by smtp.gmail.com with ESMTPSA id z1sm11081654qki.47.2021.06.01.06.20.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Jun 2021 06:20:03 -0700 (PDT) Sender: Mark Johnston Date: Tue, 1 Jun 2021 09:20:04 -0400 From: Mark Johnston To: Dmitry Chagin Cc: src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Subject: Re: git: f3851b235b23 - main - ktrace: Fix a race with fork() Message-ID: References: <202105271953.14RJrN36017670@gitrepo.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 4FvXnx1g77z3Dwm X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Jun 2021 13:20:05 -0000 On Tue, Jun 01, 2021 at 12:44:30PM +0000, Dmitry Chagin wrote: > On Thu, May 27, 2021 at 07:53:23PM +0000, Mark Johnston wrote: > > The branch main has been updated by markj: > > > > URL: https://cgit.FreeBSD.org/src/commit/?id=f3851b235b23d9220ace31bbc89b1fe0a78fc75c > > > > commit f3851b235b23d9220ace31bbc89b1fe0a78fc75c > > Author: Mark Johnston > > AuthorDate: 2021-05-27 19:49:59 +0000 > > Commit: Mark Johnston > > CommitDate: 2021-05-27 19:52:20 +0000 > > > > ktrace: Fix a race with fork() > > > > ktrace(2) may toggle trace points in any of > > 1. a single process > > 2. all members of a process group > > 3. all descendents of the processes in 1 or 2 > > > > In the first two cases, we do not permit the operation if the process is > > being forked or not visible. However, in case 3 we did not enforce this > > restriction for descendents. As a result, the assertions about the child > > in ktrprocfork() may be violated. > > > > Move these checks into ktrops() so that they are applied consistently. > > > > Allow KTROP_CLEAR for nascent processes. Otherwise, there is a window > > where we cannot clear trace points for a nascent child if they are > > inherited from the parent. > > > > Reported by: syzbot+d96676592978f137e05c@syzkaller.appspotmail.com > > Reported by: syzbot+7c98fcf84a4439f2817f@syzkaller.appspotmail.com > > Reviewed by: kib > > MFC after: 1 week > > Sponsored by: The FreeBSD Foundation > > Differential Revision: https://reviews.freebsd.org/D30481 > > --- > > sys/kern/kern_ktrace.c | 43 ++++++++++++++++++++++--------------------- > > 1 file changed, 22 insertions(+), 21 deletions(-) > > > > diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c > > index dc064d9ebd67..4a2dad20b035 100644 > > --- a/sys/kern/kern_ktrace.c > > +++ b/sys/kern/kern_ktrace.c > > @@ -1006,7 +1006,7 @@ sys_ktrace(struct thread *td, struct ktrace_args *uap) > > int facs = uap->facs & ~KTRFAC_ROOT; > > int ops = KTROP(uap->ops); > > int descend = uap->ops & KTRFLAG_DESCEND; > > - int nfound, ret = 0; > > + int ret = 0; > > int flags, error = 0; > > struct nameidata nd; > > struct ktr_io_params *kiop, *old_kiop; > > @@ -1080,42 +1080,31 @@ restart: > > error = ESRCH; > > goto done; > > } > > + > > /* > > * ktrops() may call vrele(). Lock pg_members > > * by the proctree_lock rather than pg_mtx. > > */ > > PGRP_UNLOCK(pg); > > - nfound = 0; > > + if (LIST_EMPTY(&pg->pg_members)) { > > + sx_sunlock(&proctree_lock); > > + error = ESRCH; > > + goto done; > > + } > > LIST_FOREACH(p, &pg->pg_members, p_pglist) { > > PROC_LOCK(p); > > - if (p->p_state == PRS_NEW || > > - p_cansee(td, p) != 0) { > > - PROC_UNLOCK(p); > > - continue; > > - } > > - nfound++; > > if (descend) > > ret |= ktrsetchildren(td, p, ops, facs, kiop); > > else > > ret |= ktrops(td, p, ops, facs, kiop); > > } > > - if (nfound == 0) { > > - sx_sunlock(&proctree_lock); > > - error = ESRCH; > > - goto done; > > - } > > } else { > > /* > > * by pid > > */ > > p = pfind(uap->pid); > > - if (p == NULL) > > + if (p == NULL) { > > error = ESRCH; > > - else > > - error = p_cansee(td, p); > > - if (error) { > > - if (p != NULL) > > - PROC_UNLOCK(p); > > sx_sunlock(&proctree_lock); > > goto done; > > } > > @@ -1187,8 +1176,20 @@ ktrops(struct thread *td, struct proc *p, int ops, int facs, > > PROC_UNLOCK(p); > > return (0); > > } > > - if (p->p_flag & P_WEXIT) { > > - /* If the process is exiting, just ignore it. */ > > + if ((ops == KTROP_SET && p->p_state == PRS_NEW) || !p_cansee(td, p)) { > > ^^^^^^^^^^^^^^ seems that it broke ktrace: > root@mordor:~ # ktrace ls > ktrace: ktrace.out: Operation not permitted Indeed, I pushed a fix to main. Sorry for the breakage.