Fix: Report process uptime and harden the login reserved list

The "启动信息" elapsed time was computed by subtracting a client-formatted
wall-clock string from the master's own local time. Windows clients send their
local time, Linux/Android/macOS send Beijing time, so a host whose clock runs
ahead of the master (UTC+8 clients on a UTC+1 master, a fast CMOS clock) gave a
negative difference that was clamped to 0s — permanently, for that host.

Add RES_UPTIME to the login reserved list: the client reports its process
uptime in seconds, a difference between two readings of the same clock, so
timezone and clock skew cancel out. The master adds the connection's alive
time to get the current value, and keeps the old string path as the fallback
for clients that do not send the field. That fallback now also treats MAC as
Beijing time, which was simply missed.

AddReserved() now goes through one bounds-checked append. The separator used
to be written without any length check (an overflow aborts the client, which
installs no invalid parameter handler), and an over-long value now leaves an
empty slot instead of dropping its separator, so positional lookups cannot
shift.

Verified with an ASan/UBSan test over the encoding boundaries and Debug builds
of the client and the master. The Linux/Android/macOS clients only append the
same field at the end of the list; they were not compiled here.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-08 21:49:20 +01:00
1 parent 2e34e49186
commit 2cbed0dcdf
6 files changed
+149 -38

No files matched your search

+34 -12
View File
@@ -1144,6 +1144,11 @@ enum LOGIN_RES {
RES_PID = 17, // 进程ID
RES_FILESIZE = 18, // 文件大小
RES_TLS = 19, // TLS 传输启用标志 0/1
// 登录时刻的「进程已运行秒数」。与 RES_* 里任何时间字符串都不同,它是差值而不是
// 时刻:客户端用同源的两次读数相减(Windows 是 GetProcessTimes 与
// GetSystemTimeAsFileTime,Linux/Android 是同一块墙钟),时区与整机时钟偏移自动
// 抵消。服务端优先用它算运行时长,缺失时退回 szStartTime 字符串那套老逻辑。
RES_UPTIME = 20,
RES_MAX,
};
@@ -1205,33 +1210,50 @@ typedef struct LOGIN_INFOR {
}
void AddReserved(const char* v)
{
if (strlen(szReserved))
strcat_s(szReserved, "|");
if (strlen(szReserved) + strlen(v) < sizeof(szReserved))
strcat_s(szReserved, v);
AppendField(v ? v : "");
}
void AddReserved(int n)
{
if (strlen(szReserved))
strcat_s(szReserved, "|");
char buf[24] = {};
sprintf_s(buf, "%d", n);
if (strlen(szReserved) + strlen(buf) < sizeof(szReserved))
strcat_s(szReserved, buf);
AppendField(buf);
}
void AddReserved(double f)
{
if (strlen(szReserved))
strcat_s(szReserved, "|");
char buf[24] = {};
sprintf_s(buf, "%.2f", f);
if (strlen(szReserved) + strlen(buf) < sizeof(szReserved))
strcat_s(szReserved, buf);
AppendField(buf);
}
std::vector<std::string> ParseReserved(int n = 1) const
{
return StringToVector(szReserved, '|', n);
}
private:
// 追加一个保留字段。szReserved 是「按位置」解析的(服务端用 LOGIN_RES 下标取值),
// 因此这里有两个硬约束:
// 1. 任何一次写入都不能越界。带长度参数的 strcat_s 溢出时会调用 invalid parameter
// handler —— 服务端装了(2015Remote.cpp),客户端没装,在客户端就是直接 abort。
// 2. 值放不下时只留空槽、不能连槽一起丢。少写一个 '|' 会让后面所有字段整体前移
// 一位,服务端就把安装时间读成程序位数、把用户名读成类型……空槽至少位置是对的。
void AppendField(const char* v)
{
const size_t cap = sizeof(szReserved);
// 用 strlen 而不是 strnlen_s:后者是 MSVC 的 Annex K 扩展,Linux/Android 分支
// 没有(commands.h 在该分支只把 strcat_s/sprintf_s 映射到非安全版本)。
// szReserved 与 v 都是 NUL 结尾的,长度不会越界读取。
size_t cur = strlen(szReserved);
if (cur + 1 >= cap)
return; // 已满:连分隔符都放不下
if (cur > 0)
szReserved[cur++] = '|';
szReserved[cur] = '\0';
size_t len = strlen(v);
if (len >= cap - cur)
return; // 值放不下(含结尾 NUL 的空间):留空槽
memcpy(szReserved + cur, v, len);
szReserved[cur + len] = '\0';
}
} LOGIN_INFOR;
inline uint64_t GetUnixMs()