/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 | 29.8k | { |
45 | 29.8k | debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER); |
46 | 29.8k | const char suffixes[] = "dhms"; |
47 | 29.8k | const char *cp = timestr; |
48 | 29.8k | int timeout = 0; |
49 | 29.8k | int idx = 0; |
50 | | |
51 | 32.8k | do { |
52 | 32.8k | char *ep; |
53 | 32.8k | int ch; |
54 | 32.8k | long l; |
55 | | |
56 | | /* Parse number, must be present and positive. */ |
57 | 32.8k | errno = 0; |
58 | 32.8k | l = strtol(cp, &ep, 10); |
59 | 32.8k | if (ep == cp) { |
60 | | /* missing timeout */ |
61 | 370 | errno = EINVAL; |
62 | 370 | debug_return_int(-1); |
63 | 370 | } |
64 | 32.4k | if (errno == ERANGE || l < 0 || l > INT_MAX) |
65 | 1.49k | goto overflow; |
66 | | |
67 | | /* Find a matching suffix or return an error. */ |
68 | 30.9k | if (*ep != '\0') { |
69 | 5.18k | ch = tolower((unsigned char)*ep++); |
70 | 9.18k | while (suffixes[idx] != ch) { |
71 | 4.36k | if (suffixes[idx] == '\0') { |
72 | | /* parse error */ |
73 | 357 | errno = EINVAL; |
74 | 357 | debug_return_int(-1); |
75 | 357 | } |
76 | 4.00k | idx++; |
77 | 4.00k | } |
78 | | |
79 | | /* Apply suffix. */ |
80 | 4.82k | switch (ch) { |
81 | 1.51k | case 'd': |
82 | 1.51k | if (l > INT_MAX / (24 * 60 * 60)) |
83 | 390 | goto overflow; |
84 | 1.12k | l *= 24 * 60 * 60; |
85 | 1.12k | break; |
86 | 1.59k | case 'h': |
87 | 1.59k | if (l > INT_MAX / (60 * 60)) |
88 | 240 | goto overflow; |
89 | 1.35k | l *= 60 * 60; |
90 | 1.35k | break; |
91 | 799 | case 'm': |
92 | 799 | if (l > INT_MAX / 60) |
93 | 214 | goto overflow; |
94 | 585 | l *= 60; |
95 | 585 | break; |
96 | 4.82k | } |
97 | 4.82k | } |
98 | 29.7k | cp = ep; |
99 | | |
100 | 29.7k | if (l > INT_MAX - timeout) |
101 | 257 | goto overflow; |
102 | 29.4k | timeout += (int)l; |
103 | 29.4k | } while (*cp != '\0'); |
104 | | |
105 | 26.5k | debug_return_int(timeout); |
106 | 2.59k | overflow: |
107 | 2.59k | errno = ERANGE; |
108 | 2.59k | debug_return_int(-1); |
109 | 2.59k | } |