Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1747918imn; Sun, 31 Jul 2022 21:00:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR5TDPwdDnj4OQqLR5OIMm9v6bvOo7ufgrFvXWsGB7iitQ5dgsMvd1VJ4L71uOjSvNn670J4 X-Received: by 2002:a17:906:cc46:b0:730:9423:2fb2 with SMTP id mm6-20020a170906cc4600b0073094232fb2mr372020ejb.221.1659326455884; Sun, 31 Jul 2022 21:00:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659326455; cv=none; d=google.com; s=arc-20160816; b=FyG+5Tuad9GJAjypSFajQ5vwqIXaCFcZVD5/WNlerbnjkX1CyeBIYBoiDJJKn8vTvG eO6/aut4qXJG6xDJOHxGKjUKGS2CPokQeY434yaX52/GKu4FrW3RRkfSoFt8S4nHCEhK nNu3QgRKMwieCiSMDGK/scxYt3uJdKg+hQBBsJ3jmhjJ7dHHjJPvZ/i0eC/168pV9ZmU XetWEN/Uv6KZzFdyNUOXZzHJr+O5KVq5yjOfIBazwG4iZ8lPrOTVmOm4A39CeXfWx9SB t9oqFF6z9GSW6milCZyBcQo4z326L70mnpzsEtaUy/NfVNevpkjareUvtzzAUtC8jExP Hiyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=tYxxtQBXjBsyOUrb5EBhptIVZpfxGKIxFkEYAYncqL4=; b=EG+VtIjyF0RJ63C6GvV66HH/qffFxWaL20W6jva9lrZGpDvoGX72gKTqnbYw6RuwdW Cfg+Ewqx60gOFNaJ/d29CP+iuYV9zhYQFStcHIZAlOdegtWX0HQn68ilFcyETF4H+F9h VnltoSExicBnHHrohbJlAYpa4ekrCSZBGavVeWn6eJXZNcRqzCW5JjqY4msGsHhKy30A uAhpuOgGokV0VzjQbCwRy/PRUQpSnHTa2aUOznREJWCozulnOa6gZ51Qc1m4rwRzCJHf KdWwNA3gxLz15O6MOVTHBQWDF5lUSdNArDFPXiXBTvaSPn80R0oydB/qpw9AXPWh1HTc gVpA== 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hw19-20020a170907a0d300b0072b1b099d55si6082480ejc.268.2022.07.31.21.00.31; Sun, 31 Jul 2022 21:00:55 -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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238364AbiHADkQ (ORCPT + 99 others); Sun, 31 Jul 2022 23:40:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233331AbiHADkM (ORCPT ); Sun, 31 Jul 2022 23:40:12 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4F78279; Sun, 31 Jul 2022 20:40:10 -0700 (PDT) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Lx3jy1hffzmVSf; Mon, 1 Aug 2022 11:38:14 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 1 Aug 2022 11:40:08 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 1 Aug 2022 11:40:08 +0800 From: Chen Zhongjin To: , CC: , , , , , , , , Subject: [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog Date: Mon, 1 Aug 2022 11:37:19 +0800 Message-ID: <20220801033719.228248-1-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 kernel_text_address returns ftrace_trampoline, kprobe_insn_slot and bpf_text_address as kprobe legal address. These text are removable and changeable without any notifier to kprobes. Probing on them can trigger some unexpected behavior[1]. Considering that jump_label and static_call text are already be forbiden to probe, kernel_text_address should be replaced with core_kernel_text and is_module_text_address to check other text which is unsafe to kprobe. [1] https://lkml.org/lkml/2022/7/26/1148 Fixes: 5b485629ba0d ("kprobes, extable: Identify kprobes trampolines as kernel text area") Fixes: 74451e66d516 ("bpf: make jited programs visible in traces") Signed-off-by: Chen Zhongjin --- v2 -> v3: Remove '-next' carelessly added in title. v1 -> v2: Check core_kernel_text and is_module_text_address rather than only kprobe_insn. Also fix title and commit message for this. See old patch at [1]. --- kernel/kprobes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index f214f8c088ed..80697e5e03e4 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p, preempt_disable(); /* Ensure it is not in reserved area nor out of text */ - if (!kernel_text_address((unsigned long) p->addr) || + if (!(core_kernel_text((unsigned long) p->addr) || + is_module_text_address((unsigned long) p->addr)) || within_kprobe_blacklist((unsigned long) p->addr) || jump_label_text_reserved(p->addr, p->addr) || static_call_text_reserved(p->addr, p->addr) || -- 2.17.1