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 77998C433EF for ; Mon, 25 Oct 2021 19:30:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DDC916101C for ; Mon, 25 Oct 2021 19:30:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DDC916101C 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 0DE31940008; Mon, 25 Oct 2021 15:30:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 08D5D940007; Mon, 25 Oct 2021 15:30:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EBE67940008; Mon, 25 Oct 2021 15:30:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0019.hostedemail.com [216.40.44.19]) by kanga.kvack.org (Postfix) with ESMTP id D9ACB940007 for ; Mon, 25 Oct 2021 15:30:00 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 8BD802BFCE for ; Mon, 25 Oct 2021 19:30:00 +0000 (UTC) X-FDA: 78735950160.21.5333CE9 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf16.hostedemail.com (Postfix) with ESMTP id A0FC6F000091 for ; Mon, 25 Oct 2021 19:29:55 +0000 (UTC) Received: by mail-pf1-f170.google.com with SMTP id m14so11698405pfc.9 for ; Mon, 25 Oct 2021 12:29:59 -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=CJiasAWjpoN1bOCkWk9RL6DVjlUXQV+BQQ0HuB5WrXA=; b=LL/FMPprNtV88++Wudo+pFadJq0f6H5RmMxWs3l1E1iJNXWcowIokPgCgd0HLhInjF nCIOsVCS7hsmvGxilh+sdZhj+iir5zv5Ny8NB5sz225exGIjmwXFZ14CSBzOlxr3wKju 94aZrdaRhKvw+RYXAl1qQbq5xE4Q0WX6sMXr0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=CJiasAWjpoN1bOCkWk9RL6DVjlUXQV+BQQ0HuB5WrXA=; b=l3WYYEb5WFrZB7m6+9PcKyfiIybctAL4aqxUZELDXIQQ3gQu8XVq0b92nl5R6YW4Lb jeJDV40oQ2Sh5xjGat0w8sftUugGzx0jGCkVsDVGbfxI2U2yfF8QFDgmrooll4uUQ+aS axooYalL5nhH+QHOoutQeAV8wADfGm+KOTy2WGsCqMNF/N9yOWcKI/RbaSiIZux0iwBx KVHLtelyuUcMGNCNO5P0Awyrwnec81aPbNR505QaE1GhTzBKzh0mhBpn656VgqDDS/XI aQ3ugvk3J3rxsUP1NaMcEqneBQ1OKON3tJuTnxnQeO7/U7wWFu3EFg2OV5YgtcrxLIAB Lhdg== X-Gm-Message-State: AOAM531tt3/aOP4klwhDnO1XOYLQUfEX45mmLJlKMIpnr77q3IEbyHA7 /UETheK20QoiMo/cI2kds0GeVA== X-Google-Smtp-Source: ABdhPJxS79fizC3hkJ53knMOyPrU9iw8czM0SHXOfrQymGZvEAEK2sfxwqA6TPCg2w7k2s7AoLs7gQ== X-Received: by 2002:a62:8496:0:b0:47b:d189:5ce9 with SMTP id k144-20020a628496000000b0047bd1895ce9mr18055408pfd.19.1635190187884; Mon, 25 Oct 2021 12:29:47 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q6sm16648707pgc.1.2021.10.25.12.29.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 12:29:47 -0700 (PDT) Date: Mon, 25 Oct 2021 12:29:46 -0700 From: Kees Cook To: "Matthew Wilcox (Oracle)" Cc: Linus Torvalds , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Jordy Zomer , James Bottomley , Mike Rapoport , Andrew Morton Subject: Re: [PATCH] secretmem: Prevent secretmem_users from wrapping to zero Message-ID: <202110251225.D01841AE67@keescook> References: <20211025181634.3889666-1-willy@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211025181634.3889666-1-willy@infradead.org> X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A0FC6F000091 X-Stat-Signature: x6n9ru1mkp5xhee6g8mx5kuf5gpurhx1 Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="LL/FMPpr"; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf16.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.170 as permitted sender) smtp.mailfrom=keescook@chromium.org X-HE-Tag: 1635190195-888233 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 Mon, Oct 25, 2021 at 07:16:34PM +0100, Matthew Wilcox (Oracle) wrote: > Commit 110860541f44 ("mm/secretmem: use refcount_t instead of atomic_t") > attempted to fix the problem of secretmem_users wrapping to zero and > allowing suspend once again. Prevent secretmem_users from wrapping to > zero by forbidding new users if the number of users has wrapped from > positive to negative. This stops a long way short of reaching the > necessary 4 billion users, so there's no need to be clever with special > anti-wrap types or checking the return value from atomic_inc(). I still prefer refcount_t here because it provides deterministic saturation, but the risk right now is so narrow ("don't hibernate"), I'm not going to fight for it. I think it'd be fine to use it initialized to 1, and have the removal check for == 0 as a failure state, which would deterministically cover the underflow case too. -Kees > > Signed-off-by: Matthew Wilcox (Oracle) > Cc: Jordy Zomer > Cc: Kees Cook , > Cc: James Bottomley > Cc: Mike Rapoport > Cc: Andrew Morton > --- > mm/secretmem.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/mm/secretmem.c b/mm/secretmem.c > index 030f02ddc7c1..c2dda408bb36 100644 > --- a/mm/secretmem.c > +++ b/mm/secretmem.c > @@ -203,6 +203,8 @@ SYSCALL_DEFINE1(memfd_secret, unsigned int, flags) > > if (flags & ~(SECRETMEM_FLAGS_MASK | O_CLOEXEC)) > return -EINVAL; > + if (atomic_read(&secretmem_users) < 0) > + return -ENFILE; > > fd = get_unused_fd_flags(flags & O_CLOEXEC); > if (fd < 0) > -- > 2.33.0 > -- Kees Cook