flawopen.com/Teardowns/CVE-2021-3156 Sudo Baron Samedit
Imagine a sign-painter who turns escaped letters like '\n' into new lines. Whenever he sees a backslash '\', he skips it and copies the next letter. But a prankster ends a word with a single backslash right at the end of the sign. The painter sees the backslash, skips it, and copies the letter after it—which is the empty blank wall beyond the sign! He keeps writing all over the wall, destroying the house's master deeds. In Sudo, a trailing backslash allowed an unprivileged user to overwrite root authentication structures in heap memory.
sudoedit -s '\' with unescaped backslashesWhen sudo executed in shell mode (-s or sudoedit -s), it concatenated command line arguments with escaping backslashes. It then called set_cmnd() to unescape the string. If an argument ended with an unescaped backslash, the copy loop incremented past the null terminator (from[1] != '\0'), copying out-of-bounds heap data and overflowing destination buffers.
/* VULNERABLE: Unescape loop skips past string termination null byte */
for (to = user_args, from = NewArgv[0]; *from != '\0'; from++) {
if (from[0] == '\\' && !isspace((unsigned char)from[1]))
from++; /* BUG: If from[1] == '\0', loop skips past null terminator! */
*to++ = *from;
}
*to = '\0'; /* Corrupts adjacent heap chunks with root auth structures */
/* FIXED: Explicitly check that from[1] is neither space nor the null byte */
for (to = user_args, from = NewArgv[0]; *from != '\0'; from++) {
if (from[0] == '\\' && from[1] != '\0' && !isspace((unsigned char)from[1]))
from++;
*to++ = *from;
}
*to = '\0';
sudoedit -s '\', bypassing normal escaping flags.:sudo_nss service structure in glibc.:/tmp/libnss_x.so.2) as root, granting instant root shell access.:from++) without verifying that the destination index remains within valid buffer bounds.