Skip to content

Commit 62356fc

Browse files
committed
fix direfctory creation
1 parent ca322e5 commit 62356fc

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

include/nuts/SystemInterface.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ class NUTS_EXPORT SystemInterface : public QObject {
6464
qint64 getAvailableSpace(const QString& path);
6565

6666
// Security validation
67-
bool validatePath(const QString& path, const QString& allowedPrefix);
67+
// When logError is false the function returns false silently; use this
68+
// when the result is combined with a second OR-condition validatePath()
69+
// call to avoid a spurious security log on the first failing attempt.
70+
bool validatePath(const QString& path, const QString& allowedPrefix,
71+
bool logError = true);
6872
bool validateURL(const QString& url);
6973
QString sanitizeVersionString(const QString& version);
7074

src/lib/BackupManager.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ bool BackupManager::compressBackup(const QString& inputPath, const QString& outp
8383
QString backupDir = Config::instance().backupDir();
8484
QString xfsDir = Config::instance().xfsDir();
8585

86-
if (!m_sysInterface->validatePath(inputPath, backupDir) &&
87-
!m_sysInterface->validatePath(inputPath, xfsDir)) {
86+
// The XFS backup file always lives in xfsDir; backupDir is a secondary
87+
// allowed location. The first validatePath() call is silent so that it
88+
// doesn't log a spurious security error when the file is legitimately in
89+
// xfsDir and the backupDir check would never be reached.
90+
if (!m_sysInterface->validatePath(inputPath, xfsDir, /*logError=*/false) &&
91+
!m_sysInterface->validatePath(inputPath, backupDir)) {
8892
Logger::instance().error("SECURITY: Invalid input path for compression");
8993
return false;
9094
}

src/lib/SystemInterface.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,12 @@ qint64 SystemInterface::getAvailableSpace(const QString& path) {
595595
return storage.bytesAvailable();
596596
}
597597

598-
bool SystemInterface::validatePath(const QString& path, const QString& allowedPrefix) {
598+
bool SystemInterface::validatePath(const QString& path, const QString& allowedPrefix,
599+
bool logError) {
599600
// Check for path traversal sequences
600601
if (path.contains("../") || path.contains("..\\")) {
601-
Logger::instance().error("SECURITY: Path traversal sequence detected: " + path);
602+
if (logError)
603+
Logger::instance().error("SECURITY: Path traversal sequence detected: " + path);
602604
return false;
603605
}
604606

@@ -620,8 +622,10 @@ bool SystemInterface::validatePath(const QString& path, const QString& allowedPr
620622

621623
// Ensure path doesn't escape allowed directory
622624
if (!allowedPrefix.isEmpty() && !canonicalPath.startsWith(allowedPrefix)) {
623-
Logger::instance().error("SECURITY: Path escapes allowed directory: " + path);
624-
Logger::instance().error("Canonical: " + canonicalPath + ", Allowed: " + allowedPrefix);
625+
if (logError) {
626+
Logger::instance().error("SECURITY: Path escapes allowed directory: " + path);
627+
Logger::instance().error("Canonical: " + canonicalPath + ", Allowed: " + allowedPrefix);
628+
}
625629
return false;
626630
}
627631

src/lib/UpdateManager.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,17 @@ bool UpdateManager::verifyUpdateArchive(const QString& filePath, const QString&
376376
}
377377

378378
bool UpdateManager::checkDiskSpace() {
379-
// Check space on /home for OTA download
379+
// Check space on /home for OTA download.
380+
// downloadDir (/home/.nuts/downloads) may not exist yet — it is created
381+
// inside the chroot later. Walk up to the nearest existing ancestor so
382+
// QStorageInfo can resolve the correct mount point.
380383
QString downloadDir = Config::instance().downloadDir();
384+
{
385+
QDir d(downloadDir);
386+
while (!d.exists() && !d.isRoot())
387+
d.cdUp();
388+
downloadDir = d.absolutePath();
389+
}
381390
qint64 availableHome = m_sysInterface->getAvailableSpace(downloadDir);
382391

383392
// Estimate needed space: OTA size (from metadata) + 20% buffer
@@ -404,9 +413,11 @@ bool UpdateManager::checkDiskSpace() {
404413
requiredHome = estimatedOtaSize + buffer; // +20% buffer
405414

406415
if (availableHome < requiredHome) {
407-
Logger::instance().error(QString("Insufficient space on /home. Required: %1 GB, Available: %2 GB")
408-
.arg(requiredHome / (1024.0 * 1024 * 1024), 0, 'f', 2)
409-
.arg(availableHome / (1024.0 * 1024 * 1024), 0, 'f', 2));
416+
Logger::instance().error(
417+
QString("Insufficient space on %1. Required: %2 GB, Available: %3 GB")
418+
.arg(downloadDir)
419+
.arg(requiredHome / (1024.0 * 1024 * 1024), 0, 'f', 2)
420+
.arg(availableHome / (1024.0 * 1024 * 1024), 0, 'f', 2));
410421
return false;
411422
}
412423

0 commit comments

Comments
 (0)