Apply a PAM session to run/task/sysv/services Finit starts, pam_limits
above all, so a service running as a given user picks up that user's
limits the way a login does.
Add a new `pam` setting for the new block format (only), like the
per-service directories, naming a file in /etc/pam.d:
service weston {
user = "weston"
pam = "weston-autologin"
command = "/usr/bin/weston --continue-without-input"
}
pam_close_session() has to be called by a process still holding the
handle, and the handle does not survive exec(). Hence the keeper: it
holds the handle, drops to the service's credentials, and waits for a
parent-death signal before closing the session. Same shape as
systemd's (sd-pam), for the same reason, and one per fork, so the
script hooks open and close their own.
The keeper closes the descriptors it inherited from Finit and only
those. Closing everything would also take out what pam_open_session()
opened for itself, a keyring fd or a lock file, and leave the modules
to close a session with those pulled out from under them. Closing
nothing, as (sd-pam) does, would leave it holding the write end of the
notify pipe for the service's whole lifetime and starve notify = "s6"
services of their ready signal. So the fds open before pam_start()
are snapshotted and exactly those are closed, while the ones PAM opens
after are marked close-on-exec so the daemon does not inherit them
either.
A refused value, a denied account stack, an uninstalled pam.d file,
and a build without PAM support all keep the service from starting
rather than running it with the stacks skipped: one that quietly loses
pam_limits and its private /tmp, with nothing said. Capabilities a
module like pam_cap.so granted are merged into the IAB Finit applies
instead of being replaced by it, which only helps a service that also
sets capabilities, the other arm being a plain setuid() with nothing
left to restore once permitted is empty.
The test sysroot gains pam_permit.so, pam_deny.so and pam_limits.so,
which ldd cannot see, libpam dlopen()s them, and the test skips when
the host has none to stage. The negative cases pin the exit status
rather than only asserting crashed, which serv reports for any early
exit, so a bad command or an unwritable pidfile cannot pass for a
rejected session.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
5.9 KiB
PAM Sessions
pam = "NAME" runs a service inside a PAM session set up from
/etc/pam.d/NAME. The session stack in that file runs for the
process that goes on to become the daemon, so modules like pam_limits,
pam_env, or pam_keyinit see the service the way they see a login.
Basic Usage
A display server that needs the session a login would have arranged for it:
service weston {
user = "weston"
pam = "weston-autologin"
command = "/usr/bin/weston --continue-without-input"
}
With /etc/pam.d/weston-autologin:
auth required pam_permit.so
account required pam_unix.so
session required pam_unix.so
session required pam_limits.so
The auth line is needed even though nothing is ever authenticated.
Opening the session goes through pam_setcred(), which consults the
auth stack, and an empty stack comes back as a permission denial, so a
pam.d file with only account and session lines keeps the service
from starting.
The value names a file in /etc/pam.d, it is not a path. A value
holding / or .., or one too long to fit, is refused, with an error
in the log when the .conf file is read. The service does not start
either, initctl status reports it as missing. Like the per-service
directory keys, pam exists in the block format only.
No Authentication
Finit runs the account and session stacks, pam_acct_mgmt(),
pam_setcred(), and pam_open_session(), and never
pam_authenticate(). A module that asks a question gets a
conversation error back and its entry in the stack fails.
A service whose account is denied, an expired account say, does not
start, and neither does one naming a pam.d file that is not installed.
The child exits 71 (EX_OSERR), initctl status shows the service as
crashed, and PAM's own reason is in the log.
Requirements
PAM support is built by default, see Building Finit.
Without it, e.g. after --disable-pam, a service declaring pam is
refused rather than started with the stacks skipped:
weston: pam weston-autologin requires Finit built with --enable-pam
Which Blocks Take It
service, task, run, and sysv. Not tty: login opens a
session of its own there.
Every fork gets its own session, so the pre:, post:, ready:, and
cleanup: scripts each open and close one too, as do the stop and
reload scripts and the stop call on a sysv script.
A refused value stops those too. A pre: script forks before the
start-time check runs, so it exits 71 without running instead of the
service being reported missing.
The User
user decides who the session is for. Without it the session is for
root.
systemd documents the opposite for its equivalent. systemd.exec(5)
says PAMName= is "only useful in conjunction with the User=
setting, and is otherwise ignored". That was true of systemd up to and
including v256, v257 changed it to open a session for the manager's own
user, and the man page was never updated. Finit does what v257 does.
A service with a controlling tty
has it passed to PAM as PAM_TTY, for the modules that care which
terminal a session is on.
Precedence
Three places where PAM and a Finit setting cover the same ground:
pam_limitsoverrides a per-servicerlimit. A block asking fornofile = 4096under alimits.confthat says 512 gets 512.pam_envoverrides Finit's own environment defaults,PATH,USER,LOGNAME, andHOME, andenvfilein turn overridespam_env. AHOMEfrom the session stack also moves the working directory, which otherwise is the home directory from/etc/passwd.- The groups from
/etc/groupandextra-groupsoverridepam_group.
The Session Keeper
A helper is forked next to the service to close the session when the
service exits, (finit-pam):
CGroup : /system/weston cpu 0 [100, max] mem [--.--, max]
|- 312 /usr/bin/weston --continue-without-input
`- 313 (finit-pam)
There is one per fork. It runs as the service's user, in the service's cgroup, and stopping the service takes it along.
A daemon that reaps children in its own wait() loop will see a child
it never forked. systemd has the same property, with (sd-pam).
Limitations
-
type = "forking"closes the session early. The initial process exits by design, the keeper's parent-death signal fires with it, and the session is closed while the real daemon runs on. Finit warns about the combination when it reads the .conf file, and starts the service anyway:/etc/finit.d/foo.conf: foo: pam with type = forking closes the session when the initial process exits -
A daemon whose initial thread exits while the process lives closes the session the same way. The parent-death signal follows the thread that forked the keeper, not the process.
-
A capability granted by
pam_cap.sois only kept when the service also setscapabilities. -
The keeper shares the service's cgroup, so an empty cgroup directory can outlive a stop until the next start reuses it. Cosmetic.
-
A module that blocks has no time bound.
pam_open_session()waits for as long as the module does,pam_ldapagainst an unreachable server, say, orpam_mounton a hung network mount. The fork has already succeeded by then, so the service reaches the running state and stays there with no daemon behind it. Nothing crashes and nothing restarts.
See Also
- Service Options - the other run/task/service keys
- Linux Capabilities - the key
pam_cap.soneeds - Building Finit -
--disable-pamand its dependency - pam(8) - the PAM library
- pam.d(5) - the file format
- pam_limits(8) - limits from
limits.conf