mailing list of musl libc
 help / color / mirror / code / Atom feed
* Weird bug in syslog
@ 2013-03-19 19:32 William Haddon
  2013-03-20 12:41 ` Szabolcs Nagy
  0 siblings, 1 reply; 5+ messages in thread
From: William Haddon @ 2013-03-19 19:32 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 665 bytes --]


Hi all.

I noticed seg-faults and other weird behavior when using the syslog() 
function with large messages. I've attached the simplest test program 
that reproduces the problem. I've observed it to break on 0.9.9 on i386 
and current git on x86_64. The problem seems to be that although the 
syslog function successfully truncates its input to 256 bytes, it 
passes the size of the un-truncated form to the sendto() call because 
snprintf returns the number of bytes that would be written if 
truncation did not occur. Fixing syslog to check if truncation occurred 
seems to fix the problem. I've attached the patch that does this.

William Haddon

[-- Attachment #2: test3.c --]
[-- Type: text/x-csrc, Size: 362 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>

#define A 8
#define B 8

int main(int argc, char **argv)
{
	char *temp;
	char v;
	size_t i, j;

	for (j = A; j < 10000; j += B) {
		temp = malloc(j+1);
		for (i = 0; i < j; i++)
			temp[i] = 'x';
		temp[j] = 0;
		syslog(LOG_ERR, temp);
		free(temp);
	}
	printf("Success\n");
}


[-- Attachment #3: musl-syslog.patch --]
[-- Type: text/x-patch, Size: 549 bytes --]

Report the correct length of the datagram to the kernel to fix strange behavior
in the syslog function.
--- musl-0.9.9/src/misc/syslog.c
+++ src/src/misc/syslog.c
@@ -90,9 +90,11 @@
 		priority, timebuf,
 		log_ident ? log_ident : "",
 		"["+!pid, pid, "]"+!pid);
+	if (l > sizeof buf) l = sizeof buf - 1;
 	l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
 	if (l2 >= 0) {
 		l += l2;
+		if (l > sizeof buf) l = sizeof buf - 1;
 		if (buf[l-1] != '\n') buf[l++] = '\n';
 		sendto(log_fd, buf, l, 0, (void *)&log_addr, 11);
 	}


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-03-21  1:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-19 19:32 Weird bug in syslog William Haddon
2013-03-20 12:41 ` Szabolcs Nagy
2013-03-20 18:55   ` Szabolcs Nagy
2013-03-20 19:02   ` Rich Felker
2013-03-21  1:43   ` William Haddon

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).