2016-03-29 15:29:50 +03:00
|
|
|
#include "command.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "sync.hh"
|
|
|
|
#include "thread-pool.hh"
|
2018-03-30 01:56:13 +03:00
|
|
|
#include "references.hh"
|
2016-03-29 15:29:50 +03:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2016-04-14 22:14:29 +03:00
|
|
|
struct CmdVerify : StorePathsCommand
|
2016-03-29 15:29:50 +03:00
|
|
|
{
|
|
|
|
bool noContents = false;
|
2016-04-07 16:14:12 +03:00
|
|
|
bool noTrust = false;
|
2016-03-30 12:18:54 +03:00
|
|
|
Strings substituterUris;
|
2019-10-10 16:03:01 +03:00
|
|
|
size_t sigsNeeded = 0;
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-14 22:14:29 +03:00
|
|
|
CmdVerify()
|
2016-03-29 15:29:50 +03:00
|
|
|
{
|
|
|
|
mkFlag(0, "no-contents", "do not verify the contents of each store path", &noContents);
|
2016-04-07 16:14:12 +03:00
|
|
|
mkFlag(0, "no-trust", "do not verify whether each store path is trusted", &noTrust);
|
2020-05-04 23:40:19 +03:00
|
|
|
addFlag({
|
|
|
|
.longName = "substituter",
|
|
|
|
.shortName = 's',
|
|
|
|
.description = "use signatures from specified store",
|
|
|
|
.labels = {"store-uri"},
|
|
|
|
.handler = {[&](std::string s) { substituterUris.push_back(s); }}
|
|
|
|
});
|
2016-04-07 16:14:12 +03:00
|
|
|
mkIntFlag('n', "sigs-needed", "require that each path has at least N valid signatures", &sigsNeeded);
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
|
|
|
|
2016-04-14 22:14:29 +03:00
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "verify the integrity of store paths";
|
|
|
|
}
|
|
|
|
|
2016-04-21 15:58:32 +03:00
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
|
|
|
"To verify the entire Nix store:",
|
|
|
|
"nix verify --all"
|
|
|
|
},
|
|
|
|
Example{
|
|
|
|
"To check whether each path in the closure of Firefox has at least 2 signatures:",
|
|
|
|
"nix verify -r -n2 --no-contents $(type -p firefox)"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:18:23 +03:00
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void run(ref<Store> store, StorePaths storePaths) override
|
2016-03-29 15:29:50 +03:00
|
|
|
{
|
2016-03-30 12:18:54 +03:00
|
|
|
std::vector<ref<Store>> substituters;
|
|
|
|
for (auto & s : substituterUris)
|
2016-09-02 13:35:48 +03:00
|
|
|
substituters.push_back(openStore(s));
|
2016-03-30 12:18:54 +03:00
|
|
|
|
2016-03-29 15:29:50 +03:00
|
|
|
auto publicKeys = getDefaultPublicKeys();
|
|
|
|
|
2020-06-19 01:09:22 +03:00
|
|
|
Activity act(*logger, actVerifyPaths);
|
2017-08-16 20:23:46 +03:00
|
|
|
|
2016-04-25 16:26:07 +03:00
|
|
|
std::atomic<size_t> done{0};
|
2016-03-29 15:29:50 +03:00
|
|
|
std::atomic<size_t> untrusted{0};
|
|
|
|
std::atomic<size_t> corrupted{0};
|
|
|
|
std::atomic<size_t> failed{0};
|
2017-08-16 20:23:46 +03:00
|
|
|
std::atomic<size_t> active{0};
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2017-08-16 20:23:46 +03:00
|
|
|
auto update = [&]() {
|
|
|
|
act.progress(done, storePaths.size(), active, failed);
|
|
|
|
};
|
2016-03-29 15:29:50 +03:00
|
|
|
|
|
|
|
ThreadPool pool;
|
|
|
|
|
|
|
|
auto doPath = [&](const Path & storePath) {
|
|
|
|
try {
|
2016-03-30 12:18:54 +03:00
|
|
|
checkInterrupt();
|
|
|
|
|
2020-06-19 01:09:22 +03:00
|
|
|
Activity act2(*logger, lvlInfo, actUnknown, fmt("checking '%s'", storePath));
|
2017-08-16 20:23:46 +03:00
|
|
|
|
|
|
|
MaintainCount<std::atomic<size_t>> mcActive(active);
|
|
|
|
update();
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
auto info = store->queryPathInfo(store->parseStorePath(storePath));
|
2016-03-29 15:29:50 +03:00
|
|
|
|
|
|
|
if (!noContents) {
|
|
|
|
|
2018-03-30 01:56:13 +03:00
|
|
|
std::unique_ptr<AbstractHashSink> hashSink;
|
2020-06-02 19:47:18 +03:00
|
|
|
if (!info->ca)
|
2020-06-19 23:50:28 +03:00
|
|
|
hashSink = std::make_unique<HashSink>(info->narHash->type);
|
2018-03-30 01:56:13 +03:00
|
|
|
else
|
2020-06-19 23:50:28 +03:00
|
|
|
hashSink = std::make_unique<HashModuloSink>(info->narHash->type, std::string(info->path.hashPart()));
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2018-03-30 01:56:13 +03:00
|
|
|
store->narFromPath(info->path, *hashSink);
|
|
|
|
|
|
|
|
auto hash = hashSink->finish();
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2020-06-19 23:50:28 +03:00
|
|
|
if (hash.first != *info->narHash) {
|
2017-08-16 20:23:46 +03:00
|
|
|
corrupted++;
|
2020-06-19 01:09:22 +03:00
|
|
|
act2.result(resCorruptedPath, store->printStorePath(info->path));
|
2020-06-15 15:12:39 +03:00
|
|
|
logError({
|
2020-06-02 17:22:24 +03:00
|
|
|
.name = "Hash error - path modified",
|
|
|
|
.hint = hintfmt(
|
|
|
|
"path '%s' was modified! expected hash '%s', got '%s'",
|
|
|
|
store->printStorePath(info->path),
|
2020-06-19 23:50:28 +03:00
|
|
|
info->narHash->to_string(Base32, true),
|
2020-06-19 01:09:22 +03:00
|
|
|
hash.first.to_string(Base32, true))
|
2020-06-02 17:22:24 +03:00
|
|
|
});
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 16:14:12 +03:00
|
|
|
if (!noTrust) {
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-03-30 12:18:54 +03:00
|
|
|
bool good = false;
|
|
|
|
|
2016-04-19 19:50:15 +03:00
|
|
|
if (info->ultimate && !sigsNeeded)
|
2016-03-30 12:18:54 +03:00
|
|
|
good = true;
|
|
|
|
|
2016-04-07 16:14:12 +03:00
|
|
|
else {
|
|
|
|
|
|
|
|
StringSet sigsSeen;
|
2019-10-10 16:03:01 +03:00
|
|
|
size_t actualSigsNeeded = std::max(sigsNeeded, (size_t) 1);
|
2016-04-07 16:14:12 +03:00
|
|
|
size_t validSigs = 0;
|
|
|
|
|
|
|
|
auto doSigs = [&](StringSet sigs) {
|
|
|
|
for (auto sig : sigs) {
|
2019-10-09 16:51:52 +03:00
|
|
|
if (!sigsSeen.insert(sig).second) continue;
|
2019-12-05 20:11:09 +02:00
|
|
|
if (validSigs < ValidPathInfo::maxSigs && info->checkSignature(*store, publicKeys, sig))
|
2016-04-07 16:14:12 +03:00
|
|
|
validSigs++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-03 14:17:11 +03:00
|
|
|
if (info->isContentAddressed(*store)) validSigs = ValidPathInfo::maxSigs;
|
|
|
|
|
2016-04-19 19:50:15 +03:00
|
|
|
doSigs(info->sigs);
|
2016-03-30 12:18:54 +03:00
|
|
|
|
|
|
|
for (auto & store2 : substituters) {
|
2016-04-07 16:14:12 +03:00
|
|
|
if (validSigs >= actualSigsNeeded) break;
|
|
|
|
try {
|
2016-08-03 14:17:11 +03:00
|
|
|
auto info2 = store2->queryPathInfo(info->path);
|
|
|
|
if (info2->isContentAddressed(*store)) validSigs = ValidPathInfo::maxSigs;
|
|
|
|
doSigs(info2->sigs);
|
2016-04-19 19:50:15 +03:00
|
|
|
} catch (InvalidPath &) {
|
2016-04-07 16:14:12 +03:00
|
|
|
} catch (Error & e) {
|
2020-05-01 01:31:47 +03:00
|
|
|
logError(e.info());
|
2016-03-30 12:18:54 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 16:14:12 +03:00
|
|
|
|
|
|
|
if (validSigs >= actualSigsNeeded)
|
|
|
|
good = true;
|
2016-03-30 12:18:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!good) {
|
2016-03-29 15:29:50 +03:00
|
|
|
untrusted++;
|
2020-06-19 01:09:22 +03:00
|
|
|
act2.result(resUntrustedPath, store->printStorePath(info->path));
|
2020-06-15 15:12:39 +03:00
|
|
|
logError({
|
2020-06-02 17:22:24 +03:00
|
|
|
.name = "Untrusted path",
|
|
|
|
.hint = hintfmt("path '%s' is untrusted",
|
|
|
|
store->printStorePath(info->path))
|
2020-05-03 17:01:25 +03:00
|
|
|
});
|
2020-06-19 01:09:22 +03:00
|
|
|
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
done++;
|
|
|
|
|
|
|
|
} catch (Error & e) {
|
2020-05-01 01:31:47 +03:00
|
|
|
logError(e.info());
|
2016-03-29 15:29:50 +03:00
|
|
|
failed++;
|
|
|
|
}
|
2017-08-16 20:23:46 +03:00
|
|
|
|
|
|
|
update();
|
2016-03-29 15:29:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto & storePath : storePaths)
|
2019-12-05 20:11:09 +02:00
|
|
|
pool.enqueue(std::bind(doPath, store->printStorePath(storePath)));
|
2016-03-29 15:29:50 +03:00
|
|
|
|
|
|
|
pool.process();
|
|
|
|
|
|
|
|
throw Exit(
|
|
|
|
(corrupted ? 1 : 0) |
|
|
|
|
(untrusted ? 2 : 0) |
|
|
|
|
(failed ? 4 : 0));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 17:01:35 +03:00
|
|
|
static auto r1 = registerCommand<CmdVerify>("verify");
|