Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/sudoers/goodpath.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 1996, 1998-2005, 2010-2012, 2014-2016
5
* Todd C. Miller <[email protected]>
6
*
7
* Permission to use, copy, modify, and distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
10
*
11
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
*
19
* Sponsored in part by the Defense Advanced Research Projects
20
* Agency (DARPA) and Air Force Research Laboratory, Air Force
21
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
22
*/
23
24
#include <config.h>
25
26
#include <sys/stat.h>
27
#include <stdio.h>
28
#include <string.h>
29
#include <errno.h>
30
31
#include <sudoers.h>
32
33
/*
34
* Verify that path is a normal file and executable by root.
35
*/
36
bool
37
sudo_goodpath(const char *path, const char *runchroot, struct stat *sbp)
38
{
39
bool ret = false;
40
debug_decl(sudo_goodpath, SUDOERS_DEBUG_UTIL);
41
42
if (path != NULL) {
43
char pathbuf[PATH_MAX];
44
struct stat sb;
45
46
if (runchroot != NULL) {
47
/* XXX - handle symlinks and '..' in path outside chroot */
48
const int len =
49
snprintf(pathbuf, sizeof(pathbuf), "%s%s", runchroot, path);
50
if (len >= ssizeof(pathbuf)) {
51
errno = ENAMETOOLONG;
52
goto done;
53
}
54
path = pathbuf; // -V507
55
}
56
if (sbp == NULL)
57
sbp = &sb;
58
59
if (stat(path, sbp) == 0) {
60
/* Make sure path describes an executable regular file. */
61
if (S_ISREG(sbp->st_mode) && ISSET(sbp->st_mode, S_IXUSR|S_IXGRP|S_IXOTH))
62
ret = true;
63
else
64
errno = EACCES;
65
}
66
}
67
done:
68
debug_return_bool(ret);
69
}
70
71