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

Use explicit_bzero(3) to clear passwords, if it is available #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ crypt = cc.find_library('crypt', required: not libpam.found())
math = cc.find_library('m')
rt = cc.find_library('rt')

have_explicit_bzero = cc.has_function(
'explicit_bzero',
args: ['-D_BSD_SOURCE'],
prefix: '#include <string.h>'
)
if not have_explicit_bzero
warning('Your system does not support explicit_bzero(3), using precarious fallback function to clear passwords')
endif

git = find_program('git', required: false)
scdoc = find_program('scdoc', required: get_option('man-pages'))
wayland_scanner_prog = find_program(wayland_scanner.get_variable('wayland_scanner'), native: true)
Expand Down Expand Up @@ -81,6 +90,7 @@ conf_data = configuration_data()
conf_data.set_quoted('SYSCONFDIR', get_option('prefix') / get_option('sysconfdir'))
conf_data.set_quoted('SWAYLOCK_VERSION', version)
conf_data.set10('HAVE_GDK_PIXBUF', gdk_pixbuf.found())
conf_data.set10('HAVE_EXPLICIT_BZERO', have_explicit_bzero)

subdir('include')

Expand Down
5 changes: 5 additions & 0 deletions password.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _BSD_SOURCE // for explicit_bzero
Copy link
Author

@nmeum nmeum Mar 30, 2024

Choose a reason for hiding this comment

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

Defining feature tests macro in the source file is somewhat unideal, only doing it here because that's what is also done elsewhere in the code. Refer to this post for more information. Also using _BSD_SOURCE instead of _GNU_SOURCE here as the BSDs may not define the prototype otherwise. musl defines it on both _GNU_SOURCE and _BSD_SOURCE, not sure about glibc.

#include <assert.h>
#include <errno.h>
#include <pwd.h>
Expand All @@ -13,12 +14,16 @@
#include "unicode.h"

void clear_buffer(char *buf, size_t size) {
#ifdef HAVE_EXPLICIT_BZERO
explicit_bzero(buf, size);
#else
// Use volatile keyword so so compiler can't optimize this out.
volatile char *buffer = buf;
volatile char zero = '\0';
for (size_t i = 0; i < size; ++i) {
buffer[i] = zero;
}
#endif
}

void clear_password_buffer(struct swaylock_password *pw) {
Expand Down