Coverage Report

Created: 2026-08-14 06:40

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
8.70k
{
40
8.70k
    debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER);
41
8.70k
    const char suffixes[] = "dhms";
42
8.70k
    const char *cp = timestr;
43
8.70k
    int timeout = 0;
44
8.70k
    int idx = 0;
45
46
11.0k
    do {
47
11.0k
  char *ep;
48
11.0k
  int ch;
49
11.0k
  long l;
50
51
  /* Parse number, must be present and positive. */
52
11.0k
  errno = 0;
53
11.0k
  l = strtol(cp, &ep, 10);
54
11.0k
  if (ep == cp) {
55
      /* missing timeout */
56
726
      errno = EINVAL;
57
726
      debug_return_int(-1);
58
726
  }
59
10.3k
  if (errno == ERANGE || l < 0 || l > INT_MAX)
60
3.64k
      goto overflow;
61
62
  /* Find a matching suffix or return an error. */
63
6.67k
  if (*ep != '\0') {
64
5.40k
      ch = tolower((unsigned char)*ep++);
65
10.7k
      while (suffixes[idx] != ch) {
66
6.01k
    if (suffixes[idx] == '\0') {
67
        /* parse error */
68
640
        errno = EINVAL;
69
640
        debug_return_int(-1);
70
640
    }
71
5.37k
    idx++;
72
5.37k
      }
73
74
      /* Apply suffix. */
75
4.76k
      switch (ch) {
76
1.98k
      case 'd':
77
1.98k
    if (l > INT_MAX / (24 * 60 * 60))
78
667
        goto overflow;
79
1.31k
    l *= 24 * 60 * 60;
80
1.31k
    break;
81
1.39k
      case 'h':
82
1.39k
    if (l > INT_MAX / (60 * 60))
83
658
        goto overflow;
84
741
    l *= 60 * 60;
85
741
    break;
86
952
      case 'm':
87
952
    if (l > INT_MAX / 60)
88
346
        goto overflow;
89
606
    l *= 60;
90
606
    break;
91
4.76k
      }
92
4.76k
  }
93
4.36k
  cp = ep;
94
95
4.36k
  if (l > INT_MAX - timeout)
96
613
      goto overflow;
97
3.75k
  timeout += (int)l;
98
3.75k
    } while (*cp != '\0');
99
100
1.41k
    debug_return_int(timeout);
101
5.92k
overflow:
102
5.92k
    errno = ERANGE;
103
5.92k
    debug_return_int(-1);
104
5.92k
}