From 60737bd31985116b7cce47f221e8568e66ab73e0 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Tue, 22 May 2018 19:53:28 -0400 Subject: [PATCH 1/2] dockerTools: fixes extraCommands for mkRootLayer. The extraCommands was, previously, simply put in the body of the script using nix expansion `${extraCommands}` (which looks exactly like bash expansion!). This causes issues like in #34779 where scripts will eventually create invalid bash. The solution is to use a script like `run-as-root`. * * * Fixes #34779 --- pkgs/build-support/docker/default.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index 374b71d42a39..34af6ad65b9f 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -360,7 +360,9 @@ rec { extraCommands ? "" }: # Generate an executable script from the `runAsRoot` text. - let runAsRootScript = shellScript "run-as-root.sh" runAsRoot; + let + runAsRootScript = shellScript "run-as-root.sh" runAsRoot; + extraCommandsScript = shellScript "extra-commands.sh" extraCommands; in runWithOverlay { name = "docker-layer-${name}"; @@ -398,7 +400,7 @@ rec { ''; postUmount = '' - (cd layer; eval "${extraCommands}") + (cd layer; ${extraCommandsScript}) echo "Packing layer..." mkdir $out From 902b0593be857e4f19b9875638af6e854aa99a9b Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Tue, 22 May 2018 20:25:04 -0400 Subject: [PATCH 2/2] tests/docker-tools: Adds regression test for #34779 --- nixos/tests/docker-tools.nix | 6 ++++++ pkgs/build-support/docker/examples.nix | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/nixos/tests/docker-tools.nix b/nixos/tests/docker-tools.nix index 4466081d01e9..e2bcfbbd1f96 100644 --- a/nixos/tests/docker-tools.nix +++ b/nixos/tests/docker-tools.nix @@ -45,5 +45,11 @@ import ./make-test.nix ({ pkgs, ... }: { $docker->succeed("docker load --input='${pkgs.dockerTools.examples.onTopOfPulledImage}'"); $docker->succeed("docker run --rm ontopofpulledimage hello"); $docker->succeed("docker rmi ontopofpulledimage"); + + # Regression test for issue #34779 + $docker->succeed("docker load --input='${pkgs.dockerTools.examples.runAsRootExtraCommands}'"); + $docker->succeed("docker run --rm runasrootextracommands cat extraCommands"); + $docker->succeed("docker run --rm runasrootextracommands cat runAsRoot"); + $docker->succeed("docker rmi '${pkgs.dockerTools.examples.runAsRootExtraCommands.imageName}'"); ''; }) diff --git a/pkgs/build-support/docker/examples.nix b/pkgs/build-support/docker/examples.nix index eb5b9fe36e41..ca7f78093794 100644 --- a/pkgs/build-support/docker/examples.nix +++ b/pkgs/build-support/docker/examples.nix @@ -124,4 +124,16 @@ rec { fromImage = nixFromDockerHub; contents = [ pkgs.hello ]; }; + + # 8. regression test for erroneous use of eval and string expansion. + # See issue #34779 and PR #40947 for details. + runAsRootExtraCommands = pkgs.dockerTools.buildImage { + name = "runAsRootExtraCommands"; + contents = [ pkgs.coreutils ]; + # The parens here are to create problematic bash to embed and eval. In case + # this is *embedded* into the script (with nix expansion) the initial quotes + # will close the string and the following parens are unexpected + runAsRoot = ''echo "(runAsRoot)" > runAsRoot''; + extraCommands = ''echo "(extraCommand)" > extraCommands''; + }; }