From d5028a00c0a3ced3b55becf96c630721ac507d56 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 28 Nov 2024 11:26:00 -0700 Subject: [PATCH] Use NODEV macro instead of explicit (dev_t)-1. Also fix an assignment of dev_t to -1 that should be NODEV. Bug #1074. --- include/sudo_compat.h | 6 +++++- lib/util/ttyname_dev.c | 2 +- plugins/sudoers/policy.c | 4 ++-- plugins/sudoers/timestamp.c | 4 ++-- src/regress/ttyname/check_ttyname.c | 4 ++-- src/sudo.c | 2 +- src/ttyname.c | 20 ++++++++++---------- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/include/sudo_compat.h b/include/sudo_compat.h index a26a575716..bf0324ba11 100644 --- a/include/sudo_compat.h +++ b/include/sudo_compat.h @@ -1,7 +1,7 @@ /* * SPDX-License-Identifier: ISC * - * Copyright (c) 1996, 1998-2005, 2008, 2009-2023 + * Copyright (c) 1996, 1998-2005, 2008, 2009-2024 * Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any @@ -40,6 +40,10 @@ * Macros and functions that may be missing on some operating systems. */ +#ifndef NODEV +# define NODEV ((dev_t)-1) /* non-existent device */ +#endif + /* * Given the pointer x to the member m of the struct s, return * a pointer to the containing structure. diff --git a/lib/util/ttyname_dev.c b/lib/util/ttyname_dev.c index 77ef76600d..6ecfddd87c 100644 --- a/lib/util/ttyname_dev.c +++ b/lib/util/ttyname_dev.c @@ -1,7 +1,7 @@ /* * SPDX-License-Identifier: ISC * - * Copyright (c) 2012-2018 Todd C. Miller + * Copyright (c) 2012-2018, 2024 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/plugins/sudoers/policy.c b/plugins/sudoers/policy.c index e3fe259c7f..f3adfb04e6 100644 --- a/plugins/sudoers/policy.c +++ b/plugins/sudoers/policy.c @@ -398,7 +398,7 @@ sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v, ctx->user.gid = (gid_t)-1; ctx->user.uid = (gid_t)-1; ctx->user.umask = (mode_t)-1; - ctx->user.ttydev = (dev_t)-1; + ctx->user.ttydev = NODEV; for (cur = info->user_info; *cur != NULL; cur++) { if (MATCHES(*cur, "user=")) { CHECK(*cur, "user="); @@ -597,7 +597,7 @@ sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v, } /* ttydev is only set in user_info[] for API 1.22 and above. */ - if (ctx->user.ttydev == (dev_t)-1 && ctx->user.ttypath != NULL) { + if (ctx->user.ttydev == NODEV && ctx->user.ttypath != NULL) { struct stat sb; if (stat(ctx->user.ttypath, &sb) == 0) ctx->user.ttydev = sb.st_rdev; diff --git a/plugins/sudoers/timestamp.c b/plugins/sudoers/timestamp.c index 7e7e4b565a..3f9db2845f 100644 --- a/plugins/sudoers/timestamp.c +++ b/plugins/sudoers/timestamp.c @@ -1,7 +1,7 @@ /* * SPDX-License-Identifier: ISC * - * Copyright (c) 2014-2023 Todd C. Miller + * Copyright (c) 2014-2024 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -397,7 +397,7 @@ ts_init_key(const struct sudoers_context *ctx, sudo_warnx("unknown time stamp ticket type %d", ticket_type); FALLTHROUGH; case tty: - if (ctx->user.ttydev != (dev_t)-1) { + if (ctx->user.ttydev != NODEV) { /* tty-based time stamp */ entry->type = TS_TTY; entry->u.ttydev = ctx->user.ttydev; diff --git a/src/regress/ttyname/check_ttyname.c b/src/regress/ttyname/check_ttyname.c index 779f73612d..1691d83b5f 100644 --- a/src/regress/ttyname/check_ttyname.c +++ b/src/regress/ttyname/check_ttyname.c @@ -69,7 +69,7 @@ main(int argc, char *argv[]) char *tty_libc = NULL, *tty_sudo = NULL; char pathbuf[PATH_MAX]; bool verbose = false; - dev_t ttydev = -1; + dev_t ttydev = NODEV; int ch, errors = 0, ntests = 1; initprogname(argc > 0 ? argv[0] : "check_ttyname"); @@ -87,7 +87,7 @@ main(int argc, char *argv[]) /* Lookup tty name using kernel info if possible. */ ttydev = get_process_ttyname(pathbuf, sizeof(pathbuf)); - if (ttydev != (dev_t)-1) { + if (ttydev != NODEV) { char numbuf[STRLEN_MAX_SIGNED(long long) + 1]; const char *errstr; dev_t newdev; diff --git a/src/sudo.c b/src/sudo.c index 49f5bd2337..0a3cfd1a22 100644 --- a/src/sudo.c +++ b/src/sudo.c @@ -619,7 +619,7 @@ get_user_info(struct user_details *ud) } ttydev = get_process_ttyname(path, sizeof(path)); - if (ttydev != (dev_t)-1) { + if (ttydev != NODEV) { if (asprintf(&info[++i], "ttydev=%lld", (long long)ttydev) == -1) goto oom; /* The terminal device file may be missing in a chroot() jail. */ diff --git a/src/ttyname.c b/src/ttyname.c index ec3160d4b8..0e6e8467f7 100644 --- a/src/ttyname.c +++ b/src/ttyname.c @@ -105,7 +105,7 @@ get_process_ttyname(char *name, size_t namelen) struct sudo_kinfo_proc *ki_proc = NULL; size_t size = sizeof(*ki_proc); int mib[6], rc, serrno = errno; - dev_t ttydev = (dev_t)-1; + dev_t ttydev = NODEV; debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL); /* @@ -133,7 +133,7 @@ get_process_ttyname(char *name, size_t namelen) } errno = ENOENT; if (rc != -1) { - if ((dev_t)ki_proc->sudo_kp_tdev != (dev_t)-1) { + if ((dev_t)ki_proc->sudo_kp_tdev != NODEV) { errno = serrno; ttydev = (dev_t)ki_proc->sudo_kp_tdev; if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) { @@ -162,7 +162,7 @@ get_process_ttyname(char *name, size_t namelen) dev_t get_process_ttyname(char *name, size_t namelen) { - dev_t ttydev = (dev_t)-1; + dev_t ttydev = NODEV; struct psinfo psinfo; char path[PATH_MAX]; ssize_t nread; @@ -181,7 +181,7 @@ get_process_ttyname(char *name, size_t namelen) ttydev = makedev(major64(psinfo.pr_ttydev), minor64(psinfo.pr_ttydev)); #endif /* On AIX, pr_ttydev is 0 (not -1) when no terminal is present. */ - if (ttydev != 0 && ttydev != (dev_t)-1) { + if (ttydev != 0 && ttydev != NODEV) { errno = serrno; if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) { sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO, @@ -192,7 +192,7 @@ get_process_ttyname(char *name, size_t namelen) } goto done; } - ttydev = (dev_t)-1; + ttydev = NODEV; } } else { struct stat sb; @@ -216,7 +216,7 @@ get_process_ttyname(char *name, size_t namelen) errno = ENOENT; done: - if (ttydev == (dev_t)-1) + if (ttydev == NODEV) sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO, "unable to resolve tty via %s", path); @@ -233,7 +233,7 @@ dev_t get_process_ttyname(char *name, size_t namelen) { const char path[] = "/proc/self/stat"; - dev_t ttydev = (dev_t)-1; + dev_t ttydev = NODEV; char *cp, buf[1024]; int serrno = errno; pid_t ppid = 0; @@ -335,7 +335,7 @@ get_process_ttyname(char *name, size_t namelen) done: if (fd != -1) close(fd); - if (ttydev == (dev_t)-1) + if (ttydev == NODEV) sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO, "unable to resolve tty via %s", path); @@ -351,7 +351,7 @@ get_process_ttyname(char *name, size_t namelen) dev_t get_process_ttyname(char *name, size_t namelen) { - dev_t ttydev = (dev_t)-1; + dev_t ttydev = NODEV; int rc, serrno = errno; struct pst_status pst; debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL); @@ -418,6 +418,6 @@ get_process_ttyname(char *name, size_t namelen) sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO, "unable to resolve tty via ttyname"); errno = ENOENT; - debug_return_dev_t((dev_t)-1); + debug_return_dev_t(NODEV); } #endif