]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
hurd: Make *utime*s catch invalid times [BZ #32802, BZ #32803]
authorSamuel Thibault <samuel.thibault@ens-lyon.org>
Tue, 18 Mar 2025 17:49:21 +0000 (18:49 +0100)
committerSamuel Thibault <samuel.thibault@ens-lyon.org>
Sat, 19 Apr 2025 22:24:56 +0000 (00:24 +0200)
sysdeps/mach/hurd/futimens.c
sysdeps/mach/hurd/futimes.c
sysdeps/mach/hurd/utime-helper.c

index 30ef0a6493323a54798a31295db5a51f11609545..12125299c44830d06953928b71e3b1fe8408d4f5 100644 (file)
@@ -32,7 +32,9 @@ __futimens (int fd, const struct timespec tsp[2])
   struct timespec atime, mtime;
   error_t err;
 
-  utime_ts_from_tspec (tsp, &atime, &mtime);
+  err = utime_ts_from_tspec (tsp, &atime, &mtime);
+  if (err)
+    return err;
 
   err = HURD_DPORT_USE (fd, __file_utimens (port, atime, mtime));
 
@@ -40,7 +42,9 @@ __futimens (int fd, const struct timespec tsp[2])
     {
       time_value_t atim, mtim;
 
-      utime_tvalue_from_tspec (tsp, &atim, &mtim);
+      err = utime_tvalue_from_tspec (tsp, &atim, &mtim);
+      if (err)
+       return err;
 
       err = HURD_DPORT_USE (fd, __file_utimes (port, atim, mtim));
   }
index 20f47f3d2874954b0d312102a47dbf60e96fb6ed..97385d7dd02cf021ac70310efb229240b707f090 100644 (file)
@@ -32,7 +32,9 @@ __futimes (int fd, const struct timeval tvp[2])
   struct timespec atime, mtime;
   error_t err;
 
-  utime_ts_from_tval (tvp, &atime, &mtime);
+  err = utime_ts_from_tval (tvp, &atime, &mtime);
+  if (err)
+    return err;
 
   err = HURD_DPORT_USE (fd, __file_utimens (port, atime, mtime));
 
@@ -40,7 +42,9 @@ __futimes (int fd, const struct timeval tvp[2])
     {
       time_value_t atim, mtim;
 
-      utime_tvalue_from_tval (tvp, &atim, &mtim);
+      err = utime_tvalue_from_tval (tvp, &atim, &mtim);
+      if (err)
+       return err;
 
       err = HURD_DPORT_USE (fd, __file_utimes (port, atim, mtim));
     }
index d88bccd7861622a747ef4d6d4eeb08be6f848b48..6afa871197bf0e877b5d7e421e9b94f2022f6ad1 100644 (file)
 #include <stddef.h>
 #include <sys/time.h>
 
+static inline bool
+check_tval (const struct timeval *tvp)
+{
+  return tvp->tv_usec >= 0 && tvp->tv_usec < USEC_PER_SEC;
+}
+
 /* Initializes atime/mtime timespec structures from an array of timeval.  */
-static inline void
+static inline error_t
 utime_ts_from_tval (const struct timeval tvp[2],
                     struct timespec *atime, struct timespec *mtime)
 {
@@ -37,13 +43,19 @@ utime_ts_from_tval (const struct timeval tvp[2],
     }
   else
     {
+      if (!check_tval (&tvp[0]))
+       return EINVAL;
+      if (!check_tval (&tvp[1]))
+       return EINVAL;
+
       TIMEVAL_TO_TIMESPEC (&tvp[0], atime);
       TIMEVAL_TO_TIMESPEC (&tvp[1], mtime);
     }
+  return 0;
 }
 
 /* Initializes atime/mtime time_value_t structures from an array of timeval.  */
-static inline void
+static inline error_t
 utime_tvalue_from_tval (const struct timeval tvp[2],
                         time_value_t *atime, time_value_t *mtime)
 {
@@ -53,11 +65,17 @@ utime_tvalue_from_tval (const struct timeval tvp[2],
     atime->microseconds = mtime->microseconds = -1;
   else
     {
+      if (!check_tval (&tvp[0]))
+       return EINVAL;
+      if (!check_tval (&tvp[1]))
+       return EINVAL;
+
       atime->seconds = tvp[0].tv_sec;
       atime->microseconds = tvp[0].tv_usec;
       mtime->seconds = tvp[1].tv_sec;
       mtime->microseconds = tvp[1].tv_usec;
     }
+  return 0;
 }
 
 /* Changes the access time of the file behind PORT using a timeval array.  */
@@ -67,7 +85,9 @@ hurd_futimes (const file_t port, const struct timeval tvp[2])
   error_t err;
   struct timespec atime, mtime;
 
-  utime_ts_from_tval (tvp, &atime, &mtime);
+  err = utime_ts_from_tval (tvp, &atime, &mtime);
+  if (err)
+    return err;
 
   err = __file_utimens (port, atime, mtime);
 
@@ -75,7 +95,9 @@ hurd_futimes (const file_t port, const struct timeval tvp[2])
     {
       time_value_t atim, mtim;
 
-      utime_tvalue_from_tval (tvp, &atim, &mtim);
+      err = utime_tvalue_from_tval (tvp, &atim, &mtim);
+      if (err)
+       return err;
 
       err = __file_utimes (port, atim, mtim);
     }
@@ -83,8 +105,16 @@ hurd_futimes (const file_t port, const struct timeval tvp[2])
   return err;
 }
 
+static inline bool
+check_tspec (const struct timespec *tsp)
+{
+  return tsp->tv_nsec == UTIME_NOW
+      || tsp->tv_nsec == UTIME_OMIT
+      || tsp->tv_nsec >= 0 && tsp->tv_nsec < NSEC_PER_SEC;
+}
+
 /* Initializes atime/mtime timespec structures from an array of timespec.  */
-static inline void
+static inline error_t
 utime_ts_from_tspec (const struct timespec tsp[2],
                      struct timespec *atime, struct timespec *mtime)
 {
@@ -99,13 +129,19 @@ utime_ts_from_tspec (const struct timespec tsp[2],
     }
   else
     {
+      if (!check_tspec (&tsp[0]))
+       return EINVAL;
+      if (!check_tspec (&tsp[1]))
+       return EINVAL;
+
       *atime = tsp[0];
       *mtime = tsp[1];
     }
+  return 0;
 }
 
 /* Initializes atime/mtime time_value_t structures from an array of timespec.  */
-static inline void
+static inline error_t
 utime_tvalue_from_tspec (const struct timespec tsp[2],
                          time_value_t *atime, time_value_t *mtime)
 {
@@ -115,6 +151,11 @@ utime_tvalue_from_tspec (const struct timespec tsp[2],
     atime->microseconds = mtime->microseconds = -1;
   else
     {
+      if (!check_tspec (&tsp[0]))
+       return EINVAL;
+      if (!check_tspec (&tsp[1]))
+       return EINVAL;
+
       if (tsp[0].tv_nsec == UTIME_NOW)
        atime->microseconds = -1;
       else if (tsp[0].tv_nsec == UTIME_OMIT)
@@ -128,6 +169,7 @@ utime_tvalue_from_tspec (const struct timespec tsp[2],
       else
        TIMESPEC_TO_TIME_VALUE (mtime, &(tsp[1]));
     }
+  return 0;
 }
 
 /* Changes the access time of the file behind PORT using a timespec array.  */
@@ -137,7 +179,9 @@ hurd_futimens (const file_t port, const struct timespec tsp[2])
   error_t err;
   struct timespec atime, mtime;
 
-  utime_ts_from_tspec (tsp, &atime, &mtime);
+  err = utime_ts_from_tspec (tsp, &atime, &mtime);
+  if (err)
+    return err;
 
   err = __file_utimens (port, atime, mtime);
 
@@ -145,7 +189,9 @@ hurd_futimens (const file_t port, const struct timespec tsp[2])
     {
       time_value_t atim, mtim;
 
-      utime_tvalue_from_tspec (tsp, &atim, &mtim);
+      err = utime_tvalue_from_tspec (tsp, &atim, &mtim);
+      if (err)
+       return err;
 
       err = __file_utimes (port, atim, mtim);
     }