/src/sudo/lib/iolog/iolog_timing.c
Line  | Count  | Source  | 
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  |  | #include <config.h>  | 
20  |  |  | 
21  |  | #include <stdio.h>  | 
22  |  | #include <stdlib.h>  | 
23  |  | #ifdef HAVE_STDBOOL_H  | 
24  |  | # include <stdbool.h>  | 
25  |  | #else  | 
26  |  | # include <compat/stdbool.h>  | 
27  |  | #endif /* HAVE_STDBOOL_H */  | 
28  |  | #include <string.h>  | 
29  |  | #include <signal.h>  | 
30  |  | #include <unistd.h>  | 
31  |  | #include <ctype.h>  | 
32  |  | #include <errno.h>  | 
33  |  | #include <limits.h>  | 
34  |  | #include <fcntl.h>  | 
35  |  | #include <time.h>  | 
36  |  |  | 
37  |  | #include <sudo_compat.h>  | 
38  |  | #include <sudo_debug.h>  | 
39  |  | #include <sudo_eventlog.h>  | 
40  |  | #include <sudo_fatal.h>  | 
41  |  | #include <sudo_gettext.h>  | 
42  |  | #include <sudo_iolog.h>  | 
43  |  | #include <sudo_util.h>  | 
44  |  |  | 
45  |  | static int timing_event_adj;  | 
46  |  |  | 
47  |  | void  | 
48  |  | iolog_adjust_delay(struct timespec *delay, struct timespec *max_delay,  | 
49  |  |      double scale_factor)  | 
50  | 0  | { | 
51  | 0  |     debug_decl(iolog_adjust_delay, SUDO_DEBUG_UTIL);  | 
52  |  |  | 
53  |  |     /* Avoid division by zero or negative delays. */  | 
54  | 0  |     if (scale_factor <= 0.0) { | 
55  | 0  |   sudo_timespecclear(delay);  | 
56  | 0  |   debug_return;  | 
57  | 0  |     }  | 
58  |  |  | 
59  | 0  |     if (scale_factor != 1.0) { | 
60  |  |   /* Order is important: we don't want to double the remainder. */  | 
61  | 0  |   const double seconds = (double)delay->tv_sec / scale_factor;  | 
62  | 0  |   const double nseconds = (double)delay->tv_nsec / scale_factor;  | 
63  | 0  |         delay->tv_sec = (time_t)seconds;  | 
64  | 0  |         delay->tv_nsec = (long)nseconds;  | 
65  | 0  |         delay->tv_nsec += (long)((seconds - (double)delay->tv_sec) * 1000000000);  | 
66  | 0  |         while (delay->tv_nsec >= 1000000000) { | 
67  | 0  |             delay->tv_sec++;  | 
68  | 0  |             delay->tv_nsec -= 1000000000;  | 
69  | 0  |         }  | 
70  | 0  |     }  | 
71  |  |  | 
72  |  |     /* Clamp to max delay. */  | 
73  | 0  |     if (max_delay != NULL) { | 
74  | 0  |   if (sudo_timespeccmp(delay, max_delay, >)) { | 
75  | 0  |       delay->tv_sec = max_delay->tv_sec;  | 
76  | 0  |       delay->tv_nsec = max_delay->tv_nsec;  | 
77  | 0  |   }  | 
78  | 0  |     }  | 
79  |  | 
  | 
80  | 0  |     debug_return;  | 
81  | 0  | }  | 
82  |  |  | 
83  |  | /*  | 
84  |  |  * Parse the delay as seconds and nanoseconds: %lld.%09ld  | 
85  |  |  * Sudo used to write this as a double, but since timing data is logged  | 
86  |  |  * in the C locale this may not match the current locale.  | 
87  |  |  */  | 
88  |  | char *  | 
89  |  | iolog_parse_delay(const char *cp, struct timespec *delay,  | 
90  |  |     const char *decimal_point)  | 
91  | 2.79k  | { | 
92  | 2.79k  |     char numbuf[STRLEN_MAX_SIGNED(long long) + 1];  | 
93  | 2.79k  |     const char *errstr, *ep;  | 
94  | 2.79k  |     long long llval;  | 
95  | 2.79k  |     size_t len;  | 
96  | 2.79k  |     debug_decl(iolog_parse_delay, SUDO_DEBUG_UTIL);  | 
97  |  |  | 
98  |  |     /* Parse seconds (whole number portion). */  | 
99  | 11.6k  |     for (ep = cp; isdigit((unsigned char)*ep); ep++)  | 
100  | 11.6k  |   continue;  | 
101  | 2.79k  |     len = (size_t)(ep - cp);  | 
102  | 2.79k  |     if (len >= sizeof(numbuf)) { | 
103  | 9  |   sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
104  | 9  |       "%s: number of seconds is too large", cp);  | 
105  | 9  |   debug_return_ptr(NULL);  | 
106  | 9  |     }  | 
107  | 2.78k  |     memcpy(numbuf, cp, len);  | 
108  | 2.78k  |     numbuf[len] = '\0';  | 
109  | 2.78k  |     delay->tv_sec = (time_t)sudo_strtonum(numbuf, 0, TIME_T_MAX, &errstr);  | 
110  | 2.78k  |     if (errstr != NULL) { | 
111  | 96  |   sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
112  | 96  |       "%s: number of seconds is %s", numbuf, errstr);  | 
113  | 96  |   debug_return_ptr(NULL);  | 
114  | 96  |     }  | 
115  |  |  | 
116  |  |     /* Radix may be in user's locale for sudo < 1.7.4 so accept that too. */  | 
117  | 2.69k  |     if (*ep != '.' && *ep != *decimal_point) { | 
118  | 1.84k  |   if (*ep == '\0' || isspace((unsigned char)*ep)) { | 
119  |  |       /* No fractional part. */  | 
120  | 1.83k  |       delay->tv_nsec = 0;  | 
121  | 1.83k  |       goto done;  | 
122  | 1.83k  |   }  | 
123  | 1.84k  |   sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
124  | 15  |       "invalid characters after seconds: %s", ep);  | 
125  | 15  |   debug_return_ptr(NULL);  | 
126  | 15  |     }  | 
127  | 845  |     cp = ep + 1;  | 
128  |  |  | 
129  |  |     /* Parse fractional part, we may read more precision than we can store. */  | 
130  | 6.58k  |     for (ep = cp; isdigit((unsigned char)*ep); ep++)  | 
131  | 6.58k  |   continue;  | 
132  | 845  |     len = (size_t)(ep - cp);  | 
133  | 845  |     if (len >= sizeof(numbuf)) { | 
134  | 3  |   sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
135  | 3  |       "%s: number of nanoseconds is too large", cp);  | 
136  | 3  |   debug_return_ptr(NULL);  | 
137  | 3  |     }  | 
138  | 842  |     memcpy(numbuf, cp, len);  | 
139  | 842  |     numbuf[len] = '\0';  | 
140  | 842  |     llval = sudo_strtonum(numbuf, 0, LLONG_MAX, &errstr);  | 
141  | 842  |     if (errstr != NULL) { | 
142  | 6  |   sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
143  | 6  |       "%s: number of nanoseconds is %s", numbuf, errstr);  | 
144  | 6  |   debug_return_ptr(NULL);  | 
145  | 6  |     }  | 
146  |  |  | 
147  |  |     /* Adjust fractional part to nanosecond precision. */  | 
148  | 836  |     if (len < 9) { | 
149  |  |   /* Convert to nanosecond precision. */  | 
150  | 2.53k  |   do { | 
151  | 2.53k  |       llval *= 10;  | 
152  | 2.53k  |   } while (++len < 9);  | 
153  | 515  |     } else if (len > 9) { | 
154  |  |   /* Clamp to nanoseconds. */  | 
155  | 1.41k  |   do { | 
156  | 1.41k  |       llval /= 10;  | 
157  | 1.41k  |   } while (--len > 9);  | 
158  | 321  |     }  | 
159  | 836  |     delay->tv_nsec = (long)llval;  | 
160  |  |  | 
161  | 2.66k  | done:  | 
162  |  |     /* Advance to the next field. */  | 
163  | 2.66k  |     while (isspace((unsigned char)*ep))  | 
164  | 2.63k  |   ep++;  | 
165  |  |  | 
166  | 2.66k  |     debug_return_str((char *)ep);  | 
167  | 2.66k  | }  | 
168  |  |  | 
169  |  | /*  | 
170  |  |  * Parse a timing line, which is formatted as:  | 
171  |  |  *  IO_EVENT_TTYOUT sleep_time num_bytes  | 
172  |  |  *  IO_EVENT_WINSIZE sleep_time lines cols  | 
173  |  |  *  IO_EVENT_SUSPEND sleep_time signo  | 
174  |  |  * Where type is IO_EVENT_*, sleep_time is the number of seconds to sleep  | 
175  |  |  * before writing the data and num_bytes is the number of bytes to output.  | 
176  |  |  * Returns true on success and false on failure.  | 
177  |  |  */  | 
178  |  | bool  | 
179  |  | iolog_parse_timing(const char *line, struct timing_closure *timing)  | 
180  | 2.91k  | { | 
181  | 2.91k  |     unsigned long ulval;  | 
182  | 2.91k  |     char *cp, *ep;  | 
183  | 2.91k  |     debug_decl(iolog_parse_timing, SUDO_DEBUG_UTIL);  | 
184  |  |  | 
185  |  |     /* Clear iolog descriptor. */  | 
186  | 2.91k  |     timing->iol = NULL;  | 
187  |  |  | 
188  |  |     /* Parse event type. */  | 
189  | 2.91k  |     ulval = strtoul(line, &ep, 10);  | 
190  | 2.91k  |     if (ep == line || !isspace((unsigned char) *ep))  | 
191  | 10  |   goto bad;  | 
192  | 2.90k  |     if (ulval >= IO_EVENT_COUNT)  | 
193  | 112  |   goto bad;  | 
194  | 2.79k  |     if (ulval == IO_EVENT_TTYOUT_1_8_7) { | 
195  |  |   /* work around a bug in timing files generated by sudo 1.8.7 */  | 
196  | 517  |   timing_event_adj = 2;  | 
197  | 517  |     }  | 
198  | 2.79k  |     timing->event = (int)ulval - timing_event_adj;  | 
199  | 2.79k  |     for (cp = ep + 1; isspace((unsigned char) *cp); cp++)  | 
200  | 196  |   continue;  | 
201  |  |  | 
202  |  |     /* Parse delay, returns the next field or NULL on error. */  | 
203  | 2.79k  |     if ((cp = iolog_parse_delay(cp, &timing->delay, timing->decimal)) == NULL)  | 
204  | 129  |   goto bad;  | 
205  |  |  | 
206  | 2.66k  |     switch (timing->event) { | 
207  | 0  |     case IO_EVENT_SUSPEND:  | 
208  |  |   /* Signal name (no leading SIG prefix) or number. */  | 
209  | 0  |   if (str2sig(cp, &timing->u.signo) == -1)  | 
210  | 0  |       goto bad;  | 
211  | 0  |   break;  | 
212  | 483  |     case IO_EVENT_WINSIZE:  | 
213  | 483  |   ulval = strtoul(cp, &ep, 10);  | 
214  | 483  |   if (ep == cp || !isspace((unsigned char) *ep))  | 
215  | 30  |       goto bad;  | 
216  | 453  |   if (ulval > INT_MAX)  | 
217  | 63  |       goto bad;  | 
218  | 390  |   timing->u.winsize.lines = (int)ulval;  | 
219  | 390  |   for (cp = ep + 1; isspace((unsigned char) *cp); cp++)  | 
220  | 194  |       continue;  | 
221  |  |  | 
222  | 390  |   ulval = strtoul(cp, &ep, 10);  | 
223  | 390  |   if (ep == cp || *ep != '\0')  | 
224  | 75  |       goto bad;  | 
225  | 315  |   if (ulval > INT_MAX)  | 
226  | 64  |       goto bad;  | 
227  | 251  |   timing->u.winsize.cols = (int)ulval;  | 
228  | 251  |   break;  | 
229  | 2.18k  |     default:  | 
230  | 2.18k  |   errno = 0;  | 
231  | 2.18k  |   ulval = strtoul(cp, &ep, 10);  | 
232  | 2.18k  |   if (ep == cp || *ep != '\0')  | 
233  | 214  |       goto bad;  | 
234  |  |   /* Note: assumes SIZE_MAX == ULONG_MAX */  | 
235  | 1.97k  |   if (errno == ERANGE && ulval == ULONG_MAX)  | 
236  | 1  |       goto bad;  | 
237  | 1.97k  |   timing->u.nbytes = (size_t)ulval;  | 
238  | 1.97k  |   break;  | 
239  | 2.66k  |     }  | 
240  |  |  | 
241  | 2.22k  |     debug_return_bool(true);  | 
242  | 698  | bad:  | 
243  | 698  |     debug_return_bool(false);  | 
244  | 698  | }  | 
245  |  |  | 
246  |  | /*  | 
247  |  |  * Read the next record from the timing file.  | 
248  |  |  * Return 0 on success, 1 on EOF and -1 on error.  | 
249  |  |  */  | 
250  |  | int  | 
251  |  | iolog_read_timing_record(struct iolog_file *iol, struct timing_closure *timing)  | 
252  | 3.04k  | { | 
253  | 3.04k  |     char line[LINE_MAX];  | 
254  | 3.04k  |     const char *errstr;  | 
255  | 3.04k  |     char *nl;  | 
256  | 3.04k  |     debug_decl(iolog_read_timing_record, SUDO_DEBUG_UTIL);  | 
257  |  |  | 
258  |  |     /* Read next record from timing file. */  | 
259  | 3.04k  |     if (iolog_gets(iol, line, sizeof(line), &errstr) == NULL) { | 
260  |  |   /* EOF or error reading timing file, we are done. */  | 
261  | 77  |   if (iolog_eof(iol))  | 
262  | 77  |       debug_return_int(1);  | 
263  | 0  |   sudo_warnx(U_("error reading timing file: %s"), errstr); | 
264  | 0  |   debug_return_int(-1);  | 
265  | 0  |     }  | 
266  |  |  | 
267  |  |     /*  | 
268  |  |      * All timing file records must end with a newline.  | 
269  |  |      * A missing newline may indicate a line longer than LINE_MAX.  | 
270  |  |      */  | 
271  | 2.96k  |     nl = strchr(line, '\n');  | 
272  | 2.96k  |     if (nl == NULL) { | 
273  | 46  |   goto invalid;  | 
274  | 46  |     }  | 
275  | 2.91k  |     *nl = '\0';  | 
276  |  |  | 
277  |  |     /* Parse timing file record. */  | 
278  | 2.91k  |     if (!iolog_parse_timing(line, timing)) { | 
279  | 698  |   goto invalid;  | 
280  | 698  |     }  | 
281  |  |  | 
282  | 2.22k  |     debug_return_int(0);  | 
283  | 744  | invalid:  | 
284  |  |     /* Truncate invalid timing file line at 128 bytes. */  | 
285  | 744  |     line[128] = '\0';  | 
286  | 744  |     sudo_warnx(U_("invalid timing file line: %s"), line); | 
287  | 744  |     debug_return_int(-1);  | 
288  | 744  | }  |