Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp17928077ybl; Thu, 2 Jan 2020 15:00:30 -0800 (PST) X-Google-Smtp-Source: APXvYqz0nDgWpEKiJRnvnMMa64FVqHl2km1YPy997F/2d76k/erpbs2HZJ+8qKCOeqlr+KF9wyR8 X-Received: by 2002:aca:5658:: with SMTP id k85mr2970958oib.153.1578006029853; Thu, 02 Jan 2020 15:00:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578006029; cv=none; d=google.com; s=arc-20160816; b=Fsk4Jz7ntEHkDkWe/zc9dPgBLHpT91e7jO5ly4IOEyCrp8EsFJbAjGR/hl2pUmpDpE QHoMFz/dLIXKEtT/jFgVf1/TSb3nz2PUu1b79o8V0Ue7BsTKc+Zer1wd/D0Kncw6z10J l9owb/vr2rjkMHmFXSjDGJ7BKD8287wEAPhN6yBcmih1qxQLHCLHGpC80Phj+EE8zk9m cqZjCY23eogIq/0q6poqBFslGGNlpWsuHEAbqfnWyFiEhZfjkPKzfUDzjx7zKqjVPgnu nK+a5om8jEr+qyFWwUqBEspS9Qtknd4TcmtmabcNChaVQnqS/Z6oJkx02ZWXIc464SMs 9+SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=f8GZlrlxg7yoHfoQEuAl+O9cLOlDDzo/c8yRv5CpbU4=; b=DBxLJd/rpmxPBwK4w6rjCHzJz6RBDynK6gFUU0/xbGTN5dqyJj+U02Oaj35DkBMu0B gYvAgbCJW+onAEaD3ZCnh+TKTUyXG9MaSt9oLWPnWvJU4jHvPfPXiV/5RB24DRyY7WP2 Uqf0X+r2WeUpMazXKwRu5afkSfl1h/2r2lywqOoJJ9JDdFX7PdabcBzPcHgUtBp8y0RL WtniEhiir2kFm2qFkZVwQ3UFOXOO4vSZyGxaYhKsfOTbyRxOtF3yR6xbG+BZ60AKnkpl eVwJqjgHQZfy0+/BgG0JpAqMsM8o0v2TAALj9PKjQDiVvwgle0a4/oFSxXdpdOmtcAM9 ZNqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vxk2GpUY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f187si27669777oia.218.2020.01.02.15.00.17; Thu, 02 Jan 2020 15:00:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vxk2GpUY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728481AbgABW6I (ORCPT + 99 others); Thu, 2 Jan 2020 17:58:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:57634 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727701AbgABWQC (ORCPT ); Thu, 2 Jan 2020 17:16:02 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B29F621582; Thu, 2 Jan 2020 22:16:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578003361; bh=OhY9CAsLkxIZOLlGYxFrgHEj3kuAGOIgU1jy5q4CoRU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vxk2GpUYMS4cPlMdygqkpq3RUrtrXpEe7jCFIiffEP7svpLto3W9pmuR2yOSVQ5/n 6hl78bLeavBMGbu9HfoSJG5vL76sZPlUzYhk6K7MDJ9oDWONeh9bAq/QJKIjmg4NEr 61GrGLwZw12kxqY88S+qIq1sq2lxz2QOh9Dxe9B4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mike Rapoport , Andrea Arcangeli , Daniel Colascione , Jann Horn , Lokesh Gidra , Nick Kralevich , Nosh Minwalla , Pavel Emelyanov , Tim Murray , Aleksa Sarai , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.4 129/191] userfaultfd: require CAP_SYS_PTRACE for UFFD_FEATURE_EVENT_FORK Date: Thu, 2 Jan 2020 23:06:51 +0100 Message-Id: <20200102215843.542976659@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200102215829.911231638@linuxfoundation.org> References: <20200102215829.911231638@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mike Rapoport [ Upstream commit 3c1c24d91ffd536de0a64688a9df7f49e58fadbc ] A while ago Andy noticed (http://lkml.kernel.org/r/CALCETrWY+5ynDct7eU_nDUqx=okQvjm=Y5wJvA4ahBja=CQXGw@mail.gmail.com) that UFFD_FEATURE_EVENT_FORK used by an unprivileged user may have security implications. As the first step of the solution the following patch limits the availably of UFFD_FEATURE_EVENT_FORK only for those having CAP_SYS_PTRACE. The usage of CAP_SYS_PTRACE ensures compatibility with CRIU. Yet, if there are other users of non-cooperative userfaultfd that run without CAP_SYS_PTRACE, they would be broken :( Current implementation of UFFD_FEATURE_EVENT_FORK modifies the file descriptor table from the read() implementation of uffd, which may have security implications for unprivileged use of the userfaultfd. Limit availability of UFFD_FEATURE_EVENT_FORK only for callers that have CAP_SYS_PTRACE. Link: http://lkml.kernel.org/r/1572967777-8812-2-git-send-email-rppt@linux.ibm.com Signed-off-by: Mike Rapoport Reviewed-by: Andrea Arcangeli Cc: Daniel Colascione Cc: Jann Horn Cc: Lokesh Gidra Cc: Nick Kralevich Cc: Nosh Minwalla Cc: Pavel Emelyanov Cc: Tim Murray Cc: Aleksa Sarai Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/userfaultfd.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index f9fd18670e22..d99d166fd892 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1834,13 +1834,12 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, if (copy_from_user(&uffdio_api, buf, sizeof(uffdio_api))) goto out; features = uffdio_api.features; - if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) { - memset(&uffdio_api, 0, sizeof(uffdio_api)); - if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api))) - goto out; - ret = -EINVAL; - goto out; - } + ret = -EINVAL; + if (uffdio_api.api != UFFD_API || (features & ~UFFD_API_FEATURES)) + goto err_out; + ret = -EPERM; + if ((features & UFFD_FEATURE_EVENT_FORK) && !capable(CAP_SYS_PTRACE)) + goto err_out; /* report all available features and ioctls to userland */ uffdio_api.features = UFFD_API_FEATURES; uffdio_api.ioctls = UFFD_API_IOCTLS; @@ -1853,6 +1852,11 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, ret = 0; out: return ret; +err_out: + memset(&uffdio_api, 0, sizeof(uffdio_api)); + if (copy_to_user(buf, &uffdio_api, sizeof(uffdio_api))) + ret = -EFAULT; + goto out; } static long userfaultfd_ioctl(struct file *file, unsigned cmd, -- 2.20.1