x86: gettimeofday() vDSO: fix segfault when tv == NULL
authorJohn Wright <john.wright@hp.com>
Wed, 29 Apr 2009 20:32:01 +0000 (14:32 -0600)
committerIngo Molnar <mingo@elte.hu>
Thu, 30 Apr 2009 10:31:45 +0000 (12:31 +0200)
According to the gettimeofday(2) manual:

       If either tv or tz is NULL, the corresponding structure is not
       set or returned.

Since it is legal to give NULL as the tv argument, the code should make
sure tv is not NULL before trying to dereference it.

This issue manifests itself on x86_64 when vdso=0 is not on the kernel
command-line and libc uses the vDSO for gettimeofday() (e.g. glibc >=
2.7).  A simple reproducer:

  #include <stdio.h>
  #include <sys/time.h>

  int main(void)
  {
      struct timezone tz;

      gettimeofday(NULL, &tz);

      return 0;
  }

See http://bugs.debian.org/466491 for more details.

[ Impact: fix gettimeofday(NULL, &tz) segfault ]

Signed-off-by: John Wright <john.wright@hp.com>
Cc: Andi Kleen <ak@suse.de>
Cc: John Wright <john.wright@hp.com>
LKML-Reference: <1241037121-14805-1-git-send-email-john.wright@hp.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
arch/x86/vdso/vclock_gettime.c

index d9d35824c56f30e56266445cdf9ed5877f94bf52..6a40b78b46aafae8273ad7ed12ebbfd44187733a 100644 (file)
@@ -104,11 +104,13 @@ notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
 {
        long ret;
        if (likely(gtod->sysctl_enabled && gtod->clock.vread)) {
-               BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
-                            offsetof(struct timespec, tv_nsec) ||
-                            sizeof(*tv) != sizeof(struct timespec));
-               do_realtime((struct timespec *)tv);
-               tv->tv_usec /= 1000;
+               if (likely(tv != NULL)) {
+                       BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
+                                    offsetof(struct timespec, tv_nsec) ||
+                                    sizeof(*tv) != sizeof(struct timespec));
+                       do_realtime((struct timespec *)tv);
+                       tv->tv_usec /= 1000;
+               }
                if (unlikely(tz != NULL)) {
                        /* Avoid memcpy. Some old compilers fail to inline it */
                        tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
This page took 0.02728 seconds and 5 git commands to generate.