From fanf@dotat.at  Thu Mar 15 11:10:55 2001
Return-Path: <fanf@dotat.at>
Received: from hand.dotat.at (sfo-gw.covalent.net [207.44.198.62])
	by hub.freebsd.org (Postfix) with ESMTP id CA32137B71C
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 15 Mar 2001 11:10:54 -0800 (PST)
	(envelope-from fanf@dotat.at)
Received: from fanf by hand.dotat.at with local (Exim 3.20 #3)
	id 14dWNj-0001Qn-00
	for FreeBSD-gnats-submit@freebsd.org; Thu, 15 Mar 2001 11:57:27 +0000
Message-Id: <E14dWNj-0001Qn-00@hand.dotat.at>
Date: Thu, 15 Mar 2001 11:57:27 +0000
From: Tony Finch <dot@dotat.at>
Reply-To: Tony Finch <dot@dotat.at>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: LOG_FAC() is bogus
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         25833
>Category:       bin
>Synopsis:       LOG_FAC() is bogus
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Mar 15 11:20:01 PST 2001
>Closed-Date:    Thu Jan 17 08:12:05 PST 2002
>Last-Modified:  Thu Jan 17 08:42:03 PST 2002
>Originator:     Tony Finch <dot@dotat.at>
>Release:        FreeBSD 4.3-BETA i386
>Organization:
Covalent Technologies, Inc.
>Environment:

System: FreeBSD hand.dotat.at 4.3-BETA FreeBSD 4.3-BETA #6: Thu Mar 15 04:57:36 GMT 2001 fanf@hand.dotat.at:/FreeBSD/releng4/sys/compile/DELL-Latitude-CSx i386

>Description:

There was recently a problem with syslogd looping messages that were
printed to the console, because of the following code:
	if (LOG_FAC(pri) == LOG_CONSOLE)
		/* blah */;
the problem being that the value of the LOG_FAC() macro cannot be
compared with a facility value, which is bogus.

There are still a couple of harmless instances of this brokenness
(in syslogd.c and dmesg.c) where the facility in question is
LOG_KERN, which is fortunately zero.

>How-To-Repeat:
>Fix:

This patch sanitizes LOG_FAC(), and adds a LOG_FACSHIFT macro to
replace the lost semantics. I have also replaced uses of LOG_FACMASK
that are now more correctly done with LOG_FAC() (where before that
would have been broken).

An alternative fix would be to replace the code in syslogd.c and
dmesg.c that bogusly compares the value of LOG_FAC() with LOG_KERN.

There's also some code in src/contrib/ipfilter which uses
LOG_PRIMASK and LOG_FACMASK instead of LOG_PRI() and LOG_FAC()
but I haven't touched it since it is contrib code.

Index: sys/sys/syslog.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/syslog.h,v
retrieving revision 1.19.2.1
diff -u -r1.19.2.1 syslog.h
--- sys/sys/syslog.h	2001/01/09 06:51:34	1.19.2.1
+++ sys/sys/syslog.h	2001/03/15 11:54:45
@@ -121,8 +121,9 @@
 
 #define	LOG_NFACILITIES	24	/* current number of facilities */
 #define	LOG_FACMASK	0x03f8	/* mask to extract facility part */
+#define	LOG_FACSHIFT	3	/* number of bits below the facility */
 				/* facility of pri */
-#define	LOG_FAC(p)	(((p) & LOG_FACMASK) >> 3)
+#define	LOG_FAC(p)	((p) & LOG_FACMASK)
 
 #ifdef SYSLOG_NAMES
 CODE facilitynames[] = {
Index: usr.sbin/syslogd/syslogd.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/syslogd/syslogd.c,v
retrieving revision 1.59.2.6
diff -u -r1.59.2.6 syslogd.c
--- usr.sbin/syslogd/syslogd.c	2001/02/18 16:22:59	1.59.2.6
+++ usr.sbin/syslogd/syslogd.c	2001/03/15 11:45:18
@@ -710,7 +710,7 @@
 			pri = 10 * pri + (*p - '0');
 		if (*p == '>')
 			++p;
-		if ((pri & LOG_FACMASK) == LOG_CONSOLE)
+		if (LOG_FAC(pri) == LOG_CONSOLE)
 			flags |= IGN_CONS;
 	} else {
 		/* kernel printf's come out on console */
@@ -771,7 +771,7 @@
 	if (flags & MARK)
 		fac = LOG_NFACILITIES;
 	else
-		fac = LOG_FAC(pri);
+		fac = LOG_FAC(pri) >> LOG_FACSHIFT;
 	prilev = LOG_PRI(pri);
 
 	/* extract program name */
@@ -913,7 +913,7 @@
 
 	if (LogFacPri) {
 	  	static char fp_buf[30];	/* Hollow laugh */
-		int fac = f->f_prevpri & LOG_FACMASK;
+		int fac = LOG_FAC(f->f_prevpri);
 		int pri = LOG_PRI(f->f_prevpri);
 		char *f_s = 0;
 		char f_n[5];	/* Hollow laugh */
@@ -937,7 +937,7 @@
 		  }
 		}
 		if (!f_s) {
-		  snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac));
+		  snprintf(f_n, sizeof f_n, "%d", fac >> LOG_FACSHIFT);
 		  f_s = f_n;
 		}
 		if (!p_s) {
Index: lib/libc/gen/syslog.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/syslog.c,v
retrieving revision 1.21
diff -u -r1.21 syslog.c
--- lib/libc/gen/syslog.c	2000/01/27 23:06:20	1.21
+++ lib/libc/gen/syslog.c	2001/03/15 11:36:26
@@ -161,7 +161,7 @@
 	saved_errno = errno;
 
 	/* Set default facility if none specified. */
-	if ((pri & LOG_FACMASK) == 0)
+	if (LOG_FAC(pri) == 0)
 		pri |= LogFacility;
 
 	/* Create the primary stdio hook */
>Release-Note:
>Audit-Trail:

From: Ruslan Ermilov <ru@FreeBSD.org>
To: Tony Finch <dot@dotat.at>
Cc: bug-followup@FreeBSD.org
Subject: Re: bin/25833: LOG_FAC() is bogus
Date: Thu, 15 Mar 2001 21:48:01 +0200

 On Thu, Mar 15, 2001 at 11:57:27AM +0000, Tony Finch wrote:
 > 
 > There was recently a problem with syslogd looping messages that were
 > printed to the console, because of the following code:
 > 	if (LOG_FAC(pri) == LOG_CONSOLE)
 > 		/* blah */;
 > 
 But you can write ``LOG_FAC(pri) == LOG_FAC(LOG_CONSOLE)'' without
 breaking backwards compatibility.
 
 
 Cheers,
 -- 
 Ruslan Ermilov		Oracle Developer/DBA,
 ru@sunbay.com		Sunbay Software AG,
 ru@FreeBSD.org		FreeBSD committer,
 +380.652.512.251	Simferopol, Ukraine
 
 http://www.FreeBSD.org	The Power To Serve
 http://www.oracle.com	Enabling The Information Age
State-Changed-From-To: open->feedback 
State-Changed-By: phk 
State-Changed-When: Wed Mar 28 10:16:06 PST 2001 
State-Changed-Why:  
I am weary at changing the sematics of LOC_FAC(), I'm afraid 
it will break too much existing code. 

Ruslan's suggestion seems to make sense to me, could you 
provide us with a patch based on it ? 


http://www.freebsd.org/cgi/query-pr.cgi?pr=25833 
State-Changed-From-To: feedback->closed 
State-Changed-By: sheldonh 
State-Changed-When: Thu Jan 17 08:12:05 PST 2002 
State-Changed-Why:  
Automatic feedback timeout.  If additional feedback that warrants 
the re-opening of this PR is available but not included in the 
audit trail, please include the feedback in a reply to this message 
(preserving the Subject line) and ask that the PR be re-opened. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=25833 
>Unformatted:
