Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Date: Thu, 21 Mar 2024 18:34:34 -0400
From: Rich Felker <dalias@...ifal.cx>
To: Maks Mishin <maks.mishinfz@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: [PATCH] syslog: Check result of connect function

On Fri, Mar 22, 2024 at 12:31:09AM +0300, Maks Mishin wrote:
> Return value of of function 'connect', called at syslog.c:55,
> is not checked. The return value possibly contains
> an error code and ignoring it may lead to missing important errors.
> 
> Found by RASU JSC.
> 
> Signed-off-by: Maks Mishin <maks.mishinFZ@...il.com>
> ---
>  src/misc/syslog.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/src/misc/syslog.c b/src/misc/syslog.c
> index 710202f9..72ad6d2c 100644
> --- a/src/misc/syslog.c
> +++ b/src/misc/syslog.c
> @@ -52,7 +52,12 @@ void closelog(void)
>  static void __openlog()
>  {
>  	log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
> -	if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
> +	if (log_fd >= 0) {
> +		int ret = connect(log_fd, (void *)&log_addr, sizeof log_addr);
> +		if (ret > 0) {
> +			errno = ret;
> +		}
> +	}
>  }
>  
>  void openlog(const char *ident, int opt, int facility)
> -- 
> 2.30.2

This patch is wrong but does nothing; connect is not permitted to
return positive values. It returns either 0 on success or -1 on error.

Since openlog cannot report failure to the caller (it returns void),
it's a best-effort operation. In the case where it fails to connect,
it's still desirable and intentional to leave a socket reserved so
that the program can't run out of file descriptors rendering it unable
to produce log output later. The lack of connection will be noticed
later when send() fails and the is_lost_conn predicate on errno
returns true.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.