Skip to content

Commit

Permalink
Add ttydev to sudoers_user_context and use for timestamp file.
Browse files Browse the repository at this point in the history
GitHub issue #329
  • Loading branch information
millert committed Nov 25, 2023
1 parent 3dfbf93 commit a85494b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
25 changes: 25 additions & 0 deletions plugins/sudoers/policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,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;
for (cur = info->user_info; *cur != NULL; cur++) {
if (MATCHES(*cur, "user=")) {
CHECK(*cur, "user=");
Expand Down Expand Up @@ -476,6 +477,21 @@ sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v,
ctx->user.tty += sizeof(_PATH_DEV) - 1;
continue;
}
if (MATCHES(*cur, "ttydev=")) {
unsigned long long ullval;
char *ep;

p = *cur + sizeof("ttydev=") - 1;
errno = 0;
ullval = strtoull(p, &ep, 10);
if ((*p == '\0' || *ep != '\0') ||
(errno == ERANGE && ullval == ULLONG_MAX)) {
INVALID("ttydev=");
goto bad;
}
ctx->user.ttydev = (dev_t)ullval;
continue;
}
if (MATCHES(*cur, "host=")) {
CHECK(*cur, "host=");
host = *cur + sizeof("host=") - 1;
Expand Down Expand Up @@ -589,6 +605,15 @@ 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) {
struct stat sb;
if (stat(ctx->user.ttypath, &sb) == 0)
ctx->user.ttydev = sb.st_rdev;
else
sudo_warn("%s", ctx->user.ttypath);
}

/* umask is only set in user_info[] for API 1.10 and above. */
if (ctx->user.umask == (mode_t)-1) {
ctx->user.umask = umask(0);
Expand Down
3 changes: 2 additions & 1 deletion plugins/sudoers/sudoers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef SUDOERS_SUDOERS_H
#define SUDOERS_SUDOERS_H

#include <sys/types.h> /* for gid_t, mode_t, pid_t, size_t, uid_t */
#include <sys/types.h> /* dev_t, gid_t, mode_t, pid_t, size_t, uid_t */
#include <limits.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
Expand Down Expand Up @@ -142,6 +142,7 @@ struct sudoers_user_context {
int lines;
int cols;
int timeout;
dev_t ttydev;
mode_t umask;
uid_t euid;
uid_t uid;
Expand Down
5 changes: 2 additions & 3 deletions plugins/sudoers/timestamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ ts_init_key(const struct sudoers_context *ctx,
struct timestamp_entry *entry, struct passwd *pw,
unsigned short flags, enum def_tuple ticket_type)
{
struct stat sb;
debug_decl(ts_init_key, SUDOERS_DEBUG_AUTH);

memset(entry, 0, sizeof(*entry));
Expand All @@ -398,10 +397,10 @@ ts_init_key(const struct sudoers_context *ctx,
sudo_warnx("unknown time stamp ticket type %d", ticket_type);
FALLTHROUGH;
case tty:
if (ctx->user.ttypath != NULL && stat(ctx->user.ttypath, &sb) == 0) {
if (ctx->user.ttydev != (dev_t)-1) {
/* tty-based time stamp */
entry->type = TS_TTY;
entry->u.ttydev = sb.st_rdev;
entry->u.ttydev = ctx->user.ttydev;
if (entry->sid != -1)
get_starttime(entry->sid, &entry->start_time);
break;
Expand Down

0 comments on commit a85494b

Please sign in to comment.