From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12ACFC433EF for ; Thu, 21 Oct 2021 16:47:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8319C60FD8 for ; Thu, 21 Oct 2021 16:47:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8319C60FD8 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=pwning.systems Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 255866B006C; Thu, 21 Oct 2021 12:47:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 206AC940007; Thu, 21 Oct 2021 12:47:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0CE966B0073; Thu, 21 Oct 2021 12:47:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0164.hostedemail.com [216.40.44.164]) by kanga.kvack.org (Postfix) with ESMTP id F2F3F6B006C for ; Thu, 21 Oct 2021 12:47:37 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A552D8249980 for ; Thu, 21 Oct 2021 16:47:37 +0000 (UTC) X-FDA: 78721025754.37.F9DF2CC Received: from MTA-05-3.privateemail.com (mta-05-3.privateemail.com [68.65.122.15]) by imf09.hostedemail.com (Postfix) with ESMTP id B9E4F300010F for ; Thu, 21 Oct 2021 16:47:34 +0000 (UTC) Received: from mta-05.privateemail.com (localhost [127.0.0.1]) by mta-05.privateemail.com (Postfix) with ESMTP id 175DE180030B; Thu, 21 Oct 2021 12:47:35 -0400 (EDT) Received: from APP-03 (unknown [10.50.14.153]) by mta-05.privateemail.com (Postfix) with ESMTPA id BDC4E18000BF; Thu, 21 Oct 2021 12:47:34 -0400 (EDT) Date: Thu, 21 Oct 2021 12:47:34 -0400 (EDT) From: Jordy Zomer To: Kees Cook , Andrew Morton Cc: Mike Rapoport , linux-mm@kvack.org, Dmitry Vyukov , James Bottomley , David Hildenbrand , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Message-ID: <813047099.1390041.1634834854748@privateemail.com> In-Reply-To: <20211021154046.880251-1-keescook@chromium.org> References: <20211021154046.880251-1-keescook@chromium.org> Subject: Re: [PATCH] mm/secretmem: Avoid letting secretmem_users drop to zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal X-Mailer: Open-Xchange Mailer v7.10.5-Rev24 X-Originating-Client: open-xchange-appsuite X-Virus-Scanned: ClamAV using ClamSMTP X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: B9E4F300010F X-Stat-Signature: 98ay879twq55bgyxws5z3sbopjdttfyi Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=none; spf=none (imf09.hostedemail.com: domain of jordy@pwning.systems has no SPF policy when checking 68.65.122.15) smtp.mailfrom=jordy@pwning.systems X-HE-Tag: 1634834854-747863 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Reviewed-by: Jordy Zomer That's a nice find, Dmitry! Thanks. Also, thank you for writing a patch in such a short period of time, Kees:) Looks good to me! > On 10/21/2021 11:40 AM Kees Cook wrote: > > > Quoting Dmitry: "refcount_inc() needs to be done before fd_install(). > After fd_install() finishes, the fd can be used by userspace and we can > have secret data in memory before the refcount_inc(). > > A straightforward mis-use where a user will predict the returned fd > in another thread before the syscall returns and will use it to store > secret data is somewhat dubious because such a user just shoots themself > in the foot. > > But a more interesting mis-use would be to close the predicted fd and > decrement the refcount before the corresponding refcount_inc, this way > one can briefly drop the refcount to zero while there are other users > of secretmem." > > Move fd_install() after refcount_inc(). > > Cc: Mike Rapoport > Cc: Jordy Zomer > Cc: linux-mm@kvack.org > Reported-by: Dmitry Vyukov > Link: https://lore.kernel.org/lkml/CACT4Y+b1sW6-Hkn8HQYw_SsT7X3tp-CJNh2ci0wG3ZnQz9jjig@mail.gmail.com > Fixes: 9a436f8ff631 ("PM: hibernate: disable when there are active secretmem users") > Signed-off-by: Kees Cook > --- > mm/secretmem.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/mm/secretmem.c b/mm/secretmem.c > index 1fea68b8d5a6..924d84ba481f 100644 > --- a/mm/secretmem.c > +++ b/mm/secretmem.c > @@ -217,8 +217,8 @@ SYSCALL_DEFINE1(memfd_secret, unsigned int, flags) > > file->f_flags |= O_LARGEFILE; > > - fd_install(fd, file); > refcount_inc(&secretmem_users); > + fd_install(fd, file); > return fd; > > err_put_fd: > -- > 2.30.2