generic: 6.6: backport patch adding support for rgmii-id in stmmac-ipq806x
[openwrt/openwrt.git] / package / network / services / dropbear / patches / 002-fix-y2038-issues.patch
1 From ec2215726cffb976019d08ebf569edd2229e9dba Mon Sep 17 00:00:00 2001
2 From: Matt Johnston <matt@ucc.asn.au>
3 Date: Thu, 1 Dec 2022 11:34:43 +0800
4 Subject: Fix y2038 issues with time_t conversion
5
6 These changes were identified by building with and without
7 -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64
8 on 32-bit arm, logging warnings to files.
9 -Wconversion was added to CFLAGS in both builds.
10
11 Then a "diff -I Wconversion log1 log2" shows new warnings that appear
12 with the 64-bit time_t. There are a few false positives that have been
13 fixed for quietness.
14
15 struct logininfo and struct wtmp are still problematic, those will
16 need to be handled by libc.
17 ---
18 common-session.c | 43 +++++++++++++++++++++++++++----------------
19 dbutil.c | 2 +-
20 loginrec.c | 2 ++
21 loginrec.h | 4 ++--
22 runopts.h | 4 ++--
23 svr-auth.c | 2 +-
24 6 files changed, 35 insertions(+), 22 deletions(-)
25
26 --- a/common-session.c
27 +++ b/common-session.c
28 @@ -519,15 +519,24 @@ static void send_msg_keepalive() {
29 ses.last_packet_time_idle = old_time_idle;
30 }
31
32 +/* Returns the difference in seconds, clamped to LONG_MAX */
33 +static long elapsed(time_t now, time_t prev) {
34 + time_t del = now - prev;
35 + if (del > LONG_MAX) {
36 + return LONG_MAX;
37 + }
38 + return (long)del;
39 +}
40 +
41 /* Check all timeouts which are required. Currently these are the time for
42 * user authentication, and the automatic rekeying. */
43 static void checktimeouts() {
44
45 time_t now;
46 now = monotonic_now();
47 -
48 +
49 if (IS_DROPBEAR_SERVER && ses.connect_time != 0
50 - && now - ses.connect_time >= AUTH_TIMEOUT) {
51 + && elapsed(now, ses.connect_time) >= AUTH_TIMEOUT) {
52 dropbear_close("Timeout before auth");
53 }
54
55 @@ -537,45 +546,47 @@ static void checktimeouts() {
56 }
57
58 if (!ses.kexstate.sentkexinit
59 - && (now - ses.kexstate.lastkextime >= KEX_REKEY_TIMEOUT
60 + && (elapsed(now, ses.kexstate.lastkextime) >= KEX_REKEY_TIMEOUT
61 || ses.kexstate.datarecv+ses.kexstate.datatrans >= KEX_REKEY_DATA)) {
62 TRACE(("rekeying after timeout or max data reached"))
63 send_msg_kexinit();
64 }
65 -
66 +
67 if (opts.keepalive_secs > 0 && ses.authstate.authdone) {
68 /* Avoid sending keepalives prior to auth - those are
69 not valid pre-auth packet types */
70
71 /* Send keepalives if we've been idle */
72 - if (now - ses.last_packet_time_any_sent >= opts.keepalive_secs) {
73 + if (elapsed(now, ses.last_packet_time_any_sent) >= opts.keepalive_secs) {
74 send_msg_keepalive();
75 }
76
77 /* Also send an explicit keepalive message to trigger a response
78 if the remote end hasn't sent us anything */
79 - if (now - ses.last_packet_time_keepalive_recv >= opts.keepalive_secs
80 - && now - ses.last_packet_time_keepalive_sent >= opts.keepalive_secs) {
81 + if (elapsed(now, ses.last_packet_time_keepalive_recv) >= opts.keepalive_secs
82 + && elapsed(now, ses.last_packet_time_keepalive_sent) >= opts.keepalive_secs) {
83 send_msg_keepalive();
84 }
85
86 - if (now - ses.last_packet_time_keepalive_recv
87 + if (elapsed(now, ses.last_packet_time_keepalive_recv)
88 >= opts.keepalive_secs * DEFAULT_KEEPALIVE_LIMIT) {
89 dropbear_exit("Keepalive timeout");
90 }
91 }
92
93 - if (opts.idle_timeout_secs > 0
94 - && now - ses.last_packet_time_idle >= opts.idle_timeout_secs) {
95 + if (opts.idle_timeout_secs > 0
96 + && elapsed(now, ses.last_packet_time_idle) >= opts.idle_timeout_secs) {
97 dropbear_close("Idle timeout");
98 }
99 }
100
101 -static void update_timeout(long limit, long now, long last_event, long * timeout) {
102 - TRACE2(("update_timeout limit %ld, now %ld, last %ld, timeout %ld",
103 - limit, now, last_event, *timeout))
104 +static void update_timeout(long limit, time_t now, time_t last_event, long * timeout) {
105 + TRACE2(("update_timeout limit %ld, now %llu, last %llu, timeout %ld",
106 + limit,
107 + (unsigned long long)now,
108 + (unsigned long long)last_event, *timeout))
109 if (last_event > 0 && limit > 0) {
110 - *timeout = MIN(*timeout, last_event+limit-now);
111 + *timeout = MIN(*timeout, elapsed(now, last_event) + limit);
112 TRACE2(("new timeout %ld", *timeout))
113 }
114 }
115 @@ -584,7 +595,7 @@ static long select_timeout() {
116 /* determine the minimum timeout that might be required, so
117 as to avoid waking when unneccessary */
118 long timeout = KEX_REKEY_TIMEOUT;
119 - long now = monotonic_now();
120 + time_t now = monotonic_now();
121
122 if (!ses.kexstate.sentkexinit) {
123 update_timeout(KEX_REKEY_TIMEOUT, now, ses.kexstate.lastkextime, &timeout);
124 @@ -596,7 +607,7 @@ static long select_timeout() {
125 }
126
127 if (ses.authstate.authdone) {
128 - update_timeout(opts.keepalive_secs, now,
129 + update_timeout(opts.keepalive_secs, now,
130 MAX(ses.last_packet_time_keepalive_recv, ses.last_packet_time_keepalive_sent),
131 &timeout);
132 }
133 --- a/dbutil.c
134 +++ b/dbutil.c
135 @@ -724,7 +724,7 @@ void gettime_wrapper(struct timespec *no
136 /* Fallback for everything else - this will sometimes go backwards */
137 gettimeofday(&tv, NULL);
138 now->tv_sec = tv.tv_sec;
139 - now->tv_nsec = 1000*tv.tv_usec;
140 + now->tv_nsec = 1000*(long)tv.tv_usec;
141 }
142
143 /* second-resolution monotonic timestamp */
144 --- a/loginrec.c
145 +++ b/loginrec.c
146 @@ -459,6 +459,7 @@ line_abbrevname(char *dst, const char *s
147 void
148 set_utmp_time(struct logininfo *li, struct utmp *ut)
149 {
150 + /* struct utmp in glibc isn't y2038 safe yet */
151 # ifdef HAVE_STRUCT_UTMP_UT_TV
152 ut->ut_tv.tv_sec = li->tv_sec;
153 ut->ut_tv.tv_usec = li->tv_usec;
154 @@ -1272,6 +1273,7 @@ lastlog_construct(struct logininfo *li,
155 (void)line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
156 strlcpy(last->ll_host, li->hostname,
157 MIN_SIZEOF(last->ll_host, li->hostname));
158 + /* struct lastlog in glibc isn't y2038 safe yet */
159 last->ll_time = li->tv_sec;
160 }
161
162 --- a/loginrec.h
163 +++ b/loginrec.h
164 @@ -139,8 +139,8 @@ struct logininfo {
165 /* struct timeval (sys/time.h) isn't always available, if it isn't we'll
166 * use time_t's value as tv_sec and set tv_usec to 0
167 */
168 - unsigned int tv_sec;
169 - unsigned int tv_usec;
170 + time_t tv_sec;
171 + suseconds_t tv_usec;
172 union login_netinfo hostaddr; /* caller's host address(es) */
173 }; /* struct logininfo */
174
175 --- a/runopts.h
176 +++ b/runopts.h
177 @@ -39,8 +39,8 @@ typedef struct runopts {
178 int listen_fwd_all;
179 #endif
180 unsigned int recv_window;
181 - time_t keepalive_secs; /* Time between sending keepalives. 0 is off */
182 - time_t idle_timeout_secs; /* Exit if no traffic is sent/received in this time */
183 + long keepalive_secs; /* Time between sending keepalives. 0 is off */
184 + long idle_timeout_secs; /* Exit if no traffic is sent/received in this time */
185 int usingsyslog;
186
187 #ifndef DISABLE_ZLIB
188 --- a/svr-auth.c
189 +++ b/svr-auth.c
190 @@ -389,7 +389,7 @@ void send_msg_userauth_failure(int parti
191 Beware of integer overflow if increasing these values */
192 const unsigned int mindelay = 250000000;
193 const unsigned int vardelay = 100000000;
194 - unsigned int rand_delay;
195 + suseconds_t rand_delay;
196 struct timespec delay;
197
198 gettime_wrapper(&delay);