Coverage Report

Created: 2026-02-14 06:05

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