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

fix(model_specific): make {Fs,Gs,KernelGs}Base::write() unsafe #528

Merged
merged 1 commit into from
Dec 24, 2024

Conversation

mkroening
Copy link
Member

This makes FsBase::write(), GsBase::write(), and KernelGsBase::write() unsafe and applies the safety doc comment from Segment64::write_base().

Closes #526.

@mkroening
Copy link
Member Author

mkroening commented Dec 24, 2024

I am not familiar with KernelGsBase, but I think the same reasoning applies.

Ah, this should target next, right?

@mkroening mkroening changed the base branch from master to next December 24, 2024 15:39
@mkroening mkroening force-pushed the unsafe-base-write branch 2 times, most recently from d2ef402 to 1ed3a36 Compare December 24, 2024 15:40
@Freax13
Copy link
Member

Freax13 commented Dec 24, 2024

I am not familiar with KernelGsBase, but I think the same reasoning applies.

I'm not completely sure about this one. Writing to KernelGsBase does not directly affect the base of the GS segment register even when in kernel mode. There's an instruction, swapgs, that can be used to exchange the GS segment register base with the value of the KernelGsBase MSR, and our wrapper for this instruction, GS::swap, is unsafe. While it could be argued that it's only the swapgs instruction that has the potential to cause UB, any code that uses swapgs likely relies on KernelGsBase containing valid values, so making writes KernelGsBase unsafe seems okay to me.

Ah, this should target next, right?

Yes

Comment on lines 359 to 360
/// The caller must ensure that this write operation has no unsafe side
/// effects, as the segment base address might be in use.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// The caller must ensure that this write operation has no unsafe side
/// effects, as the segment base address might be in use.
/// The caller must ensure that this write operation has no unsafe side
/// effects, as the segment base address might be in use.

As mentioned in my previous comment, the KernelGsBase register is not the same as the base of the GS registers, so it cannot be "in use".

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, that's what you meant. I changed it to “The caller must ensure that a future call to GS::swap has no unsafe side effects.”

Are you fine with that wording, or would you prefer a different wording?

Copy link
Member

Choose a reason for hiding this comment

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

I can't come up with anything better of the top of my head. My impression is that the safety implications are closely tied to how a user actually uses KernelGsBase, so it's hard to provide generic guidelines and this is probably the best we can do.

@mkroening
Copy link
Member Author

I'm not completely sure about this one. Writing to KernelGsBase does not directly affect the base of the GS segment register even when in kernel mode. There's an instruction, swapgs, that can be used to exchange the GS segment register base with the value of the KernelGsBase MSR, and our wrapper for this instruction, GS::swap, is unsafe. While it could be argued that it's only the swapgs instruction that has the potential to cause UB, any code that uses swapgs likely relies on KernelGsBase containing valid values, so making writes KernelGsBase unsafe seems okay to me.

Ah, yes, I remember. Sounds good to me. 👍

Copy link
Member

@Freax13 Freax13 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 to me, thanks! :shipit: 🎄

@Freax13 Freax13 merged commit cd14167 into rust-osdev:next Dec 24, 2024
11 of 13 checks passed
@mkroening mkroening deleted the unsafe-base-write branch December 24, 2024 17:13
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.

Unsound {Fs,Gs}Base::write()
2 participants