sunriseos / SunriseOS

Horizon/NX kernel reimplementation
Apache License 2.0
232 stars 13 forks source link

Kernel: Fix random userspace gs = 0 #475

Closed roblabla closed 5 years ago

roblabla commented 5 years ago

Sometimes, when running userspace, GS would be 0, leading to instructions accessing gs:[0] to fail with a null deref. I'm... still not entirely sure why that happens. It appears that our ring detection in trap_gate_asm fails, leading to gs not being changed before returning to userspace. But what confuses me is that when this happens, gs should be set to the kernel TLS, and not 0.

Anyways, this is fixed by pushing GS on the UserSpaceHardwareContext and restoring it unconditionally before returning.

Fixes (but we don't know why) #473

Orycterope commented 5 years ago

As you said, the bug makes no sense, and to me finding a way around it without trying to actually understand it, isn't the way to go about it.

I'd really like to understand the root of the problem, as it might hint toward something being really wrong somewhere. I'll do that asap.

In the mean time, backing up gs doesn't really makes sense, it should be UTLS for user, KTLS for kernel, and never anything else, there's no need for context here.

roblabla commented 5 years ago

I agree, but this still fixes the underlying crash which is crippling (at least to me), and makes the logic a bit easier to follow. We might want to open a new issue afterwards to investigate what the real cause is.

Orycterope commented 5 years ago

Well ok then, i approve, but keep an issue open