Skip to content

Commit 74b4588

Browse files
authored
Merge pull request systemd#12252 from keszybz/libmount-dont-unescape
Don't unescape paths from libmount
2 parents 52efbd8 + 9d1b2b2 commit 74b4588

File tree

6 files changed

+156
-43
lines changed

6 files changed

+156
-43
lines changed

src/core/mount.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
#include <stdio.h>
66
#include <sys/epoll.h>
77

8-
#include <libmount.h>
9-
108
#include "sd-messages.h"
119

1210
#include "alloc-util.h"
1311
#include "dbus-mount.h"
1412
#include "dbus-unit.h"
1513
#include "device.h"
16-
#include "escape.h"
1714
#include "exit-status.h"
1815
#include "format-util.h"
1916
#include "fstab-util.h"
17+
#include "libmount-util.h"
2018
#include "log.h"
2119
#include "manager.h"
2220
#include "mkdir.h"
@@ -36,9 +34,6 @@
3634

3735
#define RETRY_UMOUNT_MAX 32
3836

39-
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
40-
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);
41-
4237
static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
4338
[MOUNT_DEAD] = UNIT_INACTIVE,
4439
[MOUNT_MOUNTING] = UNIT_ACTIVATING,
@@ -1620,7 +1615,6 @@ static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
16201615
for (;;) {
16211616
struct libmnt_fs *fs;
16221617
const char *device, *path, *options, *fstype;
1623-
_cleanup_free_ char *d = NULL, *p = NULL;
16241618
int k;
16251619

16261620
k = mnt_table_next_fs(t, i, &fs);
@@ -1637,15 +1631,9 @@ static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
16371631
if (!device || !path)
16381632
continue;
16391633

1640-
if (cunescape(device, UNESCAPE_RELAX, &d) < 0)
1641-
return log_oom();
1642-
1643-
if (cunescape(path, UNESCAPE_RELAX, &p) < 0)
1644-
return log_oom();
1645-
1646-
device_found_node(m, d, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
1634+
device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
16471635

1648-
(void) mount_setup_unit(m, d, p, options, fstype, set_flags);
1636+
(void) mount_setup_unit(m, device, path, options, fstype, set_flags);
16491637
}
16501638

16511639
return 0;

src/shared/libmount-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
#pragma once
3+
4+
/* This needs to be after sys/mount.h */
5+
#include <libmount.h>
6+
7+
#include "macro.h"
8+
9+
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
10+
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);

src/shared/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ shared_sources = files('''
9595
json-internal.h
9696
json.c
9797
json.h
98+
libmount-util.h
9899
lockfile-util.c
99100
lockfile-util.h
100101
log-link.h

src/shutdown/umount.c

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
#include <sys/types.h>
1414
#include <unistd.h>
1515

16-
/* This needs to be after sys/mount.h :( */
17-
#include <libmount.h>
18-
1916
#include "sd-device.h"
2017

2118
#include "alloc-util.h"
@@ -25,6 +22,7 @@
2522
#include "escape.h"
2623
#include "fd-util.h"
2724
#include "fstab-util.h"
25+
#include "libmount-util.h"
2826
#include "linux-3.13/dm-ioctl.h"
2927
#include "mount-setup.h"
3028
#include "mount-util.h"
@@ -38,9 +36,6 @@
3836
#include "util.h"
3937
#include "virt.h"
4038

41-
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
42-
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);
43-
4439
static void mount_point_free(MountPoint **head, MountPoint *m) {
4540
assert(head);
4641
assert(m);
@@ -79,11 +74,10 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
7974
struct libmnt_fs *fs;
8075
const char *path, *fstype;
8176
_cleanup_free_ char *options = NULL;
82-
_cleanup_free_ char *p = NULL;
8377
unsigned long remount_flags = 0u;
8478
_cleanup_free_ char *remount_options = NULL;
8579
bool try_remount_ro;
86-
MountPoint *m;
80+
_cleanup_free_ MountPoint *m = NULL;
8781

8882
r = mnt_table_next_fs(t, i, &fs);
8983
if (r == 1)
@@ -95,18 +89,15 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
9589
if (!path)
9690
continue;
9791

98-
if (cunescape(path, UNESCAPE_RELAX, &p) < 0)
99-
return log_oom();
100-
10192
fstype = mnt_fs_get_fstype(fs);
10293

10394
/* Combine the generic VFS options with the FS-specific
10495
* options. Duplicates are not a problem here, because the only
10596
* options that should come up twice are typically ro/rw, which
106-
* are turned into MS_RDONLY or the invertion of it.
97+
* are turned into MS_RDONLY or the inversion of it.
10798
*
10899
* Even if there are duplicates later in mount_option_mangle()
109-
* it shouldn't hurt anyways as they override each other.
100+
* they shouldn't hurt anyways as they override each other.
110101
*/
111102
if (!strextend_with_separator(&options, ",",
112103
mnt_fs_get_vfs_options(fs),
@@ -124,9 +115,9 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
124115
* and hence not worth spending time on. Also, in
125116
* unprivileged containers we might lack the rights to
126117
* unmount these things, hence don't bother. */
127-
if (mount_point_is_api(p) ||
128-
mount_point_ignore(p) ||
129-
PATH_STARTSWITH_SET(p, "/dev", "/sys", "/proc"))
118+
if (mount_point_is_api(path) ||
119+
mount_point_ignore(path) ||
120+
PATH_STARTSWITH_SET(path, "/dev", "/sys", "/proc"))
130121
continue;
131122

132123
/* If we are in a container, don't attempt to
@@ -172,12 +163,15 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
172163
if (!m)
173164
return log_oom();
174165

175-
free_and_replace(m->path, p);
176-
free_and_replace(m->remount_options, remount_options);
166+
m->path = strdup(path);
167+
if (!m->path)
168+
return log_oom();
169+
170+
m->remount_options = TAKE_PTR(remount_options);
177171
m->remount_flags = remount_flags;
178172
m->try_remount_ro = try_remount_ro;
179173

180-
LIST_PREPEND(mount_point, *head, m);
174+
LIST_PREPEND(mount_point, *head, TAKE_PTR(m));
181175
}
182176

183177
return 0;
@@ -201,10 +195,8 @@ int swap_list_get(const char *swaps, MountPoint **head) {
201195

202196
for (;;) {
203197
struct libmnt_fs *fs;
204-
205-
MountPoint *swap;
198+
_cleanup_free_ MountPoint *swap = NULL;
206199
const char *source;
207-
_cleanup_free_ char *d = NULL;
208200

209201
r = mnt_table_next_fs(t, i, &fs);
210202
if (r == 1)
@@ -216,16 +208,15 @@ int swap_list_get(const char *swaps, MountPoint **head) {
216208
if (!source)
217209
continue;
218210

219-
r = cunescape(source, UNESCAPE_RELAX, &d);
220-
if (r < 0)
221-
return r;
222-
223211
swap = new0(MountPoint, 1);
224212
if (!swap)
225213
return -ENOMEM;
226214

227-
free_and_replace(swap->path, d);
228-
LIST_PREPEND(mount_point, *head, swap);
215+
swap->path = strdup(source);
216+
if (!swap->path)
217+
return -ENOMEM;
218+
219+
LIST_PREPEND(mount_point, *head, TAKE_PTR(swap));
229220
}
230221

231222
return 0;

src/test/meson.build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ tests += [
228228
[],
229229
[]],
230230

231+
[['src/test/test-libmount.c'],
232+
[],
233+
[threads,
234+
libmount]],
235+
231236
[['src/test/test-mount-util.c'],
232237
[],
233238
[]],

src/test/test-libmount.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
3+
#include "alloc-util.h"
4+
#include "fd-util.h"
5+
#include "escape.h"
6+
#include "libmount-util.h"
7+
#include "tests.h"
8+
9+
static void test_libmount_unescaping_one(
10+
const char *title,
11+
const char *string,
12+
bool may_fail,
13+
const char *expected_source,
14+
const char *expected_target) {
15+
/* A test for libmount really */
16+
int r;
17+
18+
log_info("/* %s %s */", __func__, title);
19+
20+
_cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
21+
_cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
22+
_cleanup_fclose_ FILE *f = NULL;
23+
24+
assert_se(table = mnt_new_table());
25+
assert_se(iter = mnt_new_iter(MNT_ITER_FORWARD));
26+
27+
f = fmemopen((char*) string, strlen(string), "re");
28+
assert_se(f);
29+
30+
assert_se(mnt_table_parse_stream(table, f, title) >= 0);
31+
32+
struct libmnt_fs *fs;
33+
const char *source, *target;
34+
_cleanup_free_ char *x = NULL, *cs = NULL, *s = NULL, *ct = NULL, *t = NULL;
35+
36+
/* We allow this call and the checks below to fail in some cases. See the case definitions below. */
37+
38+
r = mnt_table_next_fs(table, iter, &fs);
39+
if (r != 0 && may_fail) {
40+
log_error_errno(r, "mnt_table_next_fs failed: %m");
41+
return;
42+
}
43+
assert_se(r == 0);
44+
45+
assert_se(x = cescape(string));
46+
47+
assert_se(source = mnt_fs_get_source(fs));
48+
assert_se(target = mnt_fs_get_target(fs));
49+
50+
assert_se(cs = cescape(source));
51+
assert_se(ct = cescape(target));
52+
53+
assert_se(cunescape(source, UNESCAPE_RELAX, &s) >= 0);
54+
assert_se(cunescape(target, UNESCAPE_RELAX, &t) >= 0);
55+
56+
log_info("from '%s'", x);
57+
log_info("source: '%s'", source);
58+
log_info("source: '%s'", cs);
59+
log_info("source: '%s'", s);
60+
log_info("expected: '%s'", strna(expected_source));
61+
log_info("target: '%s'", target);
62+
log_info("target: '%s'", ct);
63+
log_info("target: '%s'", t);
64+
log_info("expected: '%s'", strna(expected_target));
65+
66+
assert_se(may_fail || streq(source, expected_source));
67+
assert_se(may_fail || streq(target, expected_target));
68+
69+
assert_se(mnt_table_next_fs(table, iter, &fs) == 1);
70+
}
71+
72+
static void test_libmount_unescaping(void) {
73+
test_libmount_unescaping_one(
74+
"escaped space + utf8",
75+
"729 38 0:59 / /tmp/„zupa\\040zębowa” rw,relatime shared:395 - tmpfs die\\040Brühe rw,seclabel",
76+
false,
77+
"die Brühe",
78+
"/tmp/„zupa zębowa”"
79+
);
80+
81+
test_libmount_unescaping_one(
82+
"escaped newline",
83+
"729 38 0:59 / /tmp/x\\012y rw,relatime shared:395 - tmpfs newline rw,seclabel",
84+
false,
85+
"newline",
86+
"/tmp/x\ny"
87+
);
88+
89+
/* The result of "mount -t tmpfs '' /tmp/emptysource".
90+
* This will fail with libmount <= v2.33.
91+
* See https://github.com/karelzak/util-linux/commit/18a52a5094.
92+
*/
93+
test_libmount_unescaping_one(
94+
"empty source",
95+
"760 38 0:60 / /tmp/emptysource rw,relatime shared:410 - tmpfs rw,seclabel",
96+
true,
97+
"",
98+
"/tmp/emptysource"
99+
);
100+
101+
/* The kernel leaves \r as is.
102+
* Also see https://github.com/karelzak/util-linux/issues/780.
103+
*/
104+
test_libmount_unescaping_one(
105+
"foo\\rbar",
106+
"790 38 0:61 / /tmp/foo\rbar rw,relatime shared:425 - tmpfs tmpfs rw,seclabel",
107+
true,
108+
"tmpfs",
109+
"/tmp/foo\rbar"
110+
);
111+
}
112+
113+
int main(int argc, char *argv[]) {
114+
test_setup_logging(LOG_DEBUG);
115+
116+
test_libmount_unescaping();
117+
return 0;
118+
}

0 commit comments

Comments
 (0)