From patchwork Tue Sep 21 20:02:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Amit X-Patchwork-Id: 12509419 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=-12.0 required=3.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 B6BA9C433FE for ; Wed, 22 Sep 2021 03:33:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4D2AA6115A for ; Wed, 22 Sep 2021 03:33:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 4D2AA6115A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 97A2F6B006C; Tue, 21 Sep 2021 23:33:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92AC66B0072; Tue, 21 Sep 2021 23:33:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F306900002; Tue, 21 Sep 2021 23:33:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0142.hostedemail.com [216.40.44.142]) by kanga.kvack.org (Postfix) with ESMTP id 6FCB96B006C for ; Tue, 21 Sep 2021 23:33:40 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 0FBF8181AEF15 for ; Wed, 22 Sep 2021 03:33:40 +0000 (UTC) X-FDA: 78613789800.16.AF6353A Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf23.hostedemail.com (Postfix) with ESMTP id CADE090000AF for ; Wed, 22 Sep 2021 03:33:39 +0000 (UTC) Received: by mail-pj1-f52.google.com with SMTP id on12-20020a17090b1d0c00b001997c60aa29so3028887pjb.1 for ; Tue, 21 Sep 2021 20:33:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=eQTATNYUh692iWKEczEMwnFaT7RAPjE0jlz0RulYirw=; b=a/IqhzdDO75haJO7x6YBCLKX61gvnWPnUA4hZU4h7adRLEL2brjyV+XQDAyvyK7RO/ 3pa+OLSKzIJRMUOoGOq3a2I/0xNYnMqq6vNheE363QmeOYlI9jzH22QbyOt7CIB7hd+b gF021OZ3oW+xUwQSkWki7+Woq9O3lYkVgJ54VZgyaWumhJ5rAQbC3R8HwWpIuXsAgi3w M75ic2MX9kRYp8XQWjvgTPXDnm520xhiKE4JULBJYivvhgGcpUITEDoXpx6GYuuzWXMY F3XshnhEQA5VuxYuRkllF7THaRCkA5gKjeLujlpumJ0n8hhKun71FGadjlIA2hVwNfJG BxYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=eQTATNYUh692iWKEczEMwnFaT7RAPjE0jlz0RulYirw=; b=xLJVzN8bmduDgKPWimFAD9ZJ00coFo1C3+5buK4/SCXt4lZGK/39+pVQlFvabbGoN/ dGGbU5sqDvK9RJouDDVv47xtiq8xie//9OLFOlyhnuO2YM0ajPudSbSbHb1p0eT9p7vB 7FmcEyrEDHL8gZESL5KeMQMEo0BJgXkQQ9zy+S4jNfkrd7PIweMP+N7Vq/Y5PQRvkquS ZU4/8Uiu5TSZ1zK0HbrC2cjJ/T4zByeYNiGImTj7KQOf6Nj8tCE6yXdIwtpwmVckOQ5N zbLPjSg/6KNwKd7WsdAJCn7zOb8zNKUL8crz+hWtWEtIWKvHee+p6x6JQk7ikQYLm6zz geJg== X-Gm-Message-State: AOAM530L3Zc9MnCYW5wx+buKAWEdojAgQB5XDAvOmCYoPEwErxkbv8uX QODxvtgxaIcJ7XA/fe3mQOk= X-Google-Smtp-Source: ABdhPJz2KrQag+OKt4QC063tE+Ulganv9Gy25Vr8NKz3BeobUb+mp52m7woVoBnaRctAJza3g5U/pA== X-Received: by 2002:a17:90a:353:: with SMTP id 19mr8936162pjf.83.1632281618361; Tue, 21 Sep 2021 20:33:38 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id o15sm537269pfg.14.2021.09.21.20.33.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Sep 2021 20:33:37 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Nadav Amit , Peter Xu , Andrea Arcangeli , stable@vger.kernel.org Subject: [PATCH] userfaultfd: fix a race between writeprotect and exit_mmap() Date: Tue, 21 Sep 2021 13:02:47 -0700 Message-Id: <20210921200247.25749-1-namit@vmware.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: CADE090000AF Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="a/IqhzdD"; dmarc=pass (policy=none) header.from=gmail.com; spf=none (imf23.hostedemail.com: domain of mail-pj1-f52.google.com has no SPF policy when checking 209.85.216.52) smtp.helo=mail-pj1-f52.google.com X-Stat-Signature: qsbnnu3ggf9i6x9buo6t4u8nbyth3dcf X-HE-Tag: 1632281619-405868 X-Bogosity: Ham, tests=bogofilter, spamicity=0.006583, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Nadav Amit A race is possible when a process exits, its VMAs are removed by exit_mmap() and at the same time userfaultfd_writeprotect() is called. The race was detected by KASAN on a development kernel, but it appears to be possible on vanilla kernels as well. Use mmget_not_zero() to prevent the race as done in other userfaultfd operations. Cc: Peter Xu Cc: Andrea Arcangeli Cc: stable@vger.kernel.org Fixes: 63b2d4174c4ad ("userfaultfd: wp: add the writeprotect API to userfaultfd ioctl") Signed-off-by: Nadav Amit Tested-by: Li Wang Reviewed-by: Peter Xu --- fs/userfaultfd.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 003f0d31743e..22bf14ab2d16 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1827,9 +1827,15 @@ static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx, if (mode_wp && mode_dontwake) return -EINVAL; - ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start, - uffdio_wp.range.len, mode_wp, - &ctx->mmap_changing); + if (mmget_not_zero(ctx->mm)) { + ret = mwriteprotect_range(ctx->mm, uffdio_wp.range.start, + uffdio_wp.range.len, mode_wp, + &ctx->mmap_changing); + mmput(ctx->mm); + } else { + return -ESRCH; + } + if (ret) return ret;