summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-01-01 20:12:41 +0100
committerAndreas Kling <kling@serenityos.org>2022-01-02 18:08:02 +0100
commit63e8cf8d5943cf32b3123fc7b71521858759d384 (patch)
tree2e50e394bb7e2ec2aebb13fb147d8fb3b3e7ac6a /Userland/Libraries/LibCore
parentedd8f19a1b8bd8a6dc98388ef443d2628e44a831 (diff)
downloadserenity-63e8cf8d5943cf32b3123fc7b71521858759d384.zip
LibCore: Enforce correct mode when creating new passwd and shadow files
- Use umask() to prevent the parent process from tampering with the mode bits of replacement passwd and shadow files. - Use fchmod() to set new shadow files to mode 0600.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r--Userland/Libraries/LibCore/Account.cpp8
1 files changed, 6 insertions, 2 deletions
diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp
index dc1af5ad9e..1f287a7a19 100644
--- a/Userland/Libraries/LibCore/Account.cpp
+++ b/Userland/Libraries/LibCore/Account.cpp
@@ -9,6 +9,7 @@
#include <AK/ScopeGuard.h>
#include <LibCore/Account.h>
#include <LibCore/System.h>
+#include <LibCore/UmaskScope.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
@@ -260,6 +261,8 @@ ErrorOr<String> Account::generate_shadow_file() const
ErrorOr<void> Account::sync()
{
+ Core::UmaskScope umask_scope(0777);
+
auto new_passwd_file_content = TRY(generate_passwd_file());
#ifndef AK_OS_BSD_GENERIC
auto new_shadow_file_content = TRY(generate_shadow_file());
@@ -273,13 +276,14 @@ ErrorOr<void> Account::sync()
{
auto new_passwd_fd = TRY(Core::System::mkstemp(new_passwd_name));
ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
+ TRY(Core::System::fchmod(new_passwd_fd, 0644));
+
#ifndef AK_OS_BSD_GENERIC
auto new_shadow_fd = TRY(Core::System::mkstemp(new_shadow_name));
ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); };
+ TRY(Core::System::fchmod(new_shadow_fd, 0600));
#endif
- TRY(Core::System::fchmod(new_passwd_fd, 0644));
-
auto nwritten = TRY(Core::System::write(new_passwd_fd, new_passwd_file_content.bytes()));
VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length());