All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tools/xenstored: Don't assume errno will not be overwritten in lu_arch()
@ 2021-07-29 11:06 Julien Grall
  2021-07-29 15:23 ` Julien Grall
  0 siblings, 1 reply; 4+ messages in thread
From: Julien Grall @ 2021-07-29 11:06 UTC (permalink / raw)
  To: xen-devel; +Cc: julien, Julien Grall, Ian Jackson, Wei Liu, Juergen Gross

From: Julien Grall <jgrall@amazon.com>

At the moment, do_control_lu() will set errno to 0 before calling
lu_arch() and then check errno. The expectation is nothing in lu_arch()
will change the value unless there is an error.

However, per errno(3), a function that succeeds is allowed to change
errno. In fact, syslog() will overwrite errno if the logs are rotated
at the time it is called.

To prevent any further issue, errno is now always set before
returning NULL.

Additionally, errno is only checked when returning NULL so the client
can see the error message if there is any.

Reported-by: Michael Kurth <mku@amazon.com>
Signed-off-by: Julien Grall <jgrall@amazon.com>
---
 tools/xenstore/xenstored_control.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/xenstore/xenstored_control.c b/tools/xenstore/xenstored_control.c
index 6b68b79faac7..6fcb42095b59 100644
--- a/tools/xenstore/xenstored_control.c
+++ b/tools/xenstore/xenstored_control.c
@@ -324,6 +324,7 @@ static const char *lu_binary_alloc(const void *ctx, struct connection *conn,
 	lu_status->kernel_size = size;
 	lu_status->kernel_off = 0;
 
+	errno = 0;
 	return NULL;
 }
 
@@ -339,6 +340,7 @@ static const char *lu_binary_save(const void *ctx, struct connection *conn,
 	memcpy(lu_status->kernel + lu_status->kernel_off, data, size);
 	lu_status->kernel_off += size;
 
+	errno = 0;
 	return NULL;
 }
 
@@ -798,9 +800,8 @@ static int do_control_lu(void *ctx, struct connection *conn,
 		if (!ret)
 			return errno;
 	} else {
-		errno = 0;
 		ret = lu_arch(ctx, conn, vec, num);
-		if (errno)
+		if (!ret && errno)
 			return errno;
 	}
 
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools/xenstored: Don't assume errno will not be overwritten in lu_arch()
  2021-07-29 11:06 [PATCH] tools/xenstored: Don't assume errno will not be overwritten in lu_arch() Julien Grall
@ 2021-07-29 15:23 ` Julien Grall
  2021-07-30  8:40   ` Juergen Gross
  0 siblings, 1 reply; 4+ messages in thread
From: Julien Grall @ 2021-07-29 15:23 UTC (permalink / raw)
  To: xen-devel; +Cc: Julien Grall, Ian Jackson, Wei Liu, Juergen Gross



On 29/07/2021 12:06, Julien Grall wrote:
> From: Julien Grall <jgrall@amazon.com>
> 
> At the moment, do_control_lu() will set errno to 0 before calling
> lu_arch() and then check errno. The expectation is nothing in lu_arch()
> will change the value unless there is an error.
> 
> However, per errno(3), a function that succeeds is allowed to change
> errno. In fact, syslog() will overwrite errno if the logs are rotated
> at the time it is called.
> 
> To prevent any further issue, errno is now always set before
> returning NULL.
> 
> Additionally, errno is only checked when returning NULL so the client
> can see the error message if there is any.
> 
> Reported-by: Michael Kurth <mku@amazon.com>
> Signed-off-by: Julien Grall <jgrall@amazon.com>
> ---
>   tools/xenstore/xenstored_control.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/xenstore/xenstored_control.c b/tools/xenstore/xenstored_control.c
> index 6b68b79faac7..6fcb42095b59 100644
> --- a/tools/xenstore/xenstored_control.c
> +++ b/tools/xenstore/xenstored_control.c
> @@ -324,6 +324,7 @@ static const char *lu_binary_alloc(const void *ctx, struct connection *conn,
>   	lu_status->kernel_size = size;
>   	lu_status->kernel_off = 0;
>   
> +	errno = 0;
>   	return NULL;
>   }
>   
> @@ -339,6 +340,7 @@ static const char *lu_binary_save(const void *ctx, struct connection *conn,
>   	memcpy(lu_status->kernel + lu_status->kernel_off, data, size);
>   	lu_status->kernel_off += size;
>   
> +	errno = 0;
>   	return NULL;
>   }
>   

I forgot to update lu_binary(). I will respin the patch once I get some 
feedback.

Cheers,

> @@ -798,9 +800,8 @@ static int do_control_lu(void *ctx, struct connection *conn,
>   		if (!ret)
>   			return errno;
>   	} else {
> -		errno = 0;
>   		ret = lu_arch(ctx, conn, vec, num);
> -		if (errno)
> +		if (!ret && errno)
>   			return errno;
>   	}
>   
> 

-- 
Julien Grall


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools/xenstored: Don't assume errno will not be overwritten in lu_arch()
  2021-07-29 15:23 ` Julien Grall
@ 2021-07-30  8:40   ` Juergen Gross
  2021-07-30 15:13     ` Julien Grall
  0 siblings, 1 reply; 4+ messages in thread
From: Juergen Gross @ 2021-07-30  8:40 UTC (permalink / raw)
  To: Julien Grall, xen-devel; +Cc: Julien Grall, Ian Jackson, Wei Liu


[-- Attachment #1.1.1: Type: text/plain, Size: 1958 bytes --]

On 29.07.21 17:23, Julien Grall wrote:
> 
> 
> On 29/07/2021 12:06, Julien Grall wrote:
>> From: Julien Grall <jgrall@amazon.com>
>>
>> At the moment, do_control_lu() will set errno to 0 before calling
>> lu_arch() and then check errno. The expectation is nothing in lu_arch()
>> will change the value unless there is an error.
>>
>> However, per errno(3), a function that succeeds is allowed to change
>> errno. In fact, syslog() will overwrite errno if the logs are rotated
>> at the time it is called.
>>
>> To prevent any further issue, errno is now always set before
>> returning NULL.
>>
>> Additionally, errno is only checked when returning NULL so the client
>> can see the error message if there is any.
>>
>> Reported-by: Michael Kurth <mku@amazon.com>
>> Signed-off-by: Julien Grall <jgrall@amazon.com>
>> ---
>>   tools/xenstore/xenstored_control.c | 5 +++--
>>   1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/xenstore/xenstored_control.c 
>> b/tools/xenstore/xenstored_control.c
>> index 6b68b79faac7..6fcb42095b59 100644
>> --- a/tools/xenstore/xenstored_control.c
>> +++ b/tools/xenstore/xenstored_control.c
>> @@ -324,6 +324,7 @@ static const char *lu_binary_alloc(const void 
>> *ctx, struct connection *conn,
>>       lu_status->kernel_size = size;
>>       lu_status->kernel_off = 0;
>> +    errno = 0;
>>       return NULL;
>>   }
>> @@ -339,6 +340,7 @@ static const char *lu_binary_save(const void *ctx, 
>> struct connection *conn,
>>       memcpy(lu_status->kernel + lu_status->kernel_off, data, size);
>>       lu_status->kernel_off += size;
>> +    errno = 0;
>>       return NULL;
>>   }
> 
> I forgot to update lu_binary(). I will respin the patch once I get some 
> feedback.

With setting errno to 0 before returning NULL in lu_binary() you can add

Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen


[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools/xenstored: Don't assume errno will not be overwritten in lu_arch()
  2021-07-30  8:40   ` Juergen Gross
@ 2021-07-30 15:13     ` Julien Grall
  0 siblings, 0 replies; 4+ messages in thread
From: Julien Grall @ 2021-07-30 15:13 UTC (permalink / raw)
  To: Juergen Gross, xen-devel; +Cc: Julien Grall, Ian Jackson, Wei Liu

Hi Juergen,

On 30/07/2021 09:40, Juergen Gross wrote:
> On 29.07.21 17:23, Julien Grall wrote:
>>
>>
>> On 29/07/2021 12:06, Julien Grall wrote:
>>> From: Julien Grall <jgrall@amazon.com>
>>>
>>> At the moment, do_control_lu() will set errno to 0 before calling
>>> lu_arch() and then check errno. The expectation is nothing in lu_arch()
>>> will change the value unless there is an error.
>>>
>>> However, per errno(3), a function that succeeds is allowed to change
>>> errno. In fact, syslog() will overwrite errno if the logs are rotated
>>> at the time it is called.
>>>
>>> To prevent any further issue, errno is now always set before
>>> returning NULL.
>>>
>>> Additionally, errno is only checked when returning NULL so the client
>>> can see the error message if there is any.
>>>
>>> Reported-by: Michael Kurth <mku@amazon.com>
>>> Signed-off-by: Julien Grall <jgrall@amazon.com>
>>> ---
>>>   tools/xenstore/xenstored_control.c | 5 +++--
>>>   1 file changed, 3 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/tools/xenstore/xenstored_control.c 
>>> b/tools/xenstore/xenstored_control.c
>>> index 6b68b79faac7..6fcb42095b59 100644
>>> --- a/tools/xenstore/xenstored_control.c
>>> +++ b/tools/xenstore/xenstored_control.c
>>> @@ -324,6 +324,7 @@ static const char *lu_binary_alloc(const void 
>>> *ctx, struct connection *conn,
>>>       lu_status->kernel_size = size;
>>>       lu_status->kernel_off = 0;
>>> +    errno = 0;
>>>       return NULL;
>>>   }
>>> @@ -339,6 +340,7 @@ static const char *lu_binary_save(const void 
>>> *ctx, struct connection *conn,
>>>       memcpy(lu_status->kernel + lu_status->kernel_off, data, size);
>>>       lu_status->kernel_off += size;
>>> +    errno = 0;
>>>       return NULL;
>>>   }
>>
>> I forgot to update lu_binary(). I will respin the patch once I get 
>> some feedback.
> 
> With setting errno to 0 before returning NULL in lu_binary() you can add
> 
> Reviewed-by: Juergen Gross <jgross@suse.com>

Thanks! I will commit it.

Cheers,

-- 
Julien Grall


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-30 15:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29 11:06 [PATCH] tools/xenstored: Don't assume errno will not be overwritten in lu_arch() Julien Grall
2021-07-29 15:23 ` Julien Grall
2021-07-30  8:40   ` Juergen Gross
2021-07-30 15:13     ` Julien Grall

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.