Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: make RootDirectory= and ProtectKernelModules= work #4594

Merged
merged 3 commits into from
Nov 7, 2016

Conversation

tixxdz
Copy link
Member

@tixxdz tixxdz commented Nov 6, 2016

Instead of having two fields inside BindMount struct where one is stack
based and the other one is heap, use one field to store the full path
and updated it when we chase symlinks. This way we avoid dealing with
both at the same time.

This makes RootDirectory= work with ProtectHome=, ProtectControlGroups= and
ProtectKernelModules=yes

Fixes: #4567

Copy link
Member

@evverx evverx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tixxdz , thanks! I found two small issues (see comments). I'll run systemd under valgrind and under AddressSanitizer tomorrow.

if (!path)
return -ENOMEM;

if (!path_is_absolute(path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a memleak here. I think should be something like:

_cleanup_free_ char *path = NULL;
...
path = strdup(*i);
...
set_bind_mount(p, path, mode, ignore);
path = NULL;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @evverx could you please check again, since I reworked the whole logic and those paths are cleaned https://github.com/systemd/systemd/pull/4594/files#diff-f315b72505d5f0a92ec2f4d068d4f916R913 here and in different places where we chase symlinks, drop duplicate paths etc...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. So
https://github.com/systemd/systemd/pull/4594/files#diff-f315b72505d5f0a92ec2f4d068d4f916L746

                 r = append_mounts(&m, read_write_paths, READWRITE);
                  if (r < 0)
                          return r;

should be

                 r = append_mounts(&m, read_write_paths, READWRITE);
                  if (r < 0)
                          goto finish;

Right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that strdup should be done below the !path_is_absolute check. This would avoid the memleak.

@@ -196,7 +200,7 @@ static int append_target_mounts(BindMount **p, const char *root_directory, const
* declaration we do not support "-" at the beginning.
*/
const TargetMount *m = &mounts[i];
const char *path = prefix_roota(root_directory, m->path);
char *path = prefix_root(root_directory, m->path);

if (!path_is_absolute(path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a memleak. Similar to my previous comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ? I'm a bit lost here, could you please confirm ? thanks ;-)

@evverx evverx added the pid1 label Nov 6, 2016
@tixxdz tixxdz force-pushed the djalal/fix-rootdir-apply-mntns branch from e9615f4 to ca5acd1 Compare November 6, 2016 17:46
@tixxdz tixxdz changed the title core: make RootDirectory= and ProtectKernelTunables= work core: make RootDirectory= and ProtectKernelModules= work Nov 6, 2016
@@ -756,64 +776,67 @@ int setup_namespace(
return r;

if (tmp_dir) {
m->path = prefix_roota(root_directory, "/tmp");
m->path = prefix_root(root_directory, "/tmp");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check the result of the prefix_root?

@evverx
Copy link
Member

evverx commented Nov 6, 2016

Also, I'd prefer to not mix two commits in this PR. I think we should fix the memory issue first. @tixxdz , what do you think?

@tixxdz
Copy link
Member Author

tixxdz commented Nov 6, 2016

@evverx ok, all fixed, the other patch is here #4596

Thanks!

@evverx
Copy link
Member

evverx commented Nov 6, 2016

@tixxdz , thanks!

I'll run systemd under valgrind/AddressSanitizer tomorrow

BTW TestPathsStat passed:

=== RUN   TestPathsStat
--- PASS: TestPathsStat (27.81s)
    rkt_tests.go:117: Running command: /home/vagrant/rkt/build-rkt-1.18.0+git/tmp/functional/rkt --dir=/tmp/datadir-374862404 --local-config=/tmp/localdir-303126739 --system-config=/tmp/systemdir-176106006 --user-config=/tmp/userdir-554123901 --insecure-options=image prepare /home/vagrant/rkt/build-rkt-1.18.0+git/tmp/functional/test-tmp/rkt-inspect-stat-procfs.aci
    rkt_tests.go:117: Running command: /home/vagrant/rkt/build-rkt-1.18.0+git/tmp/functional/rkt --dir=/tmp/datadir-492000184 --local-config=/tmp/localdir-105045943 --system-config=/tmp/systemdir-241623978 --user-config=/tmp/userdir-046887169 --insecure-options=image prepare /home/vagrant/rkt/build-rkt-1.18.0+git/tmp/functional/test-tmp/rkt-inspect-stat-procfs.aci
PASS
ok      github.com/coreos/rkt/tests 27.931s

Copy link
Member

@keszybz keszybz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general, but there's still a few iffy places.

if (!path)
return -ENOMEM;

if (!path_is_absolute(path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that strdup should be done below the !path_is_absolute check. This would avoid the memleak.

const char *path = prefix_roota(root_directory, m->path);
char *path = prefix_root(root_directory, m->path);

if (!path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be more idiomatic to move the initalization out of the declaration block:

char *path;

path = prefix_root(...);
if (!path)
      ...

@@ -309,6 +331,7 @@ static void drop_duplicates(BindMount *m, unsigned *n) {
* above. */
if (previous && path_equal(f->path, previous->path)) {
log_debug("%s is duplicate.", f->path);
free(f->path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, even though this free is correct, it looks dangerous. Maybe f->path = mfree(f->path) would be safer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes the mountcnt catches this but better safe than sorry, updated.

continue;
if (r < 0)
return log_debug_errno(r, "Failed to chase symlinks for %s: %m", f->path);
} if (r < 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline here.

else {
log_debug("Chased %s → %s", f->path, f->chased);
f->path = f->chased;
log_debug("Chased %s → %s", f->path, chased);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This manual string freeing looks error-prone. Maybe use _cleanup_free_ for chased, and free_and_replace here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean free_and_strdup, thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, free_and_replace. No need to duplicate the string again.

@@ -724,96 +753,96 @@ int setup_namespace(

BindMount *m, *mounts = NULL;
bool make_slave = false;
unsigned n;
unsigned mountcnt;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n_mounts?

@tixxdz tixxdz force-pushed the djalal/fix-rootdir-apply-mntns branch from cd92950 to ec066d2 Compare November 6, 2016 21:55
@tixxdz
Copy link
Member Author

tixxdz commented Nov 6, 2016

@keszybz updated, thanks!

@tixxdz
Copy link
Member Author

tixxdz commented Nov 7, 2016

@martinpitt this one failed at xeinal-i386 test where previous versions of it succeeded, and this #4596 with same commit + another one succeeded, should I re-push or maybe a valid bug ? failed at this one https://anonscm.debian.org/cgit/pkg-systemd/systemd.git/tree/debian/tests/boot-and-services#n62 ? thanks!

Instead of having two fields inside BindMount struct where one is stack
based and the other one is heap, use one field to store the full path
and updated it when we chase symlinks. This way we avoid dealing with
both at the same time.

This makes RootDirectory= work with ProtectHome= and ProtectKernelModules=yes

Fixes: systemd#4567
@tixxdz tixxdz force-pushed the djalal/fix-rootdir-apply-mntns branch from ec066d2 to f0a4feb Compare November 7, 2016 11:37
@tixxdz tixxdz added this to the v233 milestone Nov 7, 2016
@martinpitt
Copy link
Contributor

@tixxdz , the lightdm test is indeed a bit flaky; if that happens, I usually just retry it. But it's re-running now anyway due to new commits.

@tixxdz
Copy link
Member Author

tixxdz commented Nov 7, 2016

@martinpitt I see ok, thank you for the info. I forced pushed the same patch again.

@keszybz
Copy link
Member

keszybz commented Nov 7, 2016

I added two small changes on top, please re-review.

@tixxdz
Copy link
Member Author

tixxdz commented Nov 7, 2016

@keszybz ah free_and_replace() sets chased to null, didn't notice that. Lgtm thank you!

@evverx
Copy link
Member

evverx commented Nov 7, 2016

@tixxdz , sorry for the delay. I'm testing. Stay tuned :-)

@evverx evverx merged commit 453a9c7 into systemd:master Nov 7, 2016
@evverx
Copy link
Member

evverx commented Nov 8, 2016

Hm, another test:

=== RUN   TestVolumeSysfs
--- FAIL: TestVolumeSysfs (12.67s)
...
        [309106.757998] rkt-inspect[12]: *** Error in `(inspect)': double free or corruption (fasttop): 0x000055bbdd76c550 ***
        [FAILED] Failed to start Application=rkt-inspect Image=coreos.com/rkt-inspect.
...

tixxdz added a commit to endocode/systemd that referenced this pull request Nov 8, 2016
…s() fails

This certainly fixes a bug that was introduced by PR
systemd#4594 that intended to fix
systemd#4567.

The fix was not complete. This patch makes sure that we count freed
paths when chase_all_symlinks() fails and we do not return directly.

Also go ahead and remove assert() checks against mount counts being zero.
This does not make sense since we are explicitly decrementing and freeing
mount paths which makes it easy to hit the assert. If users did provide
a misconfigured paths then just log a debug message, ignore and
continue. Note that if mount count is zero at the beginning, then we
we never hit this code path.

Fixes systemd#4567
tixxdz added a commit to endocode/systemd that referenced this pull request Nov 8, 2016
…s() fails

This certainly fixes a bug that was introduced by PR
systemd#4594 that intended to fix
systemd#4567.

The fix was not complete. This patch makes sure that we count freed
paths when chase_all_symlinks() fails and we do not return directly.

Also go ahead and remove assert() checks against mount counts being zero.
This does not make sense since we are explicitly decrementing and freeing
mount paths which makes it easy to hit the assert. If users did provide
a misconfigured paths then just log a debug message, ignore and
continue. Note that if mount count is zero at the beginning, then we
we never hit this code path.

Fixes systemd#4567
tixxdz added a commit to endocode/systemd that referenced this pull request Nov 8, 2016
…s() fails

This certainly fixes a bug that was introduced by PR
systemd#4594 that intended to fix
systemd#4567.

The fix was not complete. This patch makes sure that we count freed
paths when chase_all_symlinks() fails and we do not return directly.

Also go ahead and remove assert() checks against mount counts being zero.
This does not make sense since we are explicitly decrementing and freeing
mount paths which makes it easy to hit the assert. If users did provide
a misconfigured paths then just log a debug message, ignore and
continue. Note that if mount count is zero at the beginning, then we
we never hit this code path.

Fixes systemd#4567
tixxdz added a commit to endocode/systemd that referenced this pull request Nov 9, 2016
This certainly fixes a bug that was introduced by PR
systemd#4594 that intended to fix
systemd#4567.

The fix was not complete. This patch makes sure that we count and free
all paths that fail inside chase_all_symlinks().

Fixes systemd#4567
keszybz pushed a commit that referenced this pull request Nov 10, 2016
…() (#4619)

This certainly fixes a bug that was introduced by PR
#4594 that intended to fix
#4567.

The fix was not complete. This patch makes sure that we count and free
all paths that fail inside chase_all_symlinks().

Fixes #4567
lucab added a commit to lucab/rkt that referenced this pull request Nov 28, 2016
rkt currently tries to switch on `ProtectKernelTunables` for supported
systemd versions (ie. >=232), but it does not work together with
`RootDirectory` due to systemd/systemd#4594
which is targeted at v233.

This commit bumps the version check accordingly.
keszybz pushed a commit to systemd/systemd-stable that referenced this pull request Jan 31, 2017
…() (#4619)

This certainly fixes a bug that was introduced by PR
systemd/systemd#4594 that intended to fix
systemd/systemd#4567.

The fix was not complete. This patch makes sure that we count and free
all paths that fail inside chase_all_symlinks().

Fixes systemd/systemd#4567
(cherry picked from commit 1d54cd5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

None yet

4 participants