Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix passing CA files into builtins:fetchurl sandbox (backport #11610) #11643

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1731,13 +1731,27 @@ void LocalDerivationGoal::runChild()

bool setUser = true;

/* Make the contents of netrc available to builtin:fetchurl
(which may run under a different uid and/or in a sandbox). */
/* Make the contents of netrc and the CA certificate bundle
available to builtin:fetchurl (which may run under a
different uid and/or in a sandbox). */
std::string netrcData;
<<<<<<< HEAD:src/libstore/build/local-derivation-goal.cc
try {
if (drv->isBuiltin() && drv->builder == "builtin:fetchurl")
netrcData = readFile(settings.netrcFile);
} catch (SysError &) { }
=======
std::string caFileData;
if (drv->isBuiltin() && drv->builder == "builtin:fetchurl") {
try {
netrcData = readFile(settings.netrcFile);
} catch (SystemError &) { }

try {
caFileData = readFile(settings.caFile);
} catch (SystemError &) { }
}
>>>>>>> c1ecf0bee (fix passing CA files into builtins:fetchurl sandbox):src/libstore/unix/build/local-derivation-goal.cc

#if __linux__
if (useChroot) {
Expand Down Expand Up @@ -2185,7 +2199,11 @@ void LocalDerivationGoal::runChild()
e.second = rewriteStrings(e.second, inputRewrites);

if (drv->builder == "builtin:fetchurl")
<<<<<<< HEAD:src/libstore/build/local-derivation-goal.cc
builtinFetchurl(drv2, netrcData);
=======
builtinFetchurl(*drv, outputs, netrcData, caFileData);
>>>>>>> c1ecf0bee (fix passing CA files into builtins:fetchurl sandbox):src/libstore/unix/build/local-derivation-goal.cc
else if (drv->builder == "builtin:buildenv")
builtinBuildenv(drv2);
else if (drv->builder == "builtin:unpack-channel")
Expand Down
12 changes: 12 additions & 0 deletions src/libstore/builtins.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
namespace nix {

// TODO: make pluggable.
<<<<<<< HEAD
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is trash.

void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData);
void builtinUnpackChannel(const BasicDerivation & drv);
=======
void builtinFetchurl(
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs,
const std::string & netrcData,
const std::string & caFileData);

void builtinUnpackChannel(
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs);
>>>>>>> c1ecf0bee (fix passing CA files into builtins:fetchurl sandbox)

}
17 changes: 17 additions & 0 deletions src/libstore/builtins/fetchurl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@

namespace nix {

<<<<<<< HEAD
void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData)
=======
void builtinFetchurl(
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs,
const std::string & netrcData,
const std::string & caFileData)
>>>>>>> c1ecf0bee (fix passing CA files into builtins:fetchurl sandbox)
{
/* Make the host's netrc data available. Too bad curl requires
this to be stored in a file. It would be nice if we could just
Expand All @@ -16,11 +24,20 @@ void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData)
writeFile(settings.netrcFile, netrcData, 0600);
}

<<<<<<< HEAD
auto getAttr = [&](const std::string & name) {
auto i = drv.env.find(name);
if (i == drv.env.end()) throw Error("attribute '%s' missing", name);
return i->second;
};
=======
settings.caFile = "ca-certificates.crt";
writeFile(settings.caFile, caFileData, 0600);

auto out = get(drv.outputs, "out");
if (!out)
throw Error("'builtin:fetchurl' requires an 'out' output");
>>>>>>> c1ecf0bee (fix passing CA files into builtins:fetchurl sandbox)

Path storePath = getAttr("out");
auto mainUrl = getAttr("url");
Expand Down
84 changes: 84 additions & 0 deletions tests/nixos/fetchurl.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Test whether builtin:fetchurl properly performs TLS certificate
# checks on HTTPS servers.

{ pkgs, ... }:

let

makeTlsCert = name: pkgs.runCommand name {
nativeBuildInputs = with pkgs; [ openssl ];
} ''
mkdir -p $out
openssl req -x509 \
-subj '/CN=${name}/' -days 49710 \
-addext 'subjectAltName = DNS:${name}' \
-keyout "$out/key.pem" -newkey ed25519 \
-out "$out/cert.pem" -noenc
'';

goodCert = makeTlsCert "good";
badCert = makeTlsCert "bad";

in

{
name = "nss-preload";

nodes = {
machine = { pkgs, ... }: {
services.nginx = {
enable = true;

virtualHosts."good" = {
addSSL = true;
sslCertificate = "${goodCert}/cert.pem";
sslCertificateKey = "${goodCert}/key.pem";
root = pkgs.runCommand "nginx-root" {} ''
mkdir "$out"
echo 'hello world' > "$out/index.html"
'';
};

virtualHosts."bad" = {
addSSL = true;
sslCertificate = "${badCert}/cert.pem";
sslCertificateKey = "${badCert}/key.pem";
root = pkgs.runCommand "nginx-root" {} ''
mkdir "$out"
echo 'foobar' > "$out/index.html"
'';
};
};

security.pki.certificateFiles = [ "${goodCert}/cert.pem" ];

networking.hosts."127.0.0.1" = [ "good" "bad" ];

virtualisation.writableStore = true;

nix.settings.experimental-features = "nix-command";
};
};

testScript = ''
machine.wait_for_unit("nginx")
machine.wait_for_open_port(443)

out = machine.succeed("curl https://good/index.html")
assert out == "hello world\n"

out = machine.succeed("cat ${badCert}/cert.pem > /tmp/cafile.pem; curl --cacert /tmp/cafile.pem https://bad/index.html")
assert out == "foobar\n"

# Fetching from a server with a trusted cert should work.
machine.succeed("nix build --no-substitute --expr 'import <nix/fetchurl.nix> { url = \"https://good/index.html\"; hash = \"sha256-qUiQTy8PR5uPgZdpSzAYSw0u0cHNKh7A+4XSmaGSpEc=\"; }'")

# Fetching from a server with an untrusted cert should fail.
err = machine.fail("nix build --no-substitute --expr 'import <nix/fetchurl.nix> { url = \"https://bad/index.html\"; hash = \"sha256-rsBwZF/lPuOzdjBZN2E08FjMM3JHyXit0Xi2zN+wAZ8=\"; }' 2>&1")
print(err)
assert "SSL certificate problem: self-signed certificate" in err

# Fetching from a server with a trusted cert should work via environment variable override.
machine.succeed("NIX_SSL_CERT_FILE=/tmp/cafile.pem nix build --no-substitute --expr 'import <nix/fetchurl.nix> { url = \"https://bad/index.html\"; hash = \"sha256-rsBwZF/lPuOzdjBZN2E08FjMM3JHyXit0Xi2zN+wAZ8=\"; }'")
'';
}
Loading