From 52a8618ee27334bdfafc86298fabb78f05d97077 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Mon, 26 Aug 2013 17:00:04 +0200 Subject: [PATCH 1/3] Enable debugging --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 13a7c98..afc70b8 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ ifeq ($(shell which pkg-config 2>/dev/null 1>/dev/null || echo 1),1) $(error "pkg-config was not found") endif -CFLAGS += -std=c99 +CFLAGS += -std=c99 -g CFLAGS += -pipe CFLAGS += -Wall CPPFLAGS += -D_GNU_SOURCE -- 1.8.2.2 From f6d1f56de11317d2ff3f561fddf9325b7fdd9fd4 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Mon, 26 Aug 2013 12:23:53 +0200 Subject: [PATCH 2/3] Don't leak image_path --- i3lock.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/i3lock.c b/i3lock.c index 5a87999..95631a8 100644 --- a/i3lock.c +++ b/i3lock.c @@ -661,6 +661,8 @@ int main(int argc, char *argv[]) { image_path, cairo_surface_status(img)); img = NULL; } + free(image_path); + image_path = NULL; } /* Pixmap on which the image is rendered to (if any) */ -- 1.8.2.2 From 9771b8dd0b3e63e23afd58084334be9c54b34d8d Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Mon, 26 Aug 2013 13:35:52 +0200 Subject: [PATCH 3/3] Lock the memory used for the password on FreeBSD as well --- i3lock.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/i3lock.c b/i3lock.c index 95631a8..04a416b 100644 --- a/i3lock.c +++ b/i3lock.c @@ -43,7 +43,9 @@ static xcb_cursor_t cursor; static pam_handle_t *pam_handle; int input_position = 0; /* Holds the password you enter (in UTF-8). */ -static char password[512]; +#define PASSWORD_BUFFER_SIZE 512 +#define PAGE_SIZE 4096 +static char *password; static bool beep = false; bool debug_mode = false; static bool dpms = false; @@ -146,7 +148,7 @@ static void clear_password_memory(void) { /* A volatile pointer to the password buffer to prevent the compiler from * optimizing this out. */ volatile char *vpassword = password; - for (int c = 0; c < sizeof(password); c++) + for (int c = 0; c < PASSWORD_BUFFER_SIZE; c++) /* We store a non-random pattern which consists of the (irrelevant) * index plus (!) the value of the beep variable. This prevents the * compiler from optimizing the calls away, since the value of 'beep' @@ -302,7 +304,7 @@ static void handle_key_press(xcb_key_press_event_t *event) { return; } - if ((input_position + 8) >= sizeof(password)) + if ((input_position + 8) >= PASSWORD_BUFFER_SIZE) return; #if 0 @@ -602,6 +604,10 @@ int main(int argc, char *argv[]) { if (ret != PAM_SUCCESS) errx(EXIT_FAILURE, "PAM: %s", pam_strerror(pam_handle, ret)); + if (posix_memalign((void **)&password, PAGE_SIZE, PASSWORD_BUFFER_SIZE) != 0) + err(EXIT_FAILURE, "Could not allocate aligned memory"); + memset(password, '\0', PASSWORD_BUFFER_SIZE); + /* Using mlock() as non-super-user seems only possible in Linux. Users of other * operating systems should use encrypted swap/no swap (or remove the ifdef and * run i3lock as super-user). */ @@ -609,8 +615,11 @@ int main(int argc, char *argv[]) { /* Lock the area where we store the password in memory, we don’t want it to * be swapped to disk. Since Linux 2.6.9, this does not require any * privileges, just enough bytes in the RLIMIT_MEMLOCK limit. */ - if (mlock(password, sizeof(password)) != 0) + if (mlock(password, PASSWORD_BUFFER_SIZE) != 0) err(EXIT_FAILURE, "Could not lock page in memory, check RLIMIT_MEMLOCK"); +#elif defined(__FreeBSD__) + if (mlock(password, PASSWORD_BUFFER_SIZE) != 0) + err(EXIT_FAILURE, "Could not lock page in memory, check the limits mentioned in mlock(2)"); #endif /* Initialize connection to X11 */ @@ -702,4 +711,6 @@ int main(int argc, char *argv[]) { * file descriptor becomes readable). */ ev_invoke(main_loop, xcb_check, 0); ev_loop(main_loop, 0); + + free(password); } -- 1.8.2.2