Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3280198rwa; Tue, 23 Aug 2022 01:53:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR6RdhGbSXDczEhzWv6/pzf/KshE+PmLFSlgOeftHI52LAeZJvGl4AVfKfO+0N7Nxcw9wKZ7 X-Received: by 2002:a05:6402:40c3:b0:442:d798:48ad with SMTP id z3-20020a05640240c300b00442d79848admr2743406edb.154.1661244839221; Tue, 23 Aug 2022 01:53:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661244839; cv=none; d=google.com; s=arc-20160816; b=oARpb+h9oHXATYhoTObOHZiqgriqFJfDebxcKnogfoXzTiKxQiPVIxmDhbnq3mmFSZ awserB4lh9chah/YBJjpMqI03ci8DhFW/EZOs3hHI+MHoHfHBOOP6GihhNUteqtfw6Tl Q72e7R3OHcokL6dC0J21bm7CwaHWIEehUV9Pl/A5d9fRvQQ4lksEt66PE+k0jCEpRTK0 Qf7yiNBm74ygad/qVHrod3j/GbO59SQYiJnE6BJ/uPpehinB4XGJ7lC614EhjFD0QQ/J C+8nsCmz/eGooF84m8S/4c1HDdr681+ASqzIyNUqNtoAqkyxBZlt4LXOSvXMYaqZhDzQ kyxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=VMhFngqYJp+VDLRF3JusDD7rNwFX9L/IHH+N4aT4zq4=; b=DChLj+bVOriRVPMxxvF2L1tD8Q3viFFT+1Oa8mAY8scPUrOwsxoN0fhcFO18YbsLjW 3lps2GPutRA+I8dzIuaIHShueq88GtTgnQm3b+MZU5Xf6sYecZ30xQMR5Ix4cMJu6YV2 ao4gBTPmc/yt2v6lpe+LMNgmKMgqQ5yU59L/HTa6fRWC0B6mRVBGtf04cm0s/xs9qQJW Oa5WWxtrQwtuBt20OpdeM0U5FEqvhhmG9rbEmjI4d9POJIZgPhqV/odco4YAS/XAdKPs IdHUtf16j0y9PNwdpVj9z9NmnaFG1l0rhWIMzYEl/4GMaJJt244AqR9S3TehI0IACNhB 9Qxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kWMHjdgh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn20-20020a1709070d1400b0073d677c5d34si8425189ejc.193.2022.08.23.01.53.33; Tue, 23 Aug 2022 01:53:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kWMHjdgh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241175AbiHWHqY (ORCPT + 99 others); Tue, 23 Aug 2022 03:46:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241168AbiHWHqW (ORCPT ); Tue, 23 Aug 2022 03:46:22 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3658E642D2 for ; Tue, 23 Aug 2022 00:46:21 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C5E926153C for ; Tue, 23 Aug 2022 07:46:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E814C433C1; Tue, 23 Aug 2022 07:46:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1661240780; bh=q+s3udd5d/2e9K8njeqfqoMRhxKP6I4s2s3ZXouWSOw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=kWMHjdghzCge0kjXTQmwDzIlvkw4XpdcZ9foGY7dMdjYb5H7jXARS7/YHCySBf9sG sQEz2rdX0iiIh3hGSP00oWGBEESf135BsxfEFtk7mCeaO/JznFcN/pZlxWgRKU3JqE 4PoLuaYN7WqHBlADu0Jx2QyWeoyKResn30wHOMKOy3oaeX6UJuFF06A6g5SRtKLiX2 2fUy3530qGZnZHosHS8I02JfLa9jcpUn15u3vumc2cFUNmtp+ODl6w4BjEkugldKob lknMU5oczh6jYOoGO+Jl0DBIV+LvDk2iuZkOLMlg5lQj1/kGpJgWfcJBFuaBoVZ6lw DTs6984HgJIyQ== Date: Tue, 23 Aug 2022 09:46:13 +0200 From: Christian Brauner To: Kuniyuki Iwashima Cc: Kees Cook , Andy Lutomirski , Will Drewry , Kuniyuki Iwashima , linux-kernel@vger.kernel.org, syzbot+ab17848fe269b573eb71@syzkaller.appspotmail.com, Ayushman Dutta Subject: Re: [PATCH v2] seccomp: Move copy_seccomp() to no failure path. Message-ID: <20220823074613.bmxrjgkjzagbwqqu@wittgenstein> References: <20220823004806.38681-1-kuniyu@amazon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20220823004806.38681-1-kuniyu@amazon.com> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 22, 2022 at 05:48:06PM -0700, Kuniyuki Iwashima wrote: > Our syzbot instance reported memory leaks in do_seccomp() [0], similar > to the report [1]. It shows that we miss freeing struct seccomp_filter > and some objects included in it. > > We can reproduce the issue with the program below [2] which calls one > seccomp() and two clone() syscalls. > > The first clone()d child exits earlier than its parent and sends a > signal to kill it during the second clone(), more precisely before the > fatal_signal_pending() test in copy_process(). When the parent receives > the signal, it has to destroy the embryonic process and return -EINTR to > user space. In the failure path, we have to call seccomp_filter_release() > to decrement the filter's refcount. > > Initially, we called it in free_task() called from the failure path, but > the commit 3a15fb6ed92c ("seccomp: release filter after task is fully > dead") moved it to release_task() to notify user space as early as possible > that the filter is no longer used. > > To keep the change and current seccomp refcount semantics, let's move > copy_seccomp() just after the signal check and add a WARN_ON_ONCE() in > free_task() for future debugging. > > [0]: > unreferenced object 0xffff8880063add00 (size 256): > comm "repro_seccomp", pid 230, jiffies 4294687090 (age 9.914s) > hex dump (first 32 bytes): > 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 ................ > ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ > backtrace: > do_seccomp (./include/linux/slab.h:600 ./include/linux/slab.h:733 kernel/seccomp.c:666 kernel/seccomp.c:708 kernel/seccomp.c:1871 kernel/seccomp.c:1991) > do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80) > entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120) > unreferenced object 0xffffc90000035000 (size 4096): > comm "repro_seccomp", pid 230, jiffies 4294687090 (age 9.915s) > hex dump (first 32 bytes): > 01 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 ................ > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ > backtrace: > __vmalloc_node_range (mm/vmalloc.c:3226) > __vmalloc_node (mm/vmalloc.c:3261 (discriminator 4)) > bpf_prog_alloc_no_stats (kernel/bpf/core.c:91) > bpf_prog_alloc (kernel/bpf/core.c:129) > bpf_prog_create_from_user (net/core/filter.c:1414) > do_seccomp (kernel/seccomp.c:671 kernel/seccomp.c:708 kernel/seccomp.c:1871 kernel/seccomp.c:1991) > do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80) > entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120) > unreferenced object 0xffff888003fa1000 (size 1024): > comm "repro_seccomp", pid 230, jiffies 4294687090 (age 9.915s) > hex dump (first 32 bytes): > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ > backtrace: > bpf_prog_alloc_no_stats (./include/linux/slab.h:600 ./include/linux/slab.h:733 kernel/bpf/core.c:95) > bpf_prog_alloc (kernel/bpf/core.c:129) > bpf_prog_create_from_user (net/core/filter.c:1414) > do_seccomp (kernel/seccomp.c:671 kernel/seccomp.c:708 kernel/seccomp.c:1871 kernel/seccomp.c:1991) > do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80) > entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120) > unreferenced object 0xffff888006360240 (size 16): > comm "repro_seccomp", pid 230, jiffies 4294687090 (age 9.915s) > hex dump (first 16 bytes): > 01 00 37 00 76 65 72 6c e0 83 01 06 80 88 ff ff ..7.verl........ > backtrace: > bpf_prog_store_orig_filter (net/core/filter.c:1137) > bpf_prog_create_from_user (net/core/filter.c:1428) > do_seccomp (kernel/seccomp.c:671 kernel/seccomp.c:708 kernel/seccomp.c:1871 kernel/seccomp.c:1991) > do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80) > entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120) > unreferenced object 0xffff8880060183e0 (size 8): > comm "repro_seccomp", pid 230, jiffies 4294687090 (age 9.915s) > hex dump (first 8 bytes): > 06 00 00 00 00 00 ff 7f ........ > backtrace: > kmemdup (mm/util.c:129) > bpf_prog_store_orig_filter (net/core/filter.c:1144) > bpf_prog_create_from_user (net/core/filter.c:1428) > do_seccomp (kernel/seccomp.c:671 kernel/seccomp.c:708 kernel/seccomp.c:1871 kernel/seccomp.c:1991) > do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80) > entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120) > > [1]: https://syzkaller.appspot.com/bug?id=2809bb0ac77ad9aa3f4afe42d6a610aba594a987 > > [2]: > #define _GNU_SOURCE > #include > #include > #include > #include > #include > #include > > void main(void) > { > struct sock_filter filter[] = { > BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), > }; > struct sock_fprog fprog = { > .len = sizeof(filter) / sizeof(filter[0]), > .filter = filter, > }; > long i, pid; > > syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, &fprog); > > for (i = 0; i < 2; i++) { > pid = syscall(__NR_clone, CLONE_NEWNET | SIGKILL, NULL, NULL, 0); > if (pid == 0) > return; > } > } > > Fixes: 3a15fb6ed92c ("seccomp: release filter after task is fully dead") > Reported-by: syzbot+ab17848fe269b573eb71@syzkaller.appspotmail.com > Reported-by: Ayushman Dutta > Suggested-by: Kees Cook > Signed-off-by: Kuniyuki Iwashima > --- Looks good, Reviewed-by: Christian Brauner (Microsoft)