Coverage Report

Created: 2025-07-11 06:58

/src/sudo/plugins/sudoers/timeout.c
Line
Count
Source (jump to first uncovered line)
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
/*
20
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22
 */
23
24
#include <config.h>
25
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <ctype.h>
30
#include <errno.h>
31
#include <limits.h>
32
33
#include <sudo_compat.h>
34
#include <sudoers_debug.h>
35
#include <parse.h>
36
37
/*
38
 * Parse a command timeout in sudoers in the format 1d2h3m4s
39
 * (days, hours, minutes, seconds) or a number of seconds with no suffix.
40
 * Returns the number of seconds or -1 on error.
41
 */
42
int
43
parse_timeout(const char *timestr)
44
0
{
45
0
    debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER);
46
0
    const char suffixes[] = "dhms";
47
0
    const char *cp = timestr;
48
0
    int timeout = 0;
49
0
    int idx = 0;
50
51
0
    do {
52
0
  char *ep;
53
0
  int ch;
54
0
  long l;
55
56
  /* Parse number, must be present and positive. */
57
0
  errno = 0;
58
0
  l = strtol(cp, &ep, 10);
59
0
  if (ep == cp) {
60
      /* missing timeout */
61
0
      errno = EINVAL;
62
0
      debug_return_int(-1);
63
0
  }
64
0
  if (errno == ERANGE || l < 0 || l > INT_MAX)
65
0
      goto overflow;
66
67
  /* Find a matching suffix or return an error. */
68
0
  if (*ep != '\0') {
69
0
      ch = tolower((unsigned char)*ep++);
70
0
      while (suffixes[idx] != ch) {
71
0
    if (suffixes[idx] == '\0') {
72
        /* parse error */
73
0
        errno = EINVAL;
74
0
        debug_return_int(-1);
75
0
    }
76
0
    idx++;
77
0
      }
78
79
      /* Apply suffix. */
80
0
      switch (ch) {
81
0
      case 'd':
82
0
    if (l > INT_MAX / (24 * 60 * 60))
83
0
        goto overflow;
84
0
    l *= 24 * 60 * 60;
85
0
    break;
86
0
      case 'h':
87
0
    if (l > INT_MAX / (60 * 60))
88
0
        goto overflow;
89
0
    l *= 60 * 60;
90
0
    break;
91
0
      case 'm':
92
0
    if (l > INT_MAX / 60)
93
0
        goto overflow;
94
0
    l *= 60;
95
0
    break;
96
0
      }
97
0
  }
98
0
  cp = ep;
99
100
0
  if (l > INT_MAX - timeout)
101
0
      goto overflow;
102
0
  timeout += (int)l;
103
0
    } while (*cp != '\0');
104
105
0
    debug_return_int(timeout);
106
0
overflow:
107
0
    errno = ERANGE;
108
0
    debug_return_int(-1);
109
0
}