Coverage Report

Created: 2026-08-14 06:41

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.87k
{
90
3.87k
    char numbuf[STRLEN_MAX_SIGNED(long long) + 1];
91
3.87k
    const char *errstr, *ep;
92
3.87k
    long long llval;
93
3.87k
    size_t len;
94
3.87k
    debug_decl(iolog_parse_delay, SUDO_DEBUG_UTIL);
95
96
    /* Parse seconds (whole number portion). */
97
10.3k
    for (ep = cp; isdigit((unsigned char)*ep); ep++)
98
10.3k
  continue;
99
3.87k
    len = (size_t)(ep - cp);
100
3.87k
    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.86k
    memcpy(numbuf, cp, len);
106
3.86k
    numbuf[len] = '\0';
107
3.86k
    delay->tv_sec = (time_t)sudo_strtonum(numbuf, 0, TIME_T_MAX, &errstr);
108
3.86k
    if (errstr != NULL) {
109
78
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
110
78
      "%s: number of seconds is %s", numbuf, errstr);
111
78
  debug_return_ptr(NULL);
112
78
    }
113
114
    /* Radix may be in user's locale for sudo < 1.7.4 so accept that too. */
115
3.78k
    if (*ep != '.' && *ep != *decimal_point) {
116
3.07k
  if (*ep == '\0' || isspace((unsigned char)*ep)) {
117
      /* No fractional part. */
118
3.05k
      delay->tv_nsec = 0;
119
3.05k
      goto done;
120
3.05k
  }
121
3.07k
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
122
16
      "invalid characters after seconds: %s", ep);
123
16
  debug_return_ptr(NULL);
124
16
    }
125
719
    cp = ep + 1;
126
127
    /* Parse fractional part, we may read more precision than we can store. */
128
8.63k
    for (ep = cp; isdigit((unsigned char)*ep); ep++)
129
8.63k
  continue;
130
719
    len = (size_t)(ep - cp);
131
719
    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
710
    memcpy(numbuf, cp, len);
137
710
    numbuf[len] = '\0';
138
710
    llval = sudo_strtonum(numbuf, 0, LLONG_MAX, &errstr);
139
710
    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
701
    if (len < 9) {
147
  /* Convert to nanosecond precision. */
148
2.25k
  do {
149
2.25k
      llval *= 10;
150
2.25k
  } while (++len < 9);
151
416
    } else if (len > 9) {
152
  /* Clamp to nanoseconds. */
153
446
  do {
154
446
      llval /= 10;
155
446
  } while (--len > 9);
156
222
    }
157
701
    delay->tv_nsec = (long)llval;
158
159
3.75k
done:
160
    /* Advance to the next field. */
161
3.75k
    while (isspace((unsigned char)*ep))
162
3.75k
  ep++;
163
164
3.75k
    debug_return_str((char *)ep);
165
3.75k
}
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
4.01k
{
179
4.01k
    unsigned long ulval;
180
4.01k
    char *cp, *ep;
181
4.01k
    debug_decl(iolog_parse_timing, SUDO_DEBUG_UTIL);
182
183
    /* Clear iolog descriptor. */
184
4.01k
    timing->iol = NULL;
185
186
    /* Parse event type. */
187
4.01k
    ulval = strtoul(line, &ep, 10);
188
4.01k
    if (ep == line || !isspace((unsigned char) *ep))
189
11
  goto bad;
190
4.00k
    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
4.00k
    if (ulval >= IO_EVENT_COUNT)
195
124
  goto bad;
196
3.87k
    timing->event = (int)ulval;
197
3.87k
    for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
198
207
  continue;
199
200
    /* Parse delay, returns the next field or NULL on error. */
201
3.87k
    if ((cp = iolog_parse_delay(cp, &timing->delay, timing->decimal)) == NULL)
202
121
  goto bad;
203
204
3.75k
    switch (timing->event) {
205
1.98k
    case IO_EVENT_SUSPEND:
206
  /* Signal name (no leading SIG prefix) or number. */
207
1.98k
  if (str2sig(cp, &timing->u.signo) == -1)
208
197
      goto bad;
209
1.78k
  break;
210
1.78k
    case IO_EVENT_WINSIZE:
211
463
  ulval = strtoul(cp, &ep, 10);
212
463
  if (ep == cp || !isspace((unsigned char) *ep))
213
12
      goto bad;
214
451
  if (ulval > INT_MAX)
215
65
      goto bad;
216
386
  timing->u.winsize.lines = (int)ulval;
217
386
  for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
218
194
      continue;
219
220
386
  ulval = strtoul(cp, &ep, 10);
221
386
  if (ep == cp || *ep != '\0')
222
71
      goto bad;
223
315
  if (ulval > INT_MAX)
224
64
      goto bad;
225
251
  timing->u.winsize.cols = (int)ulval;
226
251
  break;
227
1.31k
    default:
228
1.31k
  errno = 0;
229
1.31k
  ulval = strtoul(cp, &ep, 10);
230
1.31k
  if (ep == cp || *ep != '\0')
231
171
      goto bad;
232
  /* Note: assumes SIZE_MAX == ULONG_MAX */
233
1.14k
  if (errno == ERANGE && ulval == ULONG_MAX)
234
1
      goto bad;
235
1.13k
  timing->u.nbytes = (size_t)ulval;
236
1.13k
  break;
237
3.75k
    }
238
239
3.17k
    debug_return_bool(true);
240
838
bad:
241
838
    debug_return_bool(false);
242
838
}
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.22k
{
251
4.22k
    char line[LINE_MAX];
252
4.22k
    const char *errstr;
253
4.22k
    char *nl;
254
4.22k
    debug_decl(iolog_read_timing_record, SUDO_DEBUG_UTIL);
255
256
    /* Read next record from timing file. */
257
4.22k
    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.05k
    nl = strchr(line, '\n');
270
4.05k
    if (nl == NULL) {
271
43
  goto invalid;
272
43
    }
273
4.01k
    *nl = '\0';
274
275
    /* Parse timing file record. */
276
4.01k
    if (!iolog_parse_timing(line, timing)) {
277
838
  goto invalid;
278
838
    }
279
280
3.17k
    debug_return_int(0);
281
881
invalid:
282
    /* Truncate invalid timing file line at 128 bytes. */
283
881
    line[128] = '\0';
284
881
    sudo_warnx(U_("invalid timing file line: %s"), line);
285
881
    debug_return_int(-1);
286
881
}