From 9414249d740f90dfe5b65a922835306a78a1ad9f Mon Sep 17 00:00:00 2001
From: huyubiao <h13958451065@163.com>
Date: Tue, 18 Jul 2023 02:22:51 +0800
Subject: [PATCH] fix: integer overflow

---
 coms/service/Cargo.toml       |  1 +
 coms/service/src/config.rs    | 15 +++++++++++++++
 coms/service/src/mng.rs       | 10 +++++-----
 coms/service/src/rentry.rs    |  4 ++--
 coms/service/src/spawn.rs     |  2 +-
 coms/service/src/unit.rs      |  6 ------
 core/sysmaster/mount/setup.rs |  1 -
 libs/basic/src/time_util.rs   |  3 ---
 libs/constants/src/lib.rs     |  9 +++++++++
 libs/event/Cargo.toml         |  1 +
 libs/event/src/timer.rs       | 20 +++++---------------
 11 files changed, 39 insertions(+), 33 deletions(-)

diff --git a/coms/service/Cargo.toml b/coms/service/Cargo.toml
index 0f37cf8..27cedda 100644
--- a/coms/service/Cargo.toml
+++ b/coms/service/Cargo.toml
@@ -10,6 +10,7 @@ crate-type = ["dylib"]
 name = "service"
 
 [dependencies]
+constants = { path = "../../libs/constants" }
 basic = { path = "../../libs/basic" }
 macros = { path = "../../libs/macros" }
 cgroup = { path = "../../libs/cgroup" }
diff --git a/coms/service/src/config.rs b/coms/service/src/config.rs
index aa62b00..e61340b 100644
--- a/coms/service/src/config.rs
+++ b/coms/service/src/config.rs
@@ -14,6 +14,7 @@
 use super::comm::ServiceUnitComm;
 use super::rentry::{NotifyAccess, SectionService, ServiceCommand, ServiceType};
 use confique::{Config, FileFormat, Partial};
+use constants::{USEC_INFINITY, USEC_PER_SEC};
 use std::cell::RefCell;
 use std::collections::{HashMap, VecDeque};
 use std::path::PathBuf;
@@ -86,6 +87,7 @@ impl ServiceConfig {
             }
             Ok(v) => v,
         };
+        self.data.borrow_mut().verify();
 
         if update {
             self.db_update();
@@ -178,6 +180,19 @@ impl ServiceConfigData {
     pub(self) fn set_timeout_stop(&mut self, time_out: u64) {
         self.Service.set_timeout_stop(time_out);
     }
+
+    pub(self) fn verify(&mut self) {
+        if self.Service.WatchdogSec >= USEC_INFINITY / USEC_PER_SEC {
+            self.Service.WatchdogSec = 0;
+        } else {
+            self.Service.WatchdogSec *= USEC_PER_SEC;
+        }
+        if self.Service.RestartSec >= USEC_INFINITY / USEC_PER_SEC {
+            self.Service.RestartSec = USEC_PER_SEC;
+        } else {
+            self.Service.RestartSec *= USEC_PER_SEC;
+        }
+    }
 }
 
 #[cfg(test)]
diff --git a/coms/service/src/mng.rs b/coms/service/src/mng.rs
index b44a0dd..e5fbeb8 100644
--- a/coms/service/src/mng.rs
+++ b/coms/service/src/mng.rs
@@ -1228,9 +1228,9 @@ impl ServiceMng {
     }
 
     fn restart_watchdog(&self) {
-        self.monitor.borrow_mut().set_original_watchdog(
-            self.config.config_data().borrow().Service.WatchdogSec * 1000000,
-        );
+        self.monitor
+            .borrow_mut()
+            .set_original_watchdog(self.config.config_data().borrow().Service.WatchdogSec);
         let watchdog_usec = self.monitor.borrow().watchdog_usec();
         if watchdog_usec == 0 || watchdog_usec == u64::MAX {
             self.stop_watchdog();
@@ -2570,7 +2570,7 @@ mod tests {
         assert!(rt.armd_watchdog());
         assert_eq!(
             rt.watchdog().time(),
-            config.config_data().borrow().Service.WatchdogSec * 1000000
+            config.config_data().borrow().Service.WatchdogSec
         );
     }
 
@@ -2589,7 +2589,7 @@ mod tests {
         assert!(rt.armd_watchdog());
         assert_eq!(
             rt.watchdog().time(),
-            config.config_data().borrow().Service.WatchdogSec * 1000000
+            config.config_data().borrow().Service.WatchdogSec
         );
 
         messages.remove("WATCHDOG");
diff --git a/coms/service/src/rentry.rs b/coms/service/src/rentry.rs
index 7099e01..87c92b2 100644
--- a/coms/service/src/rentry.rs
+++ b/coms/service/src/rentry.rs
@@ -32,7 +32,7 @@ use sysmaster::serialize::DeserializeWith;
 use sysmaster::unit::KillMode;
 
 use basic::special::EXEC_RUNTIME_PREFIX;
-use basic::time_util::USEC_PER_SEC;
+use constants::USEC_PER_SEC;
 
 struct ServiceReDb<K, V>(ReDb<K, V>);
 
@@ -290,7 +290,7 @@ pub(super) struct SectionService {
     #[config(deserialize_with = ExitStatusSet::deserialize_with)]
     #[config(default = "")]
     pub RestartPreventExitStatus: ExitStatusSet,
-    #[config(default = 100000)]
+    #[config(default = 1)]
     pub RestartSec: u64,
     #[config(deserialize_with = Vec::<String>::deserialize_with)]
     #[config(default = "")]
diff --git a/coms/service/src/spawn.rs b/coms/service/src/spawn.rs
index 2ff9e92..9b895aa 100644
--- a/coms/service/src/spawn.rs
+++ b/coms/service/src/spawn.rs
@@ -171,7 +171,7 @@ impl ServiceSpawn {
     }
 
     fn watchdog_timer(&self) -> u64 {
-        self.config.config_data().borrow().Service.WatchdogSec * 1000000
+        self.config.config_data().borrow().Service.WatchdogSec
     }
 
     pub(super) fn set_socket_fd(&self, fd: i32) {
diff --git a/coms/service/src/unit.rs b/coms/service/src/unit.rs
index 4bb235e..ac2e05b 100644
--- a/coms/service/src/unit.rs
+++ b/coms/service/src/unit.rs
@@ -405,11 +405,5 @@ impl ServiceUnit {
     }
 }
 
-/*impl Default for ServiceUnit {
-    fn default() -> Self {
-        ServiceUnit::new()
-    }
-}*/
-
 use sysmaster::declare_unitobj_plugin_with_param;
 declare_unitobj_plugin_with_param!(ServiceUnit, ServiceUnit::new);
diff --git a/core/sysmaster/mount/setup.rs b/core/sysmaster/mount/setup.rs
index e7ca9df..850a2b2 100644
--- a/core/sysmaster/mount/setup.rs
+++ b/core/sysmaster/mount/setup.rs
@@ -32,7 +32,6 @@ use std::{
 use sysmaster::error::*;
 
 const EARLY_MOUNT_NUM: u8 = 3;
-// const CGROUP_ROOT: &str = "/sys/fs/cgroup/";
 
 type Callback = fn() -> bool;
 
diff --git a/libs/basic/src/time_util.rs b/libs/basic/src/time_util.rs
index 45a12af..a6eb5cb 100644
--- a/libs/basic/src/time_util.rs
+++ b/libs/basic/src/time_util.rs
@@ -15,9 +15,6 @@ use std::time::SystemTime;
 
 const USEC_INFINITY: u128 = u128::MAX;
 
-/// usec per sec
-pub const USEC_PER_SEC: u64 = 1000000;
-
 ///
 pub fn timespec_load(systime: SystemTime) -> u128 {
     match systime.duration_since(SystemTime::UNIX_EPOCH) {
diff --git a/libs/constants/src/lib.rs b/libs/constants/src/lib.rs
index 46a3a50..3be5723 100644
--- a/libs/constants/src/lib.rs
+++ b/libs/constants/src/lib.rs
@@ -35,3 +35,12 @@ pub const INIT_PARA_PATH: &str = "/run/sysmaster/init_para";
 
 /// invalid fd
 pub const INVALID_FD: i32 = -1;
+
+/// USec per Sec
+pub const USEC_PER_SEC: u64 = 1000000;
+/// NSec per USec
+pub const NSEC_PER_USEC: u64 = 1000;
+/// NSec per Sec
+pub const NSEC_PER_SEC: u64 = 1000000000;
+/// USec infinity
+pub const USEC_INFINITY: u64 = u64::MAX;
diff --git a/libs/event/Cargo.toml b/libs/event/Cargo.toml
index 8cd9398..55a5cb4 100644
--- a/libs/event/Cargo.toml
+++ b/libs/event/Cargo.toml
@@ -7,6 +7,7 @@ edition = "2021"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+constants = { path = "../constants" }
 libc = { version = "0.2" }
 nix = { version = "0.24" }
 snafu = { version = "0.7" }
diff --git a/libs/event/src/timer.rs b/libs/event/src/timer.rs
index a94025b..9a0cb3d 100644
--- a/libs/event/src/timer.rs
+++ b/libs/event/src/timer.rs
@@ -19,11 +19,7 @@ use std::{
 };
 
 use crate::{EventType, Source};
-
-use std::u64::MAX as USEC_INFINITY;
-const USEC_PER_SEC: u64 = 1000000;
-const NSEC_PER_USEC: u64 = 1000;
-const NSEC_PER_SEC: u64 = 1000000000;
+use constants::{NSEC_PER_SEC, NSEC_PER_USEC, USEC_INFINITY, USEC_PER_SEC};
 
 #[derive(Debug, Clone, Copy)]
 pub(crate) struct Timestamp {
@@ -137,16 +133,11 @@ impl Timer {
     pub fn push(&mut self, source: Rc<dyn Source>) {
         // calc the time
         let mut next = source.time_relative();
-        if next != u64::MAX {
-            next += self.timerid(&source.event_type());
-        } else {
+        let now = self.timerid(&source.event_type());
+        if next > USEC_INFINITY - now {
             next = source.time();
-        }
-
-        if next == u64::MAX {
-            panic!(
-                "You need to implement time() or time_relative(), and cannot be set to u64::MAX"
-            );
+        } else {
+            next += now;
         }
 
         let cd = ClockData::new(source.clone(), next);
@@ -165,7 +156,6 @@ impl Timer {
 
     pub fn pop(&mut self, et: &EventType) -> Option<Rc<dyn Source>> {
         let next = self.timerid(et);
-        // self.now();
         match self.timer_set.get_mut(et) {
             Some(timer) => {
                 if timer.data.is_empty() {
-- 
2.33.0

