From patchwork Thu Apr 4 08:55:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 785817 Delivered-To: patch@linaro.org Received: by 2002:adf:f001:0:b0:33e:7753:30bd with SMTP id j1csp724172wro; Thu, 4 Apr 2024 01:56:40 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUHDS2NQXMZWxJ++uWbtIwXgCvpKF+qVHoXUaus6axV4MQ0PmRBsGvJxJgk+LjPgC2UzdY74QKltAzQUaxvpCEf X-Google-Smtp-Source: AGHT+IEj05WoVbd3FxrJHJf5ckyV42PEb3g4eAcYLOEtA54QINL0ugTISgy6UCh3LlHPvo6/eT09 X-Received: by 2002:a05:620a:394c:b0:78d:3c75:12c6 with SMTP id qs12-20020a05620a394c00b0078d3c7512c6mr2209088qkn.16.1712220999806; Thu, 04 Apr 2024 01:56:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712220999; cv=none; d=google.com; s=arc-20160816; b=Ym/1lTY/mpQYvaoaFmW64cWAhxez1uegqL1Rqa4QT32rE7plYHEC4+2JhQD3/JMqFo eDdwzT5QIGydfouqRDBggmtqib7Zz0lqWU8hTKvW360RIbJhOAuq0hh7Z03gyaRLEGqu yW3JyHpXGK7Ic710u1qtaxQd3BZN/JrpUMRIav46aQ93szOU3JRmhcD4s+HvjqZmKB/Z /RFVajEY23trdnfdpLz4YGgv1/X18CLue3m3wnOUdn18Q6TbdX4ntBp9SNm7san/Zx4D XtI9R9GSLNkfAZAiQyJ1AVXP4Hr854ZOgVasKra7CnmgS4hvcHL6uWPoVwwP5BPPyaKq ntUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=d675Kf1VTwkAdTecByRBW+ew2Ri5wN4dvsw2IHRaYGs=; fh=PVJjyfh+xjPgP8xboX8y8cHVeNIz55Ulvi9kytzeGvk=; b=rlkPUoN4b3swjX/IOcj2QnblQC88b/kGGctxFKU+z0TlDymoMNW1hD6Mq03Se/Dtkh b5jWEANLC9Y38cBxB+QUG+sY5kFDjwZg6vYWGJnVfN6xSzVeHGi2oo3upVcGDGuzi6Zx gSlsYf31Pzn4xYR4KmYg1RMWngYZcdFXBQlYZt0l2fsq24nW6oFBvP+L5BFdYM8/0t3l t4yZ6VZfKpikzR26EVCzGLADzOFKpJxXJuv1Mohq/j0Cb9V63XrgUktZQFJ+5eeCntjg 3Pm4AtFIHUCp9AU3u6LYeA4A85MNmh4WUDjCBVAOxWCIFl6O97yJQLl8DDd9xh6E9DjV b93w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="aORh1k/q"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id c24-20020a05620a0cf800b007887d33cb0bsi16087012qkj.101.2024.04.04.01.56.39 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 04 Apr 2024 01:56:39 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="aORh1k/q"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rsIt5-0004Vj-V7; Thu, 04 Apr 2024 04:55:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rsIt4-0004TT-7d for qemu-devel@nongnu.org; Thu, 04 Apr 2024 04:55:58 -0400 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rsIt1-00042y-QZ for qemu-devel@nongnu.org; Thu, 04 Apr 2024 04:55:57 -0400 Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-34388753650so318319f8f.3 for ; Thu, 04 Apr 2024 01:55:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712220953; x=1712825753; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=d675Kf1VTwkAdTecByRBW+ew2Ri5wN4dvsw2IHRaYGs=; b=aORh1k/qGMnTnm3Da+4tIqK05K3eXYnFtD3I9AF7jq4vi6/SJJykVitZL+iQhlp+Gi mxkYNqFaHF+uEDW2l0fYEH0DQkT/bjChAraiO4ktCFlWMKCHSaKddzSY/Q9lOuNPI5ja NcVbbjFSMjP2idKNipxG4BKCibGdDG9BPtYkVO7oCrmGHAqAqJbrTsxkUpyXYziKHDk3 fmX+PYsbZLDhVOdLoirKdcnIBA86h/bNI03fioqJHq++aqWoqWHaO9X0PF/pU+i+dl7v +4SLRL5m9gAh/ee2Kckd5WbZ0DGVAtNNH9m+uwtF+ewpLn2hvs8UBhbv8JH5MixF0r2P QATg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712220953; x=1712825753; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=d675Kf1VTwkAdTecByRBW+ew2Ri5wN4dvsw2IHRaYGs=; b=TOlAjqsS3oOllwKShrzkszSBRGDR7UftaAYe3HO7OFhp/QVaRhYWQUSO3dx3dh0zIv dmmFoCaIrM4vJOdAKqh7CIYCBo7TJ9LRAkYPmN+sNdoIcae/g480YGNxcC/iDppOxp9p OS4OJB4G1vFyYO+0lh0SpiKxSc7yd2aPdczWwKcK6+U4K5rNHe596zk+if9HctDgN2qi IIgAuec1FzFvoEvu1NnNgNRjUmqPKPM7qa9H4F5Ro6qQeOZEZ9Ex4iSYUYlFi7hYURFT qWdvwlr+8NP6B0pt4zOZEOrNIAn96YH8QujColQpEIaxlfbVa/jaGFCFJmfGvjObj+A7 OuAQ== X-Gm-Message-State: AOJu0YxauNym4k3IGBQxURvNu3/tpP+fVtodUYsx4+rTF9lC4Xbqx8DI b3hnsQJg1b4ucnuiQWuDENntO525Th69XD/C1gL/qlI8KyAQc/ZYawWVNSb6mSbZ16ONZ2ScN3p AET4= X-Received: by 2002:adf:db4c:0:b0:343:b9e4:7f70 with SMTP id f12-20020adfdb4c000000b00343b9e47f70mr890946wrj.53.1712220953083; Thu, 04 Apr 2024 01:55:53 -0700 (PDT) Received: from localhost.localdomain ([77.205.40.54]) by smtp.gmail.com with ESMTPSA id w18-20020adff9d2000000b00343b09729easm2039339wrr.69.2024.04.04.01.55.51 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 04 Apr 2024 01:55:52 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Bin Meng , Mauro Matteo Cascella , qemu-stable@nongnu.org, Alexander Bulekov , Chuhong Yuan Subject: [PATCH-for-9.0] hw/sd/sdhci: Discard excess of data written to Buffer Data Port register Date: Thu, 4 Apr 2024 10:55:48 +0200 Message-ID: <20240404085549.16987-1-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::436; envelope-from=philmd@linaro.org; helo=mail-wr1-x436.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Per "SD Host Controller Standard Specification Version 3.00": * 1.7 Buffer Control - 1.7.1 Control of Buffer Pointer (3) Buffer Control with Block Size In case of write operation, the buffer accumulates the data written through the Buffer Data Port register. When the buffer pointer reaches the block size, Buffer Write Enable in the Present State register changes 1 to 0. It means no more data can be written to the buffer. Excess data of the last write is ignored. For example, if just lower 2 bytes data can be written to the buffer and a 32-bit (4-byte) block of data is written to the Buffer Data Port register, the lower 2 bytes of data is written to the buffer and the upper 2 bytes is ignored. Discard the excess of data to avoid overflow reported by fuzzer: $ cat << EOF | qemu-system-i386 \ -display none -nodefaults \ -machine accel=qtest -m 512M \ -device sdhci-pci,sd-spec-version=3 \ -device sd-card,drive=mydrive \ -drive if=none,index=0,file=null-co://,format=raw,id=mydrive -nographic \ -qtest stdio outl 0xcf8 0x80001013 outl 0xcfc 0x91 outl 0xcf8 0x80001001 outl 0xcfc 0x06000000 write 0x9100002c 0x1 0x05 write 0x91000058 0x1 0x16 write 0x91000005 0x1 0x04 write 0x91000028 0x1 0x08 write 0x16 0x1 0x21 write 0x19 0x1 0x20 write 0x9100000c 0x1 0x01 write 0x9100000e 0x1 0x20 write 0x9100000f 0x1 0x00 write 0x9100000c 0x1 0x00 write 0x91000020 0x1 0x00 EOF Stack trace (part): ================================================================= ==89993==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x615000029900 at pc 0x55d5f885700d bp 0x7ffc1e1e9470 sp 0x7ffc1e1e9468 WRITE of size 1 at 0x615000029900 thread T0 #0 0x55d5f885700c in sdhci_write_dataport hw/sd/sdhci.c:564:39 #1 0x55d5f8849150 in sdhci_write hw/sd/sdhci.c:1223:13 #2 0x55d5fa01db63 in memory_region_write_accessor system/memory.c:497:5 #3 0x55d5fa01d245 in access_with_adjusted_size system/memory.c:573:18 #4 0x55d5fa01b1a9 in memory_region_dispatch_write system/memory.c:1521:16 #5 0x55d5fa09f5c9 in flatview_write_continue system/physmem.c:2711:23 #6 0x55d5fa08f78b in flatview_write system/physmem.c:2753:12 #7 0x55d5fa08f258 in address_space_write system/physmem.c:2860:18 ... 0x615000029900 is located 0 bytes to the right of 512-byte region [0x615000029700,0x615000029900) allocated by thread T0 here: #0 0x55d5f7237b27 in __interceptor_calloc #1 0x7f9e36dd4c50 in g_malloc0 #2 0x55d5f88672f7 in sdhci_pci_realize hw/sd/sdhci-pci.c:36:5 #3 0x55d5f844b582 in pci_qdev_realize hw/pci/pci.c:2092:9 #4 0x55d5fa2ee74b in device_set_realized hw/core/qdev.c:510:13 #5 0x55d5fa325bfb in property_set_bool qom/object.c:2358:5 #6 0x55d5fa31ea45 in object_property_set qom/object.c:1472:5 #7 0x55d5fa332509 in object_property_set_qobject om/qom-qobject.c:28:10 #8 0x55d5fa31f6ed in object_property_set_bool qom/object.c:1541:15 #9 0x55d5fa2e2948 in qdev_realize hw/core/qdev.c:292:12 #10 0x55d5f8eed3f1 in qdev_device_add_from_qdict system/qdev-monitor.c:719:10 #11 0x55d5f8eef7ff in qdev_device_add system/qdev-monitor.c:738:11 #12 0x55d5f8f211f0 in device_init_func system/vl.c:1200:11 #13 0x55d5fad0877d in qemu_opts_foreach util/qemu-option.c:1135:14 #14 0x55d5f8f0df9c in qemu_create_cli_devices system/vl.c:2638:5 #15 0x55d5f8f0db24 in qmp_x_exit_preconfig system/vl.c:2706:5 #16 0x55d5f8f14dc0 in qemu_init system/vl.c:3737:9 ... SUMMARY: AddressSanitizer: heap-buffer-overflow hw/sd/sdhci.c:564:39 in sdhci_write_dataport Cc: qemu-stable@nongnu.org Fixes: d7dfca0807 ("hw/sdhci: introduce standard SD host controller") Buglink: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=58813 Reported-by: Alexander Bulekov Reported-by: Chuhong Yuan Signed-off-by: Philippe Mathieu-Daudé Reviewed-by: Peter Maydell --- hw/sd/sdhci.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hw/sd/sdhci.c b/hw/sd/sdhci.c index c5e0bc018b..2dd88fa139 100644 --- a/hw/sd/sdhci.c +++ b/hw/sd/sdhci.c @@ -552,7 +552,7 @@ static void sdhci_write_block_to_card(SDHCIState *s) * register */ static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size) { - unsigned i; + unsigned i, available; /* Check that there is free space left in a buffer */ if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) { @@ -560,6 +560,14 @@ static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size) return; } + available = s->buf_maxsz - s->data_count; + if (size > available) { + qemu_log_mask(LOG_GUEST_ERROR, "SDHC buffer data full (size: %"PRIu32")" + " discarding %u byte%s\n", + s->buf_maxsz, size - available, + size - available > 1 ? "s" : ""); + size = available; /* Excess data of the last write is ignored. */ + } for (i = 0; i < size; i++) { s->fifo_buffer[s->data_count] = value & 0xFF; s->data_count++;