]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
veritysetup-generator: add support for veritytab
authorGaël PORTAY <gael.portay@collabora.com>
Sat, 14 Nov 2020 14:21:39 +0000 (09:21 -0500)
committerGaël PORTAY <gael.portay@collabora.com>
Fri, 15 Jan 2021 16:06:11 +0000 (11:06 -0500)
This adds the support for veritytab.

The veritytab file contains at most five fields, the first four are
mandatory, the last one is optional:
 - The first field contains the name of the resulting verity volume; its
   block device is set up /dev/mapper/</filename>.
 - The second field contains a path to the underlying block data device,
   or a specification of a block device via UUID= followed by the UUID.
 - The third field contains a path to the underlying block hash device,
   or a specification of a block device via UUID= followed by the UUID.
 - The fourth field is the roothash in hexadecimal.
 - The fifth field, if present, is a comma-delimited list of options.
   The following options are recognized only: ignore-corruption,
   restart-on-corruption, panic-on-corruption, ignore-zero-blocks,
   check-at-most-once and root-hash-signature. The others options will
   be implemented later.

Also, this adds support for the new kernel verity command line boolean
option "veritytab" which enables the read for veritytab, and the new
environment variable SYSTEMD_VERITYTAB which sets the path to the file
veritytab to read.

16 files changed:
docs/ENVIRONMENT.md
man/bootup.xml
man/directives-template.xml
man/kernel-command-line.xml
man/rules/meson.build
man/systemd.special.xml
man/veritytab.xml [new file with mode: 0644]
meson.build
src/shared/generator.c
src/shared/generator.h
src/veritysetup/veritysetup-generator.c
src/veritysetup/veritysetup.c
units/meson.build
units/remote-veritysetup.target [new file with mode: 0644]
units/veritysetup-pre.target [new file with mode: 0644]
units/veritysetup.target [new file with mode: 0644]

index 74a71bba9371bdcebe135cab433675cd671adc87..7e3c48a4fc012672cc115a5cb0f05bd26e798017 100644 (file)
@@ -49,6 +49,9 @@ All tools:
 * `$SYSTEMD_CRYPTTAB` — if set, use this path instead of /etc/crypttab. Only
   useful for debugging. Currently only supported by systemd-cryptsetup-generator.
 
+* `$SYSTEMD_VERITYTAB` — if set, use this path instead of /etc/veritytab. Only
+  useful for debugging. Currently only supported by systemd-veritysetup-generator.
+
 * `$SYSTEMD_EFI_OPTIONS` — if set, used instead of the string in the
   SystemdOptions EFI variable. Analogous to `$SYSTEMD_PROC_CMDLINE`.
 
index 781e539191ed4f86f24a45fbfcf475d98102cfd2..431d19a48e524b83e449b453f7014a4833cffdc1 100644 (file)
 
     <!-- note: do not use unicode ellipsis here, because docbook will replace that
          with three dots anyway, messing up alignment -->
-<programlisting>                                     cryptsetup-pre.target
+<programlisting>                             cryptsetup-pre.target veritysetup-pre.target
                                                   |
 (various low-level                                v
- API VFS mounts:                 (various cryptsetup devices...)
+ API VFS mounts:             (various cryptsetup/veritysetup devices...)
  mqueue, configfs,                                |    |
  debugfs, ...)                                    v    |
  |                                  cryptsetup.target  |
  |    v                       local-fs-pre.target |    |     |  (network file systems)
  |  swap.target                       |           |    v     v                 |
  |    |                               v           |  remote-cryptsetup.target  |
- |    |  (various low-level  (various mounts and  |             |              |
+ |    |  (various low-level  (various mounts and  |  remote-veritysetup.target |
  |    |   services: udevd,    fsck services...)   |             |    remote-fs.target
  |    |   tmpfiles, random            |           |             |             /
  |    |   seed, sysctl, ...)          v           |             |            /
@@ -303,7 +303,8 @@ emergency.service    |              |              |
 <programlisting>                                  (conflicts with  (conflicts with
                                     all system     all file system
                                      services)     mounts, swaps,
-                                         |           cryptsetup
+                                         |           cryptsetup/
+                                         |           veritysetup
                                          |          devices, ...)
                                          |                |
                                          v                v
index addb0effdb375a4cb8f10fff57f96866694332cc..34b103de40480d70672708b9f43419323d297a39 100644 (file)
@@ -94,7 +94,8 @@
   </refsect1>
 
   <refsect1>
-    <title><filename>/etc/crypttab</filename> and
+    <title><filename>/etc/crypttab</filename>,
+    <filename>/etc/veritytab</filename> and
     <filename>/etc/fstab</filename> options</title>
 
     <para>Options which influence mounted filesystems and encrypted volumes.</para>
index c55b4b17abd77127ca18cbe488f8e0b9d0114cf7..f546a1161a5fa66497fb0e23c57f7b00371bde14 100644 (file)
       </varlistentry>
 
       <varlistentry>
+        <term><varname>veritytab=</varname></term>
+        <term><varname>rd.veritytab=</varname></term>
         <term><varname>roothash=</varname></term>
         <term><varname>systemd.verity=</varname></term>
         <term><varname>rd.systemd.verity=</varname></term>
index 70ad30c837fd55f187e02180cbfc772d0798906b..19238ec033325e9d38db35aa3926bbd518a2f7f9 100644 (file)
@@ -9,6 +9,7 @@ manpages = [
  ['coredump.conf', '5', ['coredump.conf.d'], 'ENABLE_COREDUMP'],
  ['coredumpctl', '1', [], 'ENABLE_COREDUMP'],
  ['crypttab', '5', [], 'HAVE_LIBCRYPTSETUP'],
+ ['veritytab', '5', [], 'HAVE_LIBCRYPTSETUP'],
  ['daemon', '7', [], ''],
  ['dnssec-trust-anchors.d',
   '5',
index e731c9ced26ac8863e97b3d0f4b09de278bb6eb3..ce1e8655f5683ec43e466930c1ebc00db32b4498 100644 (file)
@@ -25,6 +25,8 @@
     <filename>bluetooth.target</filename>,
     <filename>cryptsetup-pre.target</filename>,
     <filename>cryptsetup.target</filename>,
+    <filename>veritysetup-pre.target</filename>,
+    <filename>veritysetup.target</filename>,
     <filename>ctrl-alt-del.target</filename>,
     <filename>blockdev@.target</filename>,
     <filename>boot-complete.target</filename>,
@@ -60,6 +62,7 @@
     <filename>printer.target</filename>,
     <filename>reboot.target</filename>,
     <filename>remote-cryptsetup.target</filename>,
+    <filename>remote-veritysetup.target</filename>,
     <filename>remote-fs-pre.target</filename>,
     <filename>remote-fs.target</filename>,
     <filename>rescue.target</filename>,
             encrypted block devices.</para>
           </listitem>
         </varlistentry>
+        <varlistentry>
+          <term><filename>veritysetup.target</filename></term>
+          <listitem>
+            <para>A target that pulls in setup services for all
+            verity integrity protected block devices.</para>
+          </listitem>
+        </varlistentry>
         <varlistentry>
           <term><filename>dbus.service</filename></term>
           <listitem>
             entries marked with <option>_netdev</option>.</para>
           </listitem>
         </varlistentry>
+        <varlistentry>
+          <term><filename>remote-veritysetup.target</filename></term>
+          <listitem>
+            <para>Similar to <filename>veritysetup.target</filename>, but for verity
+            integrity protected devices which are accessed over the network. It is used for
+            <citerefentry><refentrytitle>veritytab</refentrytitle><manvolnum>8</manvolnum></citerefentry>
+            entries marked with <option>_netdev</option>.</para>
+          </listitem>
+        </varlistentry>
         <varlistentry>
           <term><filename>remote-fs.target</filename></term>
           <listitem>
           <listitem><para>This template unit is used to order mount units and other consumers of block
           devices after services that synthesize these block devices. In particular, this is intended to be
           used with storage services (such as
-          <citerefentry><refentrytitle>systemd-cryptsetup@.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>)
+          <citerefentry><refentrytitle>systemd-cryptsetup@.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>/
+          <citerefentry><refentrytitle>systemd-veritysetup@.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>)
           that allocate and manage a virtual block device. Storage services are ordered before an instance of
           <filename>blockdev@.target</filename>, and the consumer units after it. The ordering is
           particularly relevant during shutdown, as it ensures that the mount is deactivated first and the
             stopped.</para>
           </listitem>
         </varlistentry>
+        <varlistentry>
+          <term><filename>veritysetup-pre.target</filename></term>
+          <listitem>
+            <para>This passive target unit may be pulled in by services
+            that want to run before any verity integrity protected block
+            device is set up. All verity integrity protected block
+            devices are set up after this target has been reached. Since
+            the shutdown order is implicitly the reverse start-up order
+            between units, this target is particularly useful to ensure
+            that a service is shut down only after all verity integrity
+            protected block devices are fully stopped.</para>
+          </listitem>
+        </varlistentry>
         <varlistentry>
           <term><filename>first-boot-complete.target</filename></term>
           <listitem>
           <term><filename>remote-fs-pre.target</filename></term>
           <listitem>
             <para>This target unit is automatically ordered before all
-            mount point units (see above) and cryptsetup devices
+            mount point units (see above) and cryptsetup/veritysetup devices
             marked with the <option>_netdev</option>. It can be used to run
             certain units before remote encrypted devices and mounts are established.
             Note that this unit is generally not part of the initial
diff --git a/man/veritytab.xml b/man/veritytab.xml
new file mode 100644 (file)
index 0000000..d29e9f5
--- /dev/null
@@ -0,0 +1,198 @@
+<?xml version="1.0"?>
+<!--*-nxml-*-->
+<!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
+
+This is based on crypttab(5).
+
+-->
+<refentry id="veritytab" conditional='HAVE_LIBCRYPTSETUP' xmlns:xi="http://www.w3.org/2001/XInclude">
+
+  <refentryinfo>
+    <title>veritytab</title>
+    <productname>systemd</productname>
+  </refentryinfo>
+
+  <refmeta>
+    <refentrytitle>veritytab</refentrytitle>
+    <manvolnum>5</manvolnum>
+  </refmeta>
+
+  <refnamediv>
+    <refname>veritytab</refname>
+    <refpurpose>Configuration for verity block devices</refpurpose>
+  </refnamediv>
+
+  <refsynopsisdiv>
+    <para><filename>/etc/veritytab</filename></para>
+  </refsynopsisdiv>
+
+  <refsect1>
+    <title>Description</title>
+
+    <para>The <filename>/etc/veritytab</filename> file describes
+    verity integrity protected block devices that are set up during
+    system boot.</para>
+
+    <para>Empty lines and lines starting with the <literal>#</literal>
+    character are ignored. Each of the remaining lines describes one
+    verity integrity protected block device. Fields are delimited by
+    white space.</para>
+
+    <para>Each line is in the form<programlisting><replaceable>volume-name</replaceable> <replaceable>data-device</replaceable> <replaceable>hash-device</replaceable> <replaceable>roothash</replaceable> <replaceable>options</replaceable></programlisting>
+    The first four fields are mandatory, the remaining one is optional.</para>
+
+    <para>The first field contains the name of the resulting verity volume; its block device is set up
+    below <filename>/dev/mapper/</filename>.</para>
+
+    <para>The second field contains a path to the underlying block data device, or a specification of a block device via
+    <literal>UUID=</literal> followed by the UUID.</para>
+
+    <para>The third field contains a path to the underlying block hash device, or a specification of a block device via
+    <literal>UUID=</literal> followed by the UUID.</para>
+
+    <para>The fourth field is the <literal>roothash</literal> in hexadecimal.</para>
+
+    <para>The fifth field, if present, is a comma-delimited list of options. The following options are
+    recognized:</para>
+
+    <variablelist class='fstab-options'>
+
+      <varlistentry>
+        <term><option>ignore-corruption</option></term>
+        <term><option>restart-on-corruption</option></term>
+        <term><option>panic-on-corruption</option></term>
+
+        <listitem><para>Defines what to do if data integrity problem is detected (data corruption). Without these
+        options kernel fails the IO operation with I/O error. With <literal>--ignore-corruption</literal> option the
+        corruption is only logged. With <literal>--restart-on-corruption</literal> or
+        <literal>--panic-on-corruption</literal> the kernel is restarted (panicked) immediately.
+
+        (You have to provide way how to avoid restart loops.)</para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>ignore-zero-blocks</option></term>
+
+        <listitem><para>Instruct kernel to not verify blocks that are expected to contain zeroes and always directly
+        return zeroes instead.
+
+        WARNING: Use this option only in very specific cases. This option is available since Linux kernel version 4.5.
+        </para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>check-at-most-once</option></term>
+
+        <listitem><para>Instruct kernel to verify blocks only the first time they are read from the data device, rather
+        than every time.
+
+        WARNING: It provides a reduced level of security because only offline tampering of the data device's content
+        will be detected, not online tampering. This option is available since Linux kernel version 4.17.
+        </para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>root-hash-signature=</option></term>
+
+        <listitem><para>A base64 string encoding the root hash signature prefixed by <literal>base64:</literal> or a
+        path to roothash signature file used to verify the root hash (in kernel). This feature requires Linux kernel
+        version 5.4 or more recent.</para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>_netdev</option></term>
+
+        <listitem><para>Marks this veritysetup device as requiring network. It will be
+        started after the network is available, similarly to
+        <citerefentry><refentrytitle>systemd.mount</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+        units marked with <option>_netdev</option>. The service unit to set up this device
+        will be ordered between <filename>remote-fs-pre.target</filename> and
+        <filename>remote-veritysetup.target</filename>, instead of
+        <filename>veritysetup-pre.target</filename> and
+        <filename>veritysetup.target</filename>.</para>
+
+        <para>Hint: if this device is used for a mount point that is specified in
+        <citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+        the <option>_netdev</option> option should also be used for the mount
+        point. Otherwise, a dependency loop might be created where the mount point
+        will be pulled in by <filename>local-fs.target</filename>, while the
+        service to configure the network is usually only started <emphasis>after</emphasis>
+        the local file system has been mounted.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>noauto</option></term>
+
+        <listitem><para>This device will not be added to <filename>veritysetup.target</filename>.
+        This means that it will not be automatically enabled on boot, unless something else pulls
+        it in. In particular, if the device is used for a mount point, it'll be enabled
+        automatically during boot, unless the mount point itself is also disabled with
+        <option>noauto</option>.</para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>nofail</option></term>
+
+        <listitem><para>This device will not be a hard dependency of
+        <filename>veritysetup.target</filename>. It'll still be pulled in and started, but the system
+        will not wait for the device to show up and be enabled, and boot will not fail if this is
+        unsuccessful. Note that other units that depend on the enabled device may still fail. In
+        particular, if the device is used for a mount point, the mount point itself also needs to
+        have the <option>nofail</option> option, or the boot will fail if the device is not enabled
+        successfully.</para></listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>x-initrd.attach</option></term>
+
+        <listitem><para>Setup this verity integrity protected block device in the initramfs, similarly to
+        <citerefentry><refentrytitle>systemd.mount</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+        units marked with <option>x-initrd.mount</option>.</para>
+
+        <para>Although it's not necessary to mark the mount entry for the root file system with
+        <option>x-initrd.mount</option>, <option>x-initrd.attach</option> is still recommended with
+        the verity integrity protected block device containing the root file system as otherwise systemd
+        will attempt to detach the device during the regular system shutdown while it's still in
+        use. With this option the device will still be detached but later after the root file
+        system is unmounted.</para>
+
+        <para>All other verity integrity protected block devices that contain file systems mounted in the
+        initramfs should use this option.</para>
+        </listitem>
+      </varlistentry>
+
+    </variablelist>
+
+    <para>At early boot and when the system manager configuration is
+    reloaded, this file is translated into native systemd units by
+    <citerefentry><refentrytitle>systemd-veritysetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>.</para>
+  </refsect1>
+
+  <refsect1>
+    <title>Examples</title>
+    <example>
+      <title>/etc/veritytab example</title>
+      <para>Set up two verity integrity protected block devices. One using device blocks, another using files.</para>
+
+      <programlisting>usr  PARTUUID=783e45ae-7aa3-484a-beef-a80ff9c19cbb PARTUUID=21dc1dfe-4c33-8b48-98a9-918a22eb3e37 36e3f740ad502e2c25e2a23d9c7c17bf0fdad2300b7580842d4b7ec1fb0fa263 auto
+data /etc/data /etc/hash a5ee4b42f70ae1f46a08a7c92c2e0a20672ad2f514792730f5d49d7606ab8fdf auto
+</programlisting>
+    </example>
+  </refsect1>
+
+  <refsect1>
+    <title>See Also</title>
+    <para>
+      <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+      <citerefentry><refentrytitle>systemd-veritysetup@.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
+      <citerefentry><refentrytitle>systemd-veritysetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
+      <citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+      <citerefentry project='die-net'><refentrytitle>veritysetup</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
+    </para>
+  </refsect1>
+
+</refentry>
index a8afa0dd2f75af5b69a8bdf470a9655446d12fa2..3f177d607367b619e446a6c01d905bafcb10c3f9 100644 (file)
@@ -237,6 +237,7 @@ conf.set_quoted('ROOTPREFIX_NOSLASH',                         rootprefixdir_nosl
 conf.set_quoted('RANDOM_SEED_DIR',                            randomseeddir)
 conf.set_quoted('RANDOM_SEED',                                join_paths(randomseeddir, 'random-seed'))
 conf.set_quoted('SYSTEMD_CRYPTSETUP_PATH',                    join_paths(rootlibexecdir, 'systemd-cryptsetup'))
+conf.set_quoted('SYSTEMD_VERITYSETUP_PATH',                   join_paths(rootlibexecdir, 'systemd-veritysetup'))
 conf.set_quoted('SYSTEM_GENERATOR_DIR',                       systemgeneratordir)
 conf.set_quoted('USER_GENERATOR_DIR',                         usergeneratordir)
 conf.set_quoted('SYSTEM_ENV_GENERATOR_DIR',                   systemenvgeneratordir)
index 1eccc5a2895b991fa592e84e465e092bf722db6c..b8616cf1f2b2a563e2ba7d398b9eb0252413482b 100644 (file)
@@ -620,6 +620,81 @@ int generator_write_cryptsetup_service_section(
         return 0;
 }
 
+int generator_write_veritysetup_unit_section(
+                FILE *f,
+                const char *source) {
+
+        assert(f);
+
+        fprintf(f,
+                "[Unit]\n"
+                "Description=Integrity Protection Setup for %%I\n"
+                "Documentation=man:veritytab(5) man:systemd-veritysetup-generator(8) man:systemd-veritysetup@.service(8)\n");
+
+        if (source)
+                fprintf(f, "SourcePath=%s\n", source);
+
+        fprintf(f,
+                "DefaultDependencies=no\n"
+                "IgnoreOnIsolate=true\n"
+                "After=cryptsetup-pre.target systemd-udevd-kernel.socket\n"
+                "Before=blockdev@dev-mapper-%%i.target\n"
+                "Wants=blockdev@dev-mapper-%%i.target\n");
+
+        return 0;
+}
+
+int generator_write_veritysetup_service_section(
+                FILE *f,
+                const char *name,
+                const char *data_what,
+                const char *hash_what,
+                const char *roothash,
+                const char *options) {
+
+        _cleanup_free_ char *name_escaped = NULL, *data_what_escaped = NULL, *hash_what_escaped,
+                            *roothash_escaped = NULL, *options_escaped = NULL;
+
+        assert(f);
+        assert(name);
+        assert(data_what);
+        assert(hash_what);
+
+        name_escaped = specifier_escape(name);
+        if (!name_escaped)
+                return log_oom();
+
+        data_what_escaped = specifier_escape(data_what);
+        if (!data_what_escaped)
+                return log_oom();
+
+        hash_what_escaped = specifier_escape(hash_what);
+        if (!hash_what_escaped)
+                return log_oom();
+
+        roothash_escaped = specifier_escape(roothash);
+        if (!roothash_escaped)
+                return log_oom();
+
+        if (options) {
+                options_escaped = specifier_escape(options);
+                if (!options_escaped)
+                        return log_oom();
+        }
+
+        fprintf(f,
+                "\n"
+                "[Service]\n"
+                "Type=oneshot\n"
+                "RemainAfterExit=yes\n"
+                "ExecStart=" SYSTEMD_VERITYSETUP_PATH " attach '%s' '%s' '%s' '%s' '%s'\n"
+                "ExecStop=" SYSTEMD_VERITYSETUP_PATH " detach '%s'\n",
+                name_escaped, data_what_escaped, hash_what_escaped, roothash_escaped, strempty(options_escaped),
+                name_escaped);
+
+        return 0;
+}
+
 void log_setup_generator(void) {
         /* Disable talking to syslog/journal (i.e. the two IPC-based loggers) if we run in system context. */
         if (cg_pid_get_owner_uid(0, NULL) == -ENXIO /* not running in a per-user slice */)
index ff6072f53d8f4b376bc0ba9abeecae2df86a5259..bd768daf491c2990ff3f705a79e097ee3110d66a 100644 (file)
@@ -42,6 +42,18 @@ int generator_write_cryptsetup_service_section(
                 const char *password,
                 const char *options);
 
+int generator_write_veritysetup_unit_section(
+                FILE *f,
+                const char *source);
+
+int generator_write_veritysetup_service_section(
+                FILE *f,
+                const char *name,
+                const char *data_what,
+                const char *hash_what,
+                const char *roothash,
+                const char *options);
+
 int generator_write_device_deps(
         const char *dir,
         const char *what,
index b706eb037311dd8e7ca0dfeeb9a8f47688af9826..9f47a0515a0ed0d3d31316305b02dd5bd6bc5b80 100644 (file)
@@ -7,6 +7,7 @@
 #include <unistd.h>
 
 #include "alloc-util.h"
+#include "dropin.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "fstab-util.h"
 
 #define SYSTEMD_VERITYSETUP_SERVICE "systemd-veritysetup@root.service"
 
+typedef struct verity_device {
+        char *uuid;
+        char *datadev;
+        char *hashdev;
+        char *roothash;
+        char *name;
+        char *options;
+        bool create;
+} verity_device;
+
 static const char *arg_dest = NULL;
 static bool arg_enabled = true;
+static bool arg_read_veritytab = true;
+static const char *arg_veritytab = NULL;
 static char *arg_root_hash = NULL;
 static char *arg_data_what = NULL;
 static char *arg_hash_what = NULL;
@@ -105,8 +118,8 @@ static int create_device(void) {
                 "Conflicts=umount.target\n"
                 "BindsTo=%s %s\n"
                 "IgnoreOnIsolate=true\n"
-                "After=cryptsetup-pre.target systemd-udevd-kernel.socket %s %s\n"
-                "Before=cryptsetup.target umount.target\n"
+                "After=veritysetup-pre.target systemd-udevd-kernel.socket %s %s\n"
+                "Before=veritysetup.target umount.target\n"
                 "\n[Service]\n"
                 "Type=oneshot\n"
                 "RemainAfterExit=yes\n"
@@ -120,7 +133,7 @@ static int create_device(void) {
         if (r < 0)
                 return log_error_errno(r, "Failed to write file unit "SYSTEMD_VERITYSETUP_SERVICE": %m");
 
-        to = strjoina(arg_dest, "/cryptsetup.target.requires/" SYSTEMD_VERITYSETUP_SERVICE);
+        to = strjoina(arg_dest, "/veritysetup.target.requires/" SYSTEMD_VERITYSETUP_SERVICE);
 
         (void) mkdir_parents(to, 0755);
         if (symlink("../" SYSTEMD_VERITYSETUP_SERVICE, to) < 0)
@@ -140,6 +153,14 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
                 else
                         arg_enabled = r;
 
+        } else if (streq(key, "veritytab")) {
+
+                r = value ? parse_boolean(value) : 1;
+                if (r < 0)
+                        log_warning("Failed to parse veritytab= kernel command line switch %s. Ignoring.", value);
+                else
+                        arg_read_veritytab = r;
+
         } else if (proc_cmdline_key_streq(key, "roothash")) {
 
                 if (proc_cmdline_value_missing(key, value))
@@ -223,21 +244,221 @@ static int determine_devices(void) {
         return 1;
 }
 
+static int create_disk(
+                const char *name,
+                const char *data_device,
+                const char *hash_device,
+                const char *roothash,
+                const char *options,
+                const char *source) {
+
+        _cleanup_free_ char *n = NULL, *dd = NULL, *du = NULL, *hd = NULL, *hu = NULL, *e = NULL,
+                            *du_escaped = NULL, *hu_escaped = NULL, *name_escaped = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
+        const char *dmname;
+        bool noauto, nofail, netdev, attach_in_initrd;
+        int r;
+
+        assert(name);
+        assert(data_device);
+        assert(hash_device);
+        assert(roothash);
+
+        noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0");
+        nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
+        netdev = fstab_test_option(options, "_netdev\0");
+        attach_in_initrd = fstab_test_option(options, "x-initrd.attach\0");
+
+        name_escaped = specifier_escape(name);
+        if (!name_escaped)
+                return log_oom();
+
+        e = unit_name_escape(name);
+        if (!e)
+                return log_oom();
+
+        du = fstab_node_to_udev_node(data_device);
+        if (!du)
+                return log_oom();
+
+        hu = fstab_node_to_udev_node(hash_device);
+        if (!hu)
+                return log_oom();
+
+        r = unit_name_build("systemd-veritysetup", e, ".service", &n);
+        if (r < 0)
+                return log_error_errno(r, "Failed to generate unit name: %m");
+
+        du_escaped = specifier_escape(du);
+        if (!du_escaped)
+                return log_oom();
+
+        hu_escaped = specifier_escape(hu);
+        if (!hu_escaped)
+                return log_oom();
+
+        r = unit_name_from_path(du, ".device", &dd);
+        if (r < 0)
+                return log_error_errno(r, "Failed to generate unit name: %m");
+
+        r = unit_name_from_path(hu, ".device", &hd);
+        if (r < 0)
+                return log_error_errno(r, "Failed to generate unit name: %m");
+
+        r = generator_open_unit_file(arg_dest, NULL, n, &f);
+        if (r < 0)
+                return r;
+
+        r = generator_write_veritysetup_unit_section(f, source);
+        if (r < 0)
+                return r;
+
+        if (netdev)
+                fprintf(f, "After=remote-fs-pre.target\n");
+
+        /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */
+        if (!attach_in_initrd)
+                fprintf(f, "Conflicts=umount.target\n");
+
+        if (!nofail)
+                fprintf(f,
+                        "Before=%s\n",
+                        netdev ? "remote-veritysetup.target" : "veritysetup.target");
+
+        if (path_startswith(du, "/dev/"))
+                fprintf(f,
+                        "BindsTo=%s\n"
+                        "After=%s\n"
+                        "Before=umount.target\n",
+                        dd, dd);
+        else
+                /* For loopback devices, add systemd-tmpfiles-setup-dev.service
+                   dependency to ensure that loopback support is available in
+                   the kernel (/dev/loop-control needs to exist) */
+                fprintf(f,
+                        "RequiresMountsFor=%s\n"
+                        "Requires=systemd-tmpfiles-setup-dev.service\n"
+                        "After=systemd-tmpfiles-setup-dev.service\n",
+                        du_escaped);
+
+        if (path_startswith(hu, "/dev/"))
+                fprintf(f,
+                        "BindsTo=%s\n"
+                        "After=%s\n"
+                        "Before=umount.target\n",
+                        hd, hd);
+        else
+                /* For loopback devices, add systemd-tmpfiles-setup-dev.service
+                   dependency to ensure that loopback support is available in
+                   the kernel (/dev/loop-control needs to exist) */
+                fprintf(f,
+                        "RequiresMountsFor=%s\n"
+                        "Requires=systemd-tmpfiles-setup-dev.service\n"
+                        "After=systemd-tmpfiles-setup-dev.service\n",
+                        hu_escaped);
+
+        r = generator_write_veritysetup_service_section(f, name, du_escaped, hu_escaped, roothash, options);
+        if (r < 0)
+                return r;
+
+        r = fflush_and_check(f);
+        if (r < 0)
+                return log_error_errno(r, "Failed to write unit file %s: %m", n);
+
+        if (!noauto) {
+                r = generator_add_symlink(arg_dest,
+                                          netdev ? "remote-veritysetup.target" : "veritysetup.target",
+                                          nofail ? "wants" : "requires", n);
+                if (r < 0)
+                        return r;
+        }
+
+        dmname = strjoina("dev-mapper-", e, ".device");
+        return generator_add_symlink(arg_dest, dmname, "requires", n);
+}
+
+static int add_veritytab_devices(void) {
+        _cleanup_fclose_ FILE *f = NULL;
+        unsigned veritytab_line = 0;
+        int r;
+
+        if (!arg_read_veritytab)
+                return 0;
+
+        r = fopen_unlocked(arg_veritytab, "re", &f);
+        if (r < 0) {
+                if (errno != ENOENT)
+                        log_error_errno(errno, "Failed to open %s: %m", arg_veritytab);
+                return 0;
+        }
+
+        for (;;) {
+                _cleanup_free_ char *line = NULL, *name = NULL, *data_device = NULL, *hash_device = NULL,
+                                    *roothash = NULL, *options = NULL;
+                verity_device *d = NULL;
+                char *l, *data_uuid, *hash_uuid;
+                int k;
+
+                r = read_line(f, LONG_LINE_MAX, &line);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to read %s: %m", arg_veritytab);
+                if (r == 0)
+                        break;
+
+                veritytab_line++;
+
+                l = strstrip(line);
+                if (IN_SET(l[0], 0, '#'))
+                        continue;
+
+                k = sscanf(l, "%ms %ms %ms %ms %ms", &name, &data_device, &hash_device, &roothash, &options);
+                if (k < 4 || k > 5) {
+                        log_error("Failed to parse %s:%u, ignoring.", arg_veritytab, veritytab_line);
+                        continue;
+                }
+
+                data_uuid = startswith(data_device, "UUID=");
+                if (!data_uuid)
+                        data_uuid = path_startswith(data_device, "/dev/disk/by-uuid/");
+
+                hash_uuid = startswith(hash_device, "UUID=");
+                if (!hash_uuid)
+                        hash_uuid = path_startswith(hash_device, "/dev/disk/by-uuid/");
+
+                r = create_disk(name,
+                                data_device,
+                                hash_device,
+                                roothash,
+                                options,
+                                arg_veritytab);
+                if (r < 0)
+                        return r;
+
+                if (d)
+                        d->create = false;
+        }
+
+        return 0;
+}
+
 static int run(const char *dest, const char *dest_early, const char *dest_late) {
         int r;
 
         assert_se(arg_dest = dest);
 
+        arg_veritytab = getenv("SYSTEMD_VERITYTAB") ?: "/etc/veritytab";
+
         r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
         if (r < 0)
                 return log_warning_errno(r, "Failed to parse kernel command line: %m");
 
-        /* For now we only support the root device on verity. Later on we might want to add support for /etc/veritytab
-         * or similar to define additional mappings */
-
         if (!arg_enabled)
                 return 0;
 
+        r = add_veritytab_devices();
+        if (r < 0)
+                return r;
+
         r = determine_devices();
         if (r < 0)
                 return r;
index 1b9d4710fefcfbbf66d85ed43495f26dcab3a887..1863bdfa326494b64871f011662e1ed8093c0b19 100644 (file)
@@ -95,6 +95,9 @@ static int parse_options(const char *options) {
                 if (r == 0)
                         break;
 
+                if (STR_IN_SET(word, "noauto", "auto", "nofail", "fail", "_netdev"))
+                        continue;
+
                 if (isempty(word))
                         continue;
                 else if (streq(word, "ignore-corruption"))
index 8b8fd1173f1be21f655b3d444afbf7ee7d13f7e3..7b18f1bfea0a132e92fa80c849313baad142832f 100644 (file)
@@ -10,6 +10,9 @@ units = [
         ['cryptsetup-pre.target',               'HAVE_LIBCRYPTSETUP'],
         ['cryptsetup.target',                   'HAVE_LIBCRYPTSETUP',
          'sysinit.target.wants/'],
+        ['veritysetup-pre.target',              'HAVE_LIBCRYPTSETUP'],
+        ['veritysetup.target',                  'HAVE_LIBCRYPTSETUP',
+         'sysinit.target.wants/'],
         ['dev-hugepages.mount',                 '',
          'sysinit.target.wants/'],
         ['dev-mqueue.mount',                    '',
@@ -62,6 +65,8 @@ units = [
          'ctrl-alt-del.target' + (with_runlevels ? ' runlevel6.target' : '')],
         ['remote-cryptsetup.target',            'HAVE_LIBCRYPTSETUP',
          'initrd-root-device.target.wants/'],
+        ['remote-veritysetup.target',           'HAVE_LIBCRYPTSETUP',
+         'initrd-root-device.target.wants/'],
         ['remote-fs-pre.target',                ''],
         ['remote-fs.target',                    ''],
         ['rescue.target',                       '',
diff --git a/units/remote-veritysetup.target b/units/remote-veritysetup.target
new file mode 100644 (file)
index 0000000..bd9f71a
--- /dev/null
@@ -0,0 +1,18 @@
+#  SPDX-License-Identifier: LGPL-2.1-or-later
+#
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Remote Verity Integrity Protected Volumes
+Documentation=man:systemd.special(7)
+After=remote-fs-pre.target veritysetup-pre.target
+DefaultDependencies=no
+Conflicts=shutdown.target
+
+[Install]
+WantedBy=multi-user.target
diff --git a/units/veritysetup-pre.target b/units/veritysetup-pre.target
new file mode 100644 (file)
index 0000000..be065f3
--- /dev/null
@@ -0,0 +1,14 @@
+#  SPDX-License-Identifier: LGPL-2.1-or-later
+#
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Local Verity Integrity Protected Volumes (Pre)
+Documentation=man:systemd.special(7)
+RefuseManualStart=yes
+Before=veritysetup.target
diff --git a/units/veritysetup.target b/units/veritysetup.target
new file mode 100644 (file)
index 0000000..0ac3ad3
--- /dev/null
@@ -0,0 +1,12 @@
+#  SPDX-License-Identifier: LGPL-2.1-or-later
+#
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Local Verity Integrity Protected Volumes
+Documentation=man:systemd.special(7)