php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48346 gettimeofday raise SEGFAULT
Submitted: 2009-05-20 16:11 UTC Modified: 2009-05-21 15:35 UTC
From: adriano at guiadohardware dot net Assigned:
Status: Closed Package: Date/time related
PHP Version: 5.2.9 OS: AMD64 Gentoo Hardened, glibc 2.3
Private report: No CVE-ID: None
 [2009-05-20 16:11 UTC] adriano at guiadohardware dot net
Description:
------------
Hi,

On my server the second argument on gettimeofday raise SEGFAULT. I can't reproduce this error using another system lib version of another server. As manpage of gettimeofday said: "If tzp is not a null pointer, the behavior is unspecified. " (http://linux.die.net/man/3/gettimeofday). This old timezone is no longer used and must be kept outside of kernel. After the patch below this worked.


--- php-5.2.9/ext/standard/microtime.c	2009-05-20 12:07:39.000000000 -0300
+++ php-5.2.9/ext/standard/microtime.c	2009-05-20 12:08:33.000000000 -0300
@@ -54,13 +54,12 @@
 {
 	zend_bool get_as_float = 0;
 	struct timeval tp = {0};
-	struct timezone tz = {0};
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &get_as_float) == FAILURE) {
 		return;
 	}
 
-	if (gettimeofday(&tp, &tz)) {
+	if (gettimeofday(&tp, NULL)) {
 		RETURN_FALSE;
 	}
 


Reproduce code:
---------------
crash.php
<?
print_r(gettimeofday());
?>
gettimeofday.c
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
  char buffer[30]; struct timeval tv = {0}; struct timezone tz = {0}; time_t curtime;
  gettimeofday(&tv, &tz); 
  curtime=tv.tv_sec;
  strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));
  printf("%s%ld\n",buffer,tv.tv_usec);
  return 0;
}

Expected result:
----------------
~ # gcc -Wall -O0 gettimeofday.c -o gettimeofday
~ # ./gettimeofday 
Segmentation fault
~ # ./crash.php
Segmentation fault



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-20 21:57 UTC] jani@php.net
You really need to update your server. In modern systems I can't 
reproduce either crash.
 [2009-05-20 22:44 UTC] adriano at guiadohardware dot net
This is very strange, because the system IS updated on stable branch of Gentoo. Very stable too, never crashed any common daemon or kernel. Lastest monster hardware server (16 cores, 16gb RAM, etc).  Can be related with some security features of Gentoo Hardened Kernel on AMD64, Pax or userland SSP, etc.

sys-libs/glibc-2.8_p20080602-r1, sys-devel/gcc-3.4.6-r2, kernel hardened-sources-2.6.28-r7, sys-devel/gcc-3.4.6-r2.

I can reproduce this error on another hardware using same Gentoo configs. But I can't reproduce on my workstation Gentoo Hardened (32bits). Anyway, kept the recommendation of manpages is always a good code rules. I don't see any reason to use this useless struct timezone against the documentation too, waste of bytes.

Thanks for look it.

Regards,
Adriano
 [2009-05-20 22:47 UTC] adriano at guiadohardware dot net
* Note the server use the 64 bit AMD64 stable branch of Gentoo.
 [2009-05-20 22:52 UTC] adriano at guiadohardware dot net
The system has been updated today and rebuild all system twice to ensure that every lib/program was linked against the last available version.
 [2009-05-21 14:22 UTC] lbarnaud@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

(Both linux, posix and solaris man pages say the second argument of gettimeofday() should be set to NULL because it is deprecated / undefined result if not NULL / ignored.)
 [2009-05-21 15:35 UTC] jani@php.net
Thanks Arnaud. I should have read the man page a bit more carefully..
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 22 22:00:03 2025 UTC