Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swaylock: Clear password buffer after use. #1519

Merged
merged 1 commit into from
Dec 17, 2017

Conversation

ggreer
Copy link
Contributor

@ggreer ggreer commented Dec 15, 2017

After a user enters or clears a password, try to overwrite the buffer containing it. That way it's not sitting around in RAM for something else to read later. Also, mlock() the password buffer so that it is never swapped out.

This also replaces the dynamically allocated buffer with a static char[1024]. Any characters past that are discarded until the user hits enter or escape.

I had to close #1513 because I opened it against the wrong base. This PR addresses your comments over there.

Regarding PAM zeroing-out the strdup()'d password: I figured that out with gdb. Here's a screenshot of my debug session:

screenshot from 2017-12-14 20-40-10

I'm too lazy to set up debug symbols, and the Linux PAM code is really hard to follow, so I can't link to the exact line where the zeroing happens.

I also used the debugger to verify that swaylock's copy of the password is zeroed-out.

After a user enters or clears a password, try to overwrite the buffer containing it. That way it's not sitting around in RAM for something else to read later. Also, mlock() the password buffer so that it is never swapped out.

This also replaces the dynamically allocated buffer with a static char[1024]. Any characters past that are discarded until the user hits enter or escape.
swaylock/main.c Outdated
volatile char *pw = password;
for (size_t i = 0; i < sizeof(password); ++i) {
// show_indicator is set at runtime, so compiler can't optimize this out
pw[i] = show_indicator;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather just do

volatile char *pw = password;
volatile char zero = 0;
for (size_t i = ...) {
    pw[i] = zero;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. That was the code I meant to have in this PR, but I'd forgotten to add -f to my git push.

@ddevault ddevault merged commit 1263ea6 into swaywm:0.15 Dec 17, 2017
@ddevault
Copy link
Contributor

Thanks!

ggreer added a commit to ggreer/sway that referenced this pull request Apr 13, 2018
- Replace char* with static array. Any chars > 1024 will be discarded.
- mlock() password buffer so it can't be written to swap.
- Clear password buffer after auth succeeds or fails.

This is basically the same treatment I gave the 0.15 branch in swaywm#1519
ggreer added a commit to ggreer/sway that referenced this pull request Apr 13, 2018
- Replace char* with static array. Any chars > 1024 will be discarded.
- mlock() password buffer so it can't be written to swap.
- Clear password buffer after auth succeeds or fails.

This is basically the same treatment I gave the 0.15 branch in swaywm#1519
ddevault pushed a commit to swaywm/swaylock that referenced this pull request Jan 14, 2019
- Replace char* with static array. Any chars > 1024 will be discarded.
- mlock() password buffer so it can't be written to swap.
- Clear password buffer after auth succeeds or fails.

This is basically the same treatment I gave the 0.15 branch in swaywm/sway#1519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants