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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 366BFC021B1 for ; Thu, 20 Feb 2025 10:44:39 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.893781.1302636 (Exim 4.92) (envelope-from ) id 1tl42W-0007Ao-T9; Thu, 20 Feb 2025 10:44:20 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 893781.1302636; Thu, 20 Feb 2025 10:44:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tl42W-0007Ah-Qc; Thu, 20 Feb 2025 10:44:20 +0000 Received: by outflank-mailman (input) for mailman id 893781; Thu, 20 Feb 2025 10:44:19 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tl42V-0007Aa-FR for xen-devel@lists.xenproject.org; Thu, 20 Feb 2025 10:44:19 +0000 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [2a00:1450:4864:20::133]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a21c69b0-ef77-11ef-9896-31a8f345e629; Thu, 20 Feb 2025 11:44:17 +0100 (CET) Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-54622940ef7so863685e87.3 for ; Thu, 20 Feb 2025 02:44:17 -0800 (PST) Received: from [172.24.85.51] ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5461f0d74e6sm1355968e87.170.2025.02.20.02.44.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 20 Feb 2025 02:44:15 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a21c69b0-ef77-11ef-9896-31a8f345e629 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740048257; x=1740653057; darn=lists.xenproject.org; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=I58aMsUsGYHh19K18Tpweh/5mdV91tSQx1fWrigxIkY=; b=VXCKpPdHt/2kSkUskPVke6Hd3Z01bpcd+yLWI5aHJACBGBEOxvsCUa2J0itYScNfij ElYmmjyUkg4mP2I7iCT7lRXoVfGVA4+pjrsVQw7pyMlZbKAX3PEI1UYxM98Lr2DB6F7h +RqfR/4NeO6AdF2jSQgcoG50UWhvfk2xSBRSmvpf6E8BKWtBeomtF0MMjf9WfsmAJQn8 RKenznHTNyR3i6Xm0P4siAlbGGCP75NU1vjAqEd21qyKlwJZ815jurRtSsueZnLPHFY3 dbUNY71FeKvx+iKpOb2B4nRgjuHSRh5iobXHrl/984bNduNG/LPdAdueZoSleP37SOr6 Jx5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740048257; x=1740653057; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=I58aMsUsGYHh19K18Tpweh/5mdV91tSQx1fWrigxIkY=; b=eB7EB5J7ctCd6kYmL9Y+YAZLTLmVhBFN/wn9BgGwgFOUTmzteomlbbTjT9uEFp4GUj mdj3ORqlrFEdrXUcmzqnhhW6Bp08qpdcZgi205bBQlauarJb+VBtEtYu0MTY7XpxnxXi mBRcPH3KAk1pe+StjAVvn35Z9kyAwNg4nxav7SWy8IqBV/JFXh+ssWiIA7bz6McCpkO/ QE4sn+KKLqyLl2OX9yxLYWL9S1aeLiDHGiShvgpLkD5bxoHa398V3iBZL5oPX7zulFbG TuJL5tn2tFNQ7gmvS1GCI3rPLpAgxkOtnZd6ASiK5iDasGsEkLWLmaX0p2qDTDTXFny8 93ig== X-Gm-Message-State: AOJu0YwrZ+VhSHkxQSxvQxIwEiFJKLTwWmpnsMfUcWmNL5KB74pjfnLu 8ZhhXbeA7tfpj2A4gDwCNr7AZEYn/UsLgfE8s046qnjWSNZiqsj8 X-Gm-Gg: ASbGncs5DDr79x1E4Q/apnF3Tl/Nz+khHCp86M9ikjTeJ3j/niyHyacj4zTWwYFAMHH hCJnu1VcGTB5gBmlEuzv47XTbeyRWWw1d983/gnToFaWLgwh4fB7NJNpGRewXNesAJe391tREAC wEGjMd3IziiIbV+j3m8NBEEYJmcxdZEImct7lyb2WNsGxnISrY9wYyK+GIrzNU/JD+we59Y8jtk iCrt5ngD9abQPU23aCVFAnQQK5cfO1uEWF1f8r0LJ2TG4BmdSzLt9pEILHIfNFDoKF1vluP9EYT Zh6cmO+ChGDaCA9owMbuK6Y/ X-Google-Smtp-Source: AGHT+IEsA6CldtWAxRY8xd47P/q4bSZCszlPk/vcg0U52mZU022fj2zlDNRxF0M95dMzS7TjE458Qg== X-Received: by 2002:a05:6512:2251:b0:545:f9c:a80f with SMTP id 2adb3069b0e04-5462eed8656mr2153269e87.1.1740048256265; Thu, 20 Feb 2025 02:44:16 -0800 (PST) Content-Type: multipart/alternative; boundary="------------vwdxhIDGsRdO20ivrxwqCOG2" Message-ID: Date: Thu, 20 Feb 2025 11:44:15 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 1/2] xen/list: avoid UB in list iterators To: Stefano Stabellini , Andrew Cooper , Juergen Gross Cc: xen-devel@lists.xenproject.org, Jan Beulich , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Anthony PERARD , Michal Orzel , Julien Grall References: <20250219141818.8789-1-jgross@suse.com> <20250219141818.8789-2-jgross@suse.com> Content-Language: en-US From: Oleksii Kurochko In-Reply-To: This is a multi-part message in MIME format. --------------vwdxhIDGsRdO20ivrxwqCOG2 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2/20/25 2:38 AM, Stefano Stabellini wrote: > On Wed, 19 Feb 2025, Andrew Cooper wrote: >> On 19/02/2025 2:18 pm, Juergen Gross wrote: >>> The list_for_each_entry*() iterators are testing for having reached the >>> end of the list in a way which relies on undefined behavior: the >>> iterator (being a pointer to the struct of a list element) is advanced >>> and only then tested to have reached not the next element, but the list >>> head. This results in the list head being addressed via a list element >>> pointer, which is undefined, in case the list elements have a higher >>> alignment than the list head. >>> >>> Avoid that by testing for the end of the list before advancing the >>> iterator. In case of having reached the end of the list, set the >>> iterator to NULL and use that for stopping the loop. This has the >>> additional advantage of not leaking the iterator pointing to something >>> which isn't a list element past the loop. >>> >>> There is one case in the Xen code where the iterator is used after >>> the loop: it is tested to be non-NULL to indicate the loop has run >>> until reaching the end of the list. This case is modified to use the >>> iterator being NULL for indicating the end of the list has been >>> reached. >>> >>> Reported-by: Andrew Cooper >>> Signed-off-by: Juergen Gross >>> Release-Acked-by: Oleksii Kurochko >> I agree there's an issue here, but as said before, I do not agree with >> this patch. >> >> For starters, bloat-o-meter on a random top-of-tree build says >> >>     add/remove: 8/1 grow/shrink: 112/68 up/down: 4314/-2855 (1459) >> >> which is a horrible overhead for a case where the sequence of >> instructions are correct (only the C level types are a problem) and ... >> >>> --- >>> No proper Fixes: tag, as this bug predates Xen's git and mercurial >>> history. >>> V2: >>> - fix one use case (Jan Beulich) >>> - let list_is_first() return bool, rename parameter (Jan Beulich) >>> - paranthesize iterator when used as non-NULL test (Jan Beulich) >>> - avoid dereferencing NULL in the safe iterators (Jan Beulich) >>> --- >>> xen/drivers/passthrough/x86/hvm.c | 3 +- >> ... the need for this adjustment being discovered after-the-fact means >> it's a very risky change at this juncture in the release. > I have not reviewed the patch in enough detail to form an opinion on the > approach yet. However, I want to note that I also don't think that this > series should be committed at this stage of the release process. It > would be better to wait for the 4.21 release cycle. Based on the comments above lets consider then this patch to be merged to 4.21. Thanks. ~ Oleksii --------------vwdxhIDGsRdO20ivrxwqCOG2 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit


On 2/20/25 2:38 AM, Stefano Stabellini wrote:
On Wed, 19 Feb 2025, Andrew Cooper wrote:
On 19/02/2025 2:18 pm, Juergen Gross wrote:
The list_for_each_entry*() iterators are testing for having reached the
end of the list in a way which relies on undefined behavior: the
iterator (being a pointer to the struct of a list element) is advanced
and only then tested to have reached not the next element, but the list
head. This results in the list head being addressed via a list element
pointer, which is undefined, in case the list elements have a higher
alignment than the list head.

Avoid that by testing for the end of the list before advancing the
iterator. In case of having reached the end of the list, set the
iterator to NULL and use that for stopping the loop. This has the
additional advantage of not leaking the iterator pointing to something
which isn't a list element past the loop.

There is one case in the Xen code where the iterator is used after
the loop: it is tested to be non-NULL to indicate the loop has run
until reaching the end of the list. This case is modified to use the
iterator being NULL for indicating the end of the list has been
reached.

Reported-by: Andrew Cooper <andrew.cooper3@citrix.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
Release-Acked-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
I agree there's an issue here, but as said before, I do not agree with
this patch.

For starters, bloat-o-meter on a random top-of-tree build says

    add/remove: 8/1 grow/shrink: 112/68 up/down: 4314/-2855 (1459)

which is a horrible overhead for a case where the sequence of
instructions are correct (only the C level types are a problem) and ...

---
No proper Fixes: tag, as this bug predates Xen's git and mercurial
history.
V2:
- fix one use case (Jan Beulich)
- let list_is_first() return bool, rename parameter (Jan Beulich)
- paranthesize iterator when used as non-NULL test (Jan Beulich)
- avoid dereferencing NULL in the safe iterators (Jan Beulich)
---
 xen/drivers/passthrough/x86/hvm.c |   3 +-
... the need for this adjustment being discovered after-the-fact means
it's a very risky change at this juncture in the release.
I have not reviewed the patch in enough detail to form an opinion on the
approach yet. However, I want to note that I also don't think that this
series should be committed at this stage of the release process. It
would be better to wait for the 4.21 release cycle.
Based on the comments above lets consider then this patch to be merged to 4.21.

Thanks.

~ Oleksii
--------------vwdxhIDGsRdO20ivrxwqCOG2--