/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 1996, 1998-2005, 2010-2012, 2014-20164* Todd C. Miller <[email protected]>5*6* Permission to use, copy, modify, and distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*18* Sponsored in part by the Defense Advanced Research Projects19* Agency (DARPA) and Air Force Research Laboratory, Air Force20* Materiel Command, USAF, under agreement number F39502-99-1-0512.21*/2223#include <config.h>2425#include <sys/stat.h>26#include <stdio.h>27#include <string.h>28#include <errno.h>2930#include <sudoers.h>3132/*33* Verify that path is a normal file and executable by root.34*/35bool36sudo_goodpath(const char *path, const char *runchroot, struct stat *sbp)37{38bool ret = false;39debug_decl(sudo_goodpath, SUDOERS_DEBUG_UTIL);4041if (path != NULL) {42char pathbuf[PATH_MAX];43struct stat sb;4445if (runchroot != NULL) {46/* XXX - handle symlinks and '..' in path outside chroot */47const int len =48snprintf(pathbuf, sizeof(pathbuf), "%s%s", runchroot, path);49if (len >= ssizeof(pathbuf)) {50errno = ENAMETOOLONG;51goto done;52}53path = pathbuf; // -V50754}55if (sbp == NULL)56sbp = &sb;5758if (stat(path, sbp) == 0) {59/* Make sure path describes an executable regular file. */60if (S_ISREG(sbp->st_mode) && ISSET(sbp->st_mode, S_IXUSR|S_IXGRP|S_IXOTH))61ret = true;62else63errno = EACCES;64}65}66done:67debug_return_bool(ret);68}697071