2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2019-07-03 00:01:19 +02:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2019-07-03 00:01:19 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LuksBootKeyFileJob.h"
|
|
|
|
|
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
2021-09-21 11:58:22 +02:00
|
|
|
#include "utils/Entropy.h"
|
2019-07-03 00:01:19 +02:00
|
|
|
#include "utils/Logger.h"
|
2021-09-21 11:58:22 +02:00
|
|
|
#include "utils/NamedEnum.h"
|
2019-07-04 15:55:54 +02:00
|
|
|
#include "utils/UMask.h"
|
2019-07-03 00:01:19 +02:00
|
|
|
#include "utils/Variant.h"
|
|
|
|
|
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
|
2022-02-16 23:21:52 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
2019-07-03 00:01:19 +02:00
|
|
|
LuksBootKeyFileJob::LuksBootKeyFileJob( QObject* parent )
|
|
|
|
: Calamares::CppJob( parent )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LuksBootKeyFileJob::~LuksBootKeyFileJob() {}
|
|
|
|
|
|
|
|
QString
|
|
|
|
LuksBootKeyFileJob::prettyName() const
|
|
|
|
{
|
|
|
|
return tr( "Configuring LUKS key file." );
|
|
|
|
}
|
|
|
|
|
2019-07-04 13:45:02 +02:00
|
|
|
struct LuksDevice
|
2019-07-03 00:01:19 +02:00
|
|
|
{
|
2019-07-04 13:45:02 +02:00
|
|
|
LuksDevice( const QMap< QString, QVariant >& pinfo )
|
|
|
|
: isValid( false )
|
|
|
|
, isRoot( false )
|
|
|
|
{
|
|
|
|
if ( pinfo.contains( "luksMapperName" ) )
|
|
|
|
{
|
|
|
|
QString fs = pinfo[ "fs" ].toString();
|
|
|
|
QString mountPoint = pinfo[ "mountPoint" ].toString();
|
|
|
|
|
|
|
|
if ( !mountPoint.isEmpty() || fs == QStringLiteral( "linuxswap" ) )
|
|
|
|
{
|
|
|
|
isValid = true;
|
|
|
|
isRoot = mountPoint == '/';
|
|
|
|
device = pinfo[ "device" ].toString();
|
|
|
|
passphrase = pinfo[ "luksPassphrase" ].toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid;
|
|
|
|
bool isRoot;
|
2019-07-03 00:01:19 +02:00
|
|
|
QString device;
|
|
|
|
QString passphrase;
|
|
|
|
};
|
|
|
|
|
2019-07-04 19:38:10 +02:00
|
|
|
/** @brief Extract the luks passphrases setup.
|
|
|
|
*
|
|
|
|
* Given a list of partitions (as set up by the partitioning module,
|
|
|
|
* so there's maps with keys inside), returns just the list of
|
|
|
|
* luks passphrases for each device.
|
2019-08-01 22:59:06 +02:00
|
|
|
*/
|
2019-07-04 19:38:10 +02:00
|
|
|
static QList< LuksDevice >
|
|
|
|
getLuksDevices( const QVariantList& list )
|
|
|
|
{
|
|
|
|
QList< LuksDevice > luksItems;
|
|
|
|
|
|
|
|
for ( const auto& p : list )
|
|
|
|
{
|
|
|
|
if ( p.canConvert< QVariantMap >() )
|
|
|
|
{
|
|
|
|
LuksDevice d( p.toMap() );
|
|
|
|
if ( d.isValid )
|
|
|
|
{
|
|
|
|
luksItems.append( d );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return luksItems;
|
|
|
|
}
|
|
|
|
|
2019-07-04 13:45:02 +02:00
|
|
|
struct LuksDeviceList
|
2019-07-03 00:01:19 +02:00
|
|
|
{
|
2019-07-04 13:45:02 +02:00
|
|
|
LuksDeviceList( const QVariant& partitions )
|
2019-07-03 00:01:19 +02:00
|
|
|
: valid( false )
|
|
|
|
{
|
2019-07-04 13:45:02 +02:00
|
|
|
if ( partitions.canConvert< QVariantList >() )
|
2019-07-04 13:17:34 +02:00
|
|
|
{
|
2019-07-04 13:45:02 +02:00
|
|
|
devices = getLuksDevices( partitions.toList() );
|
2019-07-04 13:17:34 +02:00
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 13:45:02 +02:00
|
|
|
QList< LuksDevice > devices;
|
2019-07-03 00:01:19 +02:00
|
|
|
bool valid;
|
|
|
|
};
|
|
|
|
|
2019-07-04 15:47:37 +02:00
|
|
|
static const char keyfile[] = "/crypto_keyfile.bin";
|
|
|
|
|
2019-07-04 15:14:06 +02:00
|
|
|
static bool
|
|
|
|
generateTargetKeyfile()
|
|
|
|
{
|
2019-07-04 15:55:54 +02:00
|
|
|
CalamaresUtils::UMask m( CalamaresUtils::UMask::Safe );
|
2021-09-21 11:58:22 +02:00
|
|
|
|
|
|
|
// Get the data
|
|
|
|
QByteArray entropy;
|
|
|
|
auto entropySource = CalamaresUtils::getEntropy( 2048, entropy );
|
|
|
|
if ( entropySource != CalamaresUtils::EntropySource::URandom )
|
|
|
|
{
|
|
|
|
cWarning() << "Could not get entropy from /dev/urandom for LUKS.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fileResult = CalamaresUtils::System::instance()->createTargetFile(
|
|
|
|
keyfile, entropy, CalamaresUtils::System::WriteMode::Overwrite );
|
|
|
|
entropy.fill( 'A' );
|
|
|
|
if ( !fileResult )
|
2019-07-04 15:47:37 +02:00
|
|
|
{
|
2021-09-21 11:58:22 +02:00
|
|
|
cWarning() << "Could not create LUKS keyfile:" << smash( fileResult.code() );
|
2019-07-04 15:47:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-21 11:58:22 +02:00
|
|
|
|
2021-09-21 12:10:21 +02:00
|
|
|
// Give ample time to check that the file was created correctly;
|
|
|
|
// we actually expect ls to return pretty-much-instantly.
|
|
|
|
auto r = CalamaresUtils::System::instance()->targetEnvCommand(
|
|
|
|
{ "ls", "-la", "/" }, QString(), QString(), std::chrono::seconds( 5 ) );
|
2019-07-04 22:08:36 +02:00
|
|
|
cDebug() << "In target system after creating LUKS file" << r.getOutput();
|
2019-07-04 15:47:37 +02:00
|
|
|
return true;
|
2019-07-04 15:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
setupLuks( const LuksDevice& d )
|
|
|
|
{
|
2021-09-21 12:10:21 +02:00
|
|
|
// Adding the key can take some times, measured around 15 seconds with
|
|
|
|
// a HDD (spinning rust) and a slow-ish computer. Give it a minute.
|
2019-07-04 15:47:37 +02:00
|
|
|
auto r = CalamaresUtils::System::instance()->targetEnvCommand(
|
2021-09-21 12:10:21 +02:00
|
|
|
{ "cryptsetup", "luksAddKey", d.device, keyfile }, QString(), d.passphrase, std::chrono::seconds( 60 ) );
|
2019-07-04 15:47:37 +02:00
|
|
|
if ( r.getExitCode() != 0 )
|
|
|
|
{
|
|
|
|
cWarning() << "Could not configure LUKS keyfile on" << d.device << ':' << r.getOutput() << "(exit code"
|
|
|
|
<< r.getExitCode() << ')';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-07-04 15:14:06 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 14:15:35 +01:00
|
|
|
static QVariantList
|
2020-11-12 23:02:12 +01:00
|
|
|
partitions()
|
2020-11-06 20:31:28 +01:00
|
|
|
{
|
|
|
|
Calamares::GlobalStorage* globalStorage = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
return globalStorage->value( QStringLiteral( "partitions" ) ).toList();
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:15:35 +01:00
|
|
|
static bool
|
2020-11-12 23:02:12 +01:00
|
|
|
hasUnencryptedSeparateBoot()
|
2020-11-06 20:31:28 +01:00
|
|
|
{
|
2020-11-13 22:39:25 +01:00
|
|
|
const QVariantList partitions = ::partitions();
|
2020-11-06 20:31:28 +01:00
|
|
|
for ( const QVariant& partition : partitions )
|
|
|
|
{
|
|
|
|
QVariantMap partitionMap = partition.toMap();
|
|
|
|
QString mountPoint = partitionMap.value( QStringLiteral( "mountPoint" ) ).toString();
|
2022-02-16 23:21:52 +01:00
|
|
|
if ( QDir::cleanPath( mountPoint ) == QStringLiteral( "/boot" ) )
|
2020-11-06 20:31:28 +01:00
|
|
|
{
|
|
|
|
return !partitionMap.contains( QStringLiteral( "luksMapperName" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:39:07 +02:00
|
|
|
static bool
|
|
|
|
hasEncryptedRoot()
|
|
|
|
{
|
|
|
|
const QVariantList partitions = ::partitions();
|
|
|
|
for ( const QVariant& partition : partitions )
|
|
|
|
{
|
|
|
|
QVariantMap partitionMap = partition.toMap();
|
|
|
|
QString mountPoint = partitionMap.value( QStringLiteral( "mountPoint" ) ).toString();
|
|
|
|
if ( QDir::cleanPath( mountPoint ) == QStringLiteral( "/" ) )
|
|
|
|
{
|
|
|
|
return partitionMap.contains( QStringLiteral( "luksMapperName" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-03 00:01:19 +02:00
|
|
|
Calamares::JobResult
|
|
|
|
LuksBootKeyFileJob::exec()
|
|
|
|
{
|
|
|
|
const auto* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
if ( !gs )
|
|
|
|
{
|
|
|
|
return Calamares::JobResult::internalError(
|
2019-11-19 13:41:43 +01:00
|
|
|
"LuksBootKeyFile", "No GlobalStorage defined.", Calamares::JobResult::InvalidConfiguration );
|
2019-07-03 00:01:19 +02:00
|
|
|
}
|
|
|
|
if ( !gs->contains( "partitions" ) )
|
|
|
|
{
|
2019-07-04 15:14:06 +02:00
|
|
|
cError() << "No GS[partitions] key.";
|
2019-07-03 00:01:19 +02:00
|
|
|
return Calamares::JobResult::internalError(
|
2019-11-19 13:41:43 +01:00
|
|
|
"LuksBootKeyFile", tr( "No partitions are defined." ), Calamares::JobResult::InvalidConfiguration );
|
2019-07-03 00:01:19 +02:00
|
|
|
}
|
|
|
|
|
2019-07-04 13:45:02 +02:00
|
|
|
LuksDeviceList s( gs->value( "partitions" ) );
|
2019-07-04 15:14:06 +02:00
|
|
|
if ( !s.valid )
|
|
|
|
{
|
|
|
|
cError() << "GS[partitions] is invalid";
|
|
|
|
return Calamares::JobResult::internalError(
|
2019-11-19 13:41:43 +01:00
|
|
|
"LuksBootKeyFile", tr( "No partitions are defined." ), Calamares::JobResult::InvalidConfiguration );
|
2019-07-04 15:14:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-04 13:45:02 +02:00
|
|
|
cDebug() << "There are" << s.devices.count() << "LUKS partitions";
|
2019-07-04 15:14:06 +02:00
|
|
|
if ( s.devices.count() < 1 )
|
|
|
|
{
|
|
|
|
cDebug() << Logger::SubEntry << "Nothing to do for LUKS.";
|
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = std::partition( s.devices.begin(), s.devices.end(), []( const LuksDevice& d ) { return d.isRoot; } );
|
|
|
|
for ( const auto& d : s.devices )
|
|
|
|
{
|
2019-07-04 19:37:37 +02:00
|
|
|
cDebug() << Logger::SubEntry << ( d.isRoot ? "root" : "dev." ) << d.device << "passphrase?"
|
|
|
|
<< !d.passphrase.isEmpty();
|
2019-07-04 15:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( it == s.devices.begin() )
|
|
|
|
{
|
|
|
|
// Then there was no root partition
|
|
|
|
cDebug() << Logger::SubEntry << "No root partition.";
|
|
|
|
return Calamares::JobResult::ok();
|
2020-11-06 20:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// /boot partition is not encrypted, keyfile must not be used
|
2022-04-21 16:39:07 +02:00
|
|
|
// But only if root partition is not encrypted
|
|
|
|
if ( hasUnencryptedSeparateBoot() && !hasEncryptedRoot() )
|
2020-11-06 20:31:28 +01:00
|
|
|
{
|
2022-04-21 17:13:50 +02:00
|
|
|
cDebug() << Logger::SubEntry << "/boot partition is not encrypted, skipping keyfile creation.";
|
2020-11-06 20:31:28 +01:00
|
|
|
return Calamares::JobResult::ok();
|
2019-07-04 15:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( s.devices.first().passphrase.isEmpty() )
|
|
|
|
{
|
|
|
|
cDebug() << Logger::SubEntry << "No root passphrase.";
|
|
|
|
return Calamares::JobResult::error(
|
|
|
|
tr( "Encrypted rootfs setup error" ),
|
|
|
|
tr( "Root partition %1 is LUKS but no passphrase has been set." ).arg( s.devices.first().device ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !generateTargetKeyfile() )
|
|
|
|
{
|
|
|
|
return Calamares::JobResult::error(
|
|
|
|
tr( "Encrypted rootfs setup error" ),
|
|
|
|
tr( "Could not create LUKS key file for root partition %1." ).arg( s.devices.first().device ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const auto& d : s.devices )
|
2019-07-04 13:45:02 +02:00
|
|
|
{
|
2022-04-21 17:13:50 +02:00
|
|
|
// Skip setupLuks for root partition if system has an unencrypted /boot
|
2022-04-21 16:39:07 +02:00
|
|
|
if ( d.isRoot && hasUnencryptedSeparateBoot() )
|
2022-04-21 17:13:50 +02:00
|
|
|
continue;
|
2022-04-21 16:39:07 +02:00
|
|
|
|
2022-04-21 17:13:50 +02:00
|
|
|
if ( !setupLuks( d ) )
|
2019-07-04 15:14:06 +02:00
|
|
|
return Calamares::JobResult::error(
|
|
|
|
tr( "Encrypted rootfs setup error" ),
|
2020-02-16 13:09:30 +01:00
|
|
|
tr( "Could not configure LUKS key file on partition %1." ).arg( d.device ) );
|
2019-07-04 13:45:02 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 00:01:19 +02:00
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
CALAMARES_PLUGIN_FACTORY_DEFINITION( LuksBootKeyFileJobFactory, registerPlugin< LuksBootKeyFileJob >(); )
|