/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 | double seconds; |
57 | 0 | debug_decl(iolog_adjust_delay, SUDO_DEBUG_UTIL); |
58 | |
|
59 | 0 | if (scale_factor != 1.0) { |
60 | | /* Order is important: we don't want to double the remainder. */ |
61 | 0 | seconds = (double)delay->tv_sec / scale_factor; |
62 | 0 | delay->tv_sec = (time_t)seconds; |
63 | 0 | delay->tv_nsec /= scale_factor; |
64 | 0 | delay->tv_nsec += (seconds - 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 | 4.16k | { |
91 | 4.16k | char numbuf[(((sizeof(long long) * 8) + 2) / 3) + 2]; |
92 | 4.16k | const char *errstr, *ep; |
93 | 4.16k | long long llval; |
94 | 4.16k | size_t len; |
95 | 4.16k | debug_decl(iolog_parse_delay, SUDO_DEBUG_UTIL); |
96 | | |
97 | | /* Parse seconds (whole number portion). */ |
98 | 12.8k | for (ep = cp; isdigit((unsigned char)*ep); ep++) |
99 | 12.8k | continue; |
100 | 4.16k | len = (size_t)(ep - cp); |
101 | 4.16k | if (len >= sizeof(numbuf)) { |
102 | 10 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
103 | 10 | "%s: number of seconds is too large", cp); |
104 | 10 | debug_return_ptr(NULL); |
105 | 10 | } |
106 | 4.15k | memcpy(numbuf, cp, len); |
107 | 4.15k | numbuf[len] = '\0'; |
108 | 4.15k | delay->tv_sec = sudo_strtonum(numbuf, 0, TIME_T_MAX, &errstr); |
109 | 4.15k | if (errstr != NULL) { |
110 | 105 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
111 | 105 | "%s: number of seconds is %s", numbuf, errstr); |
112 | 105 | debug_return_ptr(NULL); |
113 | 105 | } |
114 | | |
115 | | /* Radix may be in user's locale for sudo < 1.7.4 so accept that too. */ |
116 | 4.04k | if (*ep != '.' && *ep != *decimal_point) { |
117 | 3.16k | if (*ep == '\0' || isspace((unsigned char)*ep)) { |
118 | | /* No fractional part. */ |
119 | 3.14k | delay->tv_nsec = 0; |
120 | 3.14k | goto done; |
121 | 3.14k | } |
122 | 3.16k | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
123 | 15 | "invalid characters after seconds: %s", ep); |
124 | 15 | debug_return_ptr(NULL); |
125 | 15 | } |
126 | 887 | cp = ep + 1; |
127 | | |
128 | | /* Parse fractional part, we may read more precision than we can store. */ |
129 | 10.6k | for (ep = cp; isdigit((unsigned char)*ep); ep++) |
130 | 10.6k | continue; |
131 | 887 | len = (size_t)(ep - cp); |
132 | 887 | 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 | 878 | memcpy(numbuf, cp, len); |
138 | 878 | numbuf[len] = '\0'; |
139 | 878 | llval = sudo_strtonum(numbuf, 0, LLONG_MAX, &errstr); |
140 | 878 | if (errstr != NULL) { |
141 | 10 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
142 | 10 | "%s: number of nanoseconds is %s", numbuf, errstr); |
143 | 10 | debug_return_ptr(NULL); |
144 | 10 | } |
145 | | |
146 | | /* Adjust fractional part to nanosecond precision. */ |
147 | 868 | if (len < 9) { |
148 | | /* Convert to nanosecond precision. */ |
149 | 2.78k | do { |
150 | 2.78k | llval *= 10; |
151 | 2.78k | } while (++len < 9); |
152 | 516 | } else if (len > 9) { |
153 | | /* Clamp to nanoseconds. */ |
154 | 1.46k | do { |
155 | 1.46k | llval /= 10; |
156 | 1.46k | } while (--len > 9); |
157 | 322 | } |
158 | 868 | delay->tv_nsec = (long)llval; |
159 | | |
160 | 4.01k | done: |
161 | | /* Advance to the next field. */ |
162 | 4.01k | while (isspace((unsigned char)*ep)) |
163 | 4.00k | ep++; |
164 | | |
165 | 4.01k | debug_return_str((char *)ep); |
166 | 4.01k | } |
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 | 4.33k | { |
180 | 4.33k | unsigned long ulval; |
181 | 4.33k | char *cp, *ep; |
182 | 4.33k | debug_decl(iolog_parse_timing, SUDO_DEBUG_UTIL); |
183 | | |
184 | | /* Clear iolog descriptor. */ |
185 | 4.33k | timing->iol = NULL; |
186 | | |
187 | | /* Parse event type. */ |
188 | 4.33k | ulval = strtoul(line, &ep, 10); |
189 | 4.33k | if (ep == line || !isspace((unsigned char) *ep)) |
190 | 55 | goto bad; |
191 | 4.27k | if (ulval >= IO_EVENT_COUNT) |
192 | 114 | goto bad; |
193 | 4.16k | if (ulval == IO_EVENT_TTYOUT_1_8_7) { |
194 | | /* work around a bug in timing files generated by sudo 1.8.7 */ |
195 | 2.25k | timing_event_adj = 2; |
196 | 2.25k | } |
197 | 4.16k | timing->event = (int)ulval - timing_event_adj; |
198 | 4.16k | for (cp = ep + 1; isspace((unsigned char) *cp); cp++) |
199 | 231 | continue; |
200 | | |
201 | | /* Parse delay, returns the next field or NULL on error. */ |
202 | 4.16k | if ((cp = iolog_parse_delay(cp, &timing->delay, timing->decimal)) == NULL) |
203 | 149 | goto bad; |
204 | | |
205 | 4.01k | 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 | 495 | case IO_EVENT_WINSIZE: |
212 | 495 | ulval = strtoul(cp, &ep, 10); |
213 | 495 | if (ep == cp || !isspace((unsigned char) *ep)) |
214 | 35 | goto bad; |
215 | 460 | if (ulval > INT_MAX) |
216 | 63 | goto bad; |
217 | 397 | timing->u.winsize.lines = (int)ulval; |
218 | 397 | for (cp = ep + 1; isspace((unsigned char) *cp); cp++) |
219 | 314 | continue; |
220 | | |
221 | 397 | ulval = strtoul(cp, &ep, 10); |
222 | 397 | if (ep == cp || *ep != '\0') |
223 | 78 | goto bad; |
224 | 319 | if (ulval > INT_MAX) |
225 | 64 | goto bad; |
226 | 255 | timing->u.winsize.cols = (int)ulval; |
227 | 255 | break; |
228 | 3.52k | default: |
229 | 3.52k | errno = 0; |
230 | 3.52k | ulval = strtoul(cp, &ep, 10); |
231 | 3.52k | if (ep == cp || *ep != '\0') |
232 | 197 | goto bad; |
233 | | /* Note: assumes SIZE_MAX == ULONG_MAX */ |
234 | 3.32k | if (errno == ERANGE && ulval == ULONG_MAX) |
235 | 1 | goto bad; |
236 | 3.32k | timing->u.nbytes = (size_t)ulval; |
237 | 3.32k | break; |
238 | 4.01k | } |
239 | | |
240 | 3.57k | debug_return_bool(true); |
241 | 756 | bad: |
242 | 756 | debug_return_bool(false); |
243 | 756 | } |
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 | 4.42k | { |
252 | 4.42k | char line[LINE_MAX]; |
253 | 4.42k | const char *errstr; |
254 | 4.42k | debug_decl(iolog_read_timing_record, SUDO_DEBUG_UTIL); |
255 | | |
256 | | /* Read next record from timing file. */ |
257 | 4.42k | if (iolog_gets(iol, line, sizeof(line), &errstr) == NULL) { |
258 | | /* EOF or error reading timing file, we are done. */ |
259 | 87 | if (iolog_eof(iol)) |
260 | 87 | 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 | 4.33k | line[strcspn(line, "\n")] = '\0'; |
267 | 4.33k | if (!iolog_parse_timing(line, timing)) { |
268 | 756 | sudo_warnx(U_("invalid timing file line: %s"), line); |
269 | 756 | debug_return_int(-1); |
270 | 756 | } |
271 | | |
272 | 3.57k | debug_return_int(0); |
273 | 3.57k | } |