Skip to content

Instantly share code, notes, and snippets.

@gsauthof
Created July 27, 2019 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsauthof/3efc1a7865fb70517ed741169b3bf11d to your computer and use it in GitHub Desktop.
Save gsauthof/3efc1a7865fb70517ed741169b3bf11d to your computer and use it in GitHub Desktop.
Redact environment variable under Linux
/* Demonstrate how to overwrite the original value of
an environment variable in the original environment vector
that is easily observable from other processes
(with sufficient permissions, i.e. same user or root).
*/
/** {{{ MIT No Attribution
Copyright 2019 Georg Sauthoff <mail@georg.so>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
cf. https://github.com/aws/mit-0
}}} */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* Method is effective on Linux, might also be effective on other systems.
It isn't effective on Solaris 10.
*/
static void redact_my_pwd(void)
{
/* defense-in-depth: overwrite password in original environment vector */
char *passwd = getenv("MY_PWD");
if (passwd) {
/* setenv copies passwd, result not visible in /proc/$pid/environ */
if (setenv("MY_PWD", passwd, 1) == -1) {
perror("setenv failed");
exit(1);
}
/* overwrite password in /proc/$pid/environ */
memset(passwd, 'x', strlen(passwd));
}
}
/* Verify effect with a command like one of the below:
ps ep $(pidof redact_env) | grep -o 'MY_PWD=[^ ]* '
< /proc/$(pidof redact_env)/environ tr '\0' '\n' | grep MY_PWD
pargs -e $(pidof redact_env) | grep MY_PWD
*/
int main(int argc, char **argv)
{
redact_my_pwd();
sleep(3600);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment