Skip to content

Commit

Permalink
privsep: If dumping lease check stdout is a tty for rlimit.
Browse files Browse the repository at this point in the history
If it's not, we can't set it.
  • Loading branch information
rsmarples committed Oct 29, 2024
1 parent b4387dc commit ade0fc6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
7 changes: 0 additions & 7 deletions src/dhcpcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1739,13 +1739,6 @@ dhcpcd_readdump2(void *arg, unsigned short events)
if (ctx->ctl_buf[ctx->ctl_buflen - 1] != '\0') /* unlikely */
ctx->ctl_buf[ctx->ctl_buflen - 1] = '\0';
script_dump(ctx->ctl_buf, ctx->ctl_buflen);
#ifdef PRIVSEP
// Check for redirect, if so, if privsep we need to open up priveleges
if (IN_PRIVSEP(ctx) && !isatty(fileno(stdout)))
{
logdebugx("dumplease stdout has been redirected");
}
#endif
fflush(stdout);
if (--ctx->ctl_extra != 0) {
putchar('\n');
Expand Down
18 changes: 5 additions & 13 deletions src/privsep.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ static int
ps_dropprivs(struct dhcpcd_ctx *ctx)
{
struct passwd *pw = ctx->ps_user;
int fd_out = ctx->options & DHCPCD_DUMPLEASE ?
STDOUT_FILENO : STDERR_FILENO;

if (ctx->options & DHCPCD_LAUNCHER)
logdebugx("chrooting as %s to %s", pw->pw_name, pw->pw_dir);
Expand All @@ -144,7 +146,6 @@ ps_dropprivs(struct dhcpcd_ctx *ctx)
}

struct rlimit rzero = { .rlim_cur = 0, .rlim_max = 0 };
struct rlimit rlease = { .rlim_cur = 5000, .rlim_max = 5000 };

/* Prohibit new files, sockets, etc */
/*
Expand Down Expand Up @@ -173,19 +174,10 @@ ps_dropprivs(struct dhcpcd_ctx *ctx)
* Obviously this won't work if we are using a logfile
* or redirecting stderr to a file. */
if ((ctx->options & DHC_NOCHKIO) == DHC_NOCHKIO ||
(ctx->logfile == NULL && isatty(STDERR_FILENO) == 1))
(ctx->logfile == NULL && isatty(fd_out) == 1))
{
/* Allow dumplease to write to file (all other priveleges are still reduced) */
if (ctx->options & DHCPCD_DUMPLEASE) {
logdebugx("%s DHCPCD_DUMPLEASE setrlimit to %d for RLIMIT_FSIZE", __func__, rlease.rlim_cur);
if (setrlimit(RLIMIT_FSIZE, &rlease) == -1)
logerr("setrlimit RLIMIT_FSIZE rlim_cur: %d max: %d", rlease.rlim_cur, rlease.rlim_max);
}
else {
logdebugx("%s DHC_NOCHKIO setrlimit to %d for RLIMIT_FSIZE", __func__, rzero.rlim_cur);
if (setrlimit(RLIMIT_FSIZE, &rzero) == -1)
logerr("setrlimit RLIMIT_FSIZE rlim_cur: %d max: %d", rzero.rlim_cur, rzero.rlim_max);
}
if (setrlimit(RLIMIT_FSIZE, &rzero) == -1)
logerr("setrlimit RLIMIT_FSIZE");
}

#ifdef RLIMIT_NPROC
Expand Down

0 comments on commit ade0fc6

Please sign in to comment.