Coverage Report

Created: 2025-07-11 06:58

/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.94k
{
91
3.94k
    char numbuf[STRLEN_MAX_SIGNED(long long) + 1];
92
3.94k
    const char *errstr, *ep;
93
3.94k
    long long llval;
94
3.94k
    size_t len;
95
3.94k
    debug_decl(iolog_parse_delay, SUDO_DEBUG_UTIL);
96
97
    /* Parse seconds (whole number portion). */
98
12.9k
    for (ep = cp; isdigit((unsigned char)*ep); ep++)
99
12.9k
  continue;
100
3.94k
    len = (size_t)(ep - cp);
101
3.94k
    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.93k
    memcpy(numbuf, cp, len);
107
3.93k
    numbuf[len] = '\0';
108
3.93k
    delay->tv_sec = (time_t)sudo_strtonum(numbuf, 0, TIME_T_MAX, &errstr);
109
3.93k
    if (errstr != NULL) {
110
80
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
111
80
      "%s: number of seconds is %s", numbuf, errstr);
112
80
  debug_return_ptr(NULL);
113
80
    }
114
115
    /* Radix may be in user's locale for sudo < 1.7.4 so accept that too. */
116
3.85k
    if (*ep != '.' && *ep != *decimal_point) {
117
1.93k
  if (*ep == '\0' || isspace((unsigned char)*ep)) {
118
      /* No fractional part. */
119
1.91k
      delay->tv_nsec = 0;
120
1.91k
      goto done;
121
1.91k
  }
122
1.93k
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
123
18
      "invalid characters after seconds: %s", ep);
124
18
  debug_return_ptr(NULL);
125
18
    }
126
1.91k
    cp = ep + 1;
127
128
    /* Parse fractional part, we may read more precision than we can store. */
129
18.5k
    for (ep = cp; isdigit((unsigned char)*ep); ep++)
130
18.5k
  continue;
131
1.91k
    len = (size_t)(ep - cp);
132
1.91k
    if (len >= sizeof(numbuf)) {
133
8
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
134
8
      "%s: number of nanoseconds is too large", cp);
135
8
  debug_return_ptr(NULL);
136
8
    }
137
1.91k
    memcpy(numbuf, cp, len);
138
1.91k
    numbuf[len] = '\0';
139
1.91k
    llval = sudo_strtonum(numbuf, 0, LLONG_MAX, &errstr);
140
1.91k
    if (errstr != NULL) {
141
14
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
142
14
      "%s: number of nanoseconds is %s", numbuf, errstr);
143
14
  debug_return_ptr(NULL);
144
14
    }
145
146
    /* Adjust fractional part to nanosecond precision. */
147
1.89k
    if (len < 9) {
148
  /* Convert to nanosecond precision. */
149
4.34k
  do {
150
4.34k
      llval *= 10;
151
4.34k
  } while (++len < 9);
152
1.32k
    } else if (len > 9) {
153
  /* Clamp to nanoseconds. */
154
1.84k
  do {
155
1.84k
      llval /= 10;
156
1.84k
  } while (--len > 9);
157
469
    }
158
1.89k
    delay->tv_nsec = (long)llval;
159
160
3.81k
done:
161
    /* Advance to the next field. */
162
3.81k
    while (isspace((unsigned char)*ep))
163
3.72k
  ep++;
164
165
3.81k
    debug_return_str((char *)ep);
166
3.81k
}
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.08k
{
180
4.08k
    unsigned long ulval;
181
4.08k
    char *cp, *ep;
182
4.08k
    debug_decl(iolog_parse_timing, SUDO_DEBUG_UTIL);
183
184
    /* Clear iolog descriptor. */
185
4.08k
    timing->iol = NULL;
186
187
    /* Parse event type. */
188
4.08k
    ulval = strtoul(line, &ep, 10);
189
4.08k
    if (ep == line || !isspace((unsigned char) *ep))
190
44
  goto bad;
191
4.03k
    if (ulval >= IO_EVENT_COUNT)
192
98
  goto bad;
193
3.94k
    if (ulval == IO_EVENT_TTYOUT_1_8_7) {
194
  /* work around a bug in timing files generated by sudo 1.8.7 */
195
793
  timing_event_adj = 2;
196
793
    }
197
3.94k
    timing->event = (int)ulval - timing_event_adj;
198
3.94k
    for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
199
254
  continue;
200
201
    /* Parse delay, returns the next field or NULL on error. */
202
3.94k
    if ((cp = iolog_parse_delay(cp, &timing->delay, timing->decimal)) == NULL)
203
131
  goto bad;
204
205
3.81k
    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
458
    case IO_EVENT_WINSIZE:
212
458
  ulval = strtoul(cp, &ep, 10);
213
458
  if (ep == cp || !isspace((unsigned char) *ep))
214
38
      goto bad;
215
420
  if (ulval > INT_MAX)
216
52
      goto bad;
217
368
  timing->u.winsize.lines = (int)ulval;
218
368
  for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
219
198
      continue;
220
221
368
  ulval = strtoul(cp, &ep, 10);
222
368
  if (ep == cp || *ep != '\0')
223
67
      goto bad;
224
301
  if (ulval > INT_MAX)
225
52
      goto bad;
226
249
  timing->u.winsize.cols = (int)ulval;
227
249
  break;
228
3.35k
    default:
229
3.35k
  errno = 0;
230
3.35k
  ulval = strtoul(cp, &ep, 10);
231
3.35k
  if (ep == cp || *ep != '\0')
232
169
      goto bad;
233
  /* Note: assumes SIZE_MAX == ULONG_MAX */
234
3.18k
  if (errno == ERANGE && ulval == ULONG_MAX)
235
1
      goto bad;
236
3.18k
  timing->u.nbytes = (size_t)ulval;
237
3.18k
  break;
238
3.81k
    }
239
240
3.43k
    debug_return_bool(true);
241
652
bad:
242
652
    debug_return_bool(false);
243
652
}
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.15k
{
252
4.15k
    char line[LINE_MAX];
253
4.15k
    const char *errstr;
254
4.15k
    debug_decl(iolog_read_timing_record, SUDO_DEBUG_UTIL);
255
256
    /* Read next record from timing file. */
257
4.15k
    if (iolog_gets(iol, line, sizeof(line), &errstr) == NULL) {
258
  /* EOF or error reading timing file, we are done. */
259
72
  if (iolog_eof(iol))
260
72
      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.08k
    line[strcspn(line, "\n")] = '\0';
267
4.08k
    if (!iolog_parse_timing(line, timing)) {
268
652
  sudo_warnx(U_("invalid timing file line: %s"), line);
269
652
  debug_return_int(-1);
270
652
    }
271
272
3.43k
    debug_return_int(0);
273
3.43k
}