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 X-Spam-Level: X-Spam-Status: No, score=-11.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3678C4338F for ; Fri, 20 Aug 2021 05:33:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6CFE560F92 for ; Fri, 20 Aug 2021 05:33:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6CFE560F92 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 13AC06B007B; Fri, 20 Aug 2021 01:33:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0EBAF6B007D; Fri, 20 Aug 2021 01:33:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1D188D0001; Fri, 20 Aug 2021 01:33:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0084.hostedemail.com [216.40.44.84]) by kanga.kvack.org (Postfix) with ESMTP id D96536B007B for ; Fri, 20 Aug 2021 01:33:52 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 8C933809B046 for ; Fri, 20 Aug 2021 05:33:52 +0000 (UTC) X-FDA: 78494342304.07.2CE92A0 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf02.hostedemail.com (Postfix) with ESMTP id 345077001A08 for ; Fri, 20 Aug 2021 05:33:52 +0000 (UTC) Received: by mail-pj1-f46.google.com with SMTP id oa17so6621038pjb.1 for ; Thu, 19 Aug 2021 22:33:52 -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=t77CbxubGxEGTMmMwfQTPijc/96kf1szGuSMv1EFAD4=; b=Ja222Isqq11kr4ZhqVigTCcrL9fIFtekeWme0RhNsM6otO6PdQquD7rbRU6+ee4EKm XhdcOSsskOxlBbZcG1GD5chnwgliVOp9yQsp5Nu7/g4B1ZIGmfUmWUnWSqXnXG/mH/vN QiqPfBEv/7xk8Mw9NlHeSrwnpWhRQHDn5K0ck= 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=t77CbxubGxEGTMmMwfQTPijc/96kf1szGuSMv1EFAD4=; b=C91NX5srR5uo/ezAm8xACxKgVPhmCvML+qHEXDx6h0Wh/1Vn9dZlI+QdRJGwsU/Mh4 G3BKzR9t3zpmRv+JT2XekETpyOU4tO/UlpjMItx+7/GWEYkYOOsQ93olyB2ud6E6ZEVN 1KzyeBizsZxrsNqJBoMbu4bw7CqtoVsrAMKY8uYoiLzT+r0+ihG9cbvl40HuQQnUAVpo JLyWZ9BiTrXtxsxI+hZ1YXsB+9vPZBDkih7fDQ/Xdj6uHeoPtOKkEJxSLXf0EaKJTbO5 A5DmQGZJBWJAP+pjdyelUR8Vzts41PV4N8up5Yom9EAJrIQVesn0bRTAB9Ny6VO8UdQT jUwg== X-Gm-Message-State: AOAM533swrH2NE1nikZfIIrFZzJNQ1TroCDzcSiGbHAhObgBvVlo4aBx frslE+6ijqGDHfFcvuBtIHwCwQ== X-Google-Smtp-Source: ABdhPJzzhNgBKICixn1BmX+FEFq9VN9d5pl11Uew/GhdXyhQuIKrjSjYMnX4OzZV4gM7ZsT0Y14CTw== X-Received: by 2002:a17:90a:ba0b:: with SMTP id s11mr2736424pjr.96.1629437631270; Thu, 19 Aug 2021 22:33:51 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id r78sm5560641pfc.206.2021.08.19.22.33.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Aug 2021 22:33:50 -0700 (PDT) Date: Thu, 19 Aug 2021 22:33:49 -0700 From: Kees Cook To: Jordy Zomer Cc: linux-kernel@vger.kernel.org, Andrew Morton , linux-mm@kvack.org, Mike Rapoport , James Bottomley Subject: Re: [PATCH] mm/secretmem: use refcount_t instead of atomic_t Message-ID: <202108192227.8BE02F1C@keescook> References: <20210820043339.2151352-1-jordy@pwning.systems> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210820043339.2151352-1-jordy@pwning.systems> X-Rspamd-Queue-Id: 345077001A08 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Ja222Isq; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf02.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.46 as permitted sender) smtp.mailfrom=keescook@chromium.org X-Rspamd-Server: rspam04 X-Stat-Signature: dsg5fai1p9drngd7p6cy4nr5rssu651x X-HE-Tag: 1629437632-103304 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: On Fri, Aug 20, 2021 at 06:33:38AM +0200, Jordy Zomer wrote: > When a secret memory region is active, memfd_secret disables > hibernation. One of the goals is to keep the secret data from being > written to persistent-storage. > > It accomplishes this by maintaining a reference count to > `secretmem_users`. Once this reference is held your system can not be > hibernated due to the check in `hibernation_available()`. However, > because `secretmem_users` is of type `atomic_t`, reference counter > overflows are possible. It's an unlikely condition to hit given max-open-fds, etc, but there's no reason to leave this weakness. Changing this to refcount_t is easy and better than using atomic_t. Reviewed-by: Kees Cook > As you can see there's an `atomic_inc` for each `memfd` that is opened > in the `memfd_secret` syscall. If a local attacker succeeds to open 2^32 > memfd's, the counter will wrap around to 0. This implies that you may > hibernate again, even though there are still regions of this secret > memory, thereby bypassing the security check. IMO, this hibernation check is also buggy, since it looks to be vulnerable to ToCToU: processes aren't frozen when hibernation_available() checks secretmem_users(), so a process could add one and fill it before the process freezer stops it. And of course, there's still the ptrace hole[1], which is think is quite serious as it renders the entire defense moot. -Kees [1] https://lore.kernel.org/lkml/202105071620.E834B1FA92@keescook/ -- Kees Cook