Skip to content
Merged
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
46 changes: 46 additions & 0 deletions src/daemon/Auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,56 @@
#include "TreelandConnector.h"

#include <pwd.h>
#include <security/pam_appl.h>

Check warning on line 17 in src/daemon/Auth.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <security/pam_appl.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <signal.h>

Check warning on line 18 in src/daemon/Auth.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <signal.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unistd.h>

Check warning on line 19 in src/daemon/Auth.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <climits>

Check warning on line 20 in src/daemon/Auth.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <climits> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/syscall.h>

Check warning on line 21 in src/daemon/Auth.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/syscall.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utmp.h>

Check warning on line 22 in src/daemon/Auth.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <utmp.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utmpx.h>

Check warning on line 23 in src/daemon/Auth.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <utmpx.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace DDM {

#ifdef SYS_close_range
bool tryCloseInheritedWithCloseRange(int preservedFd)
{
const unsigned int first = STDERR_FILENO + 1;
const unsigned int last = UINT_MAX;

// No preserved FD in range: just close everything
if (preservedFd < 0 || static_cast<unsigned int>(preservedFd) < first)
return syscall(SYS_close_range, first, last, 0) == 0;

const unsigned int uPreservedFd = static_cast<unsigned int>(preservedFd);

// Close [first, preservedFd - 1]
if (uPreservedFd > first) {
if (syscall(SYS_close_range, first, uPreservedFd - 1, 0) == -1)
return false;
}

// Close [preservedFd + 1, last]
if (uPreservedFd < last) {
if (syscall(SYS_close_range, uPreservedFd + 1, last, 0) == -1)
return false;
}
return true;
}
#endif

void closeInheritedFileDescriptors(int preservedFd)
{
#ifdef SYS_close_range
if (tryCloseInheritedWithCloseRange(preservedFd))
return;
#endif
const long maxFd = sysconf(_SC_OPEN_MAX);
for (int fd = STDERR_FILENO + 1; fd < maxFd; ++fd) {
if (fd != preservedFd)
close(fd);
}
}

///////////////////////////
// utmp helper functions //
///////////////////////////
Expand Down Expand Up @@ -255,6 +297,10 @@
// Delete old signal handlers, in order to close old fds
// which are shared with the parent process.
delete daemonApp->signalHandler();
// The session leader must not keep daemon-owned sockets alive.
// In particular, an inherited system bus fd would retain DDM's
// well-known names after the daemon exits.
closeInheritedFileDescriptors(pipefd[1]);

// Restore default SIGINT and SIGTERM handlers. We need
// the signal hander to terminate ourself, since we're
Expand Down
Loading