From patchwork Thu Nov 14 14:16:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 843122 Delivered-To: patch@linaro.org Received: by 2002:adf:9bcc:0:b0:382:184f:390f with SMTP id e12csp301750wrc; Thu, 14 Nov 2024 06:17:21 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCU/on7L8O+quZGdpPG/Ctf8jS6d+jNFsVfBsWgURtl+2E84/9YnGkmhYR/Nr/J+K4jAwiO1Tg==@linaro.org X-Google-Smtp-Source: AGHT+IGy3OeQfgkOcdsJxUUcQWYPs6Bp5ejkLebld6yZvQJ9t1Tvg5Totbvn1Bi266LTXAoZykzN X-Received: by 2002:a05:622a:293:b0:460:4e67:d67f with SMTP id d75a77b69052e-4630930e049mr346893021cf.2.1731593841002; Thu, 14 Nov 2024 06:17:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1731593840; cv=none; d=google.com; s=arc-20240605; b=e2YAkbYaFUTbMK6cd62/Co3lLbF7M0Ji3fW+TAGeQ82ydbRiQ3mHZQ9sDPI4oHdmrP qaSX8GcWOBhEOL7fS9VxuUWrNK+hbZ6Yud471Kw4WX7HMe3+mefyBdktjKLxHwfA0ZcI TKR7POJffCmVswz29RGUMnXh5rQXTR7xVx8j9/j+YttCZzG7ebKMCIu1pCENzytHXorK apGnhAvQ9/t2D87RdO+5KwjtIMdH/SG0eXTayPW9q7AOr16yyMW/wWPnZriy6E/s1+xU g2ivNxTBu3Gd/lUc1aYJVaeR1oba/PawabUWNvLHuC1YtRPxNkQ7RpO8aCtHyy+2Sdoh AkPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3cl4ZO8ew2BAiajP9BAvW5+7ZXDojU8B2khh55fONv8=; fh=Xv3tgv6XlaNAfzuwDULeg8Ugba1x/wq+cAw7U5F/8pM=; b=L/FJAeZpJUaaERG2dR/XEyhjHYgSw70q+b3g2V81huSgVyuDIeDC0BZFCsYG8de8P5 aXoDS89At8NQFgsFpDD/PrrzJpL0hHP8Qm+KLCiO6AWQFAjuTnT+9LV2dXKJ7fAICIkq gax0zRDw1XdQtvzg3W05qBvsXHNtMKy3MD0pdNo0x5UIjCTBXf4+Aq25meCX7JLIf3OA rvP7bteiVGZgaKJI9auwfD0w2kyNcNCisHj+vwSTzLiN5JeDdv95Z/TzSxPwfOlE9MXo 1mmr+GNahxY2fpe2QIPoechWPklZH8KS5q0n46Nu2eJWkH/SOvwyr4AoTNueAZ2CruHg o7OQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=lY4oVxKQ; 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; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7b35cafdc3esi126727785a.664.2024.11.14.06.17.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 14 Nov 2024 06:17:20 -0800 (PST) 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=lY4oVxKQ; 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; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tBae5-0000gQ-Ho; Thu, 14 Nov 2024 09:16:29 -0500 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 1tBae2-0000fY-OF for qemu-devel@nongnu.org; Thu, 14 Nov 2024 09:16:26 -0500 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tBae0-0005hS-HS for qemu-devel@nongnu.org; Thu, 14 Nov 2024 09:16:26 -0500 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-431ac30d379so6201885e9.1 for ; Thu, 14 Nov 2024 06:16:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1731593782; x=1732198582; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3cl4ZO8ew2BAiajP9BAvW5+7ZXDojU8B2khh55fONv8=; b=lY4oVxKQQPKtfjWHd4ITfc66T4qH8fKdEFfYCrHoIcOetEPWg3vwc6X0AG/ZyJPHCg a2+C91b5iCoWXskYGAHYhM9RdBhht2KVbh9BSleuF9+cdgforsDSQnOBGD1v81zwgN28 FxlcMr1H3Gg52WCXuu/HCGYDIWV7uvCizAX7LbSxhHTLflnPm1OPRWLEfJuGefZYA3AX OexDWzUmfavQ/1SEs/1GWE7l5rQLi7dSWC+8vXX6qxJzgvpYVc/OZdGV3+uxqVaLqcqU KQxyQqks7Kjw6FrPJo85kvrbjWa2omFGv7BeAu7+7rNJP4YcS8BWTkaNg+h/DwIZbWnR k6RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731593782; x=1732198582; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3cl4ZO8ew2BAiajP9BAvW5+7ZXDojU8B2khh55fONv8=; b=oo0lO9FJu0iftJPj+SQZudCjGeWBhsblqi56K+C23q8iP3Gyy/whITKvm6Woj156nO 9nma0bWR+DfG7JU8OEGuFl/RcoWmLI4JHtPm1rJSTwjkVhry6UX0l9oqtczRuyNHXoZK mY0r2OmZoGUdai8spN248BMDQYaBMEHJNE4ubY98c4OiExS+rMCdf8Qwr1vGjFll9Gx+ Bkupk37tLUAq2mbUvpsSJWfgRKyZ+kf0zVL/6kLZr8Mni+eA0fJMctW3UUjFmI0qX5bh 7K13D/p2loXbG6Gsq93pPHbSBzRe+UKIne+kdqIgbW5n9joX79x/fBgcUVxXU7kDvIxG ivoQ== X-Gm-Message-State: AOJu0YxeanaQ0ZVZchYm0XvRSr+G6AcjfVBgz1As3/+FO8eMpwoOcLi2 xkQIzwbMKNNE8Cjhf5WngZog4rSZYnaCX8/BrqYoxkiNs7AGiBNOOhcRMZgZrFz0gzSWJtK3wbb U X-Received: by 2002:a05:600c:4f04:b0:42e:d4a2:ce67 with SMTP id 5b1f17b1804b1-432b7505d19mr234484085e9.17.1731593782490; Thu, 14 Nov 2024 06:16:22 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432da244915sm24941655e9.3.2024.11.14.06.16.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2024 06:16:22 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Jason Wang , Dmitry Fleytman , Akihiko Odaki , Yuri Benditovich Subject: [PATCH v2 2/2] net: mark struct ip_header as QEMU_PACKED Date: Thu, 14 Nov 2024 14:16:19 +0000 Message-Id: <20241114141619.806652-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114141619.806652-1-peter.maydell@linaro.org> References: <20241114141619.806652-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::336; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x336.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=ham 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 The ip_header is not actually guaranteed to be aligned. We attempt to deal with this in some places such as net_checksum_calculate() by using stw_be_p and so on to access the fields, but this is not sufficient to be correct, because even accessing a byte member within an unaligned struct is undefined behaviour. The clang sanitizer will emit warnings like these if net_checksum_calculate() is called: Stopping network: ../../net/checksum.c:106:9: runtime error: member access within misaligned address 0x556aad9b502e for type 'struct ip_header', which requires 4 byte alignment 0x556aad9b502e: note: pointer points here 34 56 08 00 45 00 01 48 a5 09 40 00 40 11 7c 8b 0a 00 02 0f 0a 00 02 02 00 44 00 43 01 34 19 56 ^ SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../net/checksum.c:106:9 in ../../net/checksum.c:106:9: runtime error: load of misaligned address 0x556aad9b502e for type 'uint8_t' (aka 'unsigned char'), which requires 4 byte alignment 0x556aad9b502e: note: pointer points here 34 56 08 00 45 00 01 48 a5 09 40 00 40 11 7c 8b 0a 00 02 0f 0a 00 02 02 00 44 00 43 01 34 19 56 ^ Fix this by marking the ip_header struct as QEMU_PACKED, so that the compiler knows that it might be unaligned and will generate the right code for accessing fields. Signed-off-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daudé --- include/net/eth.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/net/eth.h b/include/net/eth.h index 3b80b6e07f3..14c34f530fe 100644 --- a/include/net/eth.h +++ b/include/net/eth.h @@ -56,7 +56,7 @@ struct ip_header { uint8_t ip_p; /* protocol */ uint16_t ip_sum; /* checksum */ uint32_t ip_src, ip_dst; /* source and destination address */ -}; +} QEMU_PACKED; typedef struct tcp_header { uint16_t th_sport; /* source port */