Skip to content

Commit 54bc3e7

Browse files
committed
Use a relative, not absolute, path in the log ID.
This introduces the concept of the iolog base dir, which is the iolog dir but with no escapes. For example, if the iolog dir is /var/log/sudo-io/%{host}, the base dir is /var/log/sudo-io and the log ID will contain the expanded host as part of the expanded iolog file name.
1 parent 71172a1 commit 54bc3e7

3 files changed

Lines changed: 47 additions & 12 deletions

File tree

logsrvd/logsrvd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ struct connection_closure *connection_closure_alloc(int fd, bool tls, bool relay
209209

210210
/* logsrvd_conf.c */
211211
bool logsrvd_conf_read(const char *path);
212+
const char *logsrvd_conf_iolog_base(void);
212213
const char *logsrvd_conf_iolog_dir(void);
213214
const char *logsrvd_conf_iolog_file(void);
214215
bool logsrvd_conf_iolog_log_passwords(void);

logsrvd/logsrvd_conf.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ static struct logsrvd_config {
153153
gid_t gid;
154154
mode_t mode;
155155
unsigned int maxseq;
156+
char *iolog_base;
156157
char *iolog_dir;
157158
char *iolog_file;
158159
void *passprompt_regex;
@@ -205,6 +206,12 @@ logsrvd_conf_iolog_mode(void)
205206
return logsrvd_config->iolog.mode;
206207
}
207208

209+
const char *
210+
logsrvd_conf_iolog_base(void)
211+
{
212+
return logsrvd_config->iolog.iolog_base;
213+
}
214+
208215
const char *
209216
logsrvd_conf_iolog_dir(void)
210217
{
@@ -345,14 +352,36 @@ logsrvd_conf_relay_tls_check_peer(void)
345352
static bool
346353
cb_iolog_dir(struct logsrvd_config *config, const char *path, size_t offset)
347354
{
355+
size_t base_len = 0;
348356
debug_decl(cb_iolog_dir, SUDO_DEBUG_UTIL);
349357

350358
free(config->iolog.iolog_dir);
351-
if ((config->iolog.iolog_dir = strdup(path)) == NULL) {
352-
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
353-
debug_return_bool(false);
359+
if ((config->iolog.iolog_dir = strdup(path)) == NULL)
360+
goto oom;
361+
362+
/*
363+
* iolog_base is the portion of iolog_dir that contains no escapes.
364+
* This is used to create a relative path for the log id.
365+
*/
366+
for (;;) {
367+
base_len += strcspn(path + base_len, "%");
368+
if (path[base_len] == '\0')
369+
break;
370+
if (path[base_len + 1] == '{') {
371+
/* We want the base to end on a directory boundary. */
372+
while (base_len > 0 && path[base_len] != '/')
373+
base_len--;
374+
break;
375+
}
376+
base_len++;
354377
}
378+
if ((config->iolog.iolog_base = strndup(path, base_len)) == NULL)
379+
goto oom;
380+
355381
debug_return_bool(true);
382+
oom:
383+
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
384+
debug_return_bool(false);
356385
}
357386

358387
static bool

logsrvd/logsrvd_local.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 2019-2022 Todd C. Miller <[email protected]>
4+
* Copyright (c) 2019-2025 Todd C. Miller <[email protected]>
55
*
66
* Permission to use, copy, modify, and distribute this software for any
77
* purpose with or without fee is hereby granted, provided that the above
@@ -224,9 +224,13 @@ store_accept_local(const AcceptMessage *msg, const uint8_t *buf, size_t len,
224224
}
225225

226226
if (new_session && closure->log_io) {
227-
/* Send log ID to client for restarting connections. */
228-
if (!fmt_log_id_message(closure->uuid, closure->evlog->iolog_path,
229-
closure))
227+
/*
228+
* Send log ID to client for restarting connections.
229+
* Note that iolog_base has no trailing '/'.
230+
*/
231+
const char *relative_path = closure->evlog->iolog_path +
232+
strlen(logsrvd_conf_iolog_base()) + 1;
233+
if (!fmt_log_id_message(closure->uuid, relative_path, closure))
230234
goto done;
231235
if (sudo_ev_add(closure->evbase, closure->write_ev,
232236
logsrvd_conf_server_timeout(), false) == -1) {
@@ -456,7 +460,7 @@ static char *
456460
decode_log_id(const char *b64_log_id, unsigned char uuid[restrict static 16])
457461
{
458462
unsigned char log_id_buf[PATH_MAX + 16];
459-
char *path;
463+
char *path, *ret;
460464
size_t len;
461465
debug_decl(decode_log_id, SUDO_DEBUG_UTIL);
462466

@@ -480,11 +484,12 @@ decode_log_id(const char *b64_log_id, unsigned char uuid[restrict static 16])
480484
debug_return_str(NULL);
481485
}
482486

483-
/* The caller is responsible for freeing path */
484-
path = strdup(path);
485-
if (path == NULL)
487+
/* The log_id path is relative to iolog_base. */
488+
if (asprintf(&ret, "%s/%s", logsrvd_conf_iolog_base(), path) == -1) {
489+
ret = NULL;
486490
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
487-
debug_return_str(path);
491+
}
492+
debug_return_str(ret);
488493
}
489494

490495
/*

0 commit comments

Comments
 (0)