Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp806929pxb; Thu, 5 Nov 2020 13:35:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+HXGUh5sjxX/z/sYKXo9OmiUNetU3V1jIj0gnUKczC+x/3QtZTVaJ4rYcDR2HiZ5Sfn9y X-Received: by 2002:a17:906:2f10:: with SMTP id v16mr4249971eji.320.1604612126521; Thu, 05 Nov 2020 13:35:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604612126; cv=none; d=google.com; s=arc-20160816; b=K1oqnmeIq7TEVu02oLpH+tpOiK14x6XNxESQNDT0nKZJFbLQUDGTbWDoFJKUy5Bkr5 gsPZutmiOMymlc3oUHlGqZaFt4zGEqNMw+Pqvrpe9PIiEVQhwAXOu4e3M/Bnymrs+2FJ di5yT8DkTpqYNfs5hH9hd+EjW3UMlTbUFZrqO/526wJPWV5WqnOOMbqjHVVmUb1mPMNR /7164sJgmCocGQGUBlJtHUxAXpxnq5oOyDEu2XD4wfno9OeaUvJRZYgGY20Wsb12ZMOx IoT696kSSSoNiwGXR6VV7SHny+AGwxz8Fea0629Nw35uunFT1AT9RMNsEwbAFsPvK4ru XaOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=WDkvhhk+gB3QxGd+Be5zd3cestt1bSfFZfMdeZMOFkc=; b=G5ztgtTEDR49+631gk3nFHDWpcX/5hLzfc01ZinxNxGTDiClisTluqSvUb66Qon86G BbBDuJAzo6+xSgTGmALX7hA5i7APbEyXLChFUtQd8y0+wCalTFGQCrX4XUAny67I111I 74KR2bT0TpY+crvQ7UmxsbemqEDnoR2h8esYjApnn7WLvMzFYAwaAk8WDpeVmaBVk/ey uRNSScw9wpsMw2R49dcXOna5q0mnn1wB51BEFcRvywr6VhQQZ186M+stJCFxMuvnqbFF lOibhU9LxladvOQIDe3EViFZN18XSSzBHEyATRMKA+PbVdKJl/SpG9conSnHcbVFLTaN mV7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=JfxuoQ5O; 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 r25si2200974edy.166.2020.11.05.13.35.03; Thu, 05 Nov 2020 13:35:26 -0800 (PST) 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=JfxuoQ5O; 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 S1732483AbgKEVdm (ORCPT + 99 others); Thu, 5 Nov 2020 16:33:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732446AbgKEVdk (ORCPT ); Thu, 5 Nov 2020 16:33:40 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9431C0613CF for ; Thu, 5 Nov 2020 13:33:39 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id a27so2291071pfl.17 for ; Thu, 05 Nov 2020 13:33:39 -0800 (PST) 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=WDkvhhk+gB3QxGd+Be5zd3cestt1bSfFZfMdeZMOFkc=; b=JfxuoQ5OI+fCkFO+uiyaJhQfYrpI1hS4iywaeLJW8A+3EdzhejCuKk+NY9Uvm9P0zV v35L2DQz3KjoRl4KHHMieq0FLiRof2Hex4faBQeBuElhSLQH/LzjWCSQeh27i0vUFuHI 1AHviFu/3Af5P4g39k2AzhPzlgC5qkNUekaEovkB/1vUQPZqwDFRPpwAOlqHzNR9z58q movlX7yynNs8+E96rJu9D+xdG3eBo502ma/Ts9VIZx15dSV7mIl6lxtDA2ee3vRq+XIn CtFOyMrKmdKMF846jK/KIKVX13GXnP5nrV83GrpGTWYwidFcLxAS/2c80bB6neAkiaZd EsKg== 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=WDkvhhk+gB3QxGd+Be5zd3cestt1bSfFZfMdeZMOFkc=; b=UKeLCwKQBueCGbdTt0hyAfGtpZa96Ry4Hq/RN5vC2MtZUO0zAEDfWrm3o+3uQjnsBp DFSTeipRxcvVho5/EAMi+0PAUZK6PK8O2D4+BGS2YQikKXMKQZrPy0bv9SAkTwlPGTdd 9OaQ6CD8SxN2r3iuBFlNb9u79+bLMLzRO0aOb6Yuc9LDI0eiE94d2chp/HAGsk4gNc7u /OvFeylhT8rL0h+O0sWapbfG5agiuayRdpVWLVGoYw3a0VNlRmuVNcIqs+TZWnn3hrjC q9Q42CrsJLnwEdEIzVjEiWbpGm8mfrlq2k498qpIP28h9G30DzZ4/2NK0AnHCfiaginw wR4Q== X-Gm-Message-State: AOAM532Gzaztt9sZHvUaTd4ySPIbCFR/9Fhf3sYilqHjwZTTe9kTV6G+ wrqRyWxHL60bt7nH3tGRXO4cUjIjbhpRQlfr4g== 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:a17:90b:148c:: with SMTP id js12mr4360206pjb.177.1604612019365; Thu, 05 Nov 2020 13:33:39 -0800 (PST) Date: Thu, 5 Nov 2020 13:33:24 -0800 In-Reply-To: <20201105213324.3111570-1-lokeshgidra@google.com> Message-Id: <20201105213324.3111570-5-lokeshgidra@google.com> Mime-Version: 1.0 References: <20201105213324.3111570-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v11 4/4] userfaultfd: use secure anon inodes for userfaultfd From: Lokesh Gidra To: Andrea Arcangeli , 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, linux-mm@kvack.org, Andrew Morton , hch@infradead.org, Daniel Colascione , Eric Biggers Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 000b457ad087..dd78daf06de6 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -972,14 +972,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; @@ -989,7 +989,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); @@ -1100,7 +1100,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)) { /* @@ -1160,6 +1160,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; @@ -1167,7 +1168,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))) @@ -1985,8 +1986,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); -- 2.29.1.341.ge80a0c044ae-goog