Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

[LibOS] Fix poll() crashing on pseudo-files #2498

Merged
merged 1 commit into from
Jul 5, 2021
Merged

[LibOS] Fix poll() crashing on pseudo-files #2498

merged 1 commit into from
Jul 5, 2021

Conversation

pwmarcz
Copy link
Contributor

@pwmarcz pwmarcz commented Jul 2, 2021

Found when investigating stress-ng (#2419).

Description of the changes

The code assumed that all handles (other than TYPE_FILE and TYPE_DEV) had an associated PAL handle, which caused a crash when polling files from /proc. This change adds support for polling TYPE_STR, and also adds a check for PAL handle in all cases.

(I think poll deserves a bigger rewrite, this is just a quick fix).

How to test this PR?


This change is Reviewable

Copy link

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 6 files at r1.
Reviewable status: 2 of 6 files reviewed, all discussions resolved, not enough approvals from maintainers (2 more required), not enough approvals from different teams (1 more required, approved so far: ITL)

Copy link

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 6 files at r1.
Reviewable status: all files reviewed, 2 unresolved discussions, not enough approvals from maintainers (2 more required), not enough approvals from different teams (1 more required, approved so far: ITL) (waiting on @pwmarcz)


LibOS/shim/src/fs/shim_fs_pseudo.c, line 446 at r1 (raw file):

/* TODO: add support for polling TYPE_STR handles; currently `shim_do_poll` doesn't call this for
 * anything else than TYPE_STR and TYPE_DEV */

Btw, this comment was wrong? I see that shim_do_poll() calls hdl->fs->fs_ops->poll() on TYPE_FILE and TYPE_DEV. There was no mention of TYPE_STR.


LibOS/shim/src/fs/str/fs.c, line 280 at r1 (raw file):

    }
    if (poll_type & FS_POLL_WR)
        ret |= FS_POLL_WR;

Can we really always write in a pseudo-str-file? Some files are read-only (e.g. /dev/attestation/report), don't we want to check whether the permissions allow writing?


LibOS/shim/src/sys/shim_poll.c, line 110 at r1 (raw file):

             * callback.
             *
             * TODO: we probably should use the poll() callback in all cases. */

What do you mean by this comment?

Copy link
Contributor Author

@pwmarcz pwmarcz left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 2 unresolved discussions, not enough approvals from maintainers (2 more required), not enough approvals from different teams (1 more required, approved so far: ITL) (waiting on @dimakuv)


LibOS/shim/src/fs/shim_fs_pseudo.c, line 446 at r1 (raw file):

Previously, dimakuv (Dmitrii Kuvaiskii) wrote…

Btw, this comment was wrong? I see that shim_do_poll() calls hdl->fs->fs_ops->poll() on TYPE_FILE and TYPE_DEV. There was no mention of TYPE_STR.

Yes, that was supposed to be "anything else than TYPE_FILE and TYPE_DEV".


LibOS/shim/src/fs/str/fs.c, line 280 at r1 (raw file):

Previously, dimakuv (Dmitrii Kuvaiskii) wrote…

Can we really always write in a pseudo-str-file? Some files are read-only (e.g. /dev/attestation/report), don't we want to check whether the permissions allow writing?

We check file permissions when setting handle mode, and the caller of poll callback (such as shim_do_poll) checks the handle mode. I don't think the poll callback itself should do that - the check is the same regardless of handle.

(This does allow a loophole where a user chmods the file and then writes to it. I just checked and Linux allows you to chmod a file like /proc/meminfo to be writable, and even open it, but fails with EIO on the actual write call... I don't want to bother with replicating that now, but I'm planning to rewrite the str_* filesystem so maybe that will be the time.)


LibOS/shim/src/sys/shim_poll.c, line 110 at r1 (raw file):

Previously, dimakuv (Dmitrii Kuvaiskii) wrote…

What do you mean by this comment?

This function collects PAL handles and uses DkStreamsWaitEvents on them, disregarding the fact that most of filesystems actually have a poll callback defined. It calls the poll callback only for some specific handle types. That seems weird to me: what are the other poll callbacks even for?

I don't want to touch that right now, though, it looks like bigger change and I would need to understand our implementation of poll/epoll/select better. Right now I want to fix this one thing with STR handles.

dimakuv
dimakuv previously approved these changes Jul 5, 2021
Copy link

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, all discussions resolved, not enough approvals from maintainers (1 more required)


LibOS/shim/src/fs/str/fs.c, line 280 at r1 (raw file):

Previously, pwmarcz (Paweł Marczewski) wrote…

We check file permissions when setting handle mode, and the caller of poll callback (such as shim_do_poll) checks the handle mode. I don't think the poll callback itself should do that - the check is the same regardless of handle.

(This does allow a loophole where a user chmods the file and then writes to it. I just checked and Linux allows you to chmod a file like /proc/meminfo to be writable, and even open it, but fails with EIO on the actual write call... I don't want to bother with replicating that now, but I'm planning to rewrite the str_* filesystem so maybe that will be the time.)

OK, fair enough.


LibOS/shim/src/sys/shim_poll.c, line 110 at r1 (raw file):

Previously, pwmarcz (Paweł Marczewski) wrote…

This function collects PAL handles and uses DkStreamsWaitEvents on them, disregarding the fact that most of filesystems actually have a poll callback defined. It calls the poll callback only for some specific handle types. That seems weird to me: what are the other poll callbacks even for?

I don't want to touch that right now, though, it looks like bigger change and I would need to understand our implementation of poll/epoll/select better. Right now I want to fix this one thing with STR handles.

OK. The idea of a poll callback was to have a fall-back for those handle types that do not have a corresonding PAL handle (= emulated completely inside LibOS, without the help of the host OS). But let's not touch this now.

mkow
mkow previously approved these changes Jul 5, 2021
Copy link
Member

@mkow mkow left a comment

Choose a reason for hiding this comment

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

Reviewed 6 of 6 files at r1.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

The code assumed that all handles (other than TYPE_FILE and TYPE_DEV)
had an associated PAL handle, which caused a crash when polling files
from /proc. This change adds support for polling TYPE_STR, and also adds
a check for PAL handle in all cases.

Signed-off-by: Paweł Marczewski <pawel@invisiblethingslab.com>
Copy link
Member

@mkow mkow left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r2.
Reviewable status: all files reviewed, all discussions resolved, not enough approvals from maintainers (1 more required), not enough approvals from different teams (1 more required, approved so far: ITL)

Copy link

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@mkow mkow merged commit 5f052a6 into master Jul 5, 2021
@mkow mkow deleted the pawel/fix-poll branch July 5, 2021 13:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants