Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1609676pxk; Tue, 1 Sep 2020 03:12:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxmCwtCWXfaaBzHhMUhr2BJ6lguT5JQwNSy2TqiPUMhucCKCp6JnHliIx2lLucinB19i+sb X-Received: by 2002:a50:af65:: with SMTP id g92mr1088169edd.301.1598955153981; Tue, 01 Sep 2020 03:12:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598955153; cv=none; d=google.com; s=arc-20160816; b=MnJbhm+IFeberfGAimhg+ZYORDU4QHvezx3qCC8j3RTgO4Pi5WeN++2GrFmd4wZKgv vGXlUnl4fruhzM+PWB9eAsEUpVBfBwaN1zmco0SMrjP5MX8IO2OSukqITTb/se1/jNxj uqnF5yJkHw/V1xHf7oQyVLFzKfqytOk1YpDt97tKd6GFX1FKtrlumHvvnxnpEtp3gmMo vulLwa8CNaz7oCb9zq3+ie59xUEr53GY44bWKe4AwDMS3SB0Em8epE1ohl+Klbjfo8SV whCfO0vswP26wSH3R/0hwWiB9EDvAmfBIj7R/uy+zO9Ih92hoC/dJcEqk2m6e4iGDALc +kkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=4vTsWmqREpy8WagL2blJ/4x/KfcWZyGIfdLKSBSmRiQ=; b=Su5KV4B+6DRlYuZx3lS2senBL7ua7TyP4tayuY/zc/hxh48Hr9tC+L1Wg2j+O4gweM vab/HqKbxeMToPl0eVm8UPEBH3Sg94iUdoWqPoYd7TdbnHa22KjNcYXWBbdxVanCG/Ga rRQPM8GjHo0WfY+yISit/BUq8k+elQTkg3g2NrXJXlDZ6jaYNJnNLZa2OqhJrv7Gr5JG e8FVk1DA+fX1wBH1tK28z4jDCfqcb1uNmfltDOeKDE1YumPS8yJjRValyD5r85LMiIk6 0yjnRcdfnNcoWNcNbPl22LbnwylnwZldG3iGFmnCpTQaD5HW2CHlwMFSXj5Zwa7rsJkF iPtQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v4si303563edw.450.2020.09.01.03.12.11; Tue, 01 Sep 2020 03:12:33 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726091AbgIAKHs (ORCPT + 99 others); Tue, 1 Sep 2020 06:07:48 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:42428 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725848AbgIAKHr (ORCPT ); Tue, 1 Sep 2020 06:07:47 -0400 Received: from ip5f5af70b.dynamic.kabel-deutschland.de ([95.90.247.11] helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kD3CX-0007Aq-NJ; Tue, 01 Sep 2020 10:07:41 +0000 Date: Tue, 1 Sep 2020 12:07:41 +0200 From: Christian Brauner To: Tycho Andersen Cc: Kees Cook , Christian Brauner , linux-kernel@vger.kernel.org, luto@amacapital.net, syzbot , syzkaller-bugs@googlegroups.com, wad@chromium.org Subject: Re: memory leak in do_seccomp Message-ID: <20200901100741.wx33nrfi5mrbxudd@wittgenstein> References: <000000000000e5ea9e05ac9d16c1@google.com> <000000000000df80ae05ae244c2b@google.com> <202008311620.AC4A7047D@keescook> <20200901000915.GA564594@cisco> <20200901011459.GA583718@cisco> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200901011459.GA583718@cisco> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 31, 2020 at 07:14:59PM -0600, Tycho Andersen wrote: > On Mon, Aug 31, 2020 at 06:09:15PM -0600, Tycho Andersen wrote: > > On Mon, Aug 31, 2020 at 04:25:35PM -0700, Kees Cook wrote: > > > On Sun, Aug 30, 2020 at 08:50:15PM -0700, syzbot wrote: > > > > syzbot has found a reproducer for the following issue on: > > > > > > > > HEAD commit: dcc5c6f0 Merge tag 'x86-urgent-2020-08-30' of git://git.ke.. > > > > git tree: upstream > > > > console output: https://syzkaller.appspot.com/x/log.txt?x=10b297d5900000 > > > > kernel config: https://syzkaller.appspot.com/x/.config?x=903b9fecc3c6d231 > > > > dashboard link: https://syzkaller.appspot.com/bug?extid=3ad9614a12f80994c32e > > > > compiler: gcc (GCC) 10.1.0-syz 20200507 > > > > syz repro: https://syzkaller.appspot.com/x/repro.syz?x=14649561900000 > > > > C reproducer: https://syzkaller.appspot.com/x/repro.c?x=118aacc1900000 > > > > > > > > IMPORTANT: if you fix the issue, please add the following tag to the commit: > > > > Reported-by: syzbot+3ad9614a12f80994c32e@syzkaller.appspotmail.com > > > > > > > > executing program > > > > executing program > > > > executing program > > > > executing program > > > > executing program > > > > BUG: memory leak > > > > unreferenced object 0xffff88811ba93600 (size 64): > > > > comm "syz-executor680", pid 6503, jiffies 4294951104 (age 21.940s) > > > > hex dump (first 32 bytes): > > > > 00 00 00 00 00 00 00 00 08 36 a9 1b 81 88 ff ff .........6...... > > > > 08 36 a9 1b 81 88 ff ff 11 ce 98 89 3a d5 b4 8f .6..........:... > > > > backtrace: > > > > [<00000000896418b0>] kmalloc include/linux/slab.h:554 [inline] > > > > [<00000000896418b0>] kzalloc include/linux/slab.h:666 [inline] > > > > [<00000000896418b0>] init_listener kernel/seccomp.c:1473 [inline] > > > > [<00000000896418b0>] seccomp_set_mode_filter kernel/seccomp.c:1546 [inline] > > > > [<00000000896418b0>] do_seccomp+0x8ce/0xd40 kernel/seccomp.c:1649 > > > > [<000000002b04976c>] do_syscall_64+0x2d/0x70 arch/x86/entry/common.c:46 > > > > [<00000000322b4126>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 > > > > > > I haven't narrowed this down yet (and it *might* be a false positive), > > > but it looks like this is filter->notif. The only way that's possible is > > > if seccomp_notify_release() was never called *and* seccomp_filter_free() > > > got called... which would imply a reference counting problem. The way > > > there doesn't jump out at me yet, but I haven't yet decoded the C > > > reproducer into the actual seccomp arguments, etc. > > > > Looks like it's just a bunch of threads in the same thread group > > trying to install a filter with TSYNC and NEW_LISTENER turned on. Does > > the patch below look reasonable? > > > > I didn't send it separately since I'm in the process of switching my > > e-mail address to tycho@tycho.pizza; let this e-mail serve as proof > > that that e-mail really is me too :). I can send it the normal way if > > it looks good. > > > > > > From d497e787e8e1b3e8b9230fdc4c9802616709c920 Mon Sep 17 00:00:00 2001 > > From: Tycho Andersen > > Date: Mon, 31 Aug 2020 17:55:07 -0600 > > Subject: [PATCH] seccomp: don't leak memory when filter install races > > > > In seccomp_set_mode_filter() with TSYNC | NEW_LISTENER, we first initialize > > the listener fd, then check to see if we can actually use it later in > > seccomp_may_assign_mode(), which can fail if anyone else in our thread > > group has installed a filter and caused some divergence. If we can't, we > > partially clean up the newly allocated file: we put the fd, put the file, > > but don't actually clean up the *memory* that was allocated at > > filter->notif. Let's clean that up too. > > > > Fixes: 51891498f2da ("seccomp: allow TSYNC and USER_NOTIF together") > > Reported-by: syzbot+3ad9614a12f80994c32e@syzkaller.appspotmail.com > > Signed-off-by: Tycho Andersen > > --- > > kernel/seccomp.c | 2 ++ > > 1 file changed, 2 insertions(+) > > > > diff --git a/kernel/seccomp.c b/kernel/seccomp.c > > index 3ee59ce0a323..21a76127833f 100644 > > --- a/kernel/seccomp.c > > +++ b/kernel/seccomp.c > > @@ -1581,6 +1581,8 @@ static long seccomp_set_mode_filter(unsigned int flags, > > listener_f->private_data = NULL; > > fput(listener_f); > > put_unused_fd(listener); > > + kfree(filter->notif); > > + filter->notif = NULL; > > Oof, actually this isn't quite right. It should be s/filter/prepared/g. > I can fix that and send out a real patch that's actually tested at > some point tomorrow. Please do. :) Do we have tests for this scenario in the selftests, Tycho? Christian