Coverage Report

Created: 2026-08-13 06:32

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
void
46
iolog_adjust_delay(struct timespec *delay, struct timespec *max_delay,
47
     double scale_factor)
48
0
{
49
0
    debug_decl(iolog_adjust_delay, SUDO_DEBUG_UTIL);
50
51
    /* Avoid division by zero or negative delays. */
52
0
    if (scale_factor <= 0.0) {
53
0
  sudo_timespecclear(delay);
54
0
  debug_return;
55
0
    }
56
57
0
    if (scale_factor != 1.0) {
58
  /* Order is important: we don't want to double the remainder. */
59
0
  const double seconds = (double)delay->tv_sec / scale_factor;
60
0
  const double nseconds = (double)delay->tv_nsec / scale_factor;
61
0
        delay->tv_sec = (time_t)seconds;
62
0
        delay->tv_nsec = (long)nseconds;
63
0
        delay->tv_nsec += (long)((seconds - (double)delay->tv_sec) * 1000000000);
64
0
        while (delay->tv_nsec >= 1000000000) {
65
0
            delay->tv_sec++;
66
0
            delay->tv_nsec -= 1000000000;
67
0
        }
68
0
    }
69
70
    /* Clamp to max delay. */
71
0
    if (max_delay != NULL) {
72
0
  if (sudo_timespeccmp(delay, max_delay, >)) {
73
0
      delay->tv_sec = max_delay->tv_sec;
74
0
      delay->tv_nsec = max_delay->tv_nsec;
75
0
  }
76
0
    }
77
78
0
    debug_return;
79
0
}
80
81
/*
82
 * Parse the delay as seconds and nanoseconds: %lld.%09ld
83
 * Sudo used to write this as a double, but since timing data is logged
84
 * in the C locale this may not match the current locale.
85
 */
86
char *
87
iolog_parse_delay(const char *cp, struct timespec *delay,
88
    const char *decimal_point)
89
3.84k
{
90
3.84k
    char numbuf[STRLEN_MAX_SIGNED(long long) + 1];
91
3.84k
    const char *errstr, *ep;
92
3.84k
    long long llval;
93
3.84k
    size_t len;
94
3.84k
    debug_decl(iolog_parse_delay, SUDO_DEBUG_UTIL);
95
96
    /* Parse seconds (whole number portion). */
97
10.2k
    for (ep = cp; isdigit((unsigned char)*ep); ep++)
98
10.2k
  continue;
99
3.84k
    len = (size_t)(ep - cp);
100
3.84k
    if (len >= sizeof(numbuf)) {
101
9
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
102
9
      "%s: number of seconds is too large", cp);
103
9
  debug_return_ptr(NULL);
104
9
    }
105
3.83k
    memcpy(numbuf, cp, len);
106
3.83k
    numbuf[len] = '\0';
107
3.83k
    delay->tv_sec = (time_t)sudo_strtonum(numbuf, 0, TIME_T_MAX, &errstr);
108
3.83k
    if (errstr != NULL) {
109
79
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
110
79
      "%s: number of seconds is %s", numbuf, errstr);
111
79
  debug_return_ptr(NULL);
112
79
    }
113
114
    /* Radix may be in user's locale for sudo < 1.7.4 so accept that too. */
115
3.75k
    if (*ep != '.' && *ep != *decimal_point) {
116
3.03k
  if (*ep == '\0' || isspace((unsigned char)*ep)) {
117
      /* No fractional part. */
118
3.02k
      delay->tv_nsec = 0;
119
3.02k
      goto done;
120
3.02k
  }
121
3.03k
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
122
17
      "invalid characters after seconds: %s", ep);
123
17
  debug_return_ptr(NULL);
124
17
    }
125
721
    cp = ep + 1;
126
127
    /* Parse fractional part, we may read more precision than we can store. */
128
8.60k
    for (ep = cp; isdigit((unsigned char)*ep); ep++)
129
8.60k
  continue;
130
721
    len = (size_t)(ep - cp);
131
721
    if (len >= sizeof(numbuf)) {
132
9
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
133
9
      "%s: number of nanoseconds is too large", cp);
134
9
  debug_return_ptr(NULL);
135
9
    }
136
712
    memcpy(numbuf, cp, len);
137
712
    numbuf[len] = '\0';
138
712
    llval = sudo_strtonum(numbuf, 0, LLONG_MAX, &errstr);
139
712
    if (errstr != NULL) {
140
9
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
141
9
      "%s: number of nanoseconds is %s", numbuf, errstr);
142
9
  debug_return_ptr(NULL);
143
9
    }
144
145
    /* Adjust fractional part to nanosecond precision. */
146
703
    if (len < 9) {
147
  /* Convert to nanosecond precision. */
148
2.27k
  do {
149
2.27k
      llval *= 10;
150
2.27k
  } while (++len < 9);
151
415
    } else if (len > 9) {
152
  /* Clamp to nanoseconds. */
153
432
  do {
154
432
      llval /= 10;
155
432
  } while (--len > 9);
156
221
    }
157
703
    delay->tv_nsec = (long)llval;
158
159
3.72k
done:
160
    /* Advance to the next field. */
161
3.72k
    while (isspace((unsigned char)*ep))
162
3.71k
  ep++;
163
164
3.72k
    debug_return_str((char *)ep);
165
3.72k
}
166
167
/*
168
 * Parse a timing line, which is formatted as:
169
 *  IO_EVENT_TTYOUT sleep_time num_bytes
170
 *  IO_EVENT_WINSIZE sleep_time lines cols
171
 *  IO_EVENT_SUSPEND sleep_time signo
172
 * Where type is IO_EVENT_*, sleep_time is the number of seconds to sleep
173
 * before writing the data and num_bytes is the number of bytes to output.
174
 * Returns true on success and false on failure.
175
 */
176
bool
177
iolog_parse_timing(const char *line, struct timing_closure *timing)
178
3.98k
{
179
3.98k
    unsigned long ulval;
180
3.98k
    char *cp, *ep;
181
3.98k
    debug_decl(iolog_parse_timing, SUDO_DEBUG_UTIL);
182
183
    /* Clear iolog descriptor. */
184
3.98k
    timing->iol = NULL;
185
186
    /* Parse event type. */
187
3.98k
    ulval = strtoul(line, &ep, 10);
188
3.98k
    if (ep == line || !isspace((unsigned char) *ep))
189
10
  goto bad;
190
3.97k
    if (ulval == IO_EVENT_TTYOUT_1_8_7) {
191
  /* Sudo 1.8.7 timing files event indexes are off by two. */
192
1
  goto bad;
193
1
    }
194
3.97k
    if (ulval >= IO_EVENT_COUNT)
195
130
  goto bad;
196
3.84k
    timing->event = (int)ulval;
197
3.84k
    for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
198
208
  continue;
199
200
    /* Parse delay, returns the next field or NULL on error. */
201
3.84k
    if ((cp = iolog_parse_delay(cp, &timing->delay, timing->decimal)) == NULL)
202
123
  goto bad;
203
204
3.72k
    switch (timing->event) {
205
1.93k
    case IO_EVENT_SUSPEND:
206
  /* Signal name (no leading SIG prefix) or number. */
207
1.93k
  if (str2sig(cp, &timing->u.signo) == -1)
208
199
      goto bad;
209
1.73k
  break;
210
1.73k
    case IO_EVENT_WINSIZE:
211
474
  ulval = strtoul(cp, &ep, 10);
212
474
  if (ep == cp || !isspace((unsigned char) *ep))
213
23
      goto bad;
214
451
  if (ulval > INT_MAX)
215
64
      goto bad;
216
387
  timing->u.winsize.lines = (int)ulval;
217
387
  for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
218
194
      continue;
219
220
387
  ulval = strtoul(cp, &ep, 10);
221
387
  if (ep == cp || *ep != '\0')
222
70
      goto bad;
223
317
  if (ulval > INT_MAX)
224
64
      goto bad;
225
253
  timing->u.winsize.cols = (int)ulval;
226
253
  break;
227
1.32k
    default:
228
1.32k
  errno = 0;
229
1.32k
  ulval = strtoul(cp, &ep, 10);
230
1.32k
  if (ep == cp || *ep != '\0')
231
161
      goto bad;
232
  /* Note: assumes SIZE_MAX == ULONG_MAX */
233
1.15k
  if (errno == ERANGE && ulval == ULONG_MAX)
234
1
      goto bad;
235
1.15k
  timing->u.nbytes = (size_t)ulval;
236
1.15k
  break;
237
3.72k
    }
238
239
3.14k
    debug_return_bool(true);
240
846
bad:
241
846
    debug_return_bool(false);
242
846
}
243
244
/*
245
 * Read the next record from the timing file.
246
 * Return 0 on success, 1 on EOF and -1 on error.
247
 */
248
int
249
iolog_read_timing_record(struct iolog_file *iol, struct timing_closure *timing)
250
4.19k
{
251
4.19k
    char line[LINE_MAX];
252
4.19k
    const char *errstr;
253
4.19k
    char *nl;
254
4.19k
    debug_decl(iolog_read_timing_record, SUDO_DEBUG_UTIL);
255
256
    /* Read next record from timing file. */
257
4.19k
    if (iolog_gets(iol, line, sizeof(line), &errstr) == NULL) {
258
  /* EOF or error reading timing file, we are done. */
259
165
  if (iolog_eof(iol))
260
165
      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
    /*
266
     * All timing file records must end with a newline.
267
     * A missing newline may indicate a line longer than LINE_MAX.
268
     */
269
4.03k
    nl = strchr(line, '\n');
270
4.03k
    if (nl == NULL) {
271
46
  goto invalid;
272
46
    }
273
3.98k
    *nl = '\0';
274
275
    /* Parse timing file record. */
276
3.98k
    if (!iolog_parse_timing(line, timing)) {
277
846
  goto invalid;
278
846
    }
279
280
3.14k
    debug_return_int(0);
281
892
invalid:
282
    /* Truncate invalid timing file line at 128 bytes. */
283
892
    line[128] = '\0';
284
892
    sudo_warnx(U_("invalid timing file line: %s"), line);
285
892
    debug_return_int(-1);
286
892
}