Coverage Report

Created: 2026-11-12 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/timeout.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2017 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
#include <string.h>
24
#include <ctype.h>
25
#include <errno.h>
26
#include <limits.h>
27
28
#include <sudo_compat.h>
29
#include <sudoers_debug.h>
30
#include <parse.h>
31
32
/*
33
 * Parse a command timeout in sudoers in the format 1d2h3m4s
34
 * (days, hours, minutes, seconds) or a number of seconds with no suffix.
35
 * Returns the number of seconds or -1 on error.
36
 */
37
int
38
parse_timeout(const char *timestr)
39
27.2k
{
40
27.2k
    debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER);
41
27.2k
    const char suffixes[] = "dhms";
42
27.2k
    const char *cp = timestr;
43
27.2k
    int timeout = 0;
44
27.2k
    int idx = 0;
45
46
30.0k
    do {
47
30.0k
  char *ep;
48
30.0k
  int ch;
49
30.0k
  long l;
50
51
  /* Parse number, must be present and positive. */
52
30.0k
  errno = 0;
53
30.0k
  l = strtol(cp, &ep, 10);
54
30.0k
  if (ep == cp) {
55
      /* missing timeout */
56
65
      errno = EINVAL;
57
65
      debug_return_int(-1);
58
65
  }
59
29.9k
  if (errno == ERANGE || l < 0 || l > INT_MAX)
60
228
      goto overflow;
61
62
  /* Find a matching suffix or return an error. */
63
29.7k
  if (*ep != '\0') {
64
3.92k
      ch = tolower((unsigned char)*ep++);
65
6.22k
      while (suffixes[idx] != ch) {
66
2.34k
    if (suffixes[idx] == '\0') {
67
        /* parse error */
68
41
        errno = EINVAL;
69
41
        debug_return_int(-1);
70
41
    }
71
2.30k
    idx++;
72
2.30k
      }
73
74
      /* Apply suffix. */
75
3.88k
      switch (ch) {
76
898
      case 'd':
77
898
    if (l > INT_MAX / (24 * 60 * 60))
78
40
        goto overflow;
79
858
    l *= 24 * 60 * 60;
80
858
    break;
81
810
      case 'h':
82
810
    if (l > INT_MAX / (60 * 60))
83
33
        goto overflow;
84
777
    l *= 60 * 60;
85
777
    break;
86
1.08k
      case 'm':
87
1.08k
    if (l > INT_MAX / 60)
88
16
        goto overflow;
89
1.06k
    l *= 60;
90
1.06k
    break;
91
3.88k
      }
92
3.88k
  }
93
29.6k
  cp = ep;
94
95
29.6k
  if (l > INT_MAX - timeout)
96
51
      goto overflow;
97
29.5k
  timeout += (int)l;
98
29.5k
    } while (*cp != '\0');
99
100
26.7k
    debug_return_int(timeout);
101
368
overflow:
102
368
    errno = ERANGE;
103
368
    debug_return_int(-1);
104
368
}