From nobody@FreeBSD.org  Mon Jul  8 17:45:48 2002
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 57D5137B400
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  8 Jul 2002 17:45:48 -0700 (PDT)
Received: from www.freebsd.org (www.FreeBSD.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 1AFA143E52
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  8 Jul 2002 17:45:48 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.4/8.12.4) with ESMTP id g690jlOT030264
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 8 Jul 2002 17:45:47 -0700 (PDT)
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.4/8.12.4/Submit) id g690jlNR030263;
	Mon, 8 Jul 2002 17:45:47 -0700 (PDT)
Message-Id: <200207090045.g690jlNR030263@www.freebsd.org>
Date: Mon, 8 Jul 2002 17:45:47 -0700 (PDT)
From: David Dunham <dwdunham@isilon.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Syslogd crashes in function markit
X-Send-Pr-Version: www-1.0

>Number:         40363
>Category:       misc
>Synopsis:       Syslogd crashes in function markit
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    des
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jul 08 17:50:01 PDT 2002
>Closed-Date:    Thu Oct 24 09:41:53 PDT 2002
>Last-Modified:  Thu Oct 24 09:41:53 PDT 2002
>Originator:     David Dunham
>Release:        FreeBSD 5.0
>Organization:
Isilon Systems
>Environment:
FreeBSD dwd_1 5.0-20010301-CURRENT i386

>Description:
Syslogd crashes when it tries to access memory it has just freed.
In the function markit q is freed and then TAILQ_NEXT is called using q.
>How-To-Repeat:
This is extremely rare the only way I was able to get it to happen consistently was to change syslogd as follows:

--- syslogd.c.orig	Mon Jul  8 17:26:21 2002
+++ syslogd.c	Mon Jul  8 17:24:16 2002
@@ -1084,9 +1084,9 @@
 				break;
 			}
 		}
-		if (writev(f->f_file, iov, 7) < 0) {
+		if (writev(f->f_file, iov, 7) < 0 || 1) {
 			int e = errno;
-			(void)close(f->f_file);
+		/*	(void)close(f->f_file);*/
 			if (f->f_un.f_pipe.f_pid > 0)
 				deadq_enter(f->f_un.f_pipe.f_pid,
 					    f->f_un.f_pipe.f_pname);


You also should probably run electric fence or some other memory debugger.
>Fix:
Get the next on the queue from the iterator before freeing it.

--- syslogd.c.orig	Mon Jul  8 17:26:21 2002
+++ syslogd.c	Mon Jul  8 17:24:16 2002
@@ -1775,7 +1775,7 @@
 markit(void)
 {
 	struct filed *f;
-	dq_t q;
+	dq_t q, next;
 
 	now = time((time_t *)NULL);
 	MarkSeq += TIMERINTVL;
@@ -1796,7 +1796,9 @@
 	}
 
 	/* Walk the dead queue, and see if we should signal somebody. */
-	TAILQ_FOREACH(q, &deadq_head, dq_entries) {
+	for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = next) {
+		next = TAILQ_NEXT(q, dq_entries);
+
 		switch (q->dq_timeout) {
 		case 0:
 			/* Already signalled once, try harder now. */

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: schweikh 
Responsible-Changed-When: Sat Aug 17 00:51:10 PDT 2002 
Responsible-Changed-Why:  
Dag-Erling, it seems you have modified the TAILQ_FOREACH line in the 
markit function in rev 1.101. Can you have a look if this introduced the 
bug? 

http://www.freebsd.org/cgi/query-pr.cgi?pr=40363 
State-Changed-From-To: open->closed 
State-Changed-By: des 
State-Changed-When: Thu Oct 24 09:41:48 PDT 2002 
State-Changed-Why:  
Fixed, thanks. 

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