Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp3343133ioa; Tue, 26 Apr 2022 01:37:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3VEPt3R57TUGwazbSxJxSgotxHeBYUqwdTK7G6sHIVB8OdRz5OZSUS4E+5/NtVKpRQwPq X-Received: by 2002:a17:902:e8d2:b0:15a:187c:31d7 with SMTP id v18-20020a170902e8d200b0015a187c31d7mr21998012plg.49.1650962219866; Tue, 26 Apr 2022 01:36:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650962219; cv=none; d=google.com; s=arc-20160816; b=vVKY07Lte3XsJKGraCUup6a6AzGCL4t3su+yKntIqxjLnBYEUZbIUMXgXNRzGEm07J adPRYbz/Oq1rOe4XUqH9JV3f0f4tNs4qb1YKzdkdjCUxx1IX9/+UUlDafIkmZlb+dbmM W/iKq52JZ/Lb6X6tH4wR4wfh5n+lUiwdnvzqdfIzdYUjk9zR4GQxyimWgwy1/1R7nND1 krDEOXRj1WWtdzT2tGTtKbeBYcO+Lk/q/z8z1sg0A1zRwU94XFZu0VvnN11aZkO8YhVA xUyKxyarAUqLe1GI5HPDvGUpQLPuQ3LpaQGRfbpnO5p+VqSe2A8vwmOI0oq+c2pK7X/V /DSw== 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=q0yfWgqGK/ywdcaa92vUPGU5tPlMC7DWcXPjFVCNGCU=; b=gTS0qo/w+6Bh2N0ZN/My31KCXLOKLQdMxkGh8/sRlGSDNjUhszTwpeJMhTZXUrpw16 JcqOO6xgALWichZDvALNNTYRwsMMvbyYGEL72eLWDH1hYC9WdIwxfRKnwjrf27sXuztl DjnA4bxAv2Da2tyuaOfD4KtiG3kIRhiYhjTV2btcUTuObyxllAR1FTYdQOD8QI+wOE20 Dcbup45k65Ju252nrBBn2bR7kXp0XFed0+LcQGTvakBKYtuj23svVIsUGaMXvwuVEnCG Q40MuWZgWW4+DX9sWiq+vQ7AiJSRz8xLyuyAtOgX60V1EFnTDg/kKL1pJWGhXkzc0Zzv eLvw== 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 y8-20020a056a00190800b004fa3a8e00b1si18805486pfi.360.2022.04.26.01.36.44; Tue, 26 Apr 2022 01:36: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; 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 S245643AbiDYUqb convert rfc822-to-8bit (ORCPT + 99 others); Mon, 25 Apr 2022 16:46:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245623AbiDYUqY (ORCPT ); Mon, 25 Apr 2022 16:46:24 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FD93762B5 for ; Mon, 25 Apr 2022 13:43:18 -0700 (PDT) Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23PIVi2G009635 for ; Mon, 25 Apr 2022 13:43:17 -0700 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3fp10e910a-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 25 Apr 2022 13:43:17 -0700 Received: from twshared19572.14.frc2.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 25 Apr 2022 13:43:15 -0700 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id 56C29652E7A1; Mon, 25 Apr 2022 13:40:35 -0700 (PDT) From: Song Liu To: , , CC: , , , , , , , Song Liu Subject: [PATCH v2 bpf 1/3] bpf: fill new bpf_prog_pack with illegal instructions Date: Mon, 25 Apr 2022 13:39:45 -0700 Message-ID: <20220425203947.3311308-2-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220425203947.3311308-1-song@kernel.org> References: <20220425203947.3311308-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: INI8vr9ioNuf80TL6F1p_u15f-q9KBeu X-Proofpoint-ORIG-GUID: INI8vr9ioNuf80TL6F1p_u15f-q9KBeu 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-04-25_10,2022-04-25_03,2022-02-23_01 X-Spam-Status: No, score=-2.3 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 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 bpf_prog_pack enables sharing huge pages among multiple BPF programs. These pages are marked as executable before the JIT engine fill it with BPF programs. To make these pages safe, fill the hole bpf_prog_pack with illegal instructions before making it executable. Fixes: 57631054fae6 ("bpf: Introduce bpf_prog_pack allocator") Fixes: 33c9805860e5 ("bpf: Introduce bpf_jit_binary_pack_[alloc|finalize|free]") Signed-off-by: Song Liu --- kernel/bpf/core.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 13e9dbeeedf3..132dfba389be 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -873,7 +873,7 @@ static size_t select_bpf_prog_pack_size(void) return size; } -static struct bpf_prog_pack *alloc_new_pack(void) +static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_insns) { struct bpf_prog_pack *pack; @@ -886,6 +886,7 @@ static struct bpf_prog_pack *alloc_new_pack(void) kfree(pack); return NULL; } + bpf_fill_ill_insns(pack->ptr, bpf_prog_pack_size); bitmap_zero(pack->bitmap, bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE); list_add_tail(&pack->list, &pack_list); @@ -895,7 +896,7 @@ static struct bpf_prog_pack *alloc_new_pack(void) return pack; } -static void *bpf_prog_pack_alloc(u32 size) +static void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns) { unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size); struct bpf_prog_pack *pack; @@ -910,6 +911,7 @@ static void *bpf_prog_pack_alloc(u32 size) size = round_up(size, PAGE_SIZE); ptr = module_alloc(size); if (ptr) { + bpf_fill_ill_insns(ptr, size); set_vm_flush_reset_perms(ptr); set_memory_ro((unsigned long)ptr, size / PAGE_SIZE); set_memory_x((unsigned long)ptr, size / PAGE_SIZE); @@ -923,7 +925,7 @@ static void *bpf_prog_pack_alloc(u32 size) goto found_free_area; } - pack = alloc_new_pack(); + pack = alloc_new_pack(bpf_fill_ill_insns); if (!pack) goto out; @@ -1102,7 +1104,7 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr, if (bpf_jit_charge_modmem(size)) return NULL; - ro_header = bpf_prog_pack_alloc(size); + ro_header = bpf_prog_pack_alloc(size, bpf_fill_ill_insns); if (!ro_header) { bpf_jit_uncharge_modmem(size); return NULL; -- 2.30.2