From patchwork Thu Dec 5 17:05:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Ge X-Patchwork-Id: 13895818 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5AB4CE77170 for ; Thu, 5 Dec 2024 17:05:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD3B16B0157; Thu, 5 Dec 2024 12:05:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C82606B016C; Thu, 5 Dec 2024 12:05:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B71366B017A; Thu, 5 Dec 2024 12:05:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 97F7A6B0157 for ; Thu, 5 Dec 2024 12:05:58 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8A145A15F2 for ; Thu, 5 Dec 2024 17:05:57 +0000 (UTC) X-FDA: 82861532040.08.0FB109E Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) by imf19.hostedemail.com (Postfix) with ESMTP id 027EA1A0002 for ; Thu, 5 Dec 2024 17:05:35 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Qcib04EA; spf=pass (imf19.hostedemail.com: domain of hao.ge@linux.dev designates 91.218.175.183 as permitted sender) smtp.mailfrom=hao.ge@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733418346; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DwsmvYHNmo6qtxFQ5P7SXeQn3VEcbQIjYoq50ZRaOUg=; b=5yg5Ei247tYhicTbVqCwwKta3fMOlNlI2Vt/a4eduXr5qYtcQPgt9j3iBoX5V8qTi5YuiB py6oNeUBXeFaqTVlq20j+Yr5Zc9PwzgUS4bNl0xpgHFdVh8k4CW9QfGzrza3awACpMJRPl ne7wleYR2yb+/UZfqGBm+qwGM0K/xbY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Qcib04EA; spf=pass (imf19.hostedemail.com: domain of hao.ge@linux.dev designates 91.218.175.183 as permitted sender) smtp.mailfrom=hao.ge@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733418346; a=rsa-sha256; cv=none; b=0E6XHOsD58CPC5vJRLfd3S2S+LlaAXByXegyWL6T/YQDDnFH822276yRPuVGvUFO4Eh3OV yKB0F0R8isGluFrM4acL3Y7Omywd33ZZJEDYPkhyrfJXTI/wGpxzDq1p/llAg+0Sw7CRFt 9KrfHfFM8L3ZxLERutYPLRge6yiUQVs= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1733418350; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DwsmvYHNmo6qtxFQ5P7SXeQn3VEcbQIjYoq50ZRaOUg=; b=Qcib04EAhlmqGZaoKdQnoAuNK5IFCIpZt2O6VvFV/1O+mI8YunnKtZPk2YVM/BlwU1WMh3 V0mAh5HnZ04HOWyRN8KF4/MvjL6hL1CkwQGDUKZh/I26k5HLSfujjzaDM+18W7zN2h6yqM WnQEEytyqvn5OMcyZn8Kjk17MNKidxQ= From: Hao Ge To: surenb@google.com, kent.overstreet@linux.dev, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, hao.ge@linux.dev, Hao Ge Subject: [PATCH v3] mm/alloc_tag: fix vm_module_tags_populate's KASAN poisoning logic Date: Fri, 6 Dec 2024 01:05:28 +0800 Message-Id: <20241205170528.81000-1-hao.ge@linux.dev> In-Reply-To: <43bf216e-18f7-680c-ae75-773b03c8dc00@linux.dev> References: <43bf216e-18f7-680c-ae75-773b03c8dc00@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 027EA1A0002 X-Stat-Signature: hcsw9srh64r6dyt3c7geq5ymd8t4m4jn X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1733418335-883530 X-HE-Meta: U2FsdGVkX1+WAlfnmaQH2zCa9r3ma8CZeCB+tqJsZkM9raT6xUNJmM5vocqit3B1LnarmHosuMlZQZTEVusGovroMibSQbIE5bTww464jfBhFmB1mw3zTe+396vF15/4vcJvuhKVJXqjPXY9FKinlFPMGOrOzHjnsRlXUhGzkTJS77cfuoH6OpDkfVijhkb4SUnOJR+as7kW5gL2aTbB8M/KR0IalQsC14AuQfCfuraAZLTSQwLJrw8zQuz5HqR1uaX5FXr2MYqVhUfQ7PrjkxVGxaLPPQOQoBun/5rkJ0O9igIHXDnAsSg2w/Mn8eA2Vtkm/pxP+Rj+0RfuBSCs3p27du/oymKKHkkqdC8KmL1BrI5VWVcqy8o71hhquh40l5X4/Q+GQmbSD4LECjThOYlHVLVw2bIhuZVs2hYboiO8JdogTsFqol2nPHXyhyhrusu4MbkajYJ7J4ZWai2arMT7rTxn0o9rEx/mqiZSZl41RO9l8PqF5Jy7IoF5OEWQuGjakujVaVnFEOc9mNQB4eSzedsDP3juSR8Ucwm72Rs0g7HouFlRlRLd4tmWNjtXEDjKoANkN25C4xu3i9iSIHfF3Ljc2OVza+keHDHckYQ/l9mtf5L9AaCm7iaFve8sVxoO7Q9GCvs9Ue9U5DTl9qigqtAA3hhJhf6jeTSMrTlFNW9bPml5UK8hta/JlLkWc6dgctYMIjYQinwLyIhtqFt8B+3hF6bmddd4sn5GxlijExqZXlsRxFSzBkoM8dRRt36pc1nfeif3DLRQPClZEsjfGiqy/SE9izYteraTsr9vqW8ygSCpyHM1QlIpHy8Xa4UGDeXbmqmi8r6x6z7Q8mbl7hFlgcu/N5RNeSyDnRvOWScDaXRbsDzibUgSzYKC9gd0nzuvgBPx9IS3fed8y4CB57QSzuIRM2NaW13G3BzCVeWj3ZPBKizXhUMk9w4bKa0BN+j6RDoTYHgXNc5 9M4GZ/tU Qb8ylUKAsngklXU0fGxIgRZOgOAnnWA2Nv4dyc/+zMkeOQ4OzPab2Cz22pm0t0QdpcvEURTtE0J/Kec/ndW6qWGw5uyiZyBRj/nBK88san1dMXq1+AjnVAEJQHhSJCTemxIq8EwrCI8ZGHCvxIi1gteTdrXt5G5cakonuBoZ3PBYW3aKbzE01TymcJQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Hao Ge After merge commit 233e89322cbe ("alloc_tag: fix module allocation tags populated area calculation"), We still encountered a KASAN bug. This is because we have only actually performed page allocation and address mapping here. we need to unpoisoned portions of underlying memory. Here is the log for KASAN: [ 5.041171][ T1] ================================================================== [ 5.042047][ T1] BUG: KASAN: vmalloc-out-of-bounds in move_module+0x2c0/0x708 [ 5.042723][ T1] Write of size 240 at addr ffff80007e510000 by task systemd/1 [ 5.043412][ T1] [ 5.043523][ T72] input: QEMU QEMU USB Tablet as /devices/pci0000:00/0000:00:01.1/0000:02:001 [ 5.043614][ T1] CPU: 0 UID: 0 PID: 1 Comm: systemd Not tainted 6.13.0-rc1+ #28 [ 5.045560][ T1] Hardware name: QEMU KVM Virtual Machine, BIOS 0.0.0 02/06/2015 [ 5.046328][ T1] Call trace: [ 5.046670][ T1] show_stack+0x20/0x38 (C) [ 5.047127][ T1] dump_stack_lvl+0x80/0xf8 [ 5.047533][ T1] print_address_description.constprop.0+0x58/0x358 [ 5.048092][ T72] hid-generic 0003:0627:0001.0001: input,hidraw0: USB HID v0.01 Mouse [QEMU 0 [ 5.048126][ T1] print_report+0xb0/0x280 [ 5.049682][ T1] kasan_report+0xb8/0x108 [ 5.050170][ T1] kasan_check_range+0xe8/0x190 [ 5.050685][ T1] memcpy+0x58/0xa0 [ 5.051135][ T1] move_module+0x2c0/0x708 [ 5.051586][ T1] layout_and_allocate.constprop.0+0x308/0x5b8 [ 5.052219][ T1] load_module+0x134/0x16c8 [ 5.052671][ T1] init_module_from_file+0xdc/0x138 [ 5.053193][ T1] idempotent_init_module+0x344/0x600 [ 5.053742][ T1] __arm64_sys_finit_module+0xbc/0x150 [ 5.054289][ T1] invoke_syscall+0xd4/0x258 [ 5.054749][ T1] el0_svc_common.constprop.0+0xb4/0x240 [ 5.055319][ T1] do_el0_svc+0x48/0x68 [ 5.055743][ T1] el0_svc+0x40/0xe0 [ 5.056142][ T1] el0t_64_sync_handler+0x10c/0x138 [ 5.056658][ T1] el0t_64_sync+0x1ac/0x1b0 Fixes: 233e89322cbe ("alloc_tag: fix module allocation tags populated area calculation") Signed-off-by: Hao Ge Acked-by: Suren Baghdasaryan --- v3: Based on Suren's suggestion, I modified the code,Thank you for Suren. I realized that the 'poisoned' is actually not needed, so I removed it Due to these changes, update the commit message. v2: Add comments to kasan_unpoison_vmalloc like other places. commit 233e89322cbe ("alloc_tag: fix module allocation tags populated area calculation") is currently in the mm-hotfixes-unstable branch, so this patch is developed based on the mm-hotfixes-unstable branch. --- lib/alloc_tag.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 4ee6caa6d2da..f942408b53ef 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -424,6 +424,15 @@ static int vm_module_tags_populate(void) vm_module_tags->nr_pages += nr; } + /* + * Mark the pages as accessible, now that they are mapped. + * With hardware tag-based KASAN, marking is skipped for + * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). + */ + kasan_unpoison_vmalloc((void *)module_tags.start_addr, + new_end - module_tags.start_addr, + KASAN_VMALLOC_PROT_NORMAL); + return 0; }