/src/sudo/lib/iolog/iolog_timing.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2009-2020 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <stdio.h> |
27 | | #include <stdlib.h> |
28 | | #ifdef HAVE_STDBOOL_H |
29 | | # include <stdbool.h> |
30 | | #else |
31 | | # include <compat/stdbool.h> |
32 | | #endif /* HAVE_STDBOOL_H */ |
33 | | #include <string.h> |
34 | | #include <signal.h> |
35 | | #include <unistd.h> |
36 | | #include <ctype.h> |
37 | | #include <errno.h> |
38 | | #include <limits.h> |
39 | | #include <fcntl.h> |
40 | | #include <time.h> |
41 | | |
42 | | #include <sudo_compat.h> |
43 | | #include <sudo_debug.h> |
44 | | #include <sudo_eventlog.h> |
45 | | #include <sudo_fatal.h> |
46 | | #include <sudo_gettext.h> |
47 | | #include <sudo_iolog.h> |
48 | | #include <sudo_util.h> |
49 | | |
50 | | static int timing_event_adj; |
51 | | |
52 | | void |
53 | | iolog_adjust_delay(struct timespec *delay, struct timespec *max_delay, |
54 | | double scale_factor) |
55 | 0 | { |
56 | 0 | debug_decl(iolog_adjust_delay, SUDO_DEBUG_UTIL); |
57 | |
|
58 | 0 | if (scale_factor != 1.0) { |
59 | | /* Order is important: we don't want to double the remainder. */ |
60 | 0 | const double seconds = (double)delay->tv_sec / scale_factor; |
61 | 0 | const double nseconds = (double)delay->tv_nsec / scale_factor; |
62 | 0 | delay->tv_sec = (time_t)seconds; |
63 | 0 | delay->tv_nsec = (long)nseconds; |
64 | 0 | delay->tv_nsec += (long)((seconds - (double)delay->tv_sec) * 1000000000); |
65 | 0 | while (delay->tv_nsec >= 1000000000) { |
66 | 0 | delay->tv_sec++; |
67 | 0 | delay->tv_nsec -= 1000000000; |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | /* Clamp to max delay. */ |
72 | 0 | if (max_delay != NULL) { |
73 | 0 | if (sudo_timespeccmp(delay, max_delay, >)) { |
74 | 0 | delay->tv_sec = max_delay->tv_sec; |
75 | 0 | delay->tv_nsec = max_delay->tv_nsec; |
76 | 0 | } |
77 | 0 | } |
78 | |
|
79 | 0 | debug_return; |
80 | 0 | } |
81 | | |
82 | | /* |
83 | | * Parse the delay as seconds and nanoseconds: %lld.%09ld |
84 | | * Sudo used to write this as a double, but since timing data is logged |
85 | | * in the C locale this may not match the current locale. |
86 | | */ |
87 | | char * |
88 | | iolog_parse_delay(const char *cp, struct timespec *delay, |
89 | | const char *decimal_point) |
90 | 3.07k | { |
91 | 3.07k | char numbuf[STRLEN_MAX_SIGNED(long long) + 1]; |
92 | 3.07k | const char *errstr, *ep; |
93 | 3.07k | long long llval; |
94 | 3.07k | size_t len; |
95 | 3.07k | debug_decl(iolog_parse_delay, SUDO_DEBUG_UTIL); |
96 | | |
97 | | /* Parse seconds (whole number portion). */ |
98 | 12.1k | for (ep = cp; isdigit((unsigned char)*ep); ep++) |
99 | 12.1k | continue; |
100 | 3.07k | len = (size_t)(ep - cp); |
101 | 3.07k | if (len >= sizeof(numbuf)) { |
102 | 11 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
103 | 11 | "%s: number of seconds is too large", cp); |
104 | 11 | debug_return_ptr(NULL); |
105 | 11 | } |
106 | 3.06k | memcpy(numbuf, cp, len); |
107 | 3.06k | numbuf[len] = '\0'; |
108 | 3.06k | delay->tv_sec = (time_t)sudo_strtonum(numbuf, 0, TIME_T_MAX, &errstr); |
109 | 3.06k | if (errstr != NULL) { |
110 | 106 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
111 | 106 | "%s: number of seconds is %s", numbuf, errstr); |
112 | 106 | debug_return_ptr(NULL); |
113 | 106 | } |
114 | | |
115 | | /* Radix may be in user's locale for sudo < 1.7.4 so accept that too. */ |
116 | 2.96k | if (*ep != '.' && *ep != *decimal_point) { |
117 | 1.88k | if (*ep == '\0' || isspace((unsigned char)*ep)) { |
118 | | /* No fractional part. */ |
119 | 1.86k | delay->tv_nsec = 0; |
120 | 1.86k | goto done; |
121 | 1.86k | } |
122 | 1.88k | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
123 | 19 | "invalid characters after seconds: %s", ep); |
124 | 19 | debug_return_ptr(NULL); |
125 | 19 | } |
126 | 1.08k | cp = ep + 1; |
127 | | |
128 | | /* Parse fractional part, we may read more precision than we can store. */ |
129 | 10.8k | for (ep = cp; isdigit((unsigned char)*ep); ep++) |
130 | 10.8k | continue; |
131 | 1.08k | len = (size_t)(ep - cp); |
132 | 1.08k | if (len >= sizeof(numbuf)) { |
133 | 9 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
134 | 9 | "%s: number of nanoseconds is too large", cp); |
135 | 9 | debug_return_ptr(NULL); |
136 | 9 | } |
137 | 1.07k | memcpy(numbuf, cp, len); |
138 | 1.07k | numbuf[len] = '\0'; |
139 | 1.07k | llval = sudo_strtonum(numbuf, 0, LLONG_MAX, &errstr); |
140 | 1.07k | if (errstr != NULL) { |
141 | 11 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
142 | 11 | "%s: number of nanoseconds is %s", numbuf, errstr); |
143 | 11 | debug_return_ptr(NULL); |
144 | 11 | } |
145 | | |
146 | | /* Adjust fractional part to nanosecond precision. */ |
147 | 1.06k | if (len < 9) { |
148 | | /* Convert to nanosecond precision. */ |
149 | 4.29k | do { |
150 | 4.29k | llval *= 10; |
151 | 4.29k | } while (++len < 9); |
152 | 540 | } else if (len > 9) { |
153 | | /* Clamp to nanoseconds. */ |
154 | 1.47k | do { |
155 | 1.47k | llval /= 10; |
156 | 1.47k | } while (--len > 9); |
157 | 326 | } |
158 | 1.06k | delay->tv_nsec = (long)llval; |
159 | | |
160 | 2.92k | done: |
161 | | /* Advance to the next field. */ |
162 | 2.92k | while (isspace((unsigned char)*ep)) |
163 | 2.92k | ep++; |
164 | | |
165 | 2.92k | debug_return_str((char *)ep); |
166 | 2.92k | } |
167 | | |
168 | | /* |
169 | | * Parse a timing line, which is formatted as: |
170 | | * IO_EVENT_TTYOUT sleep_time num_bytes |
171 | | * IO_EVENT_WINSIZE sleep_time lines cols |
172 | | * IO_EVENT_SUSPEND sleep_time signo |
173 | | * Where type is IO_EVENT_*, sleep_time is the number of seconds to sleep |
174 | | * before writing the data and num_bytes is the number of bytes to output. |
175 | | * Returns true on success and false on failure. |
176 | | */ |
177 | | bool |
178 | | iolog_parse_timing(const char *line, struct timing_closure *timing) |
179 | 3.26k | { |
180 | 3.26k | unsigned long ulval; |
181 | 3.26k | char *cp, *ep; |
182 | 3.26k | debug_decl(iolog_parse_timing, SUDO_DEBUG_UTIL); |
183 | | |
184 | | /* Clear iolog descriptor. */ |
185 | 3.26k | timing->iol = NULL; |
186 | | |
187 | | /* Parse event type. */ |
188 | 3.26k | ulval = strtoul(line, &ep, 10); |
189 | 3.26k | if (ep == line || !isspace((unsigned char) *ep)) |
190 | 60 | goto bad; |
191 | 3.20k | if (ulval >= IO_EVENT_COUNT) |
192 | 123 | goto bad; |
193 | 3.07k | if (ulval == IO_EVENT_TTYOUT_1_8_7) { |
194 | | /* work around a bug in timing files generated by sudo 1.8.7 */ |
195 | 626 | timing_event_adj = 2; |
196 | 626 | } |
197 | 3.07k | timing->event = (int)ulval - timing_event_adj; |
198 | 3.07k | for (cp = ep + 1; isspace((unsigned char) *cp); cp++) |
199 | 194 | continue; |
200 | | |
201 | | /* Parse delay, returns the next field or NULL on error. */ |
202 | 3.07k | if ((cp = iolog_parse_delay(cp, &timing->delay, timing->decimal)) == NULL) |
203 | 156 | goto bad; |
204 | | |
205 | 2.92k | switch (timing->event) { |
206 | 0 | case IO_EVENT_SUSPEND: |
207 | | /* Signal name (no leading SIG prefix) or number. */ |
208 | 0 | if (str2sig(cp, &timing->u.signo) == -1) |
209 | 0 | goto bad; |
210 | 0 | break; |
211 | 521 | case IO_EVENT_WINSIZE: |
212 | 521 | ulval = strtoul(cp, &ep, 10); |
213 | 521 | if (ep == cp || !isspace((unsigned char) *ep)) |
214 | 52 | goto bad; |
215 | 469 | if (ulval > INT_MAX) |
216 | 68 | goto bad; |
217 | 401 | timing->u.winsize.lines = (int)ulval; |
218 | 401 | for (cp = ep + 1; isspace((unsigned char) *cp); cp++) |
219 | 195 | continue; |
220 | | |
221 | 401 | ulval = strtoul(cp, &ep, 10); |
222 | 401 | if (ep == cp || *ep != '\0') |
223 | 80 | goto bad; |
224 | 321 | if (ulval > INT_MAX) |
225 | 65 | goto bad; |
226 | 256 | timing->u.winsize.cols = (int)ulval; |
227 | 256 | break; |
228 | 2.40k | default: |
229 | 2.40k | errno = 0; |
230 | 2.40k | ulval = strtoul(cp, &ep, 10); |
231 | 2.40k | if (ep == cp || *ep != '\0') |
232 | 200 | goto bad; |
233 | | /* Note: assumes SIZE_MAX == ULONG_MAX */ |
234 | 2.20k | if (errno == ERANGE && ulval == ULONG_MAX) |
235 | 1 | goto bad; |
236 | 2.20k | timing->u.nbytes = (size_t)ulval; |
237 | 2.20k | break; |
238 | 2.92k | } |
239 | | |
240 | 2.45k | debug_return_bool(true); |
241 | 805 | bad: |
242 | 805 | debug_return_bool(false); |
243 | 805 | } |
244 | | |
245 | | /* |
246 | | * Read the next record from the timing file. |
247 | | * Return 0 on success, 1 on EOF and -1 on error. |
248 | | */ |
249 | | int |
250 | | iolog_read_timing_record(struct iolog_file *iol, struct timing_closure *timing) |
251 | 3.34k | { |
252 | 3.34k | char line[LINE_MAX]; |
253 | 3.34k | const char *errstr; |
254 | 3.34k | debug_decl(iolog_read_timing_record, SUDO_DEBUG_UTIL); |
255 | | |
256 | | /* Read next record from timing file. */ |
257 | 3.34k | if (iolog_gets(iol, line, sizeof(line), &errstr) == NULL) { |
258 | | /* EOF or error reading timing file, we are done. */ |
259 | 82 | if (iolog_eof(iol)) |
260 | 82 | debug_return_int(1); |
261 | 0 | sudo_warnx(U_("error reading timing file: %s"), errstr); |
262 | 0 | debug_return_int(-1); |
263 | 0 | } |
264 | | |
265 | | /* Parse timing file record. */ |
266 | 3.26k | line[strcspn(line, "\n")] = '\0'; |
267 | 3.26k | if (!iolog_parse_timing(line, timing)) { |
268 | 805 | sudo_warnx(U_("invalid timing file line: %s"), line); |
269 | 805 | debug_return_int(-1); |
270 | 805 | } |
271 | | |
272 | 2.45k | debug_return_int(0); |
273 | 2.45k | } |