summaryrefslogtreecommitdiffstats
path: root/plugins/sudoers/canon_path.c
blob: 79f8eef12e16861cb0ed1fe9998f01575610d676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2023 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include <config.h>

#include <sys/stat.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <sudoers.h>
#include <redblack.h>

static struct rbtree *canon_cache;

/*
 * A cache_item includes storage for both the original path and the
 * resolved path.  The resolved path is directly embedded into the
 * struct so that we can find the start of the struct cache_item
 * given the value of resolved.  Storage for pathname is embedded
 * at the end with resolved.
 */
struct cache_item {
    unsigned int refcnt;
    char *pathname;
    char resolved[];
};

/*
 * Compare function for canon_cache.
 * v1 is the key to find or data to insert, v2 is in-tree data.
 */
static int
compare(const void *v1, const void *v2)
{
    const struct cache_item *ci1 = (const struct cache_item *)v1;
    const struct cache_item *ci2 = (const struct cache_item *)v2;
    return strcmp(ci1->pathname, ci2->pathname);
}

/* Convert a pointer returned by canon_path() to a struct cache_item *. */
#define resolved_to_item(_r) ((struct cache_item *)((_r) - offsetof(struct cache_item, resolved)))

/*
 * Delete a ref from item and free if the refcount reaches 0.
 */
static void
canon_path_free_item(void *v)
{
    struct cache_item *item = v;
    debug_decl(canon_path_free_item, SUDOERS_DEBUG_UTIL);

    if (--item->refcnt == 0)
	free(item);

    debug_return;
}

/*
 * Delete a ref from the item containing "resolved" and free if
 * the refcount reaches 0.
 */
void
canon_path_free(char *resolved)
{
    debug_decl(canon_path_free, SUDOERS_DEBUG_UTIL);
    if (resolved != NULL)
	canon_path_free_item(resolved_to_item(resolved));
    debug_return;
}

/*
 * Free canon_cache.
 * This only removes the reference for that the cache owns.
 * Other references remain valid until canon_path_free() is called.
 */
void
canon_path_free_cache(void)
{
    debug_decl(canon_path_free_cache, SUDOERS_DEBUG_UTIL);

    if (canon_cache != NULL) {
	rbdestroy(canon_cache, canon_path_free_item);
	canon_cache = NULL;
    }

    debug_return;
}

/*
 * Like realpath(3) but caches the result.  Returns an entry from the
 * cache on success (with an added reference) or NULL on failure.
 */
char *
canon_path(const char *inpath)
{
    size_t item_size, inlen, reslen = 0;
    char *resolved, resbuf[PATH_MAX];
    struct cache_item key, *item;
    struct rbnode *node = NULL;
    debug_decl(canon_path, SUDOERS_DEBUG_UTIL);

    if (canon_cache == NULL) {
	canon_cache = rbcreate(compare);
	if (canon_cache == NULL) {
	    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
	    debug_return_str(NULL);
	}
    } else {
	/* Check cache. */
	key.pathname = (char *)inpath;
	if ((node = rbfind(canon_cache, &key)) != NULL) {
	    item = node->data;
	    goto done;
	}
    }

    /*
     * Not cached, call realpath(3).
     * Older realpath() doesn't support passing a NULL buffer.
     * We special-case the empty string to resolve to "/".
     * XXX - warn on errors other than ENOENT?
     */
    if (*inpath == '\0')
	resolved = (char *)"/";
    else
	resolved = realpath(inpath, resbuf);

    inlen = strlen(inpath);
    /* one for NULL terminator of resolved, one for NULL terminator of pathname */
    item_size = sizeof(*item) + inlen + 2;
    if (resolved != NULL) {
	reslen = strlen(resolved);
	item_size += reslen;
    }
    item = malloc(item_size);
    if (item == NULL) {
	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
	debug_return_str(NULL);
    }
    if (resolved != NULL)
	memcpy(item->resolved, resolved, reslen);
    item->resolved[reslen] = '\0';
    item->pathname = item->resolved + reslen + 1;
    memcpy(item->pathname, inpath, inlen);
    item->pathname[inlen] = '\0';
    item->refcnt = 1;
    switch (rbinsert(canon_cache, item, NULL)) {
    case 1:
	/* should not happen */
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "path \"%s\" already exists in the cache", inpath);
	item->refcnt = 0;
	break;
    case -1:
	/* can't cache item, just return it */
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "can't cache path \"%s\"", inpath);
	item->refcnt = 0;
	break;
    }
done:
    if (item->refcnt != 0) {
        sudo_debug_printf(SUDO_DEBUG_DEBUG,
            "%s: path %s -> %s (%s)", __func__, inpath,
	    item->resolved[0] ? item->resolved : "NULL",
            node ? "cache hit" : "cached");
    }
    if (item->resolved[0] == '\0') {
	/* negative result, free item if not cached */
	if (item->refcnt == 0)
	    free(item);
	debug_return_str(NULL);
    }
    item->refcnt++;
    debug_return_str(item->resolved);
}