Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp921974pxp; Wed, 16 Mar 2022 21:24:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxFrTsDZGa5NeSF+BBotvscPLNPE0bn8Sf+KG+qEQSuVv4MAML+gJ4YvdSvZKgv0kcM5ncx X-Received: by 2002:a17:902:e943:b0:153:f9b:99c9 with SMTP id b3-20020a170902e94300b001530f9b99c9mr2674507pll.170.1647491063537; Wed, 16 Mar 2022 21:24:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647491063; cv=none; d=google.com; s=arc-20160816; b=xfkKqOaADE/KM3bzQ7qdg23Qmdx6Mhb8tieZHkxgIW04kKSpGvY6nEn7c5p4P4xIQV h8IyZBr6y9gs4LZfObAZbMHUuvsaWNe3ehb7O9H1Z/WudyVT5HCZaIht8LDg30/aygIz uXN+iK4bDSEcjAfl2RPZp0biKyvt+zR+NPagjVylZ46drz+EdlRNS4sJab0sZX2BvybL H/aEd/n+c+mCWjx4KJQTWU/TBjPy4UuBvuTGJdQ9fX7GjMU25kdhoAIXyS/LrOo3dvjE nVPWUN9/5gSmXvfNVP186kc6TCgk/s0HNLU8H1kbarXXnlE1v1TNMJJnvB1FmluKdT2e 7OqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pyyjJpd1xiOorTF767Hgejs5a8wMiRsziTY7l40CIlE=; b=bYDIjV8F/ye8BuanrayIYLy0625GtH6XrRnVl4osiPQbJX5YN/zGL0TIUyf4cSkA1Q 4iIQasreecRYA8wPuyPaAw9YFwz2tpAebmell3UroBdYXedN56tgMwi/XPYhIFBJlteD CNN+XMXnLm6nEOKsGo62L72jfL8n+UM3clTxEUFEBZCIMjKw59x0a9rJxKc2QIKVCIYn LxgXA2L8cDUHAg9SOt5CwFGIdB+FgYmw8G7FYthCVshsz/x621gwTLFJlopUEuohii9J uto0j56KYMeQQ4joI6RwEQvGk74Ynt+AvTEHbxxt/BNmky5YArLBw3YV6iysd6mwkGnc oagA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aKizwIBl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id y8-20020a17090a134800b001bf65f92794si1236384pjf.118.2022.03.16.21.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 21:24:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aKizwIBl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E8E17E7F6F; Wed, 16 Mar 2022 20:57:10 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239923AbiCNL6n (ORCPT + 99 others); Mon, 14 Mar 2022 07:58:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239935AbiCNL5o (ORCPT ); Mon, 14 Mar 2022 07:57:44 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D092DEEB; Mon, 14 Mar 2022 04:56:29 -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 ams.source.kernel.org (Postfix) with ESMTPS id 35DF0B80DE9; Mon, 14 Mar 2022 11:56:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CEDE5C340E9; Mon, 14 Mar 2022 11:56:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1647258987; bh=0fYqO3/Kwmdjf5miuWSFOMXV9YAehpSgs/ABHv008Co=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aKizwIBl/nFSbY9v1tQRZvrD+saNa28SbYLDcgGsCVz7m27AS4tgGgvp4nRZxpKy2 U8SXYewuS4E9NhZ/d6OEqtX+UPM7GBMyYUrLy1NSvINvtjO9VwluGYwUHPQwCxAf0z CbIh1zB+5w8LtgG7YbBDP4nVFy32q1fnpZYdA/TA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kumar Kartikeya Dwivedi , Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.4 21/43] selftests/bpf: Add test for bpf_timer overwriting crash Date: Mon, 14 Mar 2022 12:53:32 +0100 Message-Id: <20220314112735.013019647@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314112734.415677317@linuxfoundation.org> References: <20220314112734.415677317@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Kumar Kartikeya Dwivedi [ Upstream commit a7e75016a0753c24d6c995bc02501ae35368e333 ] Add a test that validates that timer value is not overwritten when doing a copy_map_value call in the kernel. Without the prior fix, this test triggers a crash. Signed-off-by: Kumar Kartikeya Dwivedi Signed-off-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/20220209070324.1093182-3-memxor@gmail.com Signed-off-by: Sasha Levin --- .../selftests/bpf/prog_tests/timer_crash.c | 32 +++++++++++ .../testing/selftests/bpf/progs/timer_crash.c | 54 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/timer_crash.c create mode 100644 tools/testing/selftests/bpf/progs/timer_crash.c diff --git a/tools/testing/selftests/bpf/prog_tests/timer_crash.c b/tools/testing/selftests/bpf/prog_tests/timer_crash.c new file mode 100644 index 000000000000..f74b82305da8 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/timer_crash.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "timer_crash.skel.h" + +enum { + MODE_ARRAY, + MODE_HASH, +}; + +static void test_timer_crash_mode(int mode) +{ + struct timer_crash *skel; + + skel = timer_crash__open_and_load(); + if (!ASSERT_OK_PTR(skel, "timer_crash__open_and_load")) + return; + skel->bss->pid = getpid(); + skel->bss->crash_map = mode; + if (!ASSERT_OK(timer_crash__attach(skel), "timer_crash__attach")) + goto end; + usleep(1); +end: + timer_crash__destroy(skel); +} + +void test_timer_crash(void) +{ + if (test__start_subtest("array")) + test_timer_crash_mode(MODE_ARRAY); + if (test__start_subtest("hash")) + test_timer_crash_mode(MODE_HASH); +} diff --git a/tools/testing/selftests/bpf/progs/timer_crash.c b/tools/testing/selftests/bpf/progs/timer_crash.c new file mode 100644 index 000000000000..f8f7944e70da --- /dev/null +++ b/tools/testing/selftests/bpf/progs/timer_crash.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +struct map_elem { + struct bpf_timer timer; + struct bpf_spin_lock lock; +}; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, struct map_elem); +} amap SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1); + __type(key, int); + __type(value, struct map_elem); +} hmap SEC(".maps"); + +int pid = 0; +int crash_map = 0; /* 0 for amap, 1 for hmap */ + +SEC("fentry/do_nanosleep") +int sys_enter(void *ctx) +{ + struct map_elem *e, value = {}; + void *map = crash_map ? (void *)&hmap : (void *)&amap; + + if (bpf_get_current_task_btf()->tgid != pid) + return 0; + + *(void **)&value = (void *)0xdeadcaf3; + + bpf_map_update_elem(map, &(int){0}, &value, 0); + /* For array map, doing bpf_map_update_elem will do a + * check_and_free_timer_in_array, which will trigger the crash if timer + * pointer was overwritten, for hmap we need to use bpf_timer_cancel. + */ + if (crash_map == 1) { + e = bpf_map_lookup_elem(map, &(int){0}); + if (!e) + return 0; + bpf_timer_cancel(&e->timer); + } + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.34.1