Created
July 27, 2019 11:59
-
-
Save gsauthof/3efc1a7865fb70517ed741169b3bf11d to your computer and use it in GitHub Desktop.
Redact environment variable under Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 <[email protected]> | |
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