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

nixos/sshd: validate ssh configs during build #58718

Merged
merged 1 commit into from May 24, 2019

Conversation

Ma27
Copy link
Member

@Ma27 Ma27 commented Apr 1, 2019

Motivation for this change

With sshd -t config validation for SSH is possible. Until now, the
config generated by Nix was applied without any validation (which is
especially a problem for advanced config like Match blocks).

When deploying broken ssh config with nixops to a remote machine it gets
even harder to fix the problem due to the broken ssh that makes reverts
with nixops impossible.

This change performs the validation in a Nix build environment by
creating a store path with the config and generating a mocked host key
which seems to be needed for the validation. With a broken config, the
deployment already fails during the build of the derivation.

The original attempt was done in #56345 by adding a submodule for Match
groups to make it harder screwing that up, however that made the module
far more complex and config should be described in an easier way as
described in NixOS/rfcs#42.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nix-review --run "nix-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Assured whether relevant documentation is up to date
  • Fits CONTRIBUTING.md.

@aanderse
Copy link
Member

aanderse commented May 18, 2019

Will validation fail if someone utilizes the sshd Include command in their extraConfig?

What if I'm running nixops and specify a ChrootDirectory directory that only exist on the target machine?

Does DenyGroups get validated and fail if the groups don't exist?

If sshd -t doesn't depend on the system environment too heavily this PR is fine, but generally these validation steps implemented this way concern me because they usually don't concretely validate anything but instead simply let you know that your configuration will probably work... sometimes... hopefully... as long as you didn't do anything too exotic.

I don't want to be sounding too negative on this topic, but when a few attempts at implementing validation on the httpd service ran into all sorts of problems I was very discouraged.

It's a shame there isn't some sort of stage between the building and activation of a generation that checks like this can run and then abort activation if the checks do not see succeed, leaving the user on the current generation without having cycled any services.

@Ma27
Copy link
Member Author

Ma27 commented May 23, 2019

Will validation fail if someone utilizes the sshd Include command in their extraConfig?

Unless I'm missing something, this won't break validation (tested with sshd from OpenSSH_7.9p1 on NixOS 19.03).

Does DenyGroups get validated and fail if the groups don't exist?

Same here. It seems as no content is validated, however syntax and the specified options.

I don't want to be sounding too negative on this topic, but when a few attempts at implementing validation on the httpd service ran into all sorts of problems I was very discouraged.

IIRC this is also implemented for prometheus and I actually liked it while using that module.
Can you please send me a link to the discussion? I'd be very interested in learning which problems were faced there :)

It's a shame there isn't some sort of stage between the building and activation of a generation that checks like this can run and then abort activation if the checks do not see succeed, leaving the user on the current generation without having cycled any services.

That's actually an interesting idea for the "Future work" section of RFC#42 which actually recommends the use of config validation tools if available/possible.

@aanderse
Copy link
Member

@Ma27 the history of this problem actually goes back years...

There are more issues/prs which discuss this, but the list above is all I could find with minimal effort.

@Ma27
Copy link
Member Author

Ma27 commented May 24, 2019

So unless I'm missing something, the main issues with config validation at build time are (1) issues with cross-builds and (2) false-positives when having a complex config that only works in a certain environment.

(1) should be solvable by using nativeBuildInputs in the derivation that validates the config (I'll change that later that day)
(2) doesn't seem to be a problem according to my test results. To quote sshd(8):

       -T     Extended test mode.  Check the validity of the configuration file, output  the  effective
              configuration  to stdout and then exit.  Optionally, Match rules may be applied by speci‐
              fying the connection parameters using one or more -C options.

       -t     Test mode.  Only check the validity of the configuration file and  sanity  of  the  keys.
              This is useful for updating sshd reliably as configuration options may change

If I understand thsi correctly, my approach simply checks the syntax and the used configuration keys.

Does this look ok to you? Or am I missing something?

@aanderse
Copy link
Member

@Ma27 The apacheHttpd configuration validation validates the entire environment referenced by the configuration file, not merely syntax. In the case that a program validates environment referenced by a configuration file this approach can't work the way things are because configuration files might reference users that don't exist yet, or files that are on nixops targets but not the nixops host.

It looks like you have confirmed that the ssh configuration validation doesn't actually validate the environment at all, so in this case I'm happy that this PR is good to go 👍

With `sshd -t` config validation for SSH is possible. Until now, the
config generated by Nix was applied without any validation (which is
especially a problem for advanced config like `Match` blocks).

When deploying broken ssh config with nixops to a remote machine it gets
even harder to fix the problem due to the broken ssh that makes reverts
with nixops impossible.

This change performs the validation in a Nix build environment by
creating a store path with the config and generating a mocked host key
which seems to be needed for the validation. With a broken config, the
deployment already fails during the build of the derivation.

The original attempt was done in NixOS#56345 by adding a submodule for Match
groups to make it harder screwing that up, however that made the module
far more complex and config should be described in an easier way as
described in NixOS/rfcs#42.
@fpletz fpletz merged commit eb7c11d into NixOS:master May 24, 2019
@Ma27 Ma27 deleted the validate-ssh-configs branch May 24, 2019 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants