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/system-environment: fix variable substitution #70137

Merged
merged 2 commits into from Oct 2, 2019

Conversation

michaelpj
Copy link
Contributor

Motivation for this change

There were two issues:

  • Variables weren't being replaced in the values of variables, only in the profiles.
  • HOME isn't a PAM_ITEM so we need to use ${HOME}.

Fixes #69316.

Someone other than me should also test this!

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)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.
Notify maintainers

cc @worldofpeace

Copy link
Contributor

@worldofpeace worldofpeace left a comment

Choose a reason for hiding this comment

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

LGTM fixes the current issue.

Though there's more PAM_ITEMS that we could map and replace

and it would be good if somehow, all environment variables $SOMETHING
got substituted in the PAM format.

TBH, we should be using environment.d for all of these things.

@michaelpj
Copy link
Contributor Author

Yeah, although we're doing something a bit weird here in matching up normal environment variables and PAM_ITEMS. I'm not sure that there are many things which have sensible interpretations as environment variables and also as PAM_ITEMS. We could try and replace all other environment variables with ${VAR}, but I'm not confident in writing something that will match all environment variable dereferences 😅

We were only replacing them in the profiles. We also need to do this in
the values of variables, including both the session-relative variables
and the non-session-relative variables.
`@` synax is for `PAM_ITEM`s, `HOME` needs to use `$`.
@worldofpeace worldofpeace changed the title pam: fix variable substitution nixos/system-environment: fix variable substitution Oct 2, 2019
@worldofpeace worldofpeace merged commit 49bc1bf into NixOS:master Oct 2, 2019
@worldofpeace
Copy link
Contributor

worldofpeace commented Oct 2, 2019

Yeah, although we're doing something a bit weird here in matching up normal environment variables and PAM_ITEMS. I'm not sure that there are many things which have sensible interpretations as environment variables and also as PAM_ITEMS. We could try and replace all other environment variables with ${VAR}, but I'm not confident in writing something that will match all environment variable dereferences sweat_smile

Right, so it was only a suggestion for maybe someone else in the future to maybe notice 😄

Thanks again for fixing this 👍

dtzWill pushed a commit to dtzWill/nixpkgs that referenced this pull request Oct 2, 2019
nixos/system-environment: fix variable substitution

(cherry picked from commit 49bc1bf)
# We're trying to use the same syntax for PAM variables and env variables.
# That means we need to map the env variables that people might use to their
# equivalent PAM variable.
# Note: PAM_USER is a PAM_ITEM, HOME is an environment variable, they have
Copy link
Member

Choose a reason for hiding this comment

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

So actually '@{HOME}is special, and there are important reasons for preferring it which are outlined inman pam_env.conf`:

(Possibly non-existent) environment variables may be used in values using the ${string} syntax and (possibly non-existent)
PAM_ITEMs as well as HOME and SHELL may be used in values using the @{string} syntax. Both the $ and @ characters can be
backslash escaped to be used as literal values values can be delimited with "", escaped " not supported. Note that many
environment variables that you would like to use may not be set by the time the module is called. For example, ${HOME} is
used below several times, but many PAM applications don't make it available by the time you need it. The special variables
@{HOME} and @{SHELL} are expanded to the values for the user from his passwd entry.

I remember running into problems when using $HOME instead of @{HOME} (as the quoted bit above says, HOME often isn't set by the time it's needed) but bouncing through commit logs on various machines I'm not finding evidence of such a (mis-)adventure. It may have been related to sshd/sudo, but regardless I am inclined to suggest we continue to prefer @{HOME} in pam-processed bits. That said I'm still sorting through this and similar recent changes and don't quite have it all in my head just yet :).

Copy link
Contributor

Choose a reason for hiding this comment

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

If the docs say that, it's still a fairly easy change to revert

- replaceEnvVars = replaceStrings ["$HOME" "$USER"] ["\${HOME}" "@{PAM_USER}"];
+ replaceEnvVars = replaceStrings ["$HOME" "$USER"] ["@{HOME}" "@{PAM_USER}"];

and wasn't the motivation for this change anyways.

Copy link
Contributor

Choose a reason for hiding this comment

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

@dtzWill btw you'd be an excellent reviewer for #70430 (I think anyways) 😄

Copy link
Member

Choose a reason for hiding this comment

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

Ah, here we go. Test is sudo env-- with the latest changes it's clear everything is wrong (HOME isn't set as described above, so all the variables are borked), reverting to use @{HOME} fixes them again.

Please confirm/test and let me know what you find.

Possibly part of what's going on with #70418, but don't have a full story explaining that behavior just yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh I see. Testing this change now. PR incoming since it should be completely fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

@dtzWill Yep sudo env now looks right.

Copy link
Member

Choose a reason for hiding this comment

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

❤️ thanks for quick response, will take a look! And yes I don't think this will be hard to fix or require big reverts or anything :).

dtzWill added a commit to dtzWill/nixpkgs that referenced this pull request Oct 5, 2019
@michaelpj
Copy link
Contributor Author

Rats, sorry, I read the documentation but evidently not enough! What I thought was a comprehensive list of PAM_ITEMS didn't have HOME.

@dtzWill
Copy link
Member

dtzWill commented Oct 5, 2019

No worries, I'm not even sure this means it's a "PAM_ITEM" or if pam_env handles them in addition to "actual" "PAM_ITEM"s or if we care if it's a "PAM_ITEM" or not 😁. I saw you mention checking pam_get_id which of course doesn't mention HOME, so dunno. I'm pretty sure I only know about this because I borked it myself before haha.

@worldofpeace
Copy link
Contributor

Rats, sorry, I read the documentation but evidently not enough! What I thought was a comprehensive list of PAM_ITEMS didn't have HOME.

That's what I got from the doc as well. And no worries, you might have noticed even the best of break things regularly 😄 As long as the release doesn't see it we're usually fine.

dtzWill added a commit to dtzWill/nixpkgs that referenced this pull request Oct 5, 2019
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.

Replace Non Pam variables in sessionVariables
3 participants