Coverage Report

Created: 2026-12-01 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/lib/util/regex.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2023 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 <stddef.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <ctype.h>
25
#include <limits.h>
26
#include <regex.h>
27
28
#include <sudo_compat.h>
29
#include <sudo_debug.h>
30
#include <sudo_util.h>
31
#include <sudo_gettext.h>
32
33
static char errbuf[1024];
34
35
/*
36
 * Parse a number between 0 and INT_MAX, handling escaped digits.
37
 * Returns the number on success or -1 on error.
38
 * Sets endp to the first non-matching character.
39
 */
40
static int
41
parse_num(const char *str, char **endp)
42
12.9k
{
43
12.9k
    debug_decl(check_pattern, SUDO_DEBUG_UTIL);
44
12.9k
    const int lastval = INT_MAX / 10;
45
12.9k
    const int remainder = INT_MAX % 10;
46
12.9k
    int result = 0;
47
12.9k
    char ch;
48
49
30.8k
    while ((ch = *str++) != '\0') {
50
30.8k
  if (ch == '\\' && isdigit((unsigned char)str[0]))
51
653
      ch = *str++;
52
30.1k
  else if (!isdigit((unsigned char)ch))
53
11.6k
      break;
54
19.1k
  ch -= '0';
55
19.1k
  if (result > lastval || (result == lastval && ch > remainder)) {
56
1.31k
      result = -1;
57
1.31k
      break;
58
1.31k
  }
59
17.8k
  result *= 10;
60
17.8k
  result += ch;
61
17.8k
    }
62
12.9k
    *endp = (char *)(str - 1);
63
64
12.9k
    debug_return_int(result);
65
12.9k
}
66
67
/*
68
 * Check pattern for invalid repetition sequences.
69
 * This is implementation-specific behavior, not all regcomp(3) forbid them.
70
 * Glibc allows it but uses excessive memory for repeated '+' ops.
71
 */
72
static int
73
check_pattern(const char *pattern)
74
3.35M
{
75
3.35M
    debug_decl(check_pattern, SUDO_DEBUG_UTIL);
76
3.35M
    const char *cp = pattern;
77
3.35M
    int b1, b2 = 0;
78
3.35M
    char ch, *ep, prev = '\0';
79
80
146M
    while ((ch = *cp++) != '\0') {
81
146M
  switch (ch) {
82
28.5k
  case '\\':
83
28.5k
      if (*cp != '\0') {
84
    /* Skip escaped character. */
85
28.5k
    cp++;
86
28.5k
    prev = '\0';
87
28.5k
    continue;
88
28.5k
      }
89
0
      break;
90
6.50M
  case '?':
91
6.58M
  case '*':
92
6.59M
  case '+':
93
6.59M
      if (prev == '?' || prev == '*' || prev == '+' || prev == '{' ) {
94
    /* Invalid repetition operator. */
95
3.26M
    debug_return_int(REG_BADRPT);
96
3.26M
      }
97
3.33M
      break;
98
3.33M
  case '{':
99
      /*
100
       * Try to match bound: {[0-9\\]*\?,[0-9\\]*}
101
       * GNU libc supports escaped digits and commas.
102
       */
103
10.6k
      b1 = parse_num(cp, &ep);
104
10.6k
      switch (ep[0]) {
105
1.50k
      case '\\':
106
1.50k
    if (ep[1] != ',')
107
1.08k
        break;
108
418
    ep++;
109
418
    FALLTHROUGH;
110
2.31k
      case ',':
111
2.31k
    cp = ep + 1;
112
2.31k
    b2 = parse_num(cp, &ep);
113
2.31k
    break;
114
10.6k
      }
115
10.6k
      cp = ep;
116
10.6k
      if (*cp == '}') {
117
2.65k
    if (b1 < 0 || b1 > 255 || b2 < 0 || b2 > 255) {
118
        /* Invalid bound value. */
119
866
        debug_return_int(REG_BADBR);
120
866
    }
121
1.79k
    if (prev == '?' || prev == '*' || prev == '+' || prev == '{' ) {
122
        /* Invalid repetition operator. */
123
831
        debug_return_int(REG_BADRPT);
124
831
    }
125
    /* Skip past '}', prev will be set to '{' below */
126
962
    cp++;
127
962
    break;
128
1.79k
      }
129
7.99k
      prev = '\0';
130
7.99k
      continue;
131
146M
  }
132
143M
  prev = ch;
133
143M
    }
134
135
88.3k
    debug_return_int(0);
136
88.3k
}
137
138
/*
139
 * Wrapper around regcomp() that handles a regex starting with (?i).
140
 * Avoid using regex_t in the function args so we don't need to
141
 * include regex.h everywhere.
142
 */
143
bool
144
sudo_regex_compile_v1(void *v, const char *pattern, const char **errstr)
145
3.91M
{
146
3.91M
    int errcode, cflags = REG_EXTENDED|REG_NOSUB;
147
3.91M
    regex_t *preg;
148
3.91M
    char *copy = NULL;
149
3.91M
    const char *cp;
150
3.91M
    regex_t rebuf;
151
3.91M
    debug_decl(regex_compile, SUDO_DEBUG_UTIL);
152
153
    /* Some callers just want to check the validity of the pattern. */
154
3.91M
    preg = v ? v : &rebuf;
155
156
    /* Limit the length of regular expressions to avoid fuzzer issues. */
157
3.91M
    if (strlen(pattern) > 1024) {
158
566k
  *errstr = N_("regular expression too large");
159
566k
  debug_return_bool(false);
160
566k
    }
161
162
    /* Check for (?i) to enable case-insensitive matching. */
163
3.35M
    cp = pattern[0] == '^' ? pattern + 1 : pattern;
164
3.35M
    if (strncmp(cp, "(?i)", 4) == 0) {
165
381
  cflags |= REG_ICASE;
166
381
  copy = strdup(pattern + 4);
167
381
  if (copy == NULL) {
168
0
      *errstr = N_("unable to allocate memory");
169
0
      debug_return_bool(false);
170
0
  }
171
381
  if (pattern[0] == '^')
172
381
      copy[0] = '^';
173
381
  pattern = copy;
174
381
    }
175
176
3.35M
    errcode = check_pattern(pattern);
177
3.35M
    if (errcode == 0)
178
88.3k
  errcode = regcomp(preg, pattern, cflags);
179
3.35M
    if (errcode == 0) {
180
83.9k
  if (preg == &rebuf)
181
0
      regfree(&rebuf);
182
3.26M
    } else {
183
3.26M
        regerror(errcode, preg, errbuf, sizeof(errbuf));
184
3.26M
        *errstr = errbuf;
185
3.26M
    }
186
3.35M
    free(copy);
187
188
    debug_return_bool(errcode == 0);
189
3.35M
}