Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix potential buffer overflow when unescaping backslashes in user_args.
Also, do not try to unescaping backslashes unless in run mode *and*
we are running the command via a shell.
Found by Qualys, this fixes CVE-2021-3156.
  • Loading branch information
millert committed Jan 23, 2021
1 parent c4d3840 commit 1f86385
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions plugins/sudoers/sudoers.c
Expand Up @@ -547,7 +547,7 @@ sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[],

/* If run as root with SUDO_USER set, set sudo_user.pw to that user. */
/* XXX - causes confusion when root is not listed in sudoers */
if (sudo_mode & (MODE_RUN | MODE_EDIT) && prev_user != NULL) {
if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT) && prev_user != NULL) {
if (user_uid == 0 && strcmp(prev_user, "root") != 0) {
struct passwd *pw;

Expand Down Expand Up @@ -932,8 +932,8 @@ set_cmnd(void)
if (user_cmnd == NULL)
user_cmnd = NewArgv[0];

if (sudo_mode & (MODE_RUN | MODE_EDIT | MODE_CHECK)) {
if (ISSET(sudo_mode, MODE_RUN | MODE_CHECK)) {
if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT|MODE_CHECK)) {
if (!ISSET(sudo_mode, MODE_EDIT)) {
const char *runchroot = user_runchroot;
if (runchroot == NULL && def_runchroot != NULL &&
strcmp(def_runchroot, "*") != 0)
Expand Down Expand Up @@ -961,18 +961,31 @@ set_cmnd(void)
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_int(NOT_FOUND_ERROR);
}
if (ISSET(sudo_mode, MODE_SHELL|MODE_LOGIN_SHELL)) {
if (ISSET(sudo_mode, MODE_SHELL|MODE_LOGIN_SHELL) &&
ISSET(sudo_mode, MODE_RUN)) {
/*
* When running a command via a shell, the sudo front-end
* escapes potential meta chars. We unescape non-spaces
* for sudoers matching and logging purposes.
*/
for (to = user_args, av = NewArgv + 1; (from = *av); av++) {
while (*from) {
if (from[0] == '\\' && !isspace((unsigned char)from[1]))
if (from[0] == '\\' && from[1] != '\0' &&
!isspace((unsigned char)from[1])) {
from++;
}
if (size - (to - user_args) < 1) {
sudo_warnx(U_("internal error, %s overflow"),
__func__);
debug_return_int(NOT_FOUND_ERROR);
}
*to++ = *from++;
}
if (size - (to - user_args) < 1) {
sudo_warnx(U_("internal error, %s overflow"),
__func__);
debug_return_int(NOT_FOUND_ERROR);
}
*to++ = ' ';
}
*--to = '\0';
Expand Down

1 comment on commit 1f86385

@felixhalim
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last one! #cs4239rocks!

Please sign in to comment.