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

Is PTYDevice::Ioctl() a possible security/stability problem? #45

Open
robertlipe opened this issue Sep 6, 2022 · 2 comments
Open

Is PTYDevice::Ioctl() a possible security/stability problem? #45

robertlipe opened this issue Sep 6, 2022 · 2 comments

Comments

@robertlipe
Copy link

Hi. I'm not actually a LemonOS user. I'm just cruising around reading OS code and I like a lot of what I see in LemonOS - congrats. While I'm thus not a LemonOS dev, I have lots of experience in UNIX-like kernels. Sometimes that's helpful and sometimes it can lead me astray. :-)

There is one bit of code that catches my eye as potentially problematic. It's possible there's something in place elsewhere to prevent this, but it looks like the arg in PTYDevice::Ioctl() is treated as a kernel mode pointer by all five subcommands. I suspect that an evil caller of these, especially from user space, could use these to (slowly) read and write arbitrary kernel memory. If so, I'm sure you recognize that as a Bad Thing.

In a traditional UNIX system, there's a possibility that the pointers could be to user space that's paged out or in another process or in kernel space(!). We'd protect against that via copyin() and copyout()() and testing for errors. I think the LemonOS spelling of that is approximately:
if(!Memory::CheckUsermodePointer(arg, sizeof(THING), currentProcess->addressSpace)){
return -EFAULT;
}

Even if arbitrary read/writes aren't possible (I can't tell if tios and wSz are exposed to user space) it does seem like just casting a pointer received from user space may result in crashes, especially if your OS isn't totally identity mapped with kernel and user addresses mapped the same. (And if they are, you probably have bigger security issues.)

Additionally, are there atomicity issues where termios or wsz in the base class can be partially updated by another process while we get put on the run queue and do these ioctls and get part of an old copy and part of a new copy because no lock is (visibly) held here? Maybe a lock is held externally at some higher external layer that I just don't see looking at pty.[cc,h] in isolation.

I could be wrong and this might all be totally safe. If I am wrong, you don't need to write a dissertation to educate me, but please at least look at that code and pencil-whip it to be sure it's sensible. (I know all too well that in large, complicated products - open and otherwise - sometimes error checking falls on the floor and we assume our callers aren't hostile. It happens.)

Cool project! Thanx for creating code that's easy to read, has a sane mix of C and C++, and no more asm() than it has to be.

@robertlipe robertlipe changed the title Is PTYDevice::Ioctl() a security problem. Is PTYDevice::Ioctl() a possible security/stability problem? Sep 6, 2022
@fido2020
Copy link
Member

fido2020 commented Sep 6, 2022

Thanks! Yeah unfortunately many of the syscalls and vfs calls do unchecked reads and writes to userspace. I am however working on a rewrite of the syscalls and I am also rewriting much of the vfs in the api-rework branch, which aims to address issues like this.

@robertlipe
Copy link
Author

robertlipe commented Sep 7, 2022 via email

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

No branches or pull requests

2 participants