From patchwork Sun Oct 11 08:29:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lokesh Gidra X-Patchwork-Id: 11830719 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6124D109B for ; Sun, 11 Oct 2020 08:30:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3C4A620776 for ; Sun, 11 Oct 2020 08:30:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hBpVS7uF" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729123AbgJKI35 (ORCPT ); Sun, 11 Oct 2020 04:29:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729274AbgJKI3u (ORCPT ); Sun, 11 Oct 2020 04:29:50 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12D9DC0613CE for ; Sun, 11 Oct 2020 01:29:49 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id m23so10537958qkh.10 for ; Sun, 11 Oct 2020 01:29:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=5MfBywdxMmsLRrplRDwjfeuNu4Dz7M8KwUBLUxZj6xM=; b=hBpVS7uF0dFqV+usm5krJoTk8tnGXulcNhG8ZJQffqmJqiN/AvtuYAFH4gdM7krAf4 5Ib1hQdSL0DUZPWL/z++LUKJy8IxhRA2w+QAOv+LfPQ+YYpRjzQJCwvNAhRpalFO4xOi G1z50NqD9K8274QcDS0hen8+XCV+tHgIznVBklGHdqiIPwoQxL5C0i7MkxNIwkRG22U6 Ij6bvdO2B1/sNNhUACdwbsUxlVO81o2w0f/1AnH0QaUZztaPK6x/m8VnhMqW9Z1mKI0J BpTIQ9S8uKzPLmbXjTbUkvX2IRP7yrhbXvxq1icfcGr1nkiWu5ZAhEt7iMG4AaDY3q2/ VvUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=5MfBywdxMmsLRrplRDwjfeuNu4Dz7M8KwUBLUxZj6xM=; b=rrX1/zL2FaOzpAH39Yw1P8OtF8iLpBh/dxIs3LnMr/Iy37xgjQlKs+7PESfmMcB8Xa mMAm5+k+b/+ka8DpvFcjqRS5PsrXwBF2Vsj5jsaYAHIsCbXG9fEvyIz9TNJ/ox9/pRLt bV6aBwk/ypGoa+7o+JBhJP4qUdx8iJ/oerTvdL6v3s1lEJ59cx1D1G1JE9n722ZTlg5j BMkMtJphcfTbZdUGH8PsL8yQVHcRtX2XcFBTzC1O8sMTNkeHUeesWH65VnpbBehrNqIq EzfZmIXVzJg3qEWi+SIzLJCYrv2XbFepLEAn68Bl2bIWoNnQsiVb957AtNRVZtnOC5K5 i7cQ== X-Gm-Message-State: AOAM532N7D6OEX+Doeo0BjEUTaxW//fAWfCv/MBmKKzVzY2A5ELqa9AX YbqB3ikqZb0O5wtaKoMU/d5+5rmfYp/ZjC5VJQ== X-Google-Smtp-Source: ABdhPJyXMlomivJVMqBM7X/DtGr65xVnyJaAqT/5gaTPmnqZk1jHkrChs/crGvAIfh7DexwBcSTGjZePaDlyt9H15Q== Sender: "lokeshgidra via sendgmr" X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:f693:9fff:fef4:29dd]) (user=lokeshgidra job=sendgmr) by 2002:a0c:b6d7:: with SMTP id h23mr20732427qve.17.1602404988144; Sun, 11 Oct 2020 01:29:48 -0700 (PDT) Date: Sun, 11 Oct 2020 01:29:36 -0700 In-Reply-To: <20201011082936.4131726-1-lokeshgidra@google.com> Message-Id: <20201011082936.4131726-4-lokeshgidra@google.com> Mime-Version: 1.0 References: <20201011082936.4131726-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [PATCH v10 3/3] Use secure anon inodes for userfaultfd From: Lokesh Gidra To: Alexander Viro , James Morris , Stephen Smalley , Casey Schaufler , Eric Biggers Cc: "Serge E. Hallyn" , Paul Moore , Eric Paris , Lokesh Gidra , Daniel Colascione , Kees Cook , "Eric W. Biederman" , KP Singh , David Howells , Thomas Cedeno , Anders Roxell , Sami Tolvanen , Matthew Garrett , Aaron Goidel , Randy Dunlap , "Joel Fernandes (Google)" , YueHaibing , Christian Brauner , Alexei Starovoitov , Alexey Budankov , Adrian Reber , Aleksa Sarai , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, kaleshsingh@google.com, calin@google.com, surenb@google.com, nnk@google.com, jeffv@google.com, kernel-team@android.com, Daniel Colascione Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Daniel Colascione This change gives userfaultfd file descriptors a real security context, allowing policy to act on them. Signed-off-by: Daniel Colascione [Remove owner inode from userfaultfd_ctx] [Use anon_inode_getfd_secure() instead of anon_inode_getfile_secure() in userfaultfd syscall] [Use inode of file in userfaultfd_read() in resolve_userfault_fork()] Signed-off-by: Lokesh Gidra Reviewed-by: Eric Biggers --- fs/userfaultfd.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 0e4a3837da52..918535b49475 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -978,14 +978,14 @@ static __poll_t userfaultfd_poll(struct file *file, poll_table *wait) static const struct file_operations userfaultfd_fops; -static int resolve_userfault_fork(struct userfaultfd_ctx *ctx, - struct userfaultfd_ctx *new, +static int resolve_userfault_fork(struct userfaultfd_ctx *new, + struct inode *inode, struct uffd_msg *msg) { int fd; - fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new, - O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS)); + fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, new, + O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS), inode); if (fd < 0) return fd; @@ -995,7 +995,7 @@ static int resolve_userfault_fork(struct userfaultfd_ctx *ctx, } static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, - struct uffd_msg *msg) + struct uffd_msg *msg, struct inode *inode) { ssize_t ret; DECLARE_WAITQUEUE(wait, current); @@ -1106,7 +1106,7 @@ static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, spin_unlock_irq(&ctx->fd_wqh.lock); if (!ret && msg->event == UFFD_EVENT_FORK) { - ret = resolve_userfault_fork(ctx, fork_nctx, msg); + ret = resolve_userfault_fork(fork_nctx, inode, msg); spin_lock_irq(&ctx->event_wqh.lock); if (!list_empty(&fork_event)) { /* @@ -1166,6 +1166,7 @@ static ssize_t userfaultfd_read(struct file *file, char __user *buf, ssize_t _ret, ret = 0; struct uffd_msg msg; int no_wait = file->f_flags & O_NONBLOCK; + struct inode *inode = file_inode(file); if (ctx->state == UFFD_STATE_WAIT_API) return -EINVAL; @@ -1173,7 +1174,7 @@ static ssize_t userfaultfd_read(struct file *file, char __user *buf, for (;;) { if (count < sizeof(msg)) return ret ? ret : -EINVAL; - _ret = userfaultfd_ctx_read(ctx, no_wait, &msg); + _ret = userfaultfd_ctx_read(ctx, no_wait, &msg, inode); if (_ret < 0) return ret ? ret : _ret; if (copy_to_user((__u64 __user *) buf, &msg, sizeof(msg))) @@ -1995,8 +1996,8 @@ SYSCALL_DEFINE1(userfaultfd, int, flags) /* prevent the mm struct to be freed */ mmgrab(ctx->mm); - fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx, - O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS)); + fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, ctx, + O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS), NULL); if (fd < 0) { mmdrop(ctx->mm); kmem_cache_free(userfaultfd_ctx_cachep, ctx);