/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 | 28.2k | { |
45 | 28.2k | debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER); |
46 | 28.2k | const char suffixes[] = "dhms"; |
47 | 28.2k | const char *cp = timestr; |
48 | 28.2k | int timeout = 0; |
49 | 28.2k | int idx = 0; |
50 | | |
51 | 30.8k | do { |
52 | 30.8k | char *ep; |
53 | 30.8k | char ch; |
54 | 30.8k | long l; |
55 | | |
56 | | /* Parse number, must be present and positive. */ |
57 | 30.8k | errno = 0; |
58 | 30.8k | l = strtol(cp, &ep, 10); |
59 | 30.8k | if (ep == cp) { |
60 | | /* missing timeout */ |
61 | 280 | errno = EINVAL; |
62 | 280 | debug_return_int(-1); |
63 | 280 | } |
64 | 30.6k | if (errno == ERANGE || l < 0 || l > INT_MAX) |
65 | 820 | goto overflow; |
66 | | |
67 | | /* Find a matching suffix or return an error. */ |
68 | 29.7k | if (*ep != '\0') { |
69 | 4.54k | ch = tolower((unsigned char)*ep++); |
70 | 8.09k | while (suffixes[idx] != ch) { |
71 | 3.79k | if (suffixes[idx] == '\0') { |
72 | | /* parse error */ |
73 | 238 | errno = EINVAL; |
74 | 238 | debug_return_int(-1); |
75 | 238 | } |
76 | 3.55k | idx++; |
77 | 3.55k | } |
78 | | |
79 | | /* Apply suffix. */ |
80 | 4.30k | switch (ch) { |
81 | 1.57k | case 'd': |
82 | 1.57k | if (l > INT_MAX / (24 * 60 * 60)) |
83 | 258 | goto overflow; |
84 | 1.32k | l *= 24 * 60 * 60; |
85 | 1.32k | break; |
86 | 870 | case 'h': |
87 | 870 | if (l > INT_MAX / (60 * 60)) |
88 | 219 | goto overflow; |
89 | 651 | l *= 60 * 60; |
90 | 651 | break; |
91 | 852 | case 'm': |
92 | 852 | if (l > INT_MAX / 60) |
93 | 214 | goto overflow; |
94 | 638 | l *= 60; |
95 | 638 | break; |
96 | 4.30k | } |
97 | 3.61k | if (l > INT_MAX - timeout) |
98 | 254 | goto overflow; |
99 | 3.61k | } |
100 | 28.6k | cp = ep; |
101 | | |
102 | 28.6k | timeout += l; |
103 | 28.6k | } while (*cp != '\0'); |
104 | | |
105 | 25.9k | debug_return_int(timeout); |
106 | 1.76k | overflow: |
107 | 1.76k | errno = ERANGE; |
108 | 1.76k | debug_return_int(-1); |
109 | 1.76k | } |