Received: by 2002:ab2:687:0:b0:1f4:6588:b3a7 with SMTP id s7csp154233lqe; Tue, 9 Apr 2024 18:57:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXuDwS9bKMfcZa6/epM+cI7dzXEr7hpvjxlkfZe0vy4Bz2nSsrunks0AsF3K4nA7h80RDMZX1qqkTkewoOUrAX587slgAb2nVthlU0GNw== X-Google-Smtp-Source: AGHT+IGO291Q9aDrAAGNRz49qP2EePxysZj+C2dBR1yd/t2vl/xBy4QREBFmVA/7jS8peTBLMLpY X-Received: by 2002:a17:90b:4d86:b0:29f:76d4:306a with SMTP id oj6-20020a17090b4d8600b0029f76d4306amr1632514pjb.24.1712714255244; Tue, 09 Apr 2024 18:57:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712714255; cv=pass; d=google.com; s=arc-20160816; b=H5ktQMRNkJ5sCd+shGOY3CFNG5Kyhu0cA3kU3XeP0ItpLHA7LuNcNhThUsFxoJIE1Q 2V82kckU2xhYwEVzEl0I6lPtSkI38sgmskPAjBZTsc04s7vGzTGUDFqsCouVks+jwLOz 5U9apKoCdrGPTJKdXDxazwKyF+9EFJrXmdyr4bmZior4x2VHqJSMw2YwcWzZaR+whZX0 YlCfhymn4EYf3qGw4wa9J2iaL3/fyCsw+Y9n+PWsRseR3oxH/NGVmIcWcyACbbJ2x40x SYVZKSJKSvAKj7FHRgWzQBrxYZKIeyDSPU4rAwvveknyyMzcQ3vXDshrPD+p5QNiRpPM rixw== 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=mb26cHKLIWKLWqFSPMMa1JNsUOq47yDiZeoaN4+rcJQ=; fh=IRCNVrQ3C40JArA8Mw+Kv2Il6fcbweMdM6Ar0upMmqo=; b=c4atNnL7klmrkOhTzXvqSEU5aoW6clio6JExpDAgbS86xT9pGbaWIXUEqhqfyEVj1e RDBrUQJmnbSZxxBX73bOKJH6KCn9Tl4XyUWO/I4GRn8R1Gq0JxUvaK0Z1aDmXXOxj1Ee 8M9pMgYyiHT/a1Snf8q8asrSozgMfb5tKad0RDJQc3tG7Z/VNVbyzT8BatR3c/7//rC2 V9lOQMDxY2Q/HS6sLLRQdHCBznx21DIPAOmlU0LY8ChJ2Q+tp9w9xbxk3U/lGNpD2JH+ X0U1VmLva3mV+fINxgdCSB+tOvN26q7F3RIF0RF6eGhSSEq2eQf3dSaxrl7w6ZLFpf5A 5cfQ==; 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-137812-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137812-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 q8-20020a656248000000b005d8c19ca98dsi9372773pgv.137.2024.04.09.18.57.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 18:57:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-137812-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-137812-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137812-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 E2311283B25 for ; Wed, 10 Apr 2024 01:57:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D2212524C; Wed, 10 Apr 2024 01:57:29 +0000 (UTC) Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) (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 7718A1C2D; Wed, 10 Apr 2024 01:57:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.32 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712714249; cv=none; b=ILD8sfEjznHLVuKKGlD+IRW8LLQOOJlo/RuFhFFTdFgv68ziYMaPHpJu6pyUauCYrwqFqsMWwrOwIGhY0agKz8YhXROk7tJTbXn/kWVnIuGU8PC8gjMR20IKSloOGVO9slLqtgcjTglCTOIFMfXg+OfGmTND9M8nrViqGZ0SA4E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712714249; c=relaxed/simple; bh=9cgwPmb8EOYreT5OW6HYIHKNYI4ODMVvRnFtVH07r+g=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=SJno9v8zHEgob2Vvh67o3pN/dX3h/MrC5cIkXpFuowcwu7Yt1jg2m31KPOt/53wFWGwO5kjcXMoZIiMqrCASare1RFPkpNPJ0RGaZaGsz5tG56RclzrAPHfU3mbfhzPdvqMz9bZW2U3KHjbqDam6vNur2LmWaikhTYgKTipzlvI= 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.32 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.88.163]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4VDmCJ0WNqz21kcl; Wed, 10 Apr 2024 09:56:28 +0800 (CST) Received: from dggpeml500012.china.huawei.com (unknown [7.185.36.15]) by mail.maildlp.com (Postfix) with ESMTPS id 5A1A2180063; Wed, 10 Apr 2024 09:57:23 +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; Wed, 10 Apr 2024 09:57:23 +0800 From: Zheng Yejian To: CC: , , , , , Subject: [PATCH v3] kprobes: Fix possible use-after-free issue on kprobe registration Date: Wed, 10 Apr 2024 09:58:02 +0800 Message-ID: <20240410015802.265220-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: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500012.china.huawei.com (7.185.36.15) When unloading a module, its state is changing MODULE_STATE_LIVE -> MODULE_STATE_GOING -> MODULE_STATE_UNFORMED. Each change will take a time. `is_module_text_address()` and `__module_text_address()` works with MODULE_STATE_LIVE and MODULE_STATE_GOING. If we use `is_module_text_address()` and `__module_text_address()` separately, there is a chance that the first one is succeeded but the next one is failed because module->state becomes MODULE_STATE_UNFORMED between those operations. In `check_kprobe_address_safe()`, if the second `__module_text_address()` is failed, that is ignored because it expected a kernel_text address. But it may have failed simply because module->state has been changed to MODULE_STATE_UNFORMED. In this case, arm_kprobe() will try to modify non-exist module text address (use-after-free). To fix this problem, we should not use separated `is_module_text_address()` and `__module_text_address()`, but use only `__module_text_address()` once and do `try_module_get(module)` which is only available with MODULE_STATE_LIVE. Signed-off-by: Zheng Yejian --- kernel/kprobes.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) v3: - Update commit messages as suggested by Masami. Link: https://lore.kernel.org/all/20240409224922.5f192e8ace5f7a90937bfa69@kernel.org/ - Also change to a more appropriate title. v2: - Update commit messages and comments as suggested by Masami. Link: https://lore.kernel.org/all/20240408115038.b0c85767bf1f249eccc32fff@kernel.org/ - Link: https://lore.kernel.org/all/20240408083403.3302274-1-zhengyejian1@huawei.com/ 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