Coverage Report

Created: 2026-01-10 06:48

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
4.86k
{
40
4.86k
    debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER);
41
4.86k
    const char suffixes[] = "dhms";
42
4.86k
    const char *cp = timestr;
43
4.86k
    int timeout = 0;
44
4.86k
    int idx = 0;
45
46
6.86k
    do {
47
6.86k
  char *ep;
48
6.86k
  int ch;
49
6.86k
  long l;
50
51
  /* Parse number, must be present and positive. */
52
6.86k
  errno = 0;
53
6.86k
  l = strtol(cp, &ep, 10);
54
6.86k
  if (ep == cp) {
55
      /* missing timeout */
56
650
      errno = EINVAL;
57
650
      debug_return_int(-1);
58
650
  }
59
6.21k
  if (errno == ERANGE || l < 0 || l > INT_MAX)
60
763
      goto overflow;
61
62
  /* Find a matching suffix or return an error. */
63
5.45k
  if (*ep != '\0') {
64
5.01k
      ch = tolower((unsigned char)*ep++);
65
11.2k
      while (suffixes[idx] != ch) {
66
6.62k
    if (suffixes[idx] == '\0') {
67
        /* parse error */
68
434
        errno = EINVAL;
69
434
        debug_return_int(-1);
70
434
    }
71
6.18k
    idx++;
72
6.18k
      }
73
74
      /* Apply suffix. */
75
4.58k
      switch (ch) {
76
1.10k
      case 'd':
77
1.10k
    if (l > INT_MAX / (24 * 60 * 60))
78
229
        goto overflow;
79
880
    l *= 24 * 60 * 60;
80
880
    break;
81
898
      case 'h':
82
898
    if (l > INT_MAX / (60 * 60))
83
210
        goto overflow;
84
688
    l *= 60 * 60;
85
688
    break;
86
2.14k
      case 'm':
87
2.14k
    if (l > INT_MAX / 60)
88
1.42k
        goto overflow;
89
713
    l *= 60;
90
713
    break;
91
4.58k
      }
92
4.58k
  }
93
3.15k
  cp = ep;
94
95
3.15k
  if (l > INT_MAX - timeout)
96
260
      goto overflow;
97
2.89k
  timeout += (int)l;
98
2.89k
    } while (*cp != '\0');
99
100
887
    debug_return_int(timeout);
101
2.89k
overflow:
102
2.89k
    errno = ERANGE;
103
2.89k
    debug_return_int(-1);
104
2.89k
}