Skip to content
Permalink
Browse files

Use C++ scoping for handling buffer deletion

  • Loading branch information...
davidedmundson committed Feb 7, 2019
1 parent 6c23c7e commit 047ef56e5cfa757ebfcb03a248edad579564b5f3
Showing with 5 additions and 12 deletions.
  1. +5 −12 src/helper/UserSession.cpp
@@ -135,21 +135,19 @@ namespace SDDM {
long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize == -1)
bufsize = 16384;
char *buffer = (char *)malloc(bufsize);
if (buffer == NULL)
QScopedPointer<char, QScopedPointerPodDeleter> buffer(static_cast<char*>(malloc(bufsize)));
if (buffer.isNull())
exit(Auth::HELPER_OTHER_ERROR);
int err = getpwnam_r(username.constData(), &pw, buffer, bufsize, &rpw);
int err = getpwnam_r(username.constData(), &pw, buffer.data(), bufsize, &rpw);
if (rpw == NULL) {
if (err == 0)
qCritical() << "getpwnam_r(" << username << ") username not found!";
else
qCritical() << "getpwnam_r(" << username << ") failed with error: " << strerror(err);
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
if (setgid(pw.pw_gid) != 0) {
qCritical() << "setgid(" << pw.pw_gid << ") failed for user: " << username;
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}

@@ -181,7 +179,6 @@ namespace SDDM {
&n_user_groups)) == -1 ) {
qCritical() << "getgrouplist(" << username << ", " << pw.pw_gid
<< ") failed";
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
}
@@ -198,7 +195,6 @@ namespace SDDM {
// setgroups(2) handles duplicate groups
if (setgroups(n_groups, groups) != 0) {
qCritical() << "setgroups() failed for user: " << username;
free(buffer);
exit (Auth::HELPER_OTHER_ERROR);
}
delete[] groups;
@@ -210,31 +206,28 @@ namespace SDDM {

if (initgroups(pw.pw_name, pw.pw_gid) != 0) {
qCritical() << "initgroups(" << pw.pw_name << ", " << pw.pw_gid << ") failed for user: " << username;
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}

#endif /* USE_PAM */

if (setuid(pw.pw_uid) != 0) {
qCritical() << "setuid(" << pw.pw_uid << ") failed for user: " << username;
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
if (chdir(pw.pw_dir) != 0) {
qCritical() << "chdir(" << pw.pw_dir << ") failed for user: " << username;
qCritical() << "verify directory exist and has sufficient permissions";
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
free(buffer);
const QString homeDir = QString::fromLocal8Bit(pw.pw_dir);

//we cannot use setStandardError file as this code is run in the child process
//we want to redirect after we setuid so that the log file is owned by the user

// determine stderr log file based on session type
QString sessionLog = QStringLiteral("%1/%2")
.arg(QString::fromLocal8Bit(pw.pw_dir))
.arg(homeDir)
.arg(sessionType == QLatin1String("x11")
? mainConfig.X11.SessionLogFile.get()
: mainConfig.Wayland.SessionLogFile.get());

0 comments on commit 047ef56

Please sign in to comment.
You can’t perform that action at this time.