Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp614021pxa; Tue, 4 Aug 2020 13:32:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwZTOQFNqJ6zycPENZd9tSr9jrn6t7TedqzUHAvNDTzNK8ISNkEXCZpjAi5iEFB7/MD/uon X-Received: by 2002:a17:906:d786:: with SMTP id pj6mr23388331ejb.261.1596573158379; Tue, 04 Aug 2020 13:32:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596573158; cv=none; d=google.com; s=arc-20160816; b=swuuWgGV6JrK7K6nqjrOpf4mbsJdkS4S3v6tPzz382WPnIlpqd40P0a2vzAquO7Kq/ NnFBa0E+q9ks+ymjyR+BQXdGpkN1GSW/dt6ULOa0wfTl3JNPUd97DtY+aIo+1XAN9fkp cR7a7cXCKmbnETL5VpSmlNk753VB8RS4WcUI53DjfRIGXEnXkZAhl2jcVT/jKNAlGtF+ dpe2LpK2vnzMXXBjBQICTyVa7OsnJpykXd7xPGkUEYzHnxDtwhP1zHF/wtxYh9pT6muC ycAXbfjrRNOzEUsFOWzFkVVHbNukEgK5quYGBwjLo9uUyR5TSSSCElu6wkq36h6kdZYC 60zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=TE39gsqa8qAtAr57skK2bIVW3cJaqxb5kdkpRE6BXx4=; b=hHCOlU5MiaC+XpuSRg6+SiP5YLuYqZt4/IkiwjcevY1/lbPa7ic7J+xR/PJ79KmG/N Ai61y6FTlP9cXcg1wW3AC5wRqGfCdjkQdBoY6rrJgeLSf7Uz1bIoZkf/fbsE5Y7ooYEX sJ/hWSJehafxWXF9V8nwoth6j2t2JW4XRbxHI0hR6FprPuYu9WzKiXei8Uz4H2kzYjW5 YPn29ZLVa07V5rOAauKe+4LwebV0F31iU5sFPYqhVuVUW+OJxEfiC8BJJKFhRfMGtb17 FpOufmRom68hUmNfFn4iVfiasZjyiBg+Rcyo26NrQjHBnwnX8PlAESXSgNW0kgjXgb39 bzzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=PRIjpcg+; 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 s23si2770773edy.158.2020.08.04.13.32.14; Tue, 04 Aug 2020 13:32:38 -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=PRIjpcg+; 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 S1726859AbgHDUcK (ORCPT + 99 others); Tue, 4 Aug 2020 16:32:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726580AbgHDUcI (ORCPT ); Tue, 4 Aug 2020 16:32:08 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF6CBC061756 for ; Tue, 4 Aug 2020 13:32:07 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id b127so36432190ybh.21 for ; Tue, 04 Aug 2020 13:32:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=TE39gsqa8qAtAr57skK2bIVW3cJaqxb5kdkpRE6BXx4=; b=PRIjpcg+RHpob503zkgZ/kH0zrL/dFq+o15VbDIE+7fG//gkTUyv+BUYuFJGk77mXH J7o0LRArbZY03nboYkobXyq5Mclw1iNcylfxM3oqjztUXa4b16oc9VAe8xlPpDq3VbNL HI7knhb8Bd2R6LXwLc4T7PdFbknrVoH3KQ1n0BjGHFivqbIbJQfRFJ1y0AwH6+9NVr43 HUWdRLf+B2SEM4ZF4dJMT3G/MR6QGRE36f/gVO+YIJZKdCGAn9ByjwNRcb+g7GrstnU4 +4PcVun0xBJKIGgBUNUyn3FwtjVmpVdQelavHxqvU9qFVlEhe7P2/E9kh/EoaHxvU/jH Rtsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=TE39gsqa8qAtAr57skK2bIVW3cJaqxb5kdkpRE6BXx4=; b=P8gTITJndzLv43xCd/2ExeZyEvV6TAcF+0X1glPKROTeh4QOlhecj9ppicRZXF0CKI QYbiVQWKp7tztonBzbdRwAHiUB+J9DLiusOfK45AYXW2z+WvJP92DmX3Vumh+Amkdxkx CZDJnA3V4icYFRb1krfN5s1aq3fXmCOitMrzHrvuAoAqz8zToFaeDeie9WjP8i8jn+9Y IYAcGNuD7DTddco/v8SR9ytOMnZYkIQYWY9uoaVVuUvmjOAq0LKlaTZpxGaqWO+lW6yR wVVsEmCuL5Sqrnfw61p1kpABHlO9aRNBY8Tg5gKYgKQ0BWWomT1ReFYatES6HekICIqQ pz3g== X-Gm-Message-State: AOAM531zTFSpf3IUxLXb8G0Bi7mo9+YxK4cL+vsLPV2pdJYEknCUackj +lpczs51c4q5X5C1bCThA9+2JCcrz6of3Rcbzg== X-Received: by 2002:a25:d946:: with SMTP id q67mr33939817ybg.423.1596573126838; Tue, 04 Aug 2020 13:32:06 -0700 (PDT) Date: Tue, 4 Aug 2020 13:31:55 -0700 Message-Id: <20200804203155.2181099-1-lokeshgidra@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.28.0.163.g6104cc2f0b6-goog Subject: [PATCH] Userfaultfd: Avoid double free of userfault_ctx and remove O_CLOEXEC From: Lokesh Gidra To: viro@zeniv.linux.org.uk, stephen.smalley.work@gmail.com, casey@schaufler-ca.com, jmorris@namei.org Cc: kaleshsingh@google.com, dancol@dancol.org, surenb@google.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, nnk@google.com, jeffv@google.com, calin@google.com, kernel-team@android.com, yanfei.xu@windriver.com, Lokesh Gidra , 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 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 --- fs/userfaultfd.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) 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; } -- 2.28.0.163.g6104cc2f0b6-goog