Coverage Report

Created: 2025-10-10 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
3.14k
{
92
3.14k
    char numbuf[STRLEN_MAX_SIGNED(long long) + 1];
93
3.14k
    const char *errstr, *ep;
94
3.14k
    long long llval;
95
3.14k
    size_t len;
96
3.14k
    debug_decl(iolog_parse_delay, SUDO_DEBUG_UTIL);
97
98
    /* Parse seconds (whole number portion). */
99
12.1k
    for (ep = cp; isdigit((unsigned char)*ep); ep++)
100
12.1k
  continue;
101
3.14k
    len = (size_t)(ep - cp);
102
3.14k
    if (len >= sizeof(numbuf)) {
103
10
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
104
10
      "%s: number of seconds is too large", cp);
105
10
  debug_return_ptr(NULL);
106
10
    }
107
3.13k
    memcpy(numbuf, cp, len);
108
3.13k
    numbuf[len] = '\0';
109
3.13k
    delay->tv_sec = (time_t)sudo_strtonum(numbuf, 0, TIME_T_MAX, &errstr);
110
3.13k
    if (errstr != NULL) {
111
97
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
112
97
      "%s: number of seconds is %s", numbuf, errstr);
113
97
  debug_return_ptr(NULL);
114
97
    }
115
116
    /* Radix may be in user's locale for sudo < 1.7.4 so accept that too. */
117
3.04k
    if (*ep != '.' && *ep != *decimal_point) {
118
1.95k
  if (*ep == '\0' || isspace((unsigned char)*ep)) {
119
      /* No fractional part. */
120
1.94k
      delay->tv_nsec = 0;
121
1.94k
      goto done;
122
1.94k
  }
123
1.95k
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
124
16
      "invalid characters after seconds: %s", ep);
125
16
  debug_return_ptr(NULL);
126
16
    }
127
1.08k
    cp = ep + 1;
128
129
    /* Parse fractional part, we may read more precision than we can store. */
130
11.0k
    for (ep = cp; isdigit((unsigned char)*ep); ep++)
131
11.0k
  continue;
132
1.08k
    len = (size_t)(ep - cp);
133
1.08k
    if (len >= sizeof(numbuf)) {
134
12
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
135
12
      "%s: number of nanoseconds is too large", cp);
136
12
  debug_return_ptr(NULL);
137
12
    }
138
1.07k
    memcpy(numbuf, cp, len);
139
1.07k
    numbuf[len] = '\0';
140
1.07k
    llval = sudo_strtonum(numbuf, 0, LLONG_MAX, &errstr);
141
1.07k
    if (errstr != NULL) {
142
13
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
143
13
      "%s: number of nanoseconds is %s", numbuf, errstr);
144
13
  debug_return_ptr(NULL);
145
13
    }
146
147
    /* Adjust fractional part to nanosecond precision. */
148
1.06k
    if (len < 9) {
149
  /* Convert to nanosecond precision. */
150
4.31k
  do {
151
4.31k
      llval *= 10;
152
4.31k
  } while (++len < 9);
153
543
    } else if (len > 9) {
154
  /* Clamp to nanoseconds. */
155
1.43k
  do {
156
1.43k
      llval /= 10;
157
1.43k
  } while (--len > 9);
158
323
    }
159
1.06k
    delay->tv_nsec = (long)llval;
160
161
3.00k
done:
162
    /* Advance to the next field. */
163
3.00k
    while (isspace((unsigned char)*ep))
164
2.98k
  ep++;
165
166
3.00k
    debug_return_str((char *)ep);
167
3.00k
}
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
3.31k
{
181
3.31k
    unsigned long ulval;
182
3.31k
    char *cp, *ep;
183
3.31k
    debug_decl(iolog_parse_timing, SUDO_DEBUG_UTIL);
184
185
    /* Clear iolog descriptor. */
186
3.31k
    timing->iol = NULL;
187
188
    /* Parse event type. */
189
3.31k
    ulval = strtoul(line, &ep, 10);
190
3.31k
    if (ep == line || !isspace((unsigned char) *ep))
191
49
  goto bad;
192
3.26k
    if (ulval >= IO_EVENT_COUNT)
193
114
  goto bad;
194
3.14k
    if (ulval == IO_EVENT_TTYOUT_1_8_7) {
195
  /* work around a bug in timing files generated by sudo 1.8.7 */
196
738
  timing_event_adj = 2;
197
738
    }
198
3.14k
    timing->event = (int)ulval - timing_event_adj;
199
3.14k
    for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
200
198
  continue;
201
202
    /* Parse delay, returns the next field or NULL on error. */
203
3.14k
    if ((cp = iolog_parse_delay(cp, &timing->delay, timing->decimal)) == NULL)
204
148
  goto bad;
205
206
3.00k
    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
482
    case IO_EVENT_WINSIZE:
213
482
  ulval = strtoul(cp, &ep, 10);
214
482
  if (ep == cp || !isspace((unsigned char) *ep))
215
22
      goto bad;
216
460
  if (ulval > INT_MAX)
217
65
      goto bad;
218
395
  timing->u.winsize.lines = (int)ulval;
219
395
  for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
220
194
      continue;
221
222
395
  ulval = strtoul(cp, &ep, 10);
223
395
  if (ep == cp || *ep != '\0')
224
77
      goto bad;
225
318
  if (ulval > INT_MAX)
226
64
      goto bad;
227
254
  timing->u.winsize.cols = (int)ulval;
228
254
  break;
229
2.51k
    default:
230
2.51k
  errno = 0;
231
2.51k
  ulval = strtoul(cp, &ep, 10);
232
2.51k
  if (ep == cp || *ep != '\0')
233
205
      goto bad;
234
  /* Note: assumes SIZE_MAX == ULONG_MAX */
235
2.31k
  if (errno == ERANGE && ulval == ULONG_MAX)
236
1
      goto bad;
237
2.31k
  timing->u.nbytes = (size_t)ulval;
238
2.31k
  break;
239
3.00k
    }
240
241
2.56k
    debug_return_bool(true);
242
745
bad:
243
745
    debug_return_bool(false);
244
745
}
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.39k
{
253
3.39k
    char line[LINE_MAX];
254
3.39k
    const char *errstr;
255
3.39k
    debug_decl(iolog_read_timing_record, SUDO_DEBUG_UTIL);
256
257
    /* Read next record from timing file. */
258
3.39k
    if (iolog_gets(iol, line, sizeof(line), &errstr) == NULL) {
259
  /* EOF or error reading timing file, we are done. */
260
80
  if (iolog_eof(iol))
261
80
      debug_return_int(1);
262
0
  sudo_warnx(U_("error reading timing file: %s"), errstr);
263
0
  debug_return_int(-1);
264
0
    }
265
266
    /* Parse timing file record. */
267
3.31k
    line[strcspn(line, "\n")] = '\0';
268
3.31k
    if (!iolog_parse_timing(line, timing)) {
269
745
  sudo_warnx(U_("invalid timing file line: %s"), line);
270
745
  debug_return_int(-1);
271
745
    }
272
273
2.56k
    debug_return_int(0);
274
2.56k
}