Skip to content

Commit

Permalink
run: when invoked as "uid0", expose some sudo-like behaviour
Browse files Browse the repository at this point in the history
This turns "systemd-run" into a multi-call binary. When invoked under
the name "uid0", then it behaves a bit more like traditional "sudo".
This mostly means defaults appropriuate for that, for example a PAM
stack, interactivity and more.

Fixes: systemd#29199
  • Loading branch information
poettering committed Dec 21, 2023
1 parent ec9527d commit 7420303
Show file tree
Hide file tree
Showing 6 changed files with 496 additions and 2 deletions.
1 change: 1 addition & 0 deletions man/rules/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,7 @@ manpages = [
''],
['udev_new', '3', ['udev_ref', 'udev_unref'], ''],
['udevadm', '8', [], ''],
['uid0', '1', [], ''],
['ukify', '1', [], 'ENABLE_UKIFY'],
['user@.service',
'5',
Expand Down
3 changes: 2 additions & 1 deletion man/systemd-run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,8 @@ $ systemd-run --user --wait -p SuccessExitStatus=SIGUSR1 --expand-environment=no
<citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-mount</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>uid0</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para>
</refsect1>

Expand Down
213 changes: 213 additions & 0 deletions man/uid0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?xml version='1.0'?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->

<refentry id="uid0"
xmlns:xi="http://www.w3.org/2001/XInclude">

<refentryinfo>
<title>uid0</title>
<productname>systemd</productname>
</refentryinfo>

<refmeta>
<refentrytitle>uid0</refentrytitle>
<manvolnum>1</manvolnum>
</refmeta>

<refnamediv>
<refname>uid0</refname>
<refpurpose>Elevate privileges</refpurpose>
</refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>uid0</command>
<arg choice="opt" rep="repeat">OPTIONS</arg>
<arg choice="plain"><replaceable>COMMAND</replaceable>
<arg choice="opt" rep="repeat">ARGS</arg>
</arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsect1>
<title>Description</title>

<para><command>uid0</command> may be used to temporarily and interactively acquire elavated or different
privileges. It serves a similar purpose as <citerefentry
project='man-pages'><refentrytitle>sudo</refentrytitle><manvolnum>8</manvolnum></citerefentry>, but
operates differently in a couple of key areas:</para>

<itemizedlist>
<listitem><para>No execution or security context credentials are inherited from the caller into the
invoked commands, as they are invoked from a fresh, isolated service forked off the service
manager.</para></listitem>

<listitem><para>Authentication takes place via <ulink
url="https://www.freedesktop.org/wiki/Software/polkit">polkit</ulink>, thus isolating the
authentication prompt from the terminal (if possible).</para></listitem>

<listitem><para>An independent pseudo-tty is allocated for the invoked command, detaching its lifecycle and
isolating it for security.</para></listitem>

<listitem><para>No SetUID/SetGID file access bit functionality is used for the implementation.</para></listitem>
</itemizedlist>

<para>Altogether this should provide a safer and more robust alternative to the <command>sudo</command>
mechanism, in particular in OS environments where SetUID/SetGID support is not available (for example by
setting the <varname>NoNewPrivileges=</varname> variable in
<citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>).</para>

<para>Any session invoked via <command>uid0</command> will run through the
<literal>systemd-uid0</literal> PAM stack.</para>

<para>Note that <command>uid0</command> is implemented as an alternative multi-call invocation of
<citerefentry><refentrytitle>systemd-run</refentrytitle><manvolnum>1</manvolnum></citerefentry>.</para>
</refsect1>

<refsect1>
<title>Options</title>

<para>The following options are understood:</para>

<variablelist>
<varlistentry>
<term><option>--no-ask-password</option></term>

<listitem><para>Do not query the user for authentication for privileged operations.</para>

<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</varlistentry>

<varlistentry>
<term><option>--unit=</option></term>

<listitem><para>Use this unit name instead of an automatically generated one.</para>

<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</varlistentry>

<varlistentry>
<term><option>--property=</option></term>

<listitem><para>Sets a property on the service unit that is created. This option takes an assignment
in the same format as
<citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
<command>set-property</command> command.</para>

<xi:include href="version-info.xml" xpointer="v256"/>
</listitem>
</varlistentry>

<varlistentry>
<term><option>--description=</option></term>

<listitem><para>Provide a description for the service unit that is invoked. If not specified,
the command itself will be used as a description. See <varname>Description=</varname> in
<citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
</para>

<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</varlistentry>

<varlistentry>
<term><option>--slice=</option></term>

<listitem><para>Make the new <filename>.service</filename> unit part of the specified slice, instead
of <filename>user.slice</filename>.</para>

<xi:include href="version-info.xml" xpointer="v256"/>
</listitem>
</varlistentry>

<varlistentry>
<term><option>--slice-inherit</option></term>

<listitem><para>Make the new <filename>.service</filename> unit part of the slice the
<command>uid0</command> itself has been invoked in. This option may be combined with
<option>--slice=</option>, in which case the slice specified via <option>--slice=</option> is placed
within the slice the <command>uid0</command> command is invoked in.</para>

<para>Example: consider <command>uid0</command> being invoked in the slice
<filename>foo.slice</filename>, and the <option>--slice=</option> argument is
<filename>bar</filename>. The unit will then be placed under
<filename>foo-bar.slice</filename>.</para>

<xi:include href="version-info.xml" xpointer="v256"/>

</listitem>
</varlistentry>

<varlistentry>
<term><option>--user=</option></term>
<term><option>-u</option></term>
<term><option>--group=</option></term>
<term><option>-g</option></term>

<listitem><para>Switches to the specified user/group instead of root.</para>

<xi:include href="version-info.xml" xpointer="v256"/>
</listitem>
</varlistentry>

<varlistentry>
<term><option>--nice=</option></term>

<listitem><para>Runs the invoked session with the specified nice level.</para>

<xi:include href="version-info.xml" xpointer="v256"/>
</listitem>
</varlistentry>

<varlistentry>
<term><option>--chdir=</option></term>
<term><option>-D</option></term>

<listitem><para>Runs the invoked session with the specified working directory. If not specified
defaults to the client's current working directory if switching to the root user, or the target
user's home directory otherwise.</para>

<xi:include href="version-info.xml" xpointer="v256"/>
</listitem>
</varlistentry>

<varlistentry>
<term><option>--setenv=<replaceable>NAME</replaceable>[=<replaceable>VALUE</replaceable>]</option></term>

<listitem><para>Runs the invoked session with the specified environment variable set. This parameter
may be used more than once to set multiple variables. When <literal>=</literal> and
<replaceable>VALUE</replaceable> are omitted, the value of the variable with the same name in the
invoking environment will be used.</para>

<xi:include href="version-info.xml" xpointer="v256"/>
</listitem>
</varlistentry>

<xi:include href="user-system-options.xml" xpointer="machine" />
<xi:include href="standard-options.xml" xpointer="help" />
<xi:include href="standard-options.xml" xpointer="version" />
</variablelist>

<para>All command line arguments after the first non-option argument become part of the command line of
the launched process. If no command line is specified an interactive shell is invoked.</para>
</refsect1>

<refsect1>
<title>Exit status</title>

<para>On success, 0 is returned. If <command>uid0</command> failed to start the session or the specified command fails, a
non-zero return value will be returned.</para>
</refsect1>

<refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd-run</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>sudo</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para>
</refsect1>

</refentry>
14 changes: 14 additions & 0 deletions src/run/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ executables += [
'sources' : files('run.c'),
},
]

install_emptydir(bindir)

meson.add_install_script(sh, '-c',
ln_s.format(bindir / 'systemd-run',
bindir / 'uid0'))

custom_target(
'systemd-uid0',
input : 'systemd-uid0.in',
output : 'systemd-uid0',
command : [jinja2_cmdline, '@INPUT@', '@OUTPUT@'],
install : pamconfdir != 'no',
install_dir : pamconfdir)
Loading

0 comments on commit 7420303

Please sign in to comment.