Received: by 2002:a17:90a:9103:0:0:0:0 with SMTP id k3csp11795087pjo; Thu, 2 Jan 2020 14:24:41 -0800 (PST) X-Google-Smtp-Source: APXvYqzfbmSjPBhv0xysspFB1LdxpIaLeEyRp0hzXJ969zA1nU5R9nroR68PFFjb+tglDmRvzjEV X-Received: by 2002:a9d:5c02:: with SMTP id o2mr87305124otk.176.1578003881136; Thu, 02 Jan 2020 14:24:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578003881; cv=none; d=google.com; s=arc-20160816; b=XpTIBTlkbuwxzI1Mjb1m8z4beibGkkC3SU3vkQhwoG05NqW8spizRrqPIkkxsSrjAA xOsj2UheqeXIuK7NKFRb1K+PToOj/c5znBdGcm0tQM7LafCvUtOjPb3kwSNN5FHALTfz i1waHrw6uWn5GXtSTSKWX5c0J63YXAZGHxA1CS77gq0FZWF8A3gCprwO5CkrRLKoInsV sW8FgrtM4uLIPeiwnSKlnQfew/0X5U5Jvc/oai7EoO1RbIiEnAGd2FxbQ1zQXGQ3KShx FvhkKUrez0xBz23uWdz8EZJlWwcdZ8HoJnyWWTA5JxxjyMKxg+/yYeYW/eoLRc/8PmGO R1Ag== 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=MVTXASC7FnhJr8gseMawPhI412FbdxWZtWMXTjdFeq4=; b=pWj790ZLSW5b566UMhqYY7iiaJvn/jame0TD/Ct4f04T/o+GJiEH42xnlGERRLRB75 99b48PZU1jmXabJz8ijUyq+3LS/ioVkTrvSzNje4KmvBzUTgY7xSYLpj2Q/lZSfnhgVa k0jOUtnVGJWo5XTBnUPJSQ8EU1lOUrvMkCNHOz73W5QKlrNjzSvAqO9L0gAw0YodVgQu Bf7xjm4OTK2OsFQ/pTp5WAdsnGmb4RXH3O1qR1Fd9YdLcR/bfLcK3ZL7JM/MpZfzgARU ENsr0YWkCVMAx+TBNyU20JBM5NigI/QEvOIs4c/a/ZLtkXyUFbkRjPAfiwY5wP8NgC3A Mzrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pGqx7E7b; 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 i11si28900119otc.105.2020.01.02.14.24.29; Thu, 02 Jan 2020 14:24:41 -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=pGqx7E7b; 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 S1729069AbgABWW5 (ORCPT + 99 others); Thu, 2 Jan 2020 17:22:57 -0500 Received: from mail.kernel.org ([198.145.29.99]:44034 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728884AbgABWWy (ORCPT ); Thu, 2 Jan 2020 17:22:54 -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 4E5D2222C3; Thu, 2 Jan 2020 22:22:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578003773; bh=p+ZuY98fhFtxM5QoJ6E1kKPS3QlhaamLCoV2jNhfMlg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pGqx7E7bqw/VgnBsc/L7N8/0A1/Wb8q6QZeYhw47nGRiJeYC4XdfQUsDw/kVv+/aL 5CGabT0Cg9qjJnC4fpAAhfCO3qKLqeFOZPeG+bP8DbyczzFvinwe64QNlcFqUEVtEV VLNt+WKltl3kdBB4QHG8wK53bEm8oPjCXL3aY6A4= 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 4.19 072/114] userfaultfd: require CAP_SYS_PTRACE for UFFD_FEATURE_EVENT_FORK Date: Thu, 2 Jan 2020 23:07:24 +0100 Message-Id: <20200102220036.344760036@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200102220029.183913184@linuxfoundation.org> References: <20200102220029.183913184@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 9c2955f67f70..d269d1139f7f 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1833,13 +1833,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; @@ -1852,6 +1851,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