mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: erny hombre <hombre67@gmx.at>
Cc: musl@lists.openwall.com
Subject: Re: [musl] possible bug in syslog
Date: Thu, 13 Jun 2024 09:46:07 -0400	[thread overview]
Message-ID: <20240613134607.GW10433@brightrain.aerifal.cx> (raw)
In-Reply-To: <trinity-b95d35ca-723c-4b6a-bd79-1edbb37dea1f-1718279080124@3c-app-gmx-bap17>

On Thu, Jun 13, 2024 at 01:44:40PM +0200, erny hombre wrote:
> Hello,
> 
> I think there is a bug in syslog:
> 
> This assert fails (in gcc the test is ok):
> 	#if (LOG_MAKEPRI(LOG_DAEMON, LOG_WARNING) != (LOG_DAEMON|LOG_WARNING))
> 	#error "LOG_MAKEPRI"
> 	#endif

This assertion itself is not valid; the whole reason a LOG_MAKEPRI
macro exists is that it's not guaranteed that the packing be simple
or. But the functionality it goes with is intended  to work.

> This code should produce a log message, but actually it does not:
> 	setlogmask(LOG_UPTO(LOG_NOTICE));
> 	syslog(LOG_MAKEPRI(LOG_LOCAL7, LOG_NOTICE), "LOG_MAKEPRI(LOG_LOCAL7, LOG_NOTICE)");
> This works:
> 	syslog(LOG_MAKEPRI(LOG_DAEMON, LOG_NOTICE), "LOG_MAKEPRI(LOG_DAEMON, LOG_NOTICE)");
> 
> ---
> 
> Maybe these macros in syslog.h are wrong:
> 	#define	LOG_MAKEPRI(f, p) (((f)<<3)|(p))
> 	#define LOG_FACMASK 0x3f8
> 	#define LOG_FAC(p) (((p)&LOG_FACMASK)>>3)
> 
> correct version:
> 	#define	LOG_MAKEPRI(f, p) (((f))|(p))
> 	#define LOG_FACMASK 0xf8
> 	#define LOG_FAC(p) (((p)&LOG_FACMASK))
> 
> Also a line in syslog.c, __vsyslog() should be changed
> from:
> 	if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
> to:
> 	if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0xff)) return;
> 
> ----
> This is my testprogram to reproduce the error:
> 
> #include <syslog.h>
> 
> int main(void)
> {
> #if 1 // test ok
> #if (LOG_FAC(LOG_MAKEPRI(LOG_DAEMON, LOG_WARNING)) != LOG_DAEMON)
> #error "LOG_FAC"
> #endif
> 
> #if 0 // test fails
> #if (LOG_MAKEPRI(LOG_DAEMON, LOG_WARNING) != (LOG_DAEMON|LOG_WARNING))
> #error "LOG_MAKEPRI"
> #endif
> #endif
> 
> 	setlogmask(LOG_UPTO(LOG_NOTICE));
> 
> 	openlog("exampleprog", LOG_CONS | LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_LOCAL1);
> 
> 	syslog(LOG_DAEMON | LOG_NOTICE, "LOG_DAEMON | LOG_NOTICE");
> 	syslog(LOG_LOCAL7 | LOG_NOTICE, "LOG_LOCAL7 | LOG_NOTICE");
> 	syslog(LOG_MAKEPRI(LOG_DAEMON, LOG_NOTICE), "LOG_MAKEPRI(LOG_DAEMON, LOG_NOTICE)");
> 	// the following call does not write a log message:
> 	syslog(LOG_MAKEPRI(LOG_LOCAL7, LOG_NOTICE), "LOG_MAKEPRI(LOG_LOCAL7, LOG_NOTICE)");
> 
> 	closelog();
> 
> 	return 0;
> }
> ----

If I follow correctly, the bug here is that the nonstandard
functionality of passing a facility to syslog() is not working because
the facility bits are shifted into a mismatching position from where
they're checked when packing them with priority.

As for how to fix it, I assume your patch works, but it doesn't do
anything for existing binaries built with the existing LOG_MAKEPRI
macro, which will silently use the wrong facility.

Another option would be to leave the macro definitions alone (i.e.
preserve existing ABI) and make syslog accept the packing we
inadvertently defined with the 3 empty bits between priority and
facility.

I'm not sure what we should do since this functionality has never
worked right, and since it's fairly obscure and nonstandard. Let's
wait a bit and see if anyone else has opinions on which way to fix it.

Thanks for reporting.

Rich

  reply	other threads:[~2024-06-13 13:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-13 11:44 erny hombre
2024-06-13 13:46 ` Rich Felker [this message]
2024-06-13 15:20   ` Thorsten Glaser
2024-06-13 15:37     ` Rich Felker
2024-06-13 15:44       ` Thorsten Glaser

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=20240613134607.GW10433@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=hombre67@gmx.at \
    --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).