Skip to contentSkip to conversation

login-utils: import environment from user manager on systemd systems

PATCH In review Ray Strode 4 participants 12 messages Oct 25, 2016 – Dec 8, 2016 Util-Linux package

lore
Share & tools
lore

Ray Strode

author
#0 lore raw

PATCH 1/1 login-utils: import environment from user manager on systemd systems

From: Ray Strode <[email protected]>

If the user is using a systemd system, then its useful to grab the environment from the systemd user manager process.

This allows administrators to initialize the environment of the sessions via systemd configuration.

Reference: https://github.com/systemd/systemd/pull/3904

---
 login-utils/Makemodule.am |  5 +++
 login-utils/login.c       | 84 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
login-utils/Makemodule.am +5 -0
index be07ace..d65bfc1 100644
@@ -16,70 +16,75 @@ endif
 if BUILD_SULOGIN
 sbin_PROGRAMS += sulogin
 dist_man_MANS += login-utils/sulogin.8
 sulogin_SOURCES = \
 	login-utils/sulogin.c \
 	login-utils/sulogin-consoles.c \
 	login-utils/sulogin-consoles.h
 sulogin_LDADD = $(LDADD) libcommon.la
 
 if HAVE_LIBCRYPT
 sulogin_LDADD += -lcrypt
 endif
 if HAVE_SELINUX
 sulogin_LDADD += -lselinux
 endif
 
 check_PROGRAMS += test_consoles
 test_consoles_SOURCES = login-utils/sulogin-consoles.c
 test_consoles_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM
 test_consoles_LDADD = $(LDADD) libcommon.la
 endif # BUILD_SULOGIN
 
 
 if BUILD_LOGIN
 bin_PROGRAMS += login
 dist_man_MANS += login-utils/login.1
 login_SOURCES = \
 	login-utils/login.c \
 	login-utils/logindefs.c \
 	login-utils/logindefs.h
+login_CFLAGS =
 login_LDADD = $(LDADD) libcommon.la -lpam
 if HAVE_LINUXPAM
 login_LDADD += -lpam_misc
 endif
 if HAVE_AUDIT
 login_LDADD += -laudit
 endif
 if HAVE_SELINUX
 login_LDADD += -lselinux
 endif
+if HAVE_SYSTEMD
+login_LDADD += $(SYSTEMD_LIBS)
+login_CFLAGS += $(SYSTEMD_CFLAGS)
+endif
 endif # BUILD_LOGIN
 
 
 if BUILD_NOLOGIN
 sbin_PROGRAMS += nologin
 dist_man_MANS += login-utils/nologin.8
 nologin_SOURCES = login-utils/nologin.c
 endif
 
 
 if BUILD_UTMPDUMP
 usrbin_exec_PROGRAMS += utmpdump
 dist_man_MANS += login-utils/utmpdump.1
 utmpdump_SOURCES = login-utils/utmpdump.c
 utmpdump_LDADD = $(LDADD) libcommon.la
 endif
 
 
 if BUILD_CHFN_CHSH
 usrbin_exec_PROGRAMS += chfn chsh
 dist_man_MANS += \
 	login-utils/chfn.1 \
 	login-utils/chsh.1
 
 chfn_chsh_sources = \
 	login-utils/ch-common.h \
 	login-utils/ch-common.c
 chfn_chsh_cflags = $(SUID_CFLAGS) $(AM_CFLAGS)
 chfn_chsh_ldflags = $(SUID_LDFLAGS) $(AM_LDFLAGS)
 chfn_chsh_ldadd = libcommon.la
login-utils/login.c +84 -0

261 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw
Quoted 8 lines · Ray Strode

From: Ray Strode <[email protected]>

If the user is using a systemd system, then its useful to grab the environment from the systemd user manager process.

This allows administrators to initialize the environment of the sessions via systemd configuration.

---
Isn't pam_env supposed to allow setting vars in setting up
a user's first login session?

(from /etc/security/pam_env.conf)
# This is the configuration file for pam_env, a PAM module to load in

# a configurable list of environment variables for an _authorized_ session.
#
# The original idea for this came from Andrew G. Morgan ...
#<quote>
#   Mmm. Perhaps you might like to write a pam_env module that reads a
#   default environment from a file? I can see that as REALLY
#   useful... Note it would be an "auth" module that returns PAM_IGNORE
#   for the auth part and sets the environment returning PAM_SUCCESS in
#   the setcred function...
#</quote>
#
# What I wanted was the REMOTEHOST variable set...

As an aside, I use that to set my DISPLAY to the remote host on 1st login.

It's only available when a user is establishing a session from a
remote system. Calling it at other times won't have remotehost set.
(basically values of REMOTEHOST & DISPLAY should be kept intact much like the TERM or LANG settigns.

author
#2 lore raw

Hi,

Quoted 2 lines

Isn't pam_env supposed to allow setting vars in setting up
a user's first login session?

It can, but it has a few problems

1) the file format is kind of weird
2) not all distros set user_readenv=1 so users cant set the environment variables they want
3) pam_env doesn't provide a facility for 3rd party applications to adjust the environment
4) PAM modules run in the context of a user but as root. Having a bunch of independent plugins all running as root, and not necessarily integrating with each other is a recipe for security problems, especially if you throw environment variables into the mix. (if pam_env is erroneously in the session stack before a pam_exec call, the user could easily get root access)

This topic was discussed a bit in the cited systemd pull request, I think.

--Ray

#3 lore raw
Quoted 9 lines · Ray Strode

Hi,

Quoted 3 lines

Isn't pam_env supposed to allow setting vars in setting up
a user's first login session?

It can, but it has a few problems

1) the file format is kind of weird

---
People looking at systemd's config files have said similar.

Quoted 3 lines

2) not all distros set user_readenv=1 so users cant set the environment variables they want

----
pam_env wasn't designed for reading user-set environment variables.
Can users modify systemd config files at will? Can't users put their envvars in their profiles?

Quoted 3 lines

3) pam_env doesn't provide a facility for 3rd party applications to adjust the environment

---
I wasn't aware systemd could read users' profiles. I've seen
many 3rd party apps add their needed env vars to the system "profile.d" directory (for system wide changes) and some to a user's shell if they were user-specific changes.

Quoted 1 line

4) PAM modules run in the context of a user but as root.

----
Most login related and system security processes do. They are not
part of the kernel. Systemd runs in a user-context as root, as well. Are you saying that is insecure?

` Pam_env is designed to be run before the user's first session has
been setup and not again. Some environment variables are meant to have the same lifetime as the login-(or audit) UID. Just like TERM, any X programs rely on DISPLAY and those things don't change unless a new first-contact session is created when a user first accesses a secure network from an outside location.

Quoted 5 lines

Having a
bunch of independent plugins all running as root, and not necessarily integrating with each other is a recipe for security problems, especially if you throw environment variables into the mix.

----
True. The pam modules have been vetted for about 10+ years and have
good
security record, yet you seem quite willing to jump to a new solution without such a track record.

Quoted 3 lines

(if pam_env is erroneously in the session stack before a pam_exec call, the user could easily get root access).

If an administrator misconfigures ANY system security and authentication modules a user could gain root access. Pam_env was designed for a first-contact to the system, as that's the only time 'REMOTEHOST' is set (presuming the user came from another system). Does systemd set REMOTEHOST and allow for a users DISPLAY variable to be defaulted to a dependent setting?

Quoted 2 lines

This topic was discussed a bit in the cited systemd pull request, I think.

----
how is that relevant to a util-linux list?
-l

author
#4 lore raw

Hi,

Quoted 4 lines

----
Most login related and system security processes do. They are not
part of the kernel. Systemd runs in a user-context as root, as well. Are you saying that is insecure?

I'm not sure why you're bringing up the kernel, that's a weird segue, but no I'm not saying it's necessarily insecure to run user session code as root, I'm just saying if you minimize the code running as root, you minimize risk. Environment variables in particular are a large surface area for problems.

Quoted 2 lines

Pam_env is designed to be run before the user's first session has been setup and not again.

That's not correct. Most distributions that use pam_env, run it for any login session. But what does that have to do with my patch?

Quoted 5 lines

Some environment variables are meant to have
the same lifetime as the login-(or audit) UID. Just like TERM, any X programs rely on DISPLAY and those things don't change unless a new first-contact session is created when a user first accesses a secure network from an outside location.

You lost me. I think you're trying to explain to me what the scope of DISPLAY and TERM are, but I'm not sure, and I don't know why you would be doing that. Can you reiterate your point more clearly?

Quoted 2 lines

True. The pam modules have been vetted for about 10+ years and have good
security record,

An okay track record, sure, but that doesn't somehow invalidate my point. pam_env, in particular, has had a couple of CVEs, but either way it misses the point. This is about preemptive mitigation, we can choose where to put the code, and one spot is inherently less risky than the other.

Quoted 1 line

yet you seem quite willing to jump to a new solution without such a track record.

This was all discussed on the systemd pull request that you for some reason
won't read
(why? it's just a link with background information).

Originally, there was a patch to put the code in pam_systemd, but that idea was ultimately rejected, because environment variables are an easy way to perturb a system in unexpected ways. Could it be done there? sure. Is it riskier? yes! why
incur the risk?

Quoted 3 lines

Pam_env was designed for a
first-contact to the system, as that's the only time 'REMOTEHOST' is set (presuming the user came from another system).

what? I think you have some side point, but you're not articulating it well. pam_env can be configured to set REMOTEHOST from PAM_RHOST.
If so configured it will happen from any remotely initiated connection, not just the first.

And indeed, REMOTEHOST is a commented out example provided in the default pam_env conf file. It's not used by default, but I really don't see how this has anything to do with my patch.

Quoted 3 lines

Does systemd set REMOTEHOST
and allow for a users DISPLAY variable to be defaulted to a dependent setting?

No of course not. The patch is about importing non-session-specific, user environment variables into new sessions. systemd --user runs outside of a typical user session, and this makes sure environment variables configured for systemd --user get picked up by new login sessions.

If you want REMOTEHOST set to PAM_RHOST fine, but that's nothing to do with this. Anyway, ssh already sets DISPLAY for you. If you're using rsh or telnet, well...I don't know what to say other than this patch isn't getting in your way.

Quoted 2 lines
Quoted 1 line

This topic was discussed a bit in the cited systemd pull request, I think.

how is that relevant to a util-linux list?

It's extremely relevant to the patch, since the patch is motivated by the discussion there. How is that relevance not clear?

Do you just have mental block because "systemd" != "util-linux" ? util-linux already integrates with systemd. It's not like this is the first
#ifdef HAVE_LIBSYSTEMD added to the code.

Anyway, I talked with Karel about this approach before I started, and he's tentatively onboard with the concept (again, see the cited url!)

I'd appreciate patch review, if you want to jump in, but I don't want to get bogged down in a rehash of a discussion that's already been had elsewhere.

--Ray

#5 lore raw
In reply to Ray Strode
Quoted 5 lines · Ray Strode

From: Ray Strode <[email protected]>

If the user is using a systemd system, then its useful to grab the environment from the systemd user manager process.

----
The other issue you don't really touch on, is why should this be hard-
coded in various programs when this is seems exactly like why PAM was invented.

I.e. why not have a PAM module, say "pam_sysd_env", that gets the
env from the systemd user module(s), so any programs using PAM can include in their security/authentication stack, the same way pam_env might be used now (not that they are necessarily mutually exclusive).

One of the issues with systemd is that it is doesn't re-use
existing interfaces, but tends to force programs to make changes to work with sysd, where if it used the existing interfaces, no program-specific changes might be required and no explicit dependencies are built in tools so that the same tools can be used on sysd systems, vs. non-sysd systems.

Minimizing code changes in existing, working code seems like a
safer design decision. It is especially true that the more code paths that are added to programs, the more test paths and opportunities for failure are added.

author
#6 lore raw

Hi,

Quoted 3 lines

The other issue you don't really touch on, is why should this be hard-
coded in various programs when this is seems exactly like why PAM was invented.

Oh this was touched on, thoroughly, in the URL cited in the patch.

Quoted 3 lines

One of the issues with systemd is that it is doesn't re-use
existing interfaces, but tends to force programs to make changes to work with sysd

There is no existing interface that meets the needs here. The closest you can get is a pam session stack module, but as stated before, that idea has been rejected (and I've already covered why on this thread and on the systemd pull request).

Quoted 2 lines

Minimizing code changes in existing, working code seems like a
safer design decision.

Safer than needlessly running code as root that doesn't need to be? We're just going to have to agree to disagree I guess.

Quoted 3 lines

It is especially true that the more code paths
that are added to programs, the more test paths and opportunities for failure are added.

This I totally agree with!

--Ray

author
#7 lore raw
In reply to Ray Strode

Hi Karel,

Quoted 7 lines · Ray Strode

From: Ray Strode <[email protected]>

If the user is using a systemd system, then its useful to grab the environment from the systemd user manager process.

This allows administrators to initialize the environment of the sessions via systemd configuration.

Any news on this?

--Ray

#8 lore raw
Quoted 3 lines · Ray Strode

Hi Karel,

On Tue, Oct 25, 2016 at 4:34 PM, Ray Strode <[email protected]>

Quoted 9 lines · wrote:
Quoted 7 lines

From: Ray Strode <[email protected]>

If the user is using a systemd system, then its useful to grab the environment from the systemd user manager process.

This allows administrators to initialize the environment of the sessions via systemd configuration.

Any news on this?

I really don't like this as already discussed.

Quoted 5 lines

--Ray
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#9 lore raw
Quoted 10 lines · Ruediger Meier
Quoted 3 lines · Ray Strode

Hi Karel,

On Tue, Oct 25, 2016 at 4:34 PM, Ray Strode <[email protected]>

Quoted 5 lines · wrote:
Quoted 5 lines

From: Ray Strode <[email protected]>

If the user is using a systemd system, then its useful to grab the environment from the systemd user manager process.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

This allows administrators to initialize the environment of the sessions via systemd configuration.

Any news on this?

I really don't like this as already discussed.

I had discussion about it with Ray on IRC, the conclusion has been that the ideal solution would be to extend PAM to support another stack of modules. This stack will be executed on open PAM session after switch from root to non-root user (after setuid()).

We had positive feedback about it from Red Hat PAM maintainer, but it would be probably better to discuss it with PAM upstream. This change will require a new stuff in PAM API.

The question is what to do *now* before the PAM will be extended. The most simple solution will be to temporary merge Ray's patch for only util-linux v2.30 (= for year 2017) to provide guys time to found a better solution.

It's important to say that v2.30 is expected in March/April, it's a long time to do a change in PAM...

Note, the issue is not about "bad boys from systemd camp". The motivation for the change is wayland. The systemd has been selected as a place where is possible to solve the problem.

Although I'm not sure why we need systemd as mediator, would be possible to read the environment setting directly by login(1)? Or it's expected that something (e.g. script) will be executed to get the session initialized?

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
author
#10 lore raw

Hi,

Quoted 4 lines · Karel Zak

We had positive feedback about it from Red Hat PAM maintainer, but it would be probably better to discuss it with PAM upstream. This change will require a new stuff in PAM API.

Pretty sure Tomas is one of the upstream maintainers, but you're right, discussion should take place upstream.

Although I'm not sure why we need systemd as mediator, would be

Quoted 4 lines

possible to read the environment setting directly by login(1)? Or it's expected that something (e.g. script) will be executed to get the session initialized?

The point is, the environment of systemd --user services and processes started
inside a logind registered session should get the same environment. It doesn't
really make sense for them to get out of sync. login(1) could try to read all the
same environment configuration files as systemd, but systemd gets its environment
from a variety of places (user.conf / systemd service files / /etc/environment.d), so
I think getting the environment from the user manager is the best way to go.

--Ray

author
#11 lore raw

PATCH 1/1 login-utils: import environment from user manager on systemd systems

(resending because gmail.com sent it as html email first time around)

Hi,

Quoted 4 lines · Karel Zak

We had positive feedback about it from Red Hat PAM maintainer, but it would be probably better to discuss it with PAM upstream. This change will require a new stuff in PAM API.

Pretty sure Tomas is one of the upstream maintainers, but you're right, discussion should take place upstream.

Quoted 4 lines

Although I'm not sure why we need systemd as mediator, would be possible to read the environment setting directly by login(1)? Or it's expected that something (e.g. script) will be executed to get the session initialized?

The point is, the environment of systemd --user services and processes started inside a logind registered session should get the same environment. It doesn't really make sense for them to get out of sync. login(1) could try to read all the
same environment configuration files as systemd, but systemd gets its environment
from a variety of places (user.conf / systemd service files / /etc/environment.d), so
I think getting the environment from the user manager is the best way to go.

--Ray