| 1 |
/* |
| 2 |
* SPDX-License-Identifier: ISC |
| 3 |
* |
| 4 |
* Copyright (c) 2013-2020, 2022, 2024 Todd C. Miller <[email protected]> |
| 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 <sys/types.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <stdio.h> |
| 24 |
#ifdef HAVE_STDBOOL_H |
| 25 |
# include <stdbool.h> |
| 26 |
#else |
| 27 |
# include <compat/stdbool.h> |
| 28 |
#endif /* HAVE_STDBOOL_H */ |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <limits.h> |
| 33 |
#include <errno.h> |
| 34 |
|
| 35 |
#include <sudo_compat.h> |
| 36 |
#include <sudo_fatal.h> |
| 37 |
#include <sudo_util.h> |
| 38 |
#include <sudo_debug.h> |
| 39 |
|
| 40 |
sudo_dso_public int main(int argc, char *argv[]); |
| 41 |
|
| 42 |
int sudo_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER; |
| 43 |
extern dev_t get_process_ttyname(char *name, size_t namelen); |
| 44 |
|
| 45 |
static int |
| 46 |
match_ttys(const char *tty1, const char *tty2) |
| 47 |
{ |
| 48 |
struct stat sb1, sb2; |
| 49 |
|
| 50 |
if (tty1 != NULL && tty2 != NULL) { |
| 51 |
if (strcmp(tty1, tty2) == 0) |
| 52 |
return 0; |
| 53 |
/* Could be the same device with a different name. */ |
| 54 |
if (stat(tty1, &sb1) == 0 && S_ISCHR(sb1.st_mode) && |
| 55 |
stat(tty2, &sb2) == 0 && S_ISCHR(sb2.st_mode)) { |
| 56 |
if (sb1.st_rdev == sb2.st_rdev) |
| 57 |
return 0; |
| 58 |
} |
| 59 |
} else if (tty1 == NULL && tty2 == NULL) { |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
int |
| 67 |
main(int argc, char *argv[]) |
| 68 |
{ |
| 69 |
char *tty_libc = NULL, *tty_sudo = NULL; |
| 70 |
char pathbuf[PATH_MAX]; |
| 71 |
bool verbose = false; |
| 72 |
dev_t ttydev = NODEV; |
| 73 |
int ch, errors = 0, ntests = 1; |
| 74 |
|
| 75 |
initprogname(argc > 0 ? argv[0] : "check_ttyname"); |
| 76 |
|
| 77 |
while ((ch = getopt(argc, argv, "v")) != -1) { |
| 78 |
switch (ch) { |
| 79 |
case 'v': |
| 80 |
verbose = true; |
| 81 |
break; |
| 82 |
default: |
| 83 |
fprintf(stderr, "usage: %s [-v]\n", getprogname()); |
| 84 |
return EXIT_FAILURE; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/* Lookup tty name using kernel info if possible. */ |
| 89 |
ttydev = get_process_ttyname(pathbuf, sizeof(pathbuf)); |
| 90 |
if (ttydev != NODEV) { |
| 91 |
char numbuf[STRLEN_MAX_SIGNED(long long) + 1]; |
| 92 |
const char *errstr; |
| 93 |
dev_t newdev; |
| 94 |
|
| 95 |
/* For comparison below. */ |
| 96 |
tty_sudo = pathbuf; |
| 97 |
|
| 98 |
/* Check that we can format a dev_t as a string and parse it. */ |
| 99 |
ntests++; |
| 100 |
(void)snprintf(numbuf, sizeof(numbuf), "%lld", (long long)ttydev); |
| 101 |
newdev = sudo_strtonum(numbuf, LLONG_MIN, LLONG_MAX, &errstr); |
| 102 |
if (errstr != NULL) { |
| 103 |
printf("%s: FAIL unable to parse device number %s: %s", |
| 104 |
getprogname(), numbuf, errstr); |
| 105 |
errors++; |
| 106 |
} else if (ttydev != newdev) { |
| 107 |
printf("%s: FAIL device mismatch for %s, %s != %lld", |
| 108 |
getprogname(), pathbuf, numbuf, (long long)ttydev); |
| 109 |
errors++; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#if defined(HAVE_KINFO_PROC2_NETBSD) || \ |
| 114 |
defined(HAVE_KINFO_PROC_OPENBSD) || \ |
| 115 |
defined(HAVE_KINFO_PROC_FREEBSD) || \ |
| 116 |
defined(HAVE_KINFO_PROC_DFLY) || \ |
| 117 |
defined(HAVE_KINFO_PROC_44BSD) || \ |
| 118 |
defined(HAVE__TTYNAME_DEV) || defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || \ |
| 119 |
defined(HAVE_PSTAT_GETPROC) || defined(__linux__) |
| 120 |
|
| 121 |
/* Lookup tty name attached to stdin via libc. */ |
| 122 |
tty_libc = ttyname(STDIN_FILENO); |
| 123 |
#endif |
| 124 |
|
| 125 |
/* Compare libc and kernel ttys. */ |
| 126 |
if (match_ttys(tty_libc, tty_sudo) == 0) { |
| 127 |
if (verbose) |
| 128 |
printf("%s: OK (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none"); |
| 129 |
} else if (tty_libc == NULL) { |
| 130 |
if (verbose) |
| 131 |
printf("%s: SKIP (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none"); |
| 132 |
ntests = 0; |
| 133 |
} else { |
| 134 |
printf("%s: FAIL %s (sudo) vs. %s (libc)\n", getprogname(), |
| 135 |
tty_sudo ? tty_sudo : "none", tty_libc ? tty_libc : "none"); |
| 136 |
errors++; |
| 137 |
} |
| 138 |
|
| 139 |
if (ntests != 0) { |
| 140 |
printf("%s: %d tests run, %d errors, %d%% success rate\n", |
| 141 |
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests); |
| 142 |
} |
| 143 |
return errors; |
| 144 |
} |