Skip to content

Commit 3e474c2

Browse files
committed
exec_mailer: Set group as well as uid when running the mailer
Also make a setuid(), setgid() or setgroups() failure fatal. Found by the ZeroPath AI Security Engineer <https://zeropath.com>
1 parent 8487210 commit 3e474c2

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

include/sudo_eventlog.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct eventlog_config {
8080
int syslog_rejectpri;
8181
int syslog_alertpri;
8282
uid_t mailuid;
83+
gid_t mailgid;
8384
bool omit_hostname;
8485
const char *logpath;
8586
const char *time_fmt;
@@ -151,7 +152,7 @@ void eventlog_set_syslog_rejectpri(int pri);
151152
void eventlog_set_syslog_alertpri(int pri);
152153
void eventlog_set_syslog_maxlen(size_t len);
153154
void eventlog_set_file_maxlen(size_t len);
154-
void eventlog_set_mailuid(uid_t uid);
155+
void eventlog_set_mailuser(uid_t uid, gid_t gid);
155156
void eventlog_set_omit_hostname(bool omit_hostname);
156157
void eventlog_set_logpath(const char *path);
157158
void eventlog_set_time_fmt(const char *fmt);

lib/eventlog/eventlog.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,13 @@ exec_mailer(int pipein)
299299
syslog(LOG_ERR, _("unable to dup stdin: %m")); // -V618
300300
sudo_debug_printf(SUDO_DEBUG_ERROR,
301301
"unable to dup stdin: %s", strerror(errno));
302-
sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
303-
_exit(127);
302+
goto bad;
304303
}
305304

306305
/* Build up an argv based on the mailer path and flags */
307306
if ((mflags = strdup(evl_conf->mailerflags)) == NULL) {
308307
syslog(LOG_ERR, _("unable to allocate memory")); // -V618
309-
sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
310-
_exit(127);
308+
goto bad;
311309
}
312310
argv[0] = sudo_basename(mpath);
313311

@@ -326,11 +324,23 @@ exec_mailer(int pipein)
326324
if (setuid(ROOT_UID) != 0) {
327325
sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to change uid to %u",
328326
ROOT_UID);
327+
goto bad;
328+
}
329+
if (setgid(evl_conf->mailgid) != 0) {
330+
sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to change gid to %u",
331+
(unsigned int)evl_conf->mailgid);
332+
goto bad;
333+
}
334+
if (setgroups(1, &evl_conf->mailgid) != 0) {
335+
sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to set groups to %u",
336+
(unsigned int)evl_conf->mailgid);
337+
goto bad;
329338
}
330339
if (evl_conf->mailuid != ROOT_UID) {
331340
if (setuid(evl_conf->mailuid) != 0) {
332341
sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to change uid to %u",
333342
(unsigned int)evl_conf->mailuid);
343+
goto bad;
334344
}
335345
}
336346
sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
@@ -342,6 +352,9 @@ exec_mailer(int pipein)
342352
sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to execute %s: %s",
343353
mpath, strerror(errno));
344354
_exit(127);
355+
bad:
356+
sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
357+
_exit(127);
345358
}
346359

347360
/* Send a message to the mailto user */

lib/eventlog/eventlog_conf.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static struct eventlog_config evl_conf = {
6565
LOG_ALERT, /* syslog_rejectpri */
6666
LOG_ALERT, /* syslog_alertpri */
6767
ROOT_UID, /* mailuid */
68+
ROOT_GID, /* mailgid */
6869
false, /* omit_hostname */
6970
_PATH_SUDO_LOGFILE, /* logpath */
7071
"%h %e %T", /* time_fmt */
@@ -146,9 +147,10 @@ eventlog_set_file_maxlen(size_t len)
146147
}
147148

148149
void
149-
eventlog_set_mailuid(uid_t uid)
150+
eventlog_set_mailuser(uid_t uid, gid_t gid)
150151
{
151152
evl_conf.mailuid = uid;
153+
evl_conf.mailgid = gid;
152154
}
153155

154156
void

plugins/sudoers/logging.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ init_eventlog_config(void)
11521152
eventlog_set_syslog_alertpri(def_syslog_badpri);
11531153
eventlog_set_syslog_maxlen(def_syslog_maxlen);
11541154
eventlog_set_file_maxlen(def_loglinelen);
1155-
eventlog_set_mailuid(ROOT_UID);
1155+
eventlog_set_mailuser(ROOT_UID, ROOT_GID);
11561156
eventlog_set_omit_hostname(!def_log_host);
11571157
eventlog_set_logpath(def_logfile);
11581158
eventlog_set_time_fmt(def_log_year ? "%h %e %T %Y" : "%h %e %T");

plugins/sudoers/policy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v,
634634
}
635635

636636
#ifdef NO_ROOT_MAILER
637-
eventlog_set_mailuid(ctx->user.uid);
637+
eventlog_set_mailuser(ctx->user.uid, ctx->user.gid);
638638
#endif
639639

640640
/* Dump settings and user info (XXX - plugin args) */

0 commit comments

Comments
 (0)