Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1201813lqe; Mon, 8 Apr 2024 01:33:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXuRvIgPP+wlx2DQLuIfQzfMy76wFY0acetVk9iD5uk0EFJhbhRKbI5GCQYhQxiJwZ2bKbrg9OLQATEAh4oEUjADkxlRx0x5Hx6qWvsoA== X-Google-Smtp-Source: AGHT+IHDujHU78y2hVwwR56xNLLw/HSAlV+w2OYId4VqoKndoAKPDW7itJ4qkGaKK25v2hDKgL+F X-Received: by 2002:a05:6a20:c908:b0:1a7:5100:7559 with SMTP id gx8-20020a056a20c90800b001a751007559mr8947736pzb.32.1712565224857; Mon, 08 Apr 2024 01:33:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712565224; cv=pass; d=google.com; s=arc-20160816; b=Wf5PIFib/Dwyc8yS30EkK42K2nBOvaZBsc4sJxibnj3P8G0Yqf2USQgjvvOit0CbvM 6IcMplhuIhq772/Lb2BUC48PmqeCA5NoLIqPOf8IzpCUGRDopFwdUsZMA1si6A1imwUf j7TiDmeOIxrVDP6IkSJwm7yUyU1gFh0Jh58jCmOsXYwOaR9J5G9gjWdq4Gr0oCfdcwUy +X6OTShx5RBBi+fsIzyPk4sRkZPHTZMgk4KXcrHAictbKGVNpWsReYzOLK8XYXRrL1Ea lNLczBwToctxlCzPjl09ThMKUCZpPJ3KFFUAArHkpLD17P2PTZMydOktIIg1bIOlU/vr RGpg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=NzlMyh4/C3jHFyeVtIWbsYhp9fUVozaCLw/qIkGEqVw=; fh=cO8croXsA2PQg6vzvRvzC7G9VhpJmWipSRdxXvad8ow=; b=G0DfNuy4pf4LKYNZMOc21T4TlsVmAKZcI4FJdR+ckRRux7wqv9fw7hdPLTdzt2c23t CemJqORcoLhFywsVwLPuPJ5CqAry/vO2wTDlWv4SplHxY4qadkeEtTy3wKHdNtvifoI3 H5ajAapUYR6myqYVKTVGQEB8RMXrKOLAiXH9YAoC/fuAYmVLduWTLRCpxMrBj37NkEA1 HP6S1CmO514bN3LliNORb0UO61jIAJ6cGQRbFYVuiLuZEGuowpnzjA5FJBQSjN1GKwHT +N9c1kRxStagYf74l9KkeBeWfIKqdukr8pf0IFuyHPWNkXOPQO0sbqcZDfnco5mGYrbg 3cYw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-135048-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135048-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 5-20020a630b05000000b005e858e345dbsi5861536pgl.373.2024.04.08.01.33.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:33:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135048-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-135048-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135048-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 811CD289933 for ; Mon, 8 Apr 2024 08:33:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CC63C38F98; Mon, 8 Apr 2024 08:33:39 +0000 (UTC) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 807AE3A1A1; Mon, 8 Apr 2024 08:33:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.190 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565219; cv=none; b=fe+JnyIVADq3Zwvrn8CiIKUHsEEv+dxhjpIvWi6dfpP74oiDLaO/hBZqk7I7qM4kkRQw8KFn2FmHdwcuXyNs0LVsBo2aIdWwlgzGEc2xq8IFTwxDH8tnFTCOapxdtRKFWMXsN1cHJwtSZ+Rq7G8RHBexOx0vnNtZxfI5HeVYSFg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712565219; c=relaxed/simple; bh=L6Sp0fHnMYGR04YqKkhRgLAJKayv1a/l1+xEZOfPX4E=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AfgB4BXKsxFIjRpwC7OUyOLGiBOX7mP80UUYGPqsaH370te90LeFGoBgkaO8agFkbMydQJnrKX0I76fudlaxpnj/AJZHDfUNgcK05lPmVWnhg/PZaySkSGiXfKekFEbsCxO6NrCnki6GAqczZBduxZ12xv6H8dKN1ci/+Z1JBwQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.190 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4VCj3j3yVhz1ymHv; Mon, 8 Apr 2024 16:31:13 +0800 (CST) Received: from dggpeml500012.china.huawei.com (unknown [7.185.36.15]) by mail.maildlp.com (Postfix) with ESMTPS id EF9DA1401E9; Mon, 8 Apr 2024 16:33:26 +0800 (CST) Received: from localhost.localdomain (10.67.175.61) by dggpeml500012.china.huawei.com (7.185.36.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Mon, 8 Apr 2024 16:33:26 +0800 From: Zheng Yejian To: , , , CC: , , Subject: [PATCH v2] kprobes: Avoid possible warn in __arm_kprobe_ftrace() Date: Mon, 8 Apr 2024 16:34:03 +0800 Message-ID: <20240408083403.3302274-1-zhengyejian1@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240407035904.2556645-1-zhengyejian1@huawei.com> References: <20240407035904.2556645-1-zhengyejian1@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpeml500012.china.huawei.com (7.185.36.15) There is once warn in __arm_kprobe_ftrace() on: ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0); if (WARN_ONCE(..., "Failed to arm kprobe-ftrace at %pS (error %d)\n", ...) return ret; This warning is generated because 'p->addr' is detected to be not a valid ftrace location in ftrace_set_filter_ip(). The ftrace address check is done by check_ftrace_location() at the beginning of check_kprobe_address_safe(). At that point, ftrace_location(addr) == addr should return true if the module is loaded. Then the module is searched twice: 1. in is_module_text_address(), we find that 'p->addr' is in a module; 2. in __module_text_address(), we find the module; If the module has just been unloaded before the second search, then '*probed_mod' is NULL and we would not go to get the module refcount, then the return value of check_kprobe_address_safe() would be 0, but actually we need to return -EINVAL. To fix it, originally we can simply check 'p->addr' is out of text again, like below. But that would check twice respectively in kernel text and module text, so finally I reduce them to be once. if (!(core_kernel_text((unsigned long) p->addr) || is_module_text_address((unsigned long) p->addr)) || ...) { ret = -EINVAL; goto out; } ... *probed_mod = __module_text_address((unsigned long) p->addr); if (*probed_mod) { ... } else if (!core_kernel_text((unsigned long) p->addr)) { // check again! ret = -EINVAL; goto out; } Signed-off-by: Zheng Yejian --- kernel/kprobes.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) v2: - Update commit messages and comments as suggested by Masami. Link: https://lore.kernel.org/all/20240408115038.b0c85767bf1f249eccc32fff@kernel.org/ v1: - Link: https://lore.kernel.org/all/20240407035904.2556645-1-zhengyejian1@huawei.com/ diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 9d9095e81792..65adc815fc6e 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1567,10 +1567,17 @@ static int check_kprobe_address_safe(struct kprobe *p, jump_label_lock(); preempt_disable(); - /* Ensure it is not in reserved area nor out of text */ - if (!(core_kernel_text((unsigned long) p->addr) || - is_module_text_address((unsigned long) p->addr)) || - in_gate_area_no_mm((unsigned long) p->addr) || + /* Ensure the address is in a text area, and find a module if exists. */ + *probed_mod = NULL; + if (!core_kernel_text((unsigned long) p->addr)) { + *probed_mod = __module_text_address((unsigned long) p->addr); + if (!(*probed_mod)) { + ret = -EINVAL; + goto out; + } + } + /* Ensure it is not in reserved area. */ + if (in_gate_area_no_mm((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) || @@ -1580,8 +1587,7 @@ static int check_kprobe_address_safe(struct kprobe *p, goto out; } - /* Check if 'p' is probing a module. */ - *probed_mod = __module_text_address((unsigned long) p->addr); + /* Get module refcount and reject __init functions for loaded modules. */ if (*probed_mod) { /* * We must hold a refcount of the probed module while updating -- 2.25.1