From patchwork Mon Oct 25 18:16:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthew Wilcox (Oracle)" X-Patchwork-Id: 12582589 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 10FEBC433F5 for ; Mon, 25 Oct 2021 18:19:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A0F2560720 for ; Mon, 25 Oct 2021 18:19:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org A0F2560720 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id E77E680007; Mon, 25 Oct 2021 14:19:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E26BE940007; Mon, 25 Oct 2021 14:19:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3D5180007; Mon, 25 Oct 2021 14:19:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0168.hostedemail.com [216.40.44.168]) by kanga.kvack.org (Postfix) with ESMTP id C585B940007 for ; Mon, 25 Oct 2021 14:19:40 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 80FF518025863 for ; Mon, 25 Oct 2021 18:19:40 +0000 (UTC) X-FDA: 78735772920.01.6729395 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf07.hostedemail.com (Postfix) with ESMTP id 054F0100009E for ; Mon, 25 Oct 2021 18:19:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:In-Reply-To:References; bh=ZUjha4DhPn98bTvVPY+JcUANbAx2g59wrv6gM1+ix+I=; b=LEaDBOiD45JaEtaEe0dVnWgwSS TL7+hD3Kt7dR7Ls6PWNwGh2qjoF5RgC6IPN3s7YSeMvovoNk2IZEUiDBTdYXi8CFsdj+HsdCI7HrN 1al0pykeF4gXsc17uJuuWA/hg6rKcV/vSZnIz+xqH7E9NFOVy0dNNwgHw+V+2TL8Zmi144K5AaXOm 88aw5XlSk8pPwq6gO8LjP+qKn+bd9MvOwfkOe0dsh62dQIJLZB/7LDP7TSPsPXhVl3fH2tsWrfqXw qA38PQ6vwCjBA6wrF3UUgisgEb42NYrgbvvSLSDiqNTsnT0/c0vbsevl99kUjjYM6QoeZKH9+l4J1 ySCxoYIw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mf4WY-00GJuC-EZ; Mon, 25 Oct 2021 18:16:51 +0000 From: "Matthew Wilcox (Oracle)" To: Linus Torvalds , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , Jordy Zomer , Kees Cook , James Bottomley , Mike Rapoport , Andrew Morton Subject: [PATCH] secretmem: Prevent secretmem_users from wrapping to zero Date: Mon, 25 Oct 2021 19:16:34 +0100 Message-Id: <20211025181634.3889666-1-willy@infradead.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 054F0100009E Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=LEaDBOiD; spf=none (imf07.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none X-Stat-Signature: 4seokntafr4noffq3am1f56fdkauau8j X-Rspamd-Server: rspam06 X-HE-Tag: 1635185978-548341 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: 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(). 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)