imds: add new systemd-imdsd.service that makes IMDS data accessible locally

This service's job is to talk to a VM associated IMDS service provided
by the local Cloud. It tries to abstract the protocol differences
various IMDS implementations implement, but does *not* really try to
abstract more than a few basic fields of the actual IMDS metadata.

IMDS access is wrapped in a Varlink API that local clients can talk to.

If possible this makes use of the IMDS endpoint information that has
been added to hwdb in the preceeding commit. However, endpoint info can
also be provided via kernel command line and credentials. For debugging
purposes we also accept them via environment variables and command line
arguments.

This adds a concept of early-boot networking, just enough to be able to
talk to the IMDS service. It is minimally configurable via a kernel
cmdline option (and a build-time option): the user may choose between
"locked" and "unlocked" mode. In the former mode direct access to IMDS via
HTTPS is blocked via a prohibit route (and thus all IMDS communication
has to be done via systemd-imdsd@.service). In the latter case no such
lockdown takes place, and IMDS may be acquired both via this new service
and directly. The latter is typically a good idea for compatibility with
current systems, the former is preferable for secure installations.

Access to IMDS fields is controlled via PK.
This commit is contained in:
Lennart Poettering
2026-03-26 10:54:15 +01:00
parent f3eac272e6
commit eb6e5b07f1
22 changed files with 3828 additions and 3 deletions
+50
View File
@@ -0,0 +1,50 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <assert.h>
#include "imds-util.h"
#include "string-table.h"
#include "string-util.h"
#include "utf8.h"
bool imds_key_is_valid(const char *key) {
/* Just some pretty superficial validation. */
if (!key)
return false;
if (!startswith(key, "/"))
return false;
if (!ascii_is_valid(key))
return false;
if (string_has_cc(key, /* ok= */ NULL))
return false;
return true;
}
static const char* const imds_well_known_table[_IMDS_WELL_KNOWN_MAX] = {
[IMDS_BASE] = "base",
[IMDS_HOSTNAME] = "hostname",
[IMDS_REGION] = "region",
[IMDS_ZONE] = "zone",
[IMDS_IPV4_PUBLIC] = "ipv4-public",
[IMDS_IPV6_PUBLIC] = "ipv6-public",
[IMDS_SSH_KEY] = "ssh-key",
[IMDS_USERDATA] = "userdata",
[IMDS_USERDATA_BASE] = "userdata-base",
[IMDS_USERDATA_BASE64] = "userdata-base64",
};
DEFINE_STRING_TABLE_LOOKUP(imds_well_known, ImdsWellKnown);
static const char* const imds_network_mode_table[_IMDS_NETWORK_MODE_MAX] = {
[IMDS_NETWORK_OFF] = "off",
[IMDS_NETWORK_LOCKED] = "locked",
[IMDS_NETWORK_UNLOCKED] = "unlocked",
};
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(imds_network_mode, ImdsNetworkMode, IMDS_NETWORK_LOCKED);
+38
View File
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "macro.h"
#include "string-table.h" /* IWYU pragma: keep */
typedef enum ImdsNetworkMode {
IMDS_NETWORK_OFF, /* No automatic pre-IMDS network configuration, something else has to do this. (Also: no "prohibit" route) */
IMDS_NETWORK_LOCKED, /* "Prohibit" route for the IMDS server, unless you have SO_MARK set to 0x7FFF0815 */
IMDS_NETWORK_UNLOCKED, /* No "prohibit" route for the IMDS server */
_IMDS_NETWORK_MODE_MAX,
_IMDS_NETWORK_MODE_INVALID = -EINVAL,
} ImdsNetworkMode;
/* Various well-known keys */
typedef enum ImdsWellKnown {
IMDS_BASE, /* The same as "/", typically suffixed */
IMDS_HOSTNAME,
IMDS_REGION,
IMDS_ZONE,
IMDS_IPV4_PUBLIC,
IMDS_IPV6_PUBLIC,
IMDS_SSH_KEY,
IMDS_USERDATA,
IMDS_USERDATA_BASE, /* typically suffixed */
IMDS_USERDATA_BASE64,
_IMDS_WELL_KNOWN_MAX,
_IMDS_WELL_KNOWN_INVALID = -EINVAL,
} ImdsWellKnown;
static inline bool imds_well_known_can_suffix(ImdsWellKnown wk) {
return IN_SET(wk, IMDS_BASE, IMDS_USERDATA_BASE);
}
bool imds_key_is_valid(const char *key);
DECLARE_STRING_TABLE_LOOKUP(imds_well_known, ImdsWellKnown);
DECLARE_STRING_TABLE_LOOKUP(imds_network_mode, ImdsNetworkMode);
+3163
View File
File diff suppressed because it is too large Load Diff
+30
View File
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?> <!--*-nxml-*-->
<!DOCTYPE policyconfig PUBLIC "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"https://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<!--
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.
-->
<policyconfig>
<vendor>The systemd Project</vendor>
<vendor_url>https://systemd.io</vendor_url>
<action id="io.systemd.imds.get">
<description gettext-domain="systemd">Acquire IMDS instance metadata.</description>
<message gettext-domain="systemd">Authentication is required for an application to acquire IMDS instance metadata.</message>
<defaults>
<allow_any>auth_admin_keep</allow_any>
<allow_inactive>auth_admin_keep</allow_inactive>
<allow_active>auth_admin_keep</allow_active>
</defaults>
</action>
</policyconfig>
+21
View File
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
if conf.get('ENABLE_IMDS') != 1
subdir_done()
endif
executables += [
libexec_template + {
'name' : 'systemd-imdsd',
'public' : true,
'sources' : files(
'imdsd.c',
'imds-util.c'
) + import_curl_util_c,
'dependencies' : [ libcurl ],
},
]
install_data(
'io.systemd.imds.policy',
install_dir : polkitpolicydir)