Coverage Report

Created: 2026-12-01 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/rationalize.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2026 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 <assert.h>
22
#include <string.h>
23
24
#include <sudoers.h>
25
26
/*
27
 * Rationalize a path by removing consecutive '/' and "/./" path elements.
28
 * Also removes "/../" elements if dotdot is true.
29
 */
30
char *
31
rationalize_path(char *path, bool dotdot)
32
6.04M
{
33
6.04M
    char *cp, *ep, *path_end;
34
6.04M
    debug_decl(rationalize_path, SUDOERS_DEBUG_UTIL);
35
36
6.04M
    path_end = path + strlen(path);
37
3.36G
    for (cp = path; *cp != '\0';) {
38
3.35G
  if (cp[0] == '/') {
39
      /* Collapse consecutive '/' characters. */
40
7.32M
      if (cp[1] == '/') {
41
1.01M
    for (ep = cp + 1; ep[1] == '/'; ep++)
42
444k
        continue;
43
44
    /* The size argument includes the terminating NUL byte. */
45
569k
    assert(ep <= path_end);
46
569k
    memmove(cp, ep, (size_t)(path_end - ep) + 1);
47
569k
    path_end -= ep - cp;
48
569k
    assert(*path_end == '\0');
49
569k
    continue;
50
569k
      }
51
52
      /* Remove "/./" in path or "/." at path_end. */
53
6.75M
      if (cp[1] == '.' && (cp[2] == '/' || cp[2] == '\0')) {
54
    /* /./foo -> /foo OR /foo/. -> /foo */
55
27.3k
    ep = cp + 2;
56
57
    /* The size argument includes the terminating NUL byte. */
58
27.3k
    assert(ep <= path_end);
59
27.3k
    memmove(cp, ep, (size_t)(path_end - ep) + 1);
60
27.3k
    path_end -= ep - cp;
61
62
    /* An absolute path must not collapse to the empty string. */
63
27.3k
    if (cp == path && *cp == '\0') {
64
        /* /. -> / */
65
3.07k
        path[0] = '/';
66
3.07k
        path[1] = '\0';
67
3.07k
        path_end = path + 1;
68
3.07k
    }
69
27.3k
    assert(*path_end == '\0');
70
27.3k
    continue;
71
27.3k
      }
72
73
      /* Remove "/../" in path or "/.." at path_end. */
74
6.73M
      if (dotdot && cp[1] == '.' && cp[2] == '.' &&
75
176k
        (cp[3] == '/' || cp[3] == '\0')) {
76
77
    /* Skip ep past "/../" or "/.." */
78
170k
    ep = cp + 3 + (cp[3] == '/');
79
80
    /* Set cp to the previous '/' if there is one. */
81
170k
    if (cp != path) {
82
50.0k
        char *saved_cp = cp;
83
50.0k
        ep = cp + 3 + (cp[3] == '/');
84
204k
        do {
85
204k
      cp--;
86
204k
        } while (cp > path && *cp != '/');
87
50.0k
        if (*cp != '/') {
88
      /* No previous '/', path doesn't start with '/'? */
89
28.4k
      cp = saved_cp;
90
28.4k
        }
91
50.0k
    }
92
93
    /* The size argument includes the terminating NUL byte. */
94
170k
    assert(ep <= path_end);
95
170k
    memmove(cp + 1, ep, (size_t)(path_end - ep) + 1);
96
170k
    path_end -= ep - (cp + 1);
97
170k
    assert(*path_end == '\0');
98
170k
    continue;
99
170k
      }
100
6.73M
  }
101
3.35G
  cp++;
102
3.35G
    }
103
6.04M
    debug_return_str(path);
104
6.04M
}