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

pam: Verify /etc/pam.d/* file contents #146467

Merged
merged 3 commits into from
Nov 27, 2021
Merged

Conversation

l0b0
Copy link
Contributor

@l0b0 l0b0 commented Nov 18, 2021

Motivation for this change

PAM file contents are critical for system security, and verifying their contents with a reasonable configuration can go a long way towards avoiding syntax or semantic errors.

Based on the work mostly by @Artturin in #145574, and inspired by @Xe's The Surreal Horror of PAM.

Current approach

(Thanks for the tips, @Artturin and @thiagokokada!):

  1. Read a file into a Python set.
  2. Verify that all the expected functional lines are there. Ensures that changes to any of the existing lines require changing the test, making any breakage much more visible.
  3. Verify that all the rest of the lines are non-functional, that is, either a comment line or empty. Ensures that additions to the file isn't overlooked.
  4. (TBD): Concatenate tests for each of the relevant files, and include the filename in the error message.

This approach makes TDD easy (change the expected lines, see the test fail, then change the production code), and should make any breakage really easy to spot.

Questions
  • How do you like the current approach?
  • Is this sufficiently granular? That is, would you rather have separate tests for each of the conditionals in nixos/modules/security/pam.nix instead of one per /etc/pam.d/* file? This would make it harder to verify that there are no unexpected functional lines in the files. A hybrid approach which verifies the default contents and checks how the output differs based on various settings could work, but could also end up being fairly complex.
  • Should I verify that the lines occur in a specific sequence? That is, does PAM care about the sequence?
  • Is this sufficiently complete? I might need more than one configuration to test all the possible PAM file contents.

@mkg20001 @ju1m You've both done some non-trivial changes to pam.nix relatively recently. What do you think of the above/the work so far?

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual) N/A
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage N/A
  • Tested basic functionality of all binary files (usually in ./result/bin/) N/A
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added the 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS label Nov 18, 2021
@l0b0 l0b0 force-pushed the test-pam-d-generation branch 2 times, most recently from 266ae4f to b992e8e Compare November 18, 2021 08:02
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild 10.rebuild-linux: 0 This PR does not cause any packages to rebuild labels Nov 18, 2021
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-read-a-file-in-a-nixpkgs-test/16130/1

@Artturin
Copy link
Member

Artturin commented Nov 18, 2021

Theres a existing test that uses grep to check the file contents https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/pam-u2f.nix

might be useful https://github.com/pbrezina/pam-test

nixos/tests/pam.nix Outdated Show resolved Hide resolved
@l0b0 l0b0 marked this pull request as ready for review November 22, 2021 05:24
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-best-verify-etc-pam-d-file-contents/16217/1

"session required pam_env.so conffile=/etc/pam/environment readenv=0",
"session required pam_unix.so",
}
actual_lines = set(machine.succeed("cat /etc/pam.d/chfn").splitlines())
Copy link
Member

Choose a reason for hiding this comment

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

If you want to you could add more modules and also test more configuration files to get a good coverage

such as pam.d/login, pam.d/sudo, etc and some modules from https://search.nixos.org/options?channel=21.05&from=0&size=50&sort=relevance&type=packages&query=security.pam

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's the plan, as soon as I get enough feedback on this that I'm fairly certain the result can be merged without major rework.

@l0b0
Copy link
Contributor Author

l0b0 commented Nov 27, 2021

Would it be possible to merge this as-is? Then I can create a separate PR with more tests.

@Artturin
Copy link
Member

Artturin commented Nov 27, 2021

Please move the other pam tests in to the pam subdirectory
and add a comment to the pam module above where the rules are declared to remind people to update the test|s after changing them

then i will merge this because we can always rework it since its a test and not user facing

@Artturin
Copy link
Member

created a tracking issue for pam #147565

@github-actions github-actions bot added the 8.has: module (update) This PR changes an existing module in `nixos/` label Nov 27, 2021
@Artturin
Copy link
Member

Artturin commented Nov 27, 2021

nix build ".#nixosTests.pam-oath-login"
error: getting status of '/nix/store/76yr5ig54sbjfp66xl0vpqhd9qldqjd4-source/nixos/tests/pam/make-test-python.nix': No such file or directory

i fixed it for you

waiting for ofborg and then merging

@Artturin Artturin merged commit 16eb003 into NixOS:master Nov 27, 2021
@Ma27
Copy link
Member

Ma27 commented Nov 27, 2021

Isn't a full-featured VM test a little heavy to test a bunch of generated files?

@l0b0 l0b0 deleted the test-pam-d-generation branch November 28, 2021 19:22
@l0b0
Copy link
Contributor Author

l0b0 commented Nov 28, 2021

Isn't a full-featured VM test a little heavy to test a bunch of generated files?

I just copied the pattern from other tests. If you know of a more light-weight way to do it (with the same kind of guarantees about testing the actually resulting files rather than an intermediate result) I'd be interested.

mkg20001 pushed a commit to mkg20001/nixpkgs that referenced this pull request Nov 29, 2021
@Ma27
Copy link
Member

Ma27 commented Dec 12, 2021

I just copied the pattern from other tests

Well, other tests check for functionality in a VM, we only check build products after we have booted into a VM.

You could either

  • check inside the testScript the build products (in our case, PAM files) without booting the VM
  • use something like pkgs-tests and move the formatting logic of PAM into its own function.

EDIT:L I'm open to other, better ideas though :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild 10.rebuild-linux: 0 This PR does not cause any packages to rebuild
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants