forked from mortie/swaylock-effects
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
Commit bdeb891
- Loading branch information
Showing
9 changed files
with
110 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef _SWAY_PASSWORD_BUFFER_H | ||
#define _SWAY_PASSWORD_BUFFER_H | ||
|
||
#include <stddef.h> | ||
|
||
char *password_buffer_create(size_t size); | ||
void password_buffer_destroy(char *buffer, size_t size); | ||
|
||
#endif |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#define _POSIX_C_SOURCE 200809L | ||
#include "password-buffer.h" | ||
#include "log.h" | ||
#include "swaylock.h" | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#include <limits.h> | ||
#include <sys/mman.h> | ||
|
||
static bool mlock_supported = true; | ||
static long int page_size = 0; | ||
|
||
static long int get_page_size() { | ||
if (!page_size) { | ||
page_size = sysconf(_SC_PAGESIZE); | ||
} | ||
return page_size; | ||
} | ||
|
||
// password_buffer_lock expects addr to be page alligned | ||
static bool password_buffer_lock(char *addr, size_t size) { | ||
int retries = 5; | ||
while (mlock(addr, size) != 0 && retries > 0) { | ||
switch (errno) { | ||
case EAGAIN: | ||
retries--; | ||
if (retries == 0) { | ||
swaylock_log(LOG_ERROR, "mlock() supported but failed too often."); | ||
return false; | ||
} | ||
break; | ||
case EPERM: | ||
swaylock_log_errno(LOG_ERROR, "Unable to mlock() password memory: Unsupported!"); | ||
mlock_supported = false; | ||
return true; | ||
default: | ||
swaylock_log_errno(LOG_ERROR, "Unable to mlock() password memory."); | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// password_buffer_unlock expects addr to be page alligned | ||
static bool password_buffer_unlock(char *addr, size_t size) { | ||
if (mlock_supported) { | ||
if (munlock(addr, size) != 0) { | ||
swaylock_log_errno(LOG_ERROR, "Unable to munlock() password memory."); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
char *password_buffer_create(size_t size) { | ||
void *buffer; | ||
int result = posix_memalign(&buffer, get_page_size(), size); | ||
if (result) { | ||
//posix_memalign doesn't set errno according to the man page | ||
errno = result; | ||
swaylock_log_errno(LOG_ERROR, "failed to alloc password buffer"); | ||
return NULL; | ||
} | ||
|
||
if (!password_buffer_lock(buffer, size)) { | ||
free(buffer); | ||
return NULL; | ||
} | ||
|
||
return buffer; | ||
} | ||
|
||
void password_buffer_destroy(char *buffer, size_t size) { | ||
clear_buffer(buffer, size); | ||
password_buffer_unlock(buffer, size); | ||
free(buffer); | ||
} |
This file contains 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
This file contains 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