Skip to content

chrootenv: rewrite on top of GLib - #33139

Merged
lukateras merged 1 commit into
NixOS:masterfrom
lukateras:20171228.053707/chrootenv
Dec 31, 2017
Merged

lukateras merged 1 commit into
NixOS:masterfrom
lukateras:20171228.053707/chrootenv

Conversation

@lukateras

@lukateras lukateras commented Dec 28, 2017 •

Copy link
Copy Markdown
Member

Changes:

  • doesn't handle root user separately
  • doesn't chdir("/") which makes using it seamless
  • only bind mounts, doesn't symlink

Incidentally, it fixes #33106.

It's about two times shorter than the previous version (it's even shorter than the Ruby version), and much, much easier to read/follow through. It uses GLib quite heavily, along with RAII (available in GCC/Clang).

Symlinking already happens in init script, so it's redundant to have it in this code. It has only been in chrootenv for a week, and I might reintroduce it back later.

About closure size: Steam itself uses GLib, so it won't result in a larger closure for most chrootenv users. It's also likely to be required by other programs. I've tested Steam, and of course, it still works.

/cc @Mic92 @dezgeg @srhb @pbogdan

@GrahamcOfBorg GrahamcOfBorg added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. labels Dec 28, 2017
@lukateras
lukateras force-pushed the 20171228.053707/chrootenv branch from 9307ec2 to be611fd Compare December 28, 2017 17:03
@lukateras

lukateras commented Dec 28, 2017 •

Copy link
Copy Markdown
Member Author

I feel confident about new code and will merge on 31st if no one has any issues with it.

I should note that none of the changes to chrootenv are breaking buildFHSUserEnv behavior because root-specific logic was an optimization, and scripts that wrap chrootenv do chdir and symlink anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is not obvious that the equivalent of return EX_OSERR; of the old version is unneeded in the new version: the signal that terminated the child process may be ignored by default and not terminate chrootenv.

@lukateras lukateras Dec 29, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If signal is ignored, chrootenv will return 0. This seems to be logical behavior.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the child overrode the default of ignoring a signal and died on receiving it, it did not exit cleanly and chrootenv should not return 0.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You are right, it's better to return 1 in this case. I've added it, see ac40a57.

@lukateras
lukateras force-pushed the 20171228.053707/chrootenv branch 3 times, most recently from a0d1430 to 430476a Compare December 29, 2017 08:23
@FRidh

FRidh commented Dec 29, 2017

Copy link
Copy Markdown
Member

What do you think about documenting what this code is for and how it (roughly) works. I think each piece of code should explain why it is needed. So, what problem does this code solve?

Looking at the older version, there were some comments that helps the reader understand what is happening.

@lukateras

lukateras commented Dec 29, 2017 •

Copy link
Copy Markdown
Member Author

There are only two comments that still apply, specifically "Create new mount and user namespaces" and "Map users and groups to the parent namespace". I don't think these are particularly useful to keep around, because people who know about mount and user namespaces probably know about unshare anyway. If not, then man 2 unshare provides better documentation than these comments.

So, what problem does this code solve?

Setting up a chroot to ultimately run programs that assume a FHS-compliant environment.

@FRidh

FRidh commented Dec 29, 2017

Copy link
Copy Markdown
Member

Setting up a chroot to ultimately run programs that assume a FHS-compliant environment.

That at the very least should be included in the code header.

@lukateras

lukateras commented Dec 29, 2017 •

Copy link
Copy Markdown
Member Author

I'll add something like it to meta.description, thanks.

@dtzWill

dtzWill commented Dec 29, 2017

Copy link
Copy Markdown
Member

According to docs it seems g_error calls abort instead of exit which results in a coredump and is not intended for user-facing errors.

This seems inappropriate for at least the error path that prints messages to the user about running sysctl.

Maybe make a pass with this in mind, or instead adjust the g_perr macro to just log and exit instead?

@lukateras

lukateras commented Dec 29, 2017 •

Copy link
Copy Markdown
Member Author

abort is correct. Also, failed unshare might be a user configuration problem, or might be not, and coredump might be useful.

@dtzWill

dtzWill commented Dec 29, 2017

Copy link
Copy Markdown
Member

Not to bikeshed but I can't help but wonder how much of benefit glib brings here compared to C++ or something (g_autofree patterns, etc.)?

Regardless I'd appreciate, if for my curiosity if nothing else, a mention of the role glib plays in obtaining the improvements in this PR or to what extent they're orthogonal?

@lukateras

lukateras commented Dec 29, 2017 •

Copy link
Copy Markdown
Member Author

I think C++ is a bad language because it's very complex.

g_autofree is negligible convenience, and improvement mostly comes from utility functions: g_build_filename, g_mkdtemp_full (handles chmod), g_file_test, g_dir_open (doesn't return . and .. unlike opendir), g_strv_contains.

@dtzWill

dtzWill commented Dec 29, 2017

Copy link
Copy Markdown
Member

Okay, thanks for the comments and consideration of abort. LGTM review-wise.

@lukateras

Copy link
Copy Markdown
Member Author

Thanks for reviewing it!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

g_ is a glib namespace, you should not define names starting with g_.

@lukateras lukateras Dec 30, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, and names themselves are misleading (perror doesn't abort). I've pushed a new version with fail and fail_if macros: I think that separates concerns much better than previously used error macros, and names are much more straightforward. fail_if also cuts some boilerplate.

@lukateras
lukateras force-pushed the 20171228.053707/chrootenv branch 2 times, most recently from a1182bd to 37da1a8 Compare December 30, 2017 18:26
Changes:

* doesn't handle root user separately
* doesn't chdir("/") which makes using it seamless
* only bind mounts, doesn't symlink (i.e. files)

Incidentally, fixes NixOS#33106.

It's about two times shorter than the previous version, and much
easier to read/follow through. It uses GLib quite heavily, along with
RAII (available in GCC/Clang).
@lukateras
lukateras force-pushed the 20171228.053707/chrootenv branch from 37da1a8 to 4b1cf5a Compare December 30, 2017 22:28

@Mic92 Mic92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have not tested the code itself, but the rest looks good to me.

@lukateras

lukateras commented Dec 31, 2017 •

Copy link
Copy Markdown
Member Author

@Mic92: Thanks for mentioning __cleanup__ GCC/Clang attribute on IRC!

@lukateras

lukateras commented Dec 31, 2017 •

Copy link
Copy Markdown
Member Author

@orivej: Thanks for the review!

I still want to implement a different order of messages in case of failed unshare, and the only clean way that I could come up with is via a SIGABRT or SIGTRAP handler. The problem is that if I use GLib logging from the handler, call to any logging function results in recursion warning and a subsequent abort (which is technically what I want to do anyway, but it doesn't let me print two messages).

@lukateras
lukateras merged commit 60a133f into NixOS:master Dec 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chrootenv: breaks when root contains broken symlink

6 participants