From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.5 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_PASS,USER_AGENT_MUTT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5AA52C43381 for ; Mon, 25 Mar 2019 11:18:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 326AB207DD for ; Mon, 25 Mar 2019 11:18:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730950AbfCYLSD (ORCPT ); Mon, 25 Mar 2019 07:18:03 -0400 Received: from zeniv.linux.org.uk ([195.92.253.2]:47904 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729727AbfCYLSD (ORCPT ); Mon, 25 Mar 2019 07:18:03 -0400 Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92 #3 (Red Hat Linux)) id 1h8Nc5-0006Jm-13; Mon, 25 Mar 2019 11:17:57 +0000 Date: Mon, 25 Mar 2019 11:17:57 +0000 From: Al Viro To: Daniel Borkmann Cc: Linus Torvalds , syzbot , Alexei Starovoitov , linux-fsdevel , Linux List Kernel Mailing , syzkaller-bugs Subject: Re: KASAN: use-after-free Read in path_lookupat Message-ID: <20190325111756.GN2217@ZenIV.linux.org.uk> References: <0000000000006946d2057bbd0eef@google.com> <20190325045744.GK2217@ZenIV.linux.org.uk> <717eaff9-1f49-28f3-bbb6-c8f14ebbe03b@iogearbox.net> <20190325111123.GM2217@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190325111123.GM2217@ZenIV.linux.org.uk> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org On Mon, Mar 25, 2019 at 11:11:23AM +0000, Al Viro wrote: > On Mon, Mar 25, 2019 at 10:15:40AM +0100, Daniel Borkmann wrote: > > > So we have 5 broken cases, all with the same kind of fix: move freeing > > > into the RCU-delayed part of ->destroy_inode(); for debugfs and bpf > > > that requires adding ->alloc_inode()/->destroy_inode(), rather than > > > relying upon the defaults from fs/inode.c > > > > I believe I copied that logic from one of the other fs'es back then, sigh. > > Thanks for the analysis and hints for fixing. Would the below (only compile > > tested for now) look reasonable to you? I believe there is no other way > > around than to add our own inode cache, but so be it. The freeing of the > > i_link is then RCU-deferred in bpf_destroy_inode_deferred(): > > It looks like it would suffice, but it's way too much boilerplate for my > taste ;-/ > > Most of your headache here comes from messing with slab setup and the > only reason for that is being unable to free stuff into inode_cachep. > So grep for inode_cachep would be a good idea, and it digs up the > following sucker: > void free_inode_nonrcu(struct inode *inode) > { > kmem_cache_free(inode_cachep, inode); > } > EXPORT_SYMBOL(free_inode_nonrcu); > > IOW, you need nothing on ->alloc_inode() side and for ->destroy_inode() > just do call_rcu() of a callback, that would kfree(inode->link) if > it was a symlink, then call free_inode_nonrcu(inode). > > Considerably smaller patch that way... AFAICS, it boils down to just this (also only compile-tested): diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c index 2ada5e21dfa6..9437f88b6acf 100644 --- a/kernel/bpf/inode.c +++ b/kernel/bpf/inode.c @@ -80,6 +80,20 @@ static const struct inode_operations bpf_dir_iops; static const struct inode_operations bpf_prog_iops = { }; static const struct inode_operations bpf_map_iops = { }; +static void bpf_destroy_inode_deferred(struct rcu_head *head) +{ + struct inode *inode = container_of(head, struct inode, i_rcu); + + if (S_ISLNK(inode->i_mode)) + kfree(inode->i_link); + free_inode_nonrcu(inode); +} + +static void bpf_destroy_inode(struct inode *inode) +{ + call_rcu(&inode->i_rcu, bpf_destroy_inode_deferred); +} + static struct inode *bpf_get_inode(struct super_block *sb, const struct inode *dir, umode_t mode) @@ -561,8 +575,6 @@ static void bpf_evict_inode(struct inode *inode) truncate_inode_pages_final(&inode->i_data); clear_inode(inode); - if (S_ISLNK(inode->i_mode)) - kfree(inode->i_link); if (!bpf_inode_type(inode, &type)) bpf_any_put(inode->i_private, type); } @@ -584,6 +596,7 @@ static const struct super_operations bpf_super_ops = { .drop_inode = generic_delete_inode, .show_options = bpf_show_options, .evict_inode = bpf_evict_inode, + .destroy_inode = bpf_destroy_inode, }; enum {