From patchwork Tue May 28 10:48:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13676465 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 E19AAC25B78 for ; Tue, 28 May 2024 10:48:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 727976B007B; Tue, 28 May 2024 06:48:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D7476B0095; Tue, 28 May 2024 06:48:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59EBB6B0098; Tue, 28 May 2024 06:48:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 3D0926B007B for ; Tue, 28 May 2024 06:48:17 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BAE6C16048E for ; Tue, 28 May 2024 10:48:16 +0000 (UTC) X-FDA: 82167480192.07.9AD230B Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf29.hostedemail.com (Postfix) with ESMTP id 12A8A120007 for ; Tue, 28 May 2024 10:48:13 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gUfNpfff; spf=pass (imf29.hostedemail.com: domain of 3bLZVZgYKCGkNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3bLZVZgYKCGkNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@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=1716893294; 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=168aCLfPuYgGXMseZYgeOmJsf4bgrF3NmHxiEDuMCbc=; b=ngOIlTtaP99cDzaF6LUnj8sIY2aCjBZetouOw+WzXIyZkFtHyyfvf4hdCqzjj20EkXXVKD SW545mTlN2vj7gljKL/kTIkp5YnsAC0n8xfL9NFO5JhG7cKLk3ERq0D6JeP1T0ruKSiWnW qQl8eSLWF+J2XBKroqd9+kQEp5kiOCA= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gUfNpfff; spf=pass (imf29.hostedemail.com: domain of 3bLZVZgYKCGkNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3bLZVZgYKCGkNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716893294; a=rsa-sha256; cv=none; b=QR0lYndOQ/00+BJbFOnN5WEX+olvaeV2JGsIJO/grvji4cwvE8qo2dF3qA3PM6qEyLKOBx 3/HbF4LjXBD1o7tOWFtk7AOBUfVlmNXQzlDF5yIo34fQZh65tHdgPhaXH8aPwZr7qutYb2 WxNkEyqr+UzVU1eu7PGHoS1n0kRtx1o= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-62a1e9807c0so7795247b3.0 for ; Tue, 28 May 2024 03:48:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716893293; x=1717498093; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=168aCLfPuYgGXMseZYgeOmJsf4bgrF3NmHxiEDuMCbc=; b=gUfNpfffOuCZ8TpWw6DMZr5cajz/AtZF5bKBmGb7CryOkbDsU3zoZInEmexY6lkkU3 +A4kjBtz4L6nms/mFvALuKa49YxyhznLCReCoFbwEEtSqKU+Wgw+HYVKT/I+L96q4GFm ldSfGsjJfrXhfxqkuEtDshZ4KtlECx0DUAwEOJeIEy1p4fkneeUI/tb55qmWNTN+68K1 53XdDxwQqDq4glmvXX6QsiLIWmTYqXvT/YrEyJkAqLXMRMPKTXXJbqXMyCJZlcTU5EzA yDYLNh5XFHgSAyPL6R77ER7uZmzmz0itEBN5cevyXRWaSIHHIoaB1+B/CU8IWtWYmVxo BBkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716893293; x=1717498093; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=168aCLfPuYgGXMseZYgeOmJsf4bgrF3NmHxiEDuMCbc=; b=nqadpIHT1Isr5PvUULslbVO9X7/2LbUlb5FczWSthTbaiwkauTvAndEiLIGt6t10bP zsyKasGJceK8FRzaH8gikSjf8QwXQEUd3xiqvstKKU36AkQemjx8JErwjtwggA8goaBf fb4XHC+LfQ/JAvcOJ1PBwLfQA78Z5tzt2y5MGs6hXe+1ICY0CJ4w1DvCQX+HJEVdtANm 1tq5f7TH7PXkXhSr0I0jUPPvA10sLOUd/6R2CsKUnIvI+fzg74Jfe4ApGyGQBKW8L+UT 2HiUYUiFOqxo2p7rhmgPzW64KxJ9o7iTncaYAuZ/K3i7uzDXKkkOJqv1ApjBgATNJ01V /CAw== X-Forwarded-Encrypted: i=1; AJvYcCUsNMSoCjc6kIJQYXKl/QClKMliWws0Pq7cnpmtMy3EtpoFISt6OFbKQ6PnMOpyLdhqiTn0Xgsy/J8rXRtxR6dAS0c= X-Gm-Message-State: AOJu0YzEtztJv6+Kg+hlidPoj25TSua1MTXviwsCiCOC2Y+PKz93gg5H /ObHAKz1orR8r0oPLX6WHMGN90bBwOityohFDPscIZTi2SZFFh2k9Zosems75Lece5h+qD8owDY g/g== X-Google-Smtp-Source: AGHT+IEDWQiLMhofWJRVp2xMhEilUEioPkayeNWYBHymPh/j08uSwId41HBaCP4sI/H4bKASCeL6PRXSLEM= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:6416:417e:596:420a]) (user=glider job=sendgmr) by 2002:a05:690c:600b:b0:611:5a9d:bb0e with SMTP id 00721157ae682-62a076284d6mr32629097b3.4.1716893292893; Tue, 28 May 2024 03:48:12 -0700 (PDT) Date: Tue, 28 May 2024 12:48:06 +0200 Mime-Version: 1.0 X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240528104807.738758-1-glider@google.com> Subject: [PATCH 1/2] kmsan: do not wipe out origin when doing partial unpoisoning From: Alexander Potapenko To: glider@google.com Cc: elver@google.com, dvyukov@google.com, akpm@linux-foundation.org, bjohannesmeyer@gmail.com, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 12A8A120007 X-Stat-Signature: xsh4tjji1e7qyinpzfp74kipcdhc3b31 X-HE-Tag: 1716893293-318866 X-HE-Meta: U2FsdGVkX19GjEJBjX8HLg2h5VTVAeaI/ASTOUiMB+f0muNW9Rt+BJUWDZNkJ5TlCVUCQNUzRC3yL11g3xGCDAtLPNGabcQ01iGAypbtDgvnnBFsSJkTkuooNNVPVI3G3saScPRNAPkVcWiWZrguF0XQz6yU4HX9TjFb3AUXjIRi9fn8JeZ8de9LLyXoQx194GGsVp8a3RokkPumq10tY5w3h6saTQX9UQbAOOx939s9JFYjeanyKieWDXQ/819c0X3mb7oFsPcUI0z2R3GYTU3LXB4RbHZba/mBYQkDe+j540Q9pEeeZLtRc0jZ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/kEMbMgkQt46axdqkL464OQgfnSKlqBex/Bzp+dXD8HaIsOp2HLlgnG/8Bsy9tuKdZ16Kd445aiB7FCwenSA00CKDJu1nG3m5XBgM8wdbH+5am3Kx+9WVSB1Jvcs9u2C7vN5wuM4xZiLbKXfSLwESZfMIp5g9p+dPNHHFF6XTnPnaaZa8+Rzap/bRZSEOUxUBsdWdqvYyIAzO8f9B5052KcqDCwJxvVpJib13Akznr0TXJKMgpJcsaRr+YQko+xjOflhWBjEJlZzfWzJdElmCUyrMaYzUhhsM5NPl83og== X-Bogosity: Ham, tests=bogofilter, spamicity=0.007677, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: As noticed by Brian, KMSAN should not be zeroing the origin when unpoisoning parts of a four-byte uninitialized value, e.g.: char a[4]; kmsan_unpoison_memory(a, 1); This led to false negatives, as certain poisoned values could receive zero origins, preventing those values from being reported. To fix the problem, check that kmsan_internal_set_shadow_origin() writes zero origins only to slots which have zero shadow. Reported-by: Brian Johannesmeyer Link: https://lore.kernel.org/lkml/20240524232804.1984355-1-bjohannesmeyer@gmail.com/T/ Fixes: f80be4571b19 ("kmsan: add KMSAN runtime core") Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver Tested-by: Brian Johannesmeyer --- mm/kmsan/core.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mm/kmsan/core.c b/mm/kmsan/core.c index cf2d70e9c9a5f..95f859e38c533 100644 --- a/mm/kmsan/core.c +++ b/mm/kmsan/core.c @@ -196,8 +196,7 @@ void kmsan_internal_set_shadow_origin(void *addr, size_t size, int b, u32 origin, bool checked) { u64 address = (u64)addr; - void *shadow_start; - u32 *origin_start; + u32 *shadow_start, *origin_start; size_t pad = 0; KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(addr, size)); @@ -225,8 +224,16 @@ void kmsan_internal_set_shadow_origin(void *addr, size_t size, int b, origin_start = (u32 *)kmsan_get_metadata((void *)address, KMSAN_META_ORIGIN); - for (int i = 0; i < size / KMSAN_ORIGIN_SIZE; i++) - origin_start[i] = origin; + /* + * If the new origin is non-zero, assume that the shadow byte is also non-zero, + * and unconditionally overwrite the old origin slot. + * If the new origin is zero, overwrite the old origin slot iff the + * corresponding shadow slot is zero. + */ + for (int i = 0; i < size / KMSAN_ORIGIN_SIZE; i++) { + if (origin || !shadow_start[i]) + origin_start[i] = origin; + } } struct page *kmsan_vmalloc_to_page_or_null(void *vaddr)