Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp2700658iob; Mon, 16 May 2022 04:27:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzLUB23yVL/+vllMX0pfeJ55yUWt0blhMbfmvyEsBydUyNg0iWlzImQrVpTXTLqpZgPpRWL X-Received: by 2002:aa7:da04:0:b0:41c:c191:64bc with SMTP id r4-20020aa7da04000000b0041cc19164bcmr12548628eds.166.1652700439311; Mon, 16 May 2022 04:27:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652700439; cv=none; d=google.com; s=arc-20160816; b=g5tgsd4GHtg5Rut+7BRaSsjyV++KTF6WU1JlYzBtO+LqePjSg9+GiY/Dz0gjXBzqPX If9lCHh15Zi1rh4ozSZcpfRQvz1W2M92HXcokMqRshECbd45EHrz1xHkkoDw0NM1ITtJ q5rJUCg4qP0mObnUBBLT58FbWB9rr7QCHVD56eYEI5pb6HDd/gJlAkG2MQWSr4wuDPYm MgvOOEWTB9EcfslAg/yoYNKH/7BKy8yG9a0wiMqnnfuKCF/RKWENZ+oWRRVxm/xEJYtX j/0IychBUQXwe1c2A3EX719SRwsbxpPgRV8LbaBKacNSJnmeAWTJXB3IXQCzD26wGpfW WJyQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=pvX9Z33UvJ9h1E5FJ7L9S/kAoL4Yd/T1foQF8aq3SuU=; b=evx+sB2yTgPPJ3gHxH+LCxbKL9AcDHVeTOU+VwJd22DSEN2luvYUl1P4fd8ayKVEj4 PNHGYDv/K0c6lc6lPNNKeSj58hhfzxUDgUn3sfrPA8VlFLFNOH6fiImbf+gEJP5M3Qwz 0yXHZ6GI3OncJO97AkE0Udsd6d56HCy/AnOF3NuT7TzQe3lwjWFnQ192ksgjkgNtmT2e gBxv4weKZ4kUTAQ2HWTQ4y32IodC12yJbPCocoCmeAU7jjL+S/1gpyzhzcLNa53gayEq gcd6JuAHsBIi+cH/ay46xSppZrLhdGk/BQsea3lvFLq88NluRRRbF4Kf1J7Jikxyp/vN vHhw== ARC-Authentication-Results: i=1; mx.google.com; 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 ws19-20020a170907705300b006fa8af6164csi9006676ejb.222.2022.05.16.04.26.53; Mon, 16 May 2022 04:27:19 -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; 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 S239507AbiEPFl3 convert rfc822-to-8bit (ORCPT + 99 others); Mon, 16 May 2022 01:41:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239727AbiEPFlM (ORCPT ); Mon, 16 May 2022 01:41:12 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E75D7DF70 for ; Sun, 15 May 2022 22:41:11 -0700 (PDT) Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24FLaUZG008181 for ; Sun, 15 May 2022 22:41:11 -0700 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3g29xxf5eb-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Sun, 15 May 2022 22:41:11 -0700 Received: from twshared11660.23.frc3.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sun, 15 May 2022 22:41:09 -0700 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id 595B17AEBC8C; Sun, 15 May 2022 22:41:01 -0700 (PDT) From: Song Liu To: , CC: , , , , , , , Song Liu Subject: [PATCH bpf-next 3/5] bpf: introduce bpf_arch_text_invalidate for bpf_prog_pack Date: Sun, 15 May 2022 22:40:49 -0700 Message-ID: <20220516054051.114490-4-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220516054051.114490-1-song@kernel.org> References: <20220516054051.114490-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: KOGiTDju-EhY1QDQkPvTXKpZrdvBC1Br X-Proofpoint-ORIG-GUID: KOGiTDju-EhY1QDQkPvTXKpZrdvBC1Br X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-15_11,2022-05-13_01,2022-02-23_01 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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 Introduce bpf_arch_text_invalidate and use it to fill unused part of the bpf_prog_pack with illegal instructions when a BPF program is freed. Signed-off-by: Song Liu --- arch/x86/net/bpf_jit_comp.c | 5 +++++ include/linux/bpf.h | 1 + kernel/bpf/core.c | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index a2b6d197c226..f298b18a9a3d 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -228,6 +228,11 @@ static void jit_fill_hole(void *area, unsigned int size) memset(area, 0xcc, size); } +int bpf_arch_text_invalidate(void *dst, size_t len) +{ + return IS_ERR_OR_NULL(text_poke_set(dst, 0xcc, len)); +} + struct jit_context { int cleanup_addr; /* Epilogue code offset */ diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5061ccd8b2dc..0288a6464236 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2362,6 +2362,7 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t, void *addr1, void *addr2); void *bpf_arch_text_copy(void *dst, void *src, size_t len); +int bpf_arch_text_invalidate(void *dst, size_t len); struct btf_id_set; bool btf_id_set_contains(const struct btf_id_set *set, u32 id); diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 2d0c9d4696ad..cacd8684c3c4 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -968,6 +968,9 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr) nbits = BPF_PROG_SIZE_TO_NBITS(hdr->size); pos = ((unsigned long)hdr - (unsigned long)pack_ptr) >> BPF_PROG_CHUNK_SHIFT; + WARN_ONCE(bpf_arch_text_invalidate(hdr, hdr->size), + "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n"); + bitmap_clear(pack->bitmap, pos, nbits); if (bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0, bpf_prog_chunk_count(), 0) == 0) { @@ -2740,6 +2743,11 @@ void * __weak bpf_arch_text_copy(void *dst, void *src, size_t len) return ERR_PTR(-ENOTSUPP); } +int __weak bpf_arch_text_invalidate(void *dst, size_t len) +{ + return -ENOTSUPP; +} + DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key); EXPORT_SYMBOL(bpf_stats_enabled_key); -- 2.30.2