summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSertonix <sertonix@posteo.net>2024-01-16 12:25:01 +0100
committerSimon Ser <contact@emersion.fr>2024-03-05 09:50:47 +0100
commitf9ce3f193bc2f035790372b550547686075f4923 (patch)
tree7b355b2ae6687d78cde174dfe8373ff681d4d600
parent376cc5fcd486a3065b4c04ee4a1573606fb09038 (diff)
Read password hash before fork
This ensures that the parent properly errors only if the password cannot be read.
-rw-r--r--shadow.c50
1 files changed, 20 insertions, 30 deletions
diff --git a/shadow.c b/shadow.c
index be1ac0f..3399d04 100644
--- a/shadow.c
+++ b/shadow.c
@@ -1,4 +1,5 @@
#define _XOPEN_SOURCE // for crypt
+#include <assert.h>
#include <pwd.h>
#include <shadow.h>
#include <stdlib.h>
@@ -14,40 +15,16 @@
#include "password-buffer.h"
#include "swaylock.h"
-void initialize_pw_backend(int argc, char **argv) {
- if (geteuid() != 0) {
- swaylock_log(LOG_ERROR,
- "swaylock needs to be setuid to read /etc/shadow");
- exit(EXIT_FAILURE);
- }
-
- if (!spawn_comm_child()) {
- exit(EXIT_FAILURE);
- }
+char *encpw = NULL;
- if (setgid(getgid()) != 0) {
- swaylock_log_errno(LOG_ERROR, "Unable to drop root");
- exit(EXIT_FAILURE);
- }
- if (setuid(getuid()) != 0) {
- swaylock_log_errno(LOG_ERROR, "Unable to drop root");
- exit(EXIT_FAILURE);
- }
- if (setuid(0) != -1 || setgid(0) != -1) {
- swaylock_log_errno(LOG_ERROR, "Unable to drop root (we shouldn't be "
- "able to restore it after setuid/setgid)");
- exit(EXIT_FAILURE);
- }
-}
-
-void run_pw_backend_child(void) {
+void initialize_pw_backend(int argc, char **argv) {
/* This code runs as root */
struct passwd *pwent = getpwuid(getuid());
if (!pwent) {
swaylock_log_errno(LOG_ERROR, "failed to getpwuid");
exit(EXIT_FAILURE);
}
- char *encpw = pwent->pw_passwd;
+ encpw = pwent->pw_passwd;
if (strcmp(encpw, "x") == 0) {
struct spwd *swent = getspnam(pwent->pw_name);
if (!swent) {
@@ -57,21 +34,34 @@ void run_pw_backend_child(void) {
encpw = swent->sp_pwdp;
}
- /* We don't need any additional logging here because the parent process will
- * also fail here and will handle logging for us. */
if (setgid(getgid()) != 0) {
+ swaylock_log_errno(LOG_ERROR, "Unable to drop root");
exit(EXIT_FAILURE);
}
if (setuid(getuid()) != 0) {
+ swaylock_log_errno(LOG_ERROR, "Unable to drop root");
exit(EXIT_FAILURE);
}
- if (setuid(0) != -1) {
+ if (setuid(0) != -1 || setgid(0) != -1) {
+ swaylock_log_errno(LOG_ERROR, "Unable to drop root (we shouldn't be "
+ "able to restore it after setuid/setgid)");
exit(EXIT_FAILURE);
}
/* This code does not run as root */
swaylock_log(LOG_DEBUG, "Prepared to authorize user %s", pwent->pw_name);
+ if (!spawn_comm_child()) {
+ exit(EXIT_FAILURE);
+ }
+
+ /* Buffer is only used by the child */
+ clear_buffer(encpw, strlen(encpw));
+ encpw = NULL;
+}
+
+void run_pw_backend_child(void) {
+ assert(encpw != NULL);
while (1) {
char *buf;
ssize_t size = read_comm_request(&buf);