Coverage Report

Created: 2025-09-17 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/canon_path.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
/*
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 <sys/stat.h>
27
#include <stddef.h>
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <unistd.h>
32
#include <errno.h>
33
34
#include <sudoers.h>
35
#include <redblack.h>
36
37
static struct rbtree *canon_cache;
38
39
/*
40
 * A cache_item includes storage for both the original path and the
41
 * resolved path.  The resolved path is directly embedded into the
42
 * struct so that we can find the start of the struct cache_item
43
 * given the value of resolved.  Storage for pathname is embedded
44
 * at the end with resolved.
45
 */
46
struct cache_item {
47
    unsigned int refcnt;
48
    char *pathname;
49
    char resolved[];
50
};
51
52
/*
53
 * Compare function for canon_cache.
54
 * v1 is the key to find or data to insert, v2 is in-tree data.
55
 */
56
static int
57
compare(const void *v1, const void *v2)
58
9.27k
{
59
9.27k
    const struct cache_item *ci1 = (const struct cache_item *)v1;
60
9.27k
    const struct cache_item *ci2 = (const struct cache_item *)v2;
61
9.27k
    return strcmp(ci1->pathname, ci2->pathname);
62
9.27k
}
63
64
/* Convert a pointer returned by canon_path() to a struct cache_item *. */
65
13.4k
#define resolved_to_item(_r) ((struct cache_item *)((_r) - offsetof(struct cache_item, resolved)))
66
67
/*
68
 * Delete a ref from item and free if the refcount reaches 0.
69
 */
70
static void
71
canon_path_free_item(void *v)
72
19.2k
{
73
19.2k
    struct cache_item *item = v;
74
19.2k
    debug_decl(canon_path_free_item, SUDOERS_DEBUG_UTIL);
75
76
19.2k
    if (--item->refcnt == 0)
77
5.79k
  free(item);
78
79
19.2k
    debug_return;
80
19.2k
}
81
82
/*
83
 * Delete a ref from the item containing "resolved" and free if
84
 * the refcount reaches 0.
85
 */
86
void
87
canon_path_free(char *resolved)
88
47.7k
{
89
47.7k
    debug_decl(canon_path_free, SUDOERS_DEBUG_UTIL);
90
47.7k
    if (resolved != NULL)
91
13.4k
  canon_path_free_item(resolved_to_item(resolved));
92
47.7k
    debug_return;
93
47.7k
}
94
95
/*
96
 * Free canon_cache.
97
 * This only removes the reference for that the cache owns.
98
 * Other references remain valid until canon_path_free() is called.
99
 */
100
void
101
canon_path_free_cache(void)
102
25.5k
{
103
25.5k
    debug_decl(canon_path_free_cache, SUDOERS_DEBUG_UTIL);
104
105
25.5k
    if (canon_cache != NULL) {
106
5.64k
  rbdestroy(canon_cache, canon_path_free_item);
107
5.64k
  canon_cache = NULL;
108
5.64k
    }
109
110
25.5k
    debug_return;
111
25.5k
}
112
113
/*
114
 * Like realpath(3) but caches the result.  Returns an entry from the
115
 * cache on success (with an added reference) or NULL on failure.
116
 */
117
char *
118
canon_path(const char *inpath)
119
14.6k
{
120
14.6k
    size_t item_size, inlen, reslen = 0;
121
14.6k
    char *resolved, resbuf[PATH_MAX];
122
14.6k
    struct cache_item key, *item;
123
14.6k
    struct rbnode *node = NULL;
124
14.6k
    debug_decl(canon_path, SUDOERS_DEBUG_UTIL);
125
126
14.6k
    if (canon_cache == NULL) {
127
5.64k
  canon_cache = rbcreate(compare);
128
5.64k
  if (canon_cache == NULL) {
129
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
130
0
      debug_return_str(NULL);
131
0
  }
132
8.97k
    } else {
133
  /* Check cache. */
134
8.97k
  key.pathname = (char *)inpath;
135
8.97k
  if ((node = rbfind(canon_cache, &key)) != NULL) {
136
8.81k
      item = node->data;
137
8.81k
      goto done;
138
8.81k
  }
139
8.97k
    }
140
141
    /*
142
     * Not cached, call realpath(3).
143
     * Older realpath() doesn't support passing a NULL buffer.
144
     * We special-case the empty string to resolve to "/".
145
     * XXX - warn on errors other than ENOENT?
146
     */
147
5.79k
    if (*inpath == '\0')
148
0
  resolved = (char *)"/";
149
5.79k
    else
150
5.79k
  resolved = realpath(inpath, resbuf);
151
152
5.79k
    inlen = strlen(inpath);
153
    /* one for NULL terminator of resolved, one for NULL terminator of pathname */
154
5.79k
    item_size = sizeof(*item) + inlen + 2;
155
5.79k
    if (resolved != NULL) {
156
5.25k
  reslen = strlen(resolved);
157
5.25k
  item_size += reslen;
158
5.25k
    }
159
5.79k
    item = malloc(item_size);
160
5.79k
    if (item == NULL) {
161
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
162
0
  debug_return_str(NULL);
163
0
    }
164
5.79k
    if (resolved != NULL)
165
5.25k
  memcpy(item->resolved, resolved, reslen);
166
5.79k
    item->resolved[reslen] = '\0';
167
5.79k
    item->pathname = item->resolved + reslen + 1;
168
5.79k
    memcpy(item->pathname, inpath, inlen);
169
5.79k
    item->pathname[inlen] = '\0';
170
5.79k
    item->refcnt = 1;
171
5.79k
    switch (rbinsert(canon_cache, item, NULL)) {
172
0
    case 1:
173
  /* should not happen */
174
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
175
0
      "path \"%s\" already exists in the cache", inpath);
176
0
  item->refcnt = 0;
177
0
  break;
178
0
    case -1:
179
  /* can't cache item, just return it */
180
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
181
0
      "can't cache path \"%s\"", inpath);
182
0
  item->refcnt = 0;
183
0
  break;
184
5.79k
    }
185
14.6k
done:
186
14.6k
    if (item->refcnt != 0) {
187
14.6k
        sudo_debug_printf(SUDO_DEBUG_DEBUG,
188
14.6k
            "%s: path %s -> %s (%s)", __func__, inpath,
189
14.6k
      item->resolved[0] ? item->resolved : "NULL",
190
14.6k
            node ? "cache hit" : "cached");
191
14.6k
    }
192
14.6k
    if (item->resolved[0] == '\0') {
193
  /* negative result, free item if not cached */
194
1.20k
  if (item->refcnt == 0)
195
0
      free(item);
196
1.20k
  debug_return_str(NULL);
197
1.20k
    }
198
13.4k
    item->refcnt++;
199
13.4k
    debug_return_str(item->resolved);
200
13.4k
}