core: escape UTF-8 in mount unit Where field before sending to clients

Followup for: 4804da5853 #27541

Fixes: #36206
This commit is contained in:
Lennart Poettering
2025-06-20 13:16:10 +02:00
committed by Yu Watanabe
parent e04d782416
commit 222b0b05ce
4 changed files with 40 additions and 4 deletions

View File

@@ -11,6 +11,27 @@
#include "string-util.h"
#include "unit.h"
static int property_get_where(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Mount *m = ASSERT_PTR(userdata);
assert(bus);
assert(reply);
_cleanup_free_ char *escaped = mount_get_where_escaped(m);
if (!escaped)
return -ENOMEM;
return sd_bus_message_append_basic(reply, 's', escaped);
}
static int property_get_what(
sd_bus *bus,
const char *path,
@@ -60,7 +81,7 @@ static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, mount_result, MountResu
const sd_bus_vtable bus_mount_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Where", "s", NULL, offsetof(Mount, where), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Where", "s", property_get_where, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("What", "s", property_get_what, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Options", "s", property_get_options, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Type", "s", property_get_type, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),

View File

@@ -636,7 +636,11 @@ static int mount_add_extras(Mount *m) {
path_simplify(m->where);
if (!u->description) {
r = unit_set_description(u, m->where);
_cleanup_free_ char *w = mount_get_where_escaped(m);
if (!w)
return log_oom();
r = unit_set_description(u, w);
if (r < 0)
return r;
}
@@ -2390,6 +2394,15 @@ static int mount_subsystem_ratelimited(Manager *m) {
return sd_event_source_is_ratelimited(m->mount_event_source);
}
char* mount_get_where_escaped(const Mount *m) {
assert(m);
if (!m->where)
return strdup("");
return utf8_escape_invalid(m->where);
}
char* mount_get_what_escaped(const Mount *m) {
_cleanup_free_ char *escaped = NULL;
const char *s = NULL;

View File

@@ -94,6 +94,7 @@ extern const UnitVTable mount_vtable;
void mount_fd_event(Manager *m, int events);
char* mount_get_where_escaped(const Mount *m);
char* mount_get_what_escaped(const Mount *m);
char* mount_get_options_escaped(const Mount *m);
const char* mount_get_fstype(const Mount *m);

View File

@@ -23,12 +23,13 @@ TMP_MOUNTINFO="$(mktemp)"
cp /proc/1/mountinfo "$TMP_MOUNTINFO"
# Add a mount entry with a "Unicode non-character" in it
LANG="C.UTF-8" printf '69 1 252:2 / /foo/mountinfo rw,relatime shared:1 - cifs //foo\ufffebar rw,seclabel\n' >>"$TMP_MOUNTINFO"
LANG="C.UTF-8" printf '69 1 252:2 / /foo/mount\ufffeinfo rw,relatime shared:1 - cifs //foo\ufffebar rw,seclabel\n' >>"$TMP_MOUNTINFO"
mount --bind "$TMP_MOUNTINFO" /proc/1/mountinfo
systemctl daemon-reload
# On affected versions this would throw an error:
# Failed to get properties: Bad message
systemctl status foo-mountinfo.mount
systemctl list-units -t mount
systemctl status foo-mount\\xef\\xbf\\xbeinfo.mount
umount /proc/1/mountinfo
systemctl daemon-reload