Coverage Report

Created: 2025-08-26 06:57

/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
4.57k
{
45
4.57k
    debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER);
46
4.57k
    const char suffixes[] = "dhms";
47
4.57k
    const char *cp = timestr;
48
4.57k
    int timeout = 0;
49
4.57k
    int idx = 0;
50
51
6.08k
    do {
52
6.08k
  char *ep;
53
6.08k
  int ch;
54
6.08k
  long l;
55
56
  /* Parse number, must be present and positive. */
57
6.08k
  errno = 0;
58
6.08k
  l = strtol(cp, &ep, 10);
59
6.08k
  if (ep == cp) {
60
      /* missing timeout */
61
595
      errno = EINVAL;
62
595
      debug_return_int(-1);
63
595
  }
64
5.48k
  if (errno == ERANGE || l < 0 || l > INT_MAX)
65
745
      goto overflow;
66
67
  /* Find a matching suffix or return an error. */
68
4.74k
  if (*ep != '\0') {
69
3.16k
      ch = tolower((unsigned char)*ep++);
70
6.11k
      while (suffixes[idx] != ch) {
71
3.30k
    if (suffixes[idx] == '\0') {
72
        /* parse error */
73
349
        errno = EINVAL;
74
349
        debug_return_int(-1);
75
349
    }
76
2.95k
    idx++;
77
2.95k
      }
78
79
      /* Apply suffix. */
80
2.81k
      switch (ch) {
81
1.07k
      case 'd':
82
1.07k
    if (l > INT_MAX / (24 * 60 * 60))
83
394
        goto overflow;
84
679
    l *= 24 * 60 * 60;
85
679
    break;
86
831
      case 'h':
87
831
    if (l > INT_MAX / (60 * 60))
88
216
        goto overflow;
89
615
    l *= 60 * 60;
90
615
    break;
91
632
      case 'm':
92
632
    if (l > INT_MAX / 60)
93
222
        goto overflow;
94
410
    l *= 60;
95
410
    break;
96
2.81k
      }
97
2.81k
  }
98
3.56k
  cp = ep;
99
100
3.56k
  if (l > INT_MAX - timeout)
101
224
      goto overflow;
102
3.33k
  timeout += (int)l;
103
3.33k
    } while (*cp != '\0');
104
105
1.83k
    debug_return_int(timeout);
106
1.80k
overflow:
107
1.80k
    errno = ERANGE;
108
1.80k
    debug_return_int(-1);
109
1.80k
}