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

fdio: Use O_TMPFILE + rename-overwrite for regfile copies #80

Closed
wants to merge 1 commit into from

Conversation

cgwalters
Copy link
Member

I was working on rpm-ostree unified core, and hit the fact that
glnx_file_copy_at() had the same bug with fsetxattr() and files whose mode
is <= 0400 (e.g. 000 in the case of /etc/shadow) that libostree did a
while ago. This situation is masked for privileged (i.e. CAP_DAC_OVERRIDE)
code.

Looking at this, I think it's cleaner to convert to O_TMPFILE here,
since that code already handles setting the tmpfile to mode 0600. Now,
this is a behavior change in the corner case of existing files which
are symbolic links. Previously we'd do an open(O_TRUNC) which would follow
the link.

But in the big picture, I think the use cases for open(O_TRUNC) are really
rare - I audited all callers of this in ostree/rpm-ostree/flatpak, and all of
them will be fine with this behavior change. For example, the ostree /etc
merge code already explicitly unlinks the target beforehand. Other cases like
supporting repo/pubring.gpg in an ostree repo being a symlink...eh, just no.

Making this change allows us to convert to new style, and brings all of the
general benefits of using O_TMPFILE too.

I was working on rpm-ostree unified core, and hit the fact that
`glnx_file_copy_at()` had the same bug with `fsetxattr()` and files whose mode
is <= `0400` (e.g. `000` in the case of `/etc/shadow`) that libostree did a
while ago. This situation is masked for privileged (i.e. `CAP_DAC_OVERRIDE`)
code.

Looking at this, I think it's cleaner to convert to `O_TMPFILE` here,
since that code already handles setting the tmpfile to mode `0600`.  Now,
this *is* a behavior change in the corner case of existing files which
are symbolic links.  Previously we'd do an `open(O_TRUNC)` which would follow
the link.

But in the big picture, I think the use cases for `open(O_TRUNC)` are really
rare - I audited all callers of this in ostree/rpm-ostree/flatpak, and all of
them will be fine with this behavior change. For example, the ostree `/etc`
merge code already explicitly unlinks the target beforehand. Other cases like
supporting `repo/pubring.gpg` in an ostree repo being a symlink...eh, just no.

Making this change allows us to convert to new style, and brings all of the
general benefits of using `O_TMPFILE` too.
cgwalters added a commit to cgwalters/rpm-ostree that referenced this pull request Sep 11, 2017
Prep for unified core work.  This was failing due to a bug
in libglnx <GNOME/libglnx#80> but
I think this change is also correct.  There's no good reason
for us to copy xattrs like the SELinux label here - rather
we want the labels to be reset during commit.
@@ -858,11 +858,15 @@ glnx_regfile_copy_bytes (int fdf, int fdt, off_t max_bytes)
* @cancellable: cancellable
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit/not new to this patch: the src_stbuf parameter isn't in the docstring.

}

/* Always chmod after setting xattrs, in case the file has mode 0400 or less,
* like /etc/shadow.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'm missing something here. Does setting xattrs reset the mode? Can you link to the ostree issue in the commit message or the 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.

Basically if a file is mode 000 (or in general doesn't have write perms), one can write() to it but not fsetxattr(). ostreedev/ostree@3348baf is related and so is 452c371

I thought there was a better commit but I can't find it.

Copy link
Member Author

Choose a reason for hiding this comment

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

In other words we need to fchmod() after we do the xattrs, and the file needs to be writable before that, which our O_TMPFILE code ensures.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh gotcha, wasn't aware of that. Maybe make it more explicit in the comment? Otherwise, LGTM!

@cgwalters
Copy link
Member Author

Updated for both issues and pushed, thanks!

@cgwalters cgwalters closed this Sep 12, 2017
cgwalters added a commit to cgwalters/rpm-ostree that referenced this pull request Sep 25, 2017
Prep for unified core work.  This was failing due to a bug
in libglnx <GNOME/libglnx#80> but
I think this change is also correct.  There's no good reason
for us to copy xattrs like the SELinux label here - rather
we want the labels to be reset during commit.
cgwalters added a commit to cgwalters/rpm-ostree that referenced this pull request Sep 25, 2017
Prep for unified core work. This was failing due to a bug in libglnx
<GNOME/libglnx#80> but I think this change is also
correct. There's no good reason for us to copy xattrs like the SELinux label
here - rather we want the labels to be reset during commit.

I did a tree-wide grep for other users and the only other case that is odd is
the treecompose `add-files`; I'd say we should change this but out of (a likely
excess of) conservatism I just left a "FIXME" for now.
rh-atomic-bot pushed a commit to coreos/rpm-ostree that referenced this pull request Sep 26, 2017
Prep for unified core work. This was failing due to a bug in libglnx
<GNOME/libglnx#80> but I think this change is also
correct. There's no good reason for us to copy xattrs like the SELinux label
here - rather we want the labels to be reset during commit.

I did a tree-wide grep for other users and the only other case that is odd is
the treecompose `add-files`; I'd say we should change this but out of (a likely
excess of) conservatism I just left a "FIXME" for now.

Closes: #1008
Approved by: jlebon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants