From patchwork Wed Mar 20 10:18:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13597689 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 0E245C54E58 for ; Wed, 20 Mar 2024 10:19:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9159C6B0096; Wed, 20 Mar 2024 06:18:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C5EB6B0099; Wed, 20 Mar 2024 06:18:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B5C76B009A; Wed, 20 Mar 2024 06:18:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6976A6B0096 for ; Wed, 20 Mar 2024 06:18:59 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3B07AA1237 for ; Wed, 20 Mar 2024 10:18:59 +0000 (UTC) X-FDA: 81917019198.05.4687B32 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf17.hostedemail.com (Postfix) with ESMTP id 6856E40002 for ; Wed, 20 Mar 2024 10:18:57 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wg7YKLea; spf=pass (imf17.hostedemail.com: domain of 3D7j6ZQYKCKAGLIDERGOOGLE.COMLINUX-MMKVACK.ORG@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3D7j6ZQYKCKAGLIDERGOOGLE.COMLINUX-MMKVACK.ORG@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710929937; 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-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=Pe97FWHITdYkiCtSSFDy2YJv9pSAbWKeZZSBy5Rk1y0=; b=WKhUNJEKx79drdmIdZ1NigzoKJPgQ7SGe1rN9IuDmHQ05AMUqtpO3ajlJElM+DVeBzo7nV CivjfAN6bhbdfskl0i48fWXrPtTielqt1SQeV6dTRha+GAM13wErRklyMFpphDxe41LfOH 6gZ9G2kd4Qyp/5FbAPkmr6KaHr3oWY4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wg7YKLea; spf=pass (imf17.hostedemail.com: domain of 3D7j6ZQYKCKAGLIDERGOOGLE.COMLINUX-MMKVACK.ORG@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3D7j6ZQYKCKAGLIDERGOOGLE.COMLINUX-MMKVACK.ORG@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710929937; a=rsa-sha256; cv=none; b=DMsC5sl7DAKRBf4CC3TZh0aAQmjiw5XJp+V6E8ynraFOWCM3zccyn6pt+A0IR9iRPJ1wNR nWxTAeGOG3OGcOZ3kBrwiGxDC88EU07uyyRtjXLEiNfAPVj3tJnK9P2cGFkvM1iZqZvtPD 3+KigwD5VJaOFCMLI+y75+ldysYaWBY= Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-a46fd54fbc1so5139166b.3 for ; Wed, 20 Mar 2024 03:18:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710929936; x=1711534736; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Pe97FWHITdYkiCtSSFDy2YJv9pSAbWKeZZSBy5Rk1y0=; b=wg7YKLeaqetyGcGIbwtpq3NLbdXrgwTTl6LR/eLhMkElOIdVF+08jxlAjX/3LhTeV2 +2qDYhdCPAc4PUt7NQ2Qb5pCdz9ZcRT1q4vsWN89LRSOFLGDkC/cy5NSrxWdCpnLra62 ovrfS5IkEj9GtJWDkboaaaGslN5idugk0p32SxDrT2idrJuXsdVLbQwSl2IePDjRRnzi KmsQDS//4MzbYFukScFclAY1IrshWdaHD+5DVnLoovBodqhJwLFMfZJnknwkmfXVtfHn cMYrcQIlv8V+/DVSaLFogok5DYJMIVXZljXKFp3OlBz2owEOvvZMKwWjMioYUK4dkyg2 oCTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710929936; x=1711534736; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Pe97FWHITdYkiCtSSFDy2YJv9pSAbWKeZZSBy5Rk1y0=; b=QjPFqtQnMds1BApQ6nSogVoOZ28O1yEbtI/lVbTED+U+JPHUgSs/Xv+hxE4dDAv3gB sPlX77jxYxBMjXK2VlVbxrmO0wpIjptEUELB+TOD4XXvrqG6q8z0z4zjbr1mOEjeKt2Y rFtovi/gmSizh1iZEAAUBaVotthyEbDEw9M6GuNkBm0tU0FfQG0O/GbzaDW2Mi+nmm3c 6jlFNXWWGyigaVVKB26GOpuPyA2Wk/1nheVRw+HRYeOQ8r1CxwGpDbQ5ewl+Bs+KicrQ viYGtvk6+mYvRlmlQcTtYIHdFmRTzZV33tDstssl0HAsuoql7DWBPVJ8tFMZQ8SgoNaZ 55oA== X-Forwarded-Encrypted: i=1; AJvYcCUga9XjAQnHmevGdvJybB/xz0vbj4YGaSHWNCRag9OlFVEPG/sp/Jctkz3W3AHDpaEdm/dvFzdnYZVn7yMirMBg3Xk= X-Gm-Message-State: AOJu0Yx9cWSY6CMfQXcNc4RFKhTYkrCRAQyliysofke+tfbzrzsRcs/7 qxKu8W9p+sMa8cmvaT6ybOuYl/IgLq4c0pKswJWzYOqzqjAfc0g0mcHbXQbItPOX0NyTittTMug qlA== X-Google-Smtp-Source: AGHT+IFgq13bXMs5IFwZBCUn9d8o9CaVR4UgE59VD0DZm2aj6+589s0p0zY44eyaCf7x/WiMresCTSv8JYs= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:2234:4e4b:bcf0:406e]) (user=glider job=sendgmr) by 2002:a05:6402:3893:b0:568:a515:30e8 with SMTP id fd19-20020a056402389300b00568a51530e8mr77979edb.0.1710929935435; Wed, 20 Mar 2024 03:18:55 -0700 (PDT) Date: Wed, 20 Mar 2024 11:18:49 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240320101851.2589698-1-glider@google.com> Subject: [PATCH v2 1/3] mm: kmsan: implement kmsan_memmove() From: Alexander Potapenko To: glider@google.com, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, tglx@linutronix.de, x86@kernel.org, Tetsuo Handa , Dmitry Vyukov , Marco Elver , Linus Torvalds X-Rspamd-Queue-Id: 6856E40002 X-Rspam-User: X-Stat-Signature: 7oy19o53971z9cyt75pg8p4o1dbnj3m9 X-Rspamd-Server: rspam01 X-HE-Tag: 1710929937-493581 X-HE-Meta: U2FsdGVkX19woer35RTw/Hp1F0iKtlIUhcPTaYYcIa3X4Wm/zi2rhNywPz79CCqLnCyxSCFTvpn8u0rL11dXyrs8zqPsSmEZ6LaT4427mXLAWvVfwY5wrfeFXpuPVvEOPyBafXGpm7Fc0TZFl2bFQ9PXSPX1KrxTGo8bzhd4ZaijghCnPweMbjO1ViipdHf0Qf12uI9kU0saogZFLQ0Fza+wYaBs2vi+UdoQSJn8oYZ49XG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O56JgXCI580gjX7V7dZ0eB9t8BPt91uhGjGF0vH4DYlj8oe+BQUs8beM6B/b5VeD3BB4S9x5x8O9pVEwynDRDjTmLX+hQU1OXR5w7ULoAViz6fBOcGtuW5ldTSUOzojp+/ur3ZUy4FcDSDkv0oaH+bbWOro3wmEeFI8eJnLAUC51qroRFQ9jVWIRAL0B+HpzeMXhNEQZzG1Q7cdcy7PFVV9ssUs8fUuHIuULQW/KvDlSFe4fOLjSzRrvrkPAVnYXH44B4qZYdDTgvXn4OLd39Bxui+wzJCPoYCA3TbIkImWM40oN6SPk1gN5OClGI1+ZqHm+lO0tjRyX85zkwLLBJizbEnjOYGzdDXyNYHBtBra23RLw== 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: Provide a hook that can be used by custom memcpy implementations to tell KMSAN that the metadata needs to be copied. Without that, false positive reports are possible in the cases where KMSAN fails to intercept memory initialization. Link: https://lore.kernel.org/all/3b7dbd88-0861-4638-b2d2-911c97a4cadf@I-love.SAKURA.ne.jp/ Suggested-by: Tetsuo Handa Signed-off-by: Alexander Potapenko Cc: Dmitry Vyukov Cc: Marco Elver Cc: Linus Torvalds Reviewed-by: Marco Elver --- include/linux/kmsan-checks.h | 15 +++++++++++++++ mm/kmsan/hooks.c | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/kmsan-checks.h b/include/linux/kmsan-checks.h index c4cae333deec5..e1082dc40abc2 100644 --- a/include/linux/kmsan-checks.h +++ b/include/linux/kmsan-checks.h @@ -61,6 +61,17 @@ void kmsan_check_memory(const void *address, size_t size); void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, size_t left); +/** + * kmsan_memmove() - Notify KMSAN about a data copy within kernel. + * @to: destination address in the kernel. + * @from: source address in the kernel. + * @size: number of bytes to copy. + * + * Invoked after non-instrumented version (e.g. implemented using assembly + * code) of memmove()/memcpy() is called, in order to copy KMSAN's metadata. + */ +void kmsan_memmove(void *to, const void *from, size_t to_copy); + #else static inline void kmsan_poison_memory(const void *address, size_t size, @@ -78,6 +89,10 @@ static inline void kmsan_copy_to_user(void __user *to, const void *from, { } +static inline void kmsan_memmove(void *to, const void *from, size_t to_copy) +{ +} + #endif #endif /* _LINUX_KMSAN_CHECKS_H */ diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 5d6e2dee5692a..364f778ee226d 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -285,6 +285,17 @@ void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, } EXPORT_SYMBOL(kmsan_copy_to_user); +void kmsan_memmove(void *to, const void *from, size_t size) +{ + if (!kmsan_enabled || kmsan_in_runtime()) + return; + + kmsan_enter_runtime(); + kmsan_internal_memmove_metadata(to, (void *)from, size); + kmsan_leave_runtime(); +} +EXPORT_SYMBOL(kmsan_memmove); + /* Helper function to check an URB. */ void kmsan_handle_urb(const struct urb *urb, bool is_out) {