Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6715377ybi; Wed, 29 May 2019 11:58:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqzMnp1d+qwO6VCC7/wFr45d6xD9+OwAPeHVU5NwjmI9CdfEaKtVc5CSWoBUOMhyIsDoo6CB X-Received: by 2002:a63:6f06:: with SMTP id k6mr9776993pgc.170.1559156338982; Wed, 29 May 2019 11:58:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559156338; cv=none; d=google.com; s=arc-20160816; b=vS39cRdBhJv3lsk0RkOGSYS9DljDBWkX4Fh/FnDJpLRme0hgVC/vJjdBJ2fWcTBc9t Bu1rZqvxWYpE0EBS9GNYFWjIno5CfD2NozVakN2/qgIe84xPvh6m5LhecCDmMc8F07Zx bsD1Jq6Ojhpk48AHDR37dM8c4tliDARPKXMUU7OuSpEg/dds/MkkDhAM2IULmhDiKTYw uILX5rCNUFy7zsgN4ZKcd7WgajzIqaxmda2Y7cbEp2LVMK6MMyo68LD6yydIDV02/J8l Z4fpF7gb6GgCxTUSK0XsvR4OwpDLWzrBO67ZMfjPfYQZr5qxrvhV47k5H29UDEZoGYcD zf5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=znPmL9ULodOyU0PyZn6MpAN5tuQwaKzazg+9AyEInzs=; b=Z/G/AnERpFrS32yvS8dITFpbH/r7seS+mxHiiTR+g7EcEZupzrYi+L409rd9JPPL7q syqgoXToYKfqUhKCm4o80r1JiOlC7aEJOcGPl2InvCmpKy+LUoz8mCh27xiuePF62sXO TL6ZUTlI+l1vWZYW96zASJw8U8HNcZ0VfSMidIrMeWSyg5My3KY9DBT5fxG2ddMcZbYe PNGFeTl3vcj4VzA4N02IRXMOK8PYrdD2wG6GJaVKfVSq+R3dQPFx+gha6wyHcGn3lrqA Hq8fG4FKbxhv+zrqBl9R3UYyuJZ4+nsugktX1RDbSC2iiJkllgpkpfIpzjLSm7jmKjxE 7FHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TgS75tg2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t1si251718pjv.82.2019.05.29.11.58.42; Wed, 29 May 2019 11:58:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TgS75tg2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726520AbfE2Szr (ORCPT + 99 others); Wed, 29 May 2019 14:55:47 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:41225 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725956AbfE2Szq (ORCPT ); Wed, 29 May 2019 14:55:46 -0400 Received: by mail-pl1-f194.google.com with SMTP id s24so1307876plr.8 for ; Wed, 29 May 2019 11:55:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=znPmL9ULodOyU0PyZn6MpAN5tuQwaKzazg+9AyEInzs=; b=TgS75tg2RnACM5k+Dq/XHwu8cvK3iWBMwBpBhZ00zh8zt/P/CCim/B+nbQOSe+Fs38 Ycsh/5dKTPJCIpt98v1Eqc/E8kiGtJtKntCyiRzkPBARg58okkUfYrRaeulpxpnmd0Uh t/rr+WCi4oPXm0b3Hi5Uqx1er3NIT+fijO8EE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=znPmL9ULodOyU0PyZn6MpAN5tuQwaKzazg+9AyEInzs=; b=YBDwpEMRIkZ8nWBiyhKiHgySFTJPz/wqy3YZQj8Pjhmp42uoQuTZUv2+NUvQbdMUfg slhqLYNB+0fWy4zAqyhu8/+qOkjDvCUImQnMB2BV80qYzSvxu//zqfIE/Cc+JJHVyrqF mqP+c4yL9hgqyTvEbUODLxJW9fEMHv9o85j1YZQwdTBboqBpDca1lnoYJAJXw8WF4TRI URqY2BJT81i1iIWXk9YwqVA5vdj6a0tRLLmZBjdT5S8HEdjuX858Xm2b+71AFFcG8ofb 6YXJe8zAH2BJFKgkKzz5EB5v8hTHjnKtTn1DJtqV+ZA5hfa7GeFBg4dwaB0tBLO32w6d /y7A== X-Gm-Message-State: APjAAAWMWheu9WS8LWE8SGvdjwUi1PbXU4/MKKR2rUBD7ITLUOdJ9Q58 ftAw0hOtgYd44PAZcCfZTZjrmQ== X-Received: by 2002:a17:902:27a8:: with SMTP id d37mr34741076plb.150.1559156146261; Wed, 29 May 2019 11:55:46 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id u2sm304783pjv.30.2019.05.29.11.55.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 29 May 2019 11:55:45 -0700 (PDT) Date: Wed, 29 May 2019 11:55:44 -0700 From: Kees Cook To: Jann Horn Cc: Oleg Nesterov , "Eric W . Biederman" , Andrew Morton , David Howells , linux-kernel@vger.kernel.org Subject: Re: [PATCH] ptrace: restore smp_rmb() in __ptrace_may_access() Message-ID: <201905291154.E4A0CB717@keescook> References: <20190529113157.227380-1-jannh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190529113157.227380-1-jannh@google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 29, 2019 at 01:31:57PM +0200, Jann Horn wrote: > Restore the read memory barrier in __ptrace_may_access() that was deleted > a couple years ago. Also add comments on this barrier and the one it pairs > with to explain why they're there (as far as I understand). > > Fixes: bfedb589252c ("mm: Add a user_ns owner to mm_struct and fix ptrace permission checks") > Cc: stable@vger.kernel.org > Signed-off-by: Jann Horn Ah, thanks! Nice find. Acked-by: Kees Cook (And, yeah, Eric, I say snag it if you've got stuff queued up...) -Kees > --- > (I have no clue whatsoever what the relevant tree for this is, but I > guess Oleg is the relevant maintainer?) > > kernel/cred.c | 9 +++++++++ > kernel/ptrace.c | 10 ++++++++++ > 2 files changed, 19 insertions(+) > > diff --git a/kernel/cred.c b/kernel/cred.c > index 45d77284aed0..07e069d00696 100644 > --- a/kernel/cred.c > +++ b/kernel/cred.c > @@ -450,6 +450,15 @@ int commit_creds(struct cred *new) > if (task->mm) > set_dumpable(task->mm, suid_dumpable); > task->pdeath_signal = 0; > + /* > + * If a task drops privileges and becomes nondumpable, > + * the dumpability change must become visible before > + * the credential change; otherwise, a __ptrace_may_access() > + * racing with this change may be able to attach to a task it > + * shouldn't be able to attach to (as if the task had dropped > + * privileges without becoming nondumpable). > + * Pairs with a read barrier in __ptrace_may_access(). > + */ > smp_wmb(); > } > > diff --git a/kernel/ptrace.c b/kernel/ptrace.c > index 5710d07e67cf..e54452c2954b 100644 > --- a/kernel/ptrace.c > +++ b/kernel/ptrace.c > @@ -324,6 +324,16 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode) > return -EPERM; > ok: > rcu_read_unlock(); > + /* > + * If a task drops privileges and becomes nondumpable (through a syscall > + * like setresuid()) while we are trying to access it, we must ensure > + * that the dumpability is read after the credentials; otherwise, > + * we may be able to attach to a task that we shouldn't be able to > + * attach to (as if the task had dropped privileges without becoming > + * nondumpable). > + * Pairs with a write barrier in commit_creds(). > + */ > + smp_rmb(); > mm = task->mm; > if (mm && > ((get_dumpable(mm) != SUID_DUMP_USER) && > -- > 2.22.0.rc1.257.g3120a18244-goog > -- Kees Cook