xen_read_msr_safe() currently passes an uninitialized argument err to
xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
xen_read_msr_safe() could return err with an unpredictable value.
To ensure correctness, initialize err to 0 (representing success)
in xen_read_msr_safe().
Because xen_read_msr_safe() is essentially a wrapper of xen_do_read_msr(),
the latter should be responsible for initializing the value of *err to 0.
Thus initialize *err to 0 in xen_do_read_msr().
Fixes: 502ad6e5a619 ("x86/msr: Change the function type of native_read_msr_safe()")
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
Signed-off-by: Xin Li (Intel) <xin@zytor.com>
---
arch/x86/xen/enlighten_pv.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
index 3be38350f044..01f1d441347e 100644
--- a/arch/x86/xen/enlighten_pv.c
+++ b/arch/x86/xen/enlighten_pv.c
@@ -1091,6 +1091,9 @@ static u64 xen_do_read_msr(u32 msr, int *err)
{
u64 val = 0; /* Avoid uninitialized value for safe variant. */
+ if (err)
+ *err = 0;
+
if (pmu_msr_chk_emulated(msr, &val, true))
return val;
@@ -1162,7 +1165,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)
static int xen_read_msr_safe(u32 msr, u64 *val)
{
- int err;
+ int err = 0;
*val = xen_do_read_msr(msr, &err);
return err;
--
2.49.0
* Xin Li (Intel) <xin@zytor.com> wrote:
> xen_read_msr_safe() currently passes an uninitialized argument err to
> xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
> xen_read_msr_safe() could return err with an unpredictable value.
>
> To ensure correctness, initialize err to 0 (representing success)
> in xen_read_msr_safe().
>
> Because xen_read_msr_safe() is essentially a wrapper of xen_do_read_msr(),
> the latter should be responsible for initializing the value of *err to 0.
> Thus initialize *err to 0 in xen_do_read_msr().
>
> Fixes: 502ad6e5a619 ("x86/msr: Change the function type of native_read_msr_safe()")
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
> Signed-off-by: Xin Li (Intel) <xin@zytor.com>
> ---
> arch/x86/xen/enlighten_pv.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
> index 3be38350f044..01f1d441347e 100644
> --- a/arch/x86/xen/enlighten_pv.c
> +++ b/arch/x86/xen/enlighten_pv.c
> @@ -1091,6 +1091,9 @@ static u64 xen_do_read_msr(u32 msr, int *err)
> {
> u64 val = 0; /* Avoid uninitialized value for safe variant. */
>
> + if (err)
> + *err = 0;
> +
> if (pmu_msr_chk_emulated(msr, &val, true))
> return val;
>
> @@ -1162,7 +1165,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)
>
> static int xen_read_msr_safe(u32 msr, u64 *val)
> {
> - int err;
> + int err = 0;
>
> *val = xen_do_read_msr(msr, &err);
> return err;
So why not initialize 'err' with 0 in both callers, xen_read_msr_safe()
and xen_read_msr(), and avoid all the initialization trouble in
xen_do_read_msr()?
Thanks,
Ingo
On 5/15/2025 8:29 AM, Ingo Molnar wrote:
>
> * Xin Li (Intel) <xin@zytor.com> wrote:
>
>> xen_read_msr_safe() currently passes an uninitialized argument err to
>> xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
>> xen_read_msr_safe() could return err with an unpredictable value.
>>
>> To ensure correctness, initialize err to 0 (representing success)
>> in xen_read_msr_safe().
>>
>> Because xen_read_msr_safe() is essentially a wrapper of xen_do_read_msr(),
>> the latter should be responsible for initializing the value of *err to 0.
>> Thus initialize *err to 0 in xen_do_read_msr().
>>
>> Fixes: 502ad6e5a619 ("x86/msr: Change the function type of native_read_msr_safe()")
>> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
>> Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
>> Signed-off-by: Xin Li (Intel) <xin@zytor.com>
>> ---
>> arch/x86/xen/enlighten_pv.c | 5 ++++-
>> 1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
>> index 3be38350f044..01f1d441347e 100644
>> --- a/arch/x86/xen/enlighten_pv.c
>> +++ b/arch/x86/xen/enlighten_pv.c
>> @@ -1091,6 +1091,9 @@ static u64 xen_do_read_msr(u32 msr, int *err)
>> {
>> u64 val = 0; /* Avoid uninitialized value for safe variant. */
>>
>> + if (err)
>> + *err = 0;
>> +
>> if (pmu_msr_chk_emulated(msr, &val, true))
>> return val;
>>
>> @@ -1162,7 +1165,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)
>>
>> static int xen_read_msr_safe(u32 msr, u64 *val)
>> {
>> - int err;
>> + int err = 0;
>>
>> *val = xen_do_read_msr(msr, &err);
>> return err;
>
> So why not initialize 'err' with 0 in both callers, xen_read_msr_safe()
> and xen_read_msr(), and avoid all the initialization trouble in
> xen_do_read_msr()?
Yeah, I should make the change in xen_read_msr() too.
However xen_do_read_msr() should be implemented in a defensive way to
set *err properly as it's part of its return value. Actually it was so,
but one of my previous cleanup patch removed it because err is no longer
passed to pmu_msr_chk_emulated().
Thanks!
Xin
On 15.05.25 20:11, Xin Li wrote:
> On 5/15/2025 8:29 AM, Ingo Molnar wrote:
>>
>> * Xin Li (Intel) <xin@zytor.com> wrote:
>>
>>> xen_read_msr_safe() currently passes an uninitialized argument err to
>>> xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
>>> xen_read_msr_safe() could return err with an unpredictable value.
>>>
>>> To ensure correctness, initialize err to 0 (representing success)
>>> in xen_read_msr_safe().
>>>
>>> Because xen_read_msr_safe() is essentially a wrapper of xen_do_read_msr(),
>>> the latter should be responsible for initializing the value of *err to 0.
>>> Thus initialize *err to 0 in xen_do_read_msr().
>>>
>>> Fixes: 502ad6e5a619 ("x86/msr: Change the function type of
>>> native_read_msr_safe()")
>>> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
>>> Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
>>> Signed-off-by: Xin Li (Intel) <xin@zytor.com>
>>> ---
>>> arch/x86/xen/enlighten_pv.c | 5 ++++-
>>> 1 file changed, 4 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
>>> index 3be38350f044..01f1d441347e 100644
>>> --- a/arch/x86/xen/enlighten_pv.c
>>> +++ b/arch/x86/xen/enlighten_pv.c
>>> @@ -1091,6 +1091,9 @@ static u64 xen_do_read_msr(u32 msr, int *err)
>>> {
>>> u64 val = 0; /* Avoid uninitialized value for safe variant. */
>>> + if (err)
>>> + *err = 0;
>>> +
>>> if (pmu_msr_chk_emulated(msr, &val, true))
>>> return val;
>>> @@ -1162,7 +1165,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)
>>> static int xen_read_msr_safe(u32 msr, u64 *val)
>>> {
>>> - int err;
>>> + int err = 0;
>>> *val = xen_do_read_msr(msr, &err);
>>> return err;
>>
>> So why not initialize 'err' with 0 in both callers, xen_read_msr_safe()
>> and xen_read_msr(), and avoid all the initialization trouble in
>> xen_do_read_msr()?
>
> Yeah, I should make the change in xen_read_msr() too.
>
> However xen_do_read_msr() should be implemented in a defensive way to
> set *err properly as it's part of its return value. Actually it was so,
> but one of my previous cleanup patch removed it because err is no longer
> passed to pmu_msr_chk_emulated().
xen_do_read_msr() is usable only in enlighten_pv.c as it is static.
So I'd prefer to drop setting err to 0 in xen_do_read_msr() initially
and to set err to 0 in all callers.
Juergen
On 5/16/2025 6:42 AM, Jürgen Groß wrote:
> On 15.05.25 20:11, Xin Li wrote:
>> On 5/15/2025 8:29 AM, Ingo Molnar wrote:
>>>
>>> * Xin Li (Intel) <xin@zytor.com> wrote:
>>>
>>>> xen_read_msr_safe() currently passes an uninitialized argument err to
>>>> xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
>>>> xen_read_msr_safe() could return err with an unpredictable value.
>>>>
>>>> To ensure correctness, initialize err to 0 (representing success)
>>>> in xen_read_msr_safe().
>>>>
>>>> Because xen_read_msr_safe() is essentially a wrapper of
>>>> xen_do_read_msr(),
>>>> the latter should be responsible for initializing the value of *err
>>>> to 0.
>>>> Thus initialize *err to 0 in xen_do_read_msr().
>>>>
>>>> Fixes: 502ad6e5a619 ("x86/msr: Change the function type of
>>>> native_read_msr_safe()")
>>>> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
>>>> Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-
>>>> MhtBSZG@stanley.mountain/
>>>> Signed-off-by: Xin Li (Intel) <xin@zytor.com>
>>>> ---
>>>> arch/x86/xen/enlighten_pv.c | 5 ++++-
>>>> 1 file changed, 4 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
>>>> index 3be38350f044..01f1d441347e 100644
>>>> --- a/arch/x86/xen/enlighten_pv.c
>>>> +++ b/arch/x86/xen/enlighten_pv.c
>>>> @@ -1091,6 +1091,9 @@ static u64 xen_do_read_msr(u32 msr, int *err)
>>>> {
>>>> u64 val = 0; /* Avoid uninitialized value for safe variant. */
>>>> + if (err)
>>>> + *err = 0;
>>>> +
>>>> if (pmu_msr_chk_emulated(msr, &val, true))
>>>> return val;
>>>> @@ -1162,7 +1165,7 @@ static void xen_do_write_msr(u32 msr, u64 val,
>>>> int *err)
>>>> static int xen_read_msr_safe(u32 msr, u64 *val)
>>>> {
>>>> - int err;
>>>> + int err = 0;
>>>> *val = xen_do_read_msr(msr, &err);
>>>> return err;
>>>
>>> So why not initialize 'err' with 0 in both callers, xen_read_msr_safe()
>>> and xen_read_msr(), and avoid all the initialization trouble in
>>> xen_do_read_msr()?
>>
>> Yeah, I should make the change in xen_read_msr() too.
>>
>> However xen_do_read_msr() should be implemented in a defensive way to
>> set *err properly as it's part of its return value. Actually it was so,
>> but one of my previous cleanup patch removed it because err is no longer
>> passed to pmu_msr_chk_emulated().
>
> xen_do_read_msr() is usable only in enlighten_pv.c as it is static.
>
> So I'd prefer to drop setting err to 0 in xen_do_read_msr() initially
> and to set err to 0 in all callers.
Okay, I will send v1A to address this comment then.
Thanks!
Xin
> Juergen
xen_read_msr_safe() currently passes an uninitialized argument err to
xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
xen_read_msr_safe() could return err with an unpredictable value.
To ensure correctness, initialize err to 0 (representing success)
in xen_read_msr_safe().
Do the same in xen_read_msr(), even err is not used after being passed
to xen_do_read_msr().
Fixes: d815da84fdd0 ("x86/msr: Change the function type of native_read_msr_safe()"
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
Signed-off-by: Xin Li (Intel) <xin@zytor.com>
---
Change in v1A:
*) Drop setting err to 0 in xen_do_read_msr() initially and set err to
0 in all callers (Jürgen Groß).
---
arch/x86/xen/enlighten_pv.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
index 7f9ded1bc707..26bbaf4b7330 100644
--- a/arch/x86/xen/enlighten_pv.c
+++ b/arch/x86/xen/enlighten_pv.c
@@ -1162,7 +1162,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)
static int xen_read_msr_safe(u32 msr, u64 *val)
{
- int err;
+ int err = 0;
*val = xen_do_read_msr(msr, &err);
return err;
@@ -1179,7 +1179,7 @@ static int xen_write_msr_safe(u32 msr, u64 val)
static u64 xen_read_msr(u32 msr)
{
- int err;
+ int err = 0;
return xen_do_read_msr(msr, xen_msr_safe ? &err : NULL);
}
--
2.49.0
On 17.05.25 18:57, Xin Li (Intel) wrote:
> xen_read_msr_safe() currently passes an uninitialized argument err to
> xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
> xen_read_msr_safe() could return err with an unpredictable value.
>
> To ensure correctness, initialize err to 0 (representing success)
> in xen_read_msr_safe().
>
> Do the same in xen_read_msr(), even err is not used after being passed
> to xen_do_read_msr().
>
> Fixes: d815da84fdd0 ("x86/msr: Change the function type of native_read_msr_safe()"
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
> Signed-off-by: Xin Li (Intel) <xin@zytor.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Juergen
The following commit has been merged into the x86/core branch of tip:
Commit-ID: e95534e107d2e9e136aa4d7cbededb3827e80074
Gitweb: https://git.kernel.org/tip/e95534e107d2e9e136aa4d7cbededb3827e80074
Author: Xin Li (Intel) <xin@zytor.com>
AuthorDate: Sat, 17 May 2025 09:57:12 -07:00
Committer: Ingo Molnar <mingo@kernel.org>
CommitterDate: Wed, 21 May 2025 08:44:31 +02:00
x86/xen/msr: Fix uninitialized variable 'err'
xen_read_msr_safe() currently passes an uninitialized argument 'err' to
xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
xen_read_msr_safe() could return err with an unpredictable value.
To ensure correctness, initialize err to 0 (representing success)
in xen_read_msr_safe().
Do the same in xen_read_msr(), even err is not used after being passed
to xen_do_read_msr().
Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Xin Li (Intel) <xin@zytor.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Juergen Gross <jgross@suse.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Link: https://lore.kernel.org/r/20250517165713.935384-1-xin@zytor.com
---
arch/x86/xen/enlighten_pv.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
index 7f9ded1..26bbaf4 100644
--- a/arch/x86/xen/enlighten_pv.c
+++ b/arch/x86/xen/enlighten_pv.c
@@ -1162,7 +1162,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)
static int xen_read_msr_safe(u32 msr, u64 *val)
{
- int err;
+ int err = 0;
*val = xen_do_read_msr(msr, &err);
return err;
@@ -1179,7 +1179,7 @@ static int xen_write_msr_safe(u32 msr, u64 val)
static u64 xen_read_msr(u32 msr)
{
- int err;
+ int err = 0;
return xen_do_read_msr(msr, xen_msr_safe ? &err : NULL);
}
The following commit has been merged into the x86/core branch of tip:
Commit-ID: 54c2c688cd9305bdbab4883b9da6ff63f4deca5d
Gitweb: https://git.kernel.org/tip/54c2c688cd9305bdbab4883b9da6ff63f4deca5d
Author: Xin Li (Intel) <xin@zytor.com>
AuthorDate: Sat, 17 May 2025 09:57:12 -07:00
Committer: Ingo Molnar <mingo@kernel.org>
CommitterDate: Sun, 18 May 2025 08:39:16 +02:00
x86/xen/msr: Fix uninitialized variable 'err'
xen_read_msr_safe() currently passes an uninitialized argument 'err' to
xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
xen_read_msr_safe() could return err with an unpredictable value.
To ensure correctness, initialize err to 0 (representing success)
in xen_read_msr_safe().
Do the same in xen_read_msr(), even err is not used after being passed
to xen_do_read_msr().
Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
Fixes: d815da84fdd0 ("x86/msr: Change the function type of native_read_msr_safe()"
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Xin Li (Intel) <xin@zytor.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Juergen Gross <jgross@suse.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Link: https://lore.kernel.org/r/20250517165713.935384-1-xin@zytor.com
---
arch/x86/xen/enlighten_pv.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
index 7f9ded1..26bbaf4 100644
--- a/arch/x86/xen/enlighten_pv.c
+++ b/arch/x86/xen/enlighten_pv.c
@@ -1162,7 +1162,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)
static int xen_read_msr_safe(u32 msr, u64 *val)
{
- int err;
+ int err = 0;
*val = xen_do_read_msr(msr, &err);
return err;
@@ -1179,7 +1179,7 @@ static int xen_write_msr_safe(u32 msr, u64 val)
static u64 xen_read_msr(u32 msr)
{
- int err;
+ int err = 0;
return xen_do_read_msr(msr, xen_msr_safe ? &err : NULL);
}
* Xin Li <xin@zytor.com> wrote:
> On 5/15/2025 8:29 AM, Ingo Molnar wrote:
> >
> > * Xin Li (Intel) <xin@zytor.com> wrote:
> >
> > > xen_read_msr_safe() currently passes an uninitialized argument err to
> > > xen_do_read_msr(). But as xen_do_read_msr() may not set the argument,
> > > xen_read_msr_safe() could return err with an unpredictable value.
> > >
> > > To ensure correctness, initialize err to 0 (representing success)
> > > in xen_read_msr_safe().
> > >
> > > Because xen_read_msr_safe() is essentially a wrapper of xen_do_read_msr(),
> > > the latter should be responsible for initializing the value of *err to 0.
> > > Thus initialize *err to 0 in xen_do_read_msr().
> > >
> > > Fixes: 502ad6e5a619 ("x86/msr: Change the function type of native_read_msr_safe()")
> > > Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > > Closes: https://lore.kernel.org/xen-devel/aBxNI_Q0-MhtBSZG@stanley.mountain/
> > > Signed-off-by: Xin Li (Intel) <xin@zytor.com>
> > > ---
> > > arch/x86/xen/enlighten_pv.c | 5 ++++-
> > > 1 file changed, 4 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
> > > index 3be38350f044..01f1d441347e 100644
> > > --- a/arch/x86/xen/enlighten_pv.c
> > > +++ b/arch/x86/xen/enlighten_pv.c
> > > @@ -1091,6 +1091,9 @@ static u64 xen_do_read_msr(u32 msr, int *err)
> > > {
> > > u64 val = 0; /* Avoid uninitialized value for safe variant. */
> > > + if (err)
> > > + *err = 0;
> > > +
> > > if (pmu_msr_chk_emulated(msr, &val, true))
> > > return val;
> > > @@ -1162,7 +1165,7 @@ static void xen_do_write_msr(u32 msr, u64 val, int *err)
> > > static int xen_read_msr_safe(u32 msr, u64 *val)
> > > {
> > > - int err;
> > > + int err = 0;
> > > *val = xen_do_read_msr(msr, &err);
> > > return err;
> >
> > So why not initialize 'err' with 0 in both callers, xen_read_msr_safe()
> > and xen_read_msr(), and avoid all the initialization trouble in
> > xen_do_read_msr()?
>
> Yeah, I should make the change in xen_read_msr() too.
>
> However xen_do_read_msr() should be implemented in a defensive way to
> set *err properly as it's part of its return value. Actually it was so,
> but one of my previous cleanup patch removed it because err is no longer
> passed to pmu_msr_chk_emulated().
Maybe. It's up to Juergen though.
Thanks,
Ingo
© 2016 - 2025 Red Hat, Inc.