Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp61865pxa; Tue, 4 Aug 2020 16:35:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZwTVa8AfhHLF5QljBTi0Wu3XoPuQacRlfiAGAnrYNwXOIA+W6QmnK4/OXmUxTLHOiCdFL X-Received: by 2002:a17:907:7204:: with SMTP id dr4mr526487ejc.258.1596584135150; Tue, 04 Aug 2020 16:35:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596584135; cv=none; d=google.com; s=arc-20160816; b=ZlYDRvabcKryP865emEEndhE6vFDDL60iA/AL8GrZHj1JxOabaeAiqy81TLx36X+Xx HMAiSjP3Sd0WpjKK1OK3rOAEbZGmigJegahbXTIVWD//suGM8E6KCDZND5NhP5S2cDlV um7MF3liBQPDPkKCpDGdn/v1Hij84ZP5XSAq1XBTNKLSLWTZVJ/YYAwCP4UEljNEo10C iIRKQQmRK1+z8PgnW4npgy6cFC9UCjQd8hWzQOeW/33eEZyc/9S7fXYi1h2pxOPfGbcu BUTWiTsO23prz7vpBKBiZf5KIQir7X7EMWs7ip8rV5il8Z7Pd2+WNS0WSRdipong6gHh WHOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=vGGF1J+ERy5X4cdVoozyTYTiQ8qrIwcki9GpVzuiAac=; b=xX5gK41HniaeO9kZwqdnq8DcWPorjn7/8D1sgan+KERRuKjwHe4E9ZCLE2aRAdczh+ ISiDFn4TocsRSKBBjTuI93SMtSlUn5dylY9dP24qOBxcSDnQ1HLlJ2tsW3G4X1hdtYm0 V1rzke9dmgHAsxcPl47p98CWoSAffFVdQ8OdlmDLCmQzwLg8KvnrkAim3iimntzg8Ukf DPUh49/7eqWanu8MKrrq0S4pSSwoGIHEauhSucUiRl5D4+ymHKewmq7WZgq19WCiUZty Qgi5rvFDkoRsemOjAVmcO/NjfCfrJvvifpyE6pIDASzZ9rQQ1NojyZ9nPJUftbnLGOLS aKRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=S49gs35w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g1si133399edy.259.2020.08.04.16.35.12; Tue, 04 Aug 2020 16:35:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=S49gs35w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726664AbgHDXev (ORCPT + 99 others); Tue, 4 Aug 2020 19:34:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725946AbgHDXet (ORCPT ); Tue, 4 Aug 2020 19:34:49 -0400 Received: from mail-io1-xd42.google.com (mail-io1-xd42.google.com [IPv6:2607:f8b0:4864:20::d42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 797E5C06174A for ; Tue, 4 Aug 2020 16:34:49 -0700 (PDT) Received: by mail-io1-xd42.google.com with SMTP id a5so28797904ioa.13 for ; Tue, 04 Aug 2020 16:34:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vGGF1J+ERy5X4cdVoozyTYTiQ8qrIwcki9GpVzuiAac=; b=S49gs35wchf/23S3uxZ+WP+evpMxe77HR56i82lZK5NggPBf9C072uwY/MRzBWTJUT uUkbo0r79QLmayMwJRaE7WWdaAvmRYlCw8pmFEetEeImITMz5f2Ch0y9KoVwOU84XUKz akVPWCRo77xUXadUti0PP1Cp9LhmUXz8zP6Tik+uyWkZVOMUlb+rhvvUm/6QBHvpcVS0 GbmAXJIh2pl/ZVu288keExqTOg9TgMKZO8x3TMPp2xYJUgXnTLSFljth/xgJZ3DD5T48 e4WzIsHdU67WlRmMsL0A8ZT3iW5ad5SKqWVlzhjuJoaQoFUlkcMv2avPx8JFFaR/UBv0 8iuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vGGF1J+ERy5X4cdVoozyTYTiQ8qrIwcki9GpVzuiAac=; b=hS28CiYIkA13ULQw+kqB1rzp2FEug5tS8Txj2CMnD+Y2TxshC8rvN0mskC00UnMPEr 7KMn41sRe2OkUv/Nrhe4sKCmga8VebrX5Ba58XioW2nNyd2CKJvMQZKvEa0CVMzDxWZJ 4uNyCQEVeECklus/7w2QlQVeaX4Ji/A+4JDdOiakXssI6Lq3MhDwXfyI/SUmTfqbla2d cVSHty0YlTidcQGoMLH0aMiRd8keFgvfEcrlhobTyoRGQ8gNSJQmkC24Oc2ccBS38iTr ehSohABBXD66QStkqxduHJOhH5kou4tZqf3pin62EBwmUrH+haizQq2uiISHBvn+gFEK 8SjQ== X-Gm-Message-State: AOAM532ZCpL5lC3rH5TEUEbLvtvJvXukKu9VKV3uHcW7j0U5D2GqusBo bYdRrFOiezQ/xFMny5+ro60C1Ozvh+9t7GAz42iaqg== X-Received: by 2002:a5d:824f:: with SMTP id n15mr405195ioo.95.1596584088451; Tue, 04 Aug 2020 16:34:48 -0700 (PDT) MIME-Version: 1.0 References: <20200804203155.2181099-1-lokeshgidra@google.com> <20200804204543.GA1992048@gmail.com> <20200804205846.GB1992048@gmail.com> In-Reply-To: <20200804205846.GB1992048@gmail.com> From: Lokesh Gidra Date: Tue, 4 Aug 2020 16:34:37 -0700 Message-ID: Subject: Re: [PATCH] Userfaultfd: Avoid double free of userfault_ctx and remove O_CLOEXEC To: Eric Biggers Cc: Alexander Viro , Stephen Smalley , casey@schaufler-ca.com, James Morris , Kalesh Singh , Daniel Colascione , Suren Baghdasaryan , Linux FS Devel , linux-kernel , Nick Kralevich , Jeffrey Vander Stoep , Calin Juravle , kernel-team@android.com, yanfei.xu@windriver.com, syzbot+75867c44841cb6373570@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 4, 2020 at 1:58 PM Eric Biggers wrote: > > On Tue, Aug 04, 2020 at 01:49:30PM -0700, Lokesh Gidra wrote: > > On Tue, Aug 4, 2020 at 1:45 PM Eric Biggers wrote: > > > > > > On Tue, Aug 04, 2020 at 01:31:55PM -0700, Lokesh Gidra wrote: > > > > when get_unused_fd_flags returns error, ctx will be freed by > > > > userfaultfd's release function, which is indirectly called by fput(). > > > > Also, if anon_inode_getfile_secure() returns an error, then > > > > userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx. > > > > > > > > Also, the O_CLOEXEC was inadvertently added to the call to > > > > get_unused_fd_flags() [1]. > > > > > > > > Adding Al Viro's suggested-by, based on [2]. > > > > > > > > [1] https://lore.kernel.org/lkml/1f69c0ab-5791-974f-8bc0-3997ab1d61ea@dancol.org/ > > > > [2] https://lore.kernel.org/lkml/20200719165746.GJ2786714@ZenIV.linux.org.uk/ > > > > > > > > Fixes: d08ac70b1e0d (Wire UFFD up to SELinux) > > > > Suggested-by: Al Viro > > > > Reported-by: syzbot+75867c44841cb6373570@syzkaller.appspotmail.com > > > > Signed-off-by: Lokesh Gidra > > > > > > What branch does this patch apply to? Neither mainline nor linux-next works. > > > > > On James Morris' tree (secure_uffd_v5.9 branch). > > > > For those of us not "in the know", that apparently means branch secure_uffd_v5.9 > of https://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security.git > > Perhaps it would make more sense to resend your original patch series with this > fix folded in? > OK. I'll resend the whole patch series with the fixes soon. > > diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c > > index ae859161908f..e15eb8fdc083 100644 > > --- a/fs/userfaultfd.c > > +++ b/fs/userfaultfd.c > > @@ -2042,24 +2042,18 @@ SYSCALL_DEFINE1(userfaultfd, int, flags) > > O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS), > > NULL); > > if (IS_ERR(file)) { > > - fd = PTR_ERR(file); > > - goto out; > > + userfaultfd_ctx_put(ctx); > > + return PTR_ERR(file); > > } > > > > - fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); > > + fd = get_unused_fd_flags(O_RDONLY); > > if (fd < 0) { > > fput(file); > > - goto out; > > + return fd; > > } > > > > ctx->owner = file_inode(file); > > fd_install(fd, file); > > - > > -out: > > - if (fd < 0) { > > - mmdrop(ctx->mm); > > - kmem_cache_free(userfaultfd_ctx_cachep, ctx); > > - } > > return fd; > > This introduces the opposite bug: now it's hardcoded to *not* use O_CLOEXEC, > instead of using the flag the user passed in the flags argument to the syscall. I get your point. I agree the flags passed in to the syscall should be used. > > - Eric