mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: Maks Mishin <maks.mishinfz@gmail.com>
Cc: musl@lists.openwall.com
Subject: [musl] Re: [PATCH] syslog: Check result of connect function
Date: Thu, 21 Mar 2024 18:34:34 -0400	[thread overview]
Message-ID: <20240321223433.GK15722@brightrain.aerifal.cx> (raw)
In-Reply-To: <20240321213109.10701-1-maks.mishinFZ@gmail.com>

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@gmail.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

           reply	other threads:[~2024-03-21 22:34 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <20240321213109.10701-1-maks.mishinFZ@gmail.com>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240321223433.GK15722@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --cc=maks.mishinfz@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).