2005-03-07 14:54:52 +00:00
|
|
|
#! /usr/bin/perl -w -I /home/eelco/.nix-profile/lib/site_perl
|
2005-03-04 11:12:48 +00:00
|
|
|
|
|
|
|
use strict;
|
2005-03-07 14:54:52 +00:00
|
|
|
use XML::LibXML;
|
|
|
|
#use XML::Simple;
|
2005-03-04 11:12:48 +00:00
|
|
|
|
|
|
|
my $blacklistFN = shift @ARGV;
|
|
|
|
die unless defined $blacklistFN;
|
|
|
|
my $userEnv = shift @ARGV;
|
|
|
|
die unless defined $userEnv;
|
|
|
|
|
|
|
|
|
|
|
|
# Read the blacklist.
|
2005-03-07 14:54:52 +00:00
|
|
|
my $parser = XML::LibXML->new();
|
|
|
|
my $blacklist = $parser->parse_file($blacklistFN)->getDocumentElement;
|
|
|
|
|
|
|
|
#print $blacklist->toString() , "\n";
|
2005-03-04 11:12:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Get all the elements of the user environment.
|
|
|
|
my $userEnvElems = `nix-store --query --references '$userEnv'`;
|
|
|
|
die "cannot query user environment elements" if $? != 0;
|
|
|
|
my @userEnvElems = split ' ', $userEnvElems;
|
|
|
|
|
|
|
|
|
|
|
|
my %storePathHashes;
|
|
|
|
|
|
|
|
|
2005-03-07 16:26:05 +00:00
|
|
|
sub getElemNodes {
|
|
|
|
my $node = shift;
|
|
|
|
my @elems = ();
|
|
|
|
foreach my $node ($node->getChildNodes) {
|
|
|
|
push @elems, $node if $node->nodeType == XML_ELEMENT_NODE;
|
|
|
|
}
|
|
|
|
return @elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my %referencesCache;
|
|
|
|
sub getReferences {
|
|
|
|
my $path = shift;
|
|
|
|
return $referencesCache{$path} if defined $referencesCache{$path};
|
|
|
|
|
|
|
|
my $references = `nix-store --query --references '$path'`;
|
|
|
|
die "cannot query references" if $? != 0;
|
|
|
|
$referencesCache{$path} = [split ' ', $references];
|
|
|
|
|
|
|
|
return $referencesCache{$path};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my %attrsCache;
|
|
|
|
sub getAttr {
|
|
|
|
my $path = shift;
|
|
|
|
my $name = shift;
|
|
|
|
my $key = "$path/$name";
|
|
|
|
return $referencesCache{$key} if defined $referencesCache{$key};
|
|
|
|
|
|
|
|
my $value = `nix-store --query --binding '$name' '$path' 2> /dev/null`;
|
|
|
|
$value = "" if $? != 0; # !!!
|
|
|
|
chomp $value;
|
|
|
|
$referencesCache{$key} = $value;
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub evalCondition;
|
|
|
|
|
|
|
|
|
|
|
|
sub traverse {
|
|
|
|
my $done = shift;
|
|
|
|
my $set = shift;
|
|
|
|
my $path = shift;
|
|
|
|
my $stopCondition = shift;
|
|
|
|
|
|
|
|
return if defined $done->{$path};
|
|
|
|
$done->{$path} = 1;
|
|
|
|
$set->{$path} = 1;
|
|
|
|
|
|
|
|
# print " in $path\n";
|
|
|
|
|
|
|
|
if (!evalCondition({$path => 1}, $stopCondition)) {
|
|
|
|
# print " STOPPING in $path\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the requisites of the deriver.
|
|
|
|
|
|
|
|
foreach my $reference (@{getReferences $path}) {
|
|
|
|
traverse($done, $set, $reference, $stopCondition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub evalSet {
|
|
|
|
my $inSet = shift;
|
|
|
|
my $expr = shift;
|
|
|
|
my $name = $expr->getName;
|
|
|
|
|
|
|
|
if ($name eq "traverse") {
|
|
|
|
my $stopCondition = (getElemNodes $expr)[0];
|
|
|
|
my $done = { };
|
|
|
|
my $set = { };
|
|
|
|
foreach my $path (keys %{$inSet}) {
|
|
|
|
traverse($done, $set, $path, $stopCondition);
|
|
|
|
}
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
die "unknown element `$name'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-04 11:12:48 +00:00
|
|
|
# Function for evaluating conditions.
|
|
|
|
sub evalCondition {
|
|
|
|
my $storePaths = shift;
|
|
|
|
my $condition = shift;
|
2005-03-07 16:26:05 +00:00
|
|
|
my $elemName = $condition->getName;
|
2005-03-07 14:54:52 +00:00
|
|
|
|
2005-03-07 16:26:05 +00:00
|
|
|
if ($elemName eq "containsSource") {
|
2005-03-07 14:54:52 +00:00
|
|
|
my $hash = $condition->attributes->getNamedItem("hash")->getValue;
|
2005-03-04 11:12:48 +00:00
|
|
|
foreach my $path (keys %{$storePathHashes{$hash}}) {
|
2005-03-07 16:26:05 +00:00
|
|
|
return 1 if defined $storePaths->{$path};
|
2005-03-04 11:12:48 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-03-07 14:54:52 +00:00
|
|
|
|
2005-03-07 16:26:05 +00:00
|
|
|
elsif ($elemName eq "hasName") {
|
|
|
|
my $nameRE = $condition->attributes->getNamedItem("name")->getValue;
|
|
|
|
foreach my $path (keys %{$storePaths}) {
|
|
|
|
return 1 if $path =~ /$nameRE/;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif ($elemName eq "hasAttr") {
|
|
|
|
my $name = $condition->attributes->getNamedItem("name")->getValue;
|
|
|
|
my $valueRE = $condition->attributes->getNamedItem("value")->getValue;
|
|
|
|
foreach my $path (keys %{$storePaths}) {
|
|
|
|
if ($path =~ /\.drv$/) {
|
|
|
|
my $value = getAttr($path, $name);
|
|
|
|
# print " $path $name $value\n";
|
|
|
|
return 1 if $value =~ /$valueRE/;
|
2005-03-07 14:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-07 16:26:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif ($elemName eq "and") {
|
|
|
|
my $result = 1;
|
|
|
|
foreach my $node (getElemNodes $condition) {
|
|
|
|
$result &= evalCondition($storePaths, $node);
|
|
|
|
}
|
2005-03-07 14:54:52 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2005-03-07 16:26:05 +00:00
|
|
|
elsif ($elemName eq "not") {
|
|
|
|
return !evalCondition($storePaths, (getElemNodes $condition)[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif ($elemName eq "within") {
|
|
|
|
my @elems = getElemNodes $condition;
|
|
|
|
my $set = evalSet($storePaths, $elems[0]);
|
|
|
|
return evalCondition($set, $elems[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif ($elemName eq "true") {
|
2005-03-07 14:54:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-03-07 16:26:05 +00:00
|
|
|
elsif ($elemName eq "false") {
|
2005-03-07 14:54:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2005-03-07 16:26:05 +00:00
|
|
|
die "unknown element `$elemName'";
|
2005-03-07 14:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub evalOr {
|
|
|
|
my $storePaths = shift;
|
|
|
|
my $nodes = shift;
|
|
|
|
|
|
|
|
my $result = 0;
|
|
|
|
foreach my $node (@{$nodes}) {
|
2005-03-07 16:26:05 +00:00
|
|
|
$result |= evalCondition($storePaths, $node);
|
2005-03-07 14:54:52 +00:00
|
|
|
}
|
2005-03-04 11:12:48 +00:00
|
|
|
|
2005-03-07 14:54:52 +00:00
|
|
|
return $result;
|
2005-03-04 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Iterate over all elements, check them.
|
|
|
|
foreach my $userEnvElem (@userEnvElems) {
|
|
|
|
|
|
|
|
# Get the deriver of this path.
|
|
|
|
my $deriver = `nix-store --query --deriver '$userEnvElem'`;
|
|
|
|
die "cannot query deriver" if $? != 0;
|
|
|
|
chomp $deriver;
|
|
|
|
|
|
|
|
if ($deriver eq "unknown-deriver") {
|
|
|
|
# print " deriver unknown, cannot check sources\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
print "CHECKING $userEnvElem\n";
|
|
|
|
|
|
|
|
|
|
|
|
# Get the requisites of the deriver.
|
2005-03-07 16:26:05 +00:00
|
|
|
# my $requisites = `nix-store --query --requisites --include-outputs '$deriver'`;
|
|
|
|
# die "cannot query requisites" if $? != 0;
|
|
|
|
# my @requisites = split ' ', $requisites;
|
2005-03-04 11:12:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Get the hashes of the requisites.
|
2005-03-07 16:26:05 +00:00
|
|
|
# my $hashes = `nix-store --query --hash @requisites`;
|
|
|
|
# die "cannot query hashes" if $? != 0;
|
|
|
|
# my @hashes = split ' ', $hashes;
|
|
|
|
# for (my $i = 0; $i < scalar @requisites; $i++) {
|
|
|
|
# die unless $i < scalar @hashes;
|
|
|
|
# my $hash = $hashes[$i];
|
|
|
|
# $storePathHashes{$hash} = {} unless defined $storePathHashes{$hash};
|
|
|
|
# my $r = $storePathHashes{$hash}; # !!! fix
|
|
|
|
# $$r{$requisites[$i]} = 1;
|
|
|
|
# }
|
2005-03-04 11:12:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Evaluate each blacklist item.
|
2005-03-07 14:54:52 +00:00
|
|
|
foreach my $item ($blacklist->getChildrenByTagName("item")) {
|
|
|
|
my $itemId = $item->getAttributeNode("id")->getValue;
|
2005-03-10 11:33:46 +00:00
|
|
|
# print " CHECKING FOR $itemId\n";
|
2005-03-04 11:12:48 +00:00
|
|
|
|
2005-03-07 14:54:52 +00:00
|
|
|
my $condition = ($item->getChildrenByTagName("condition"))[0];
|
|
|
|
die unless $condition;
|
2005-03-04 11:12:48 +00:00
|
|
|
|
|
|
|
# Evaluate the condition.
|
2005-03-07 16:26:05 +00:00
|
|
|
my @elems = getElemNodes $condition;
|
|
|
|
if (evalOr({$deriver => 1}, \@elems)) {
|
2005-03-04 11:12:48 +00:00
|
|
|
# Oops, condition triggered.
|
2005-03-07 14:54:52 +00:00
|
|
|
my $reason = ($item->getChildrenByTagName("reason"))[0]->getChildNodes->to_literal;
|
2005-03-04 11:12:48 +00:00
|
|
|
$reason =~ s/\s+/ /g;
|
|
|
|
$reason =~ s/^\s+//g;
|
|
|
|
|
|
|
|
print " VULNERABLE TO `$itemId': $reason\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|