1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 10:45:08 +00:00

Kernel: Don't keep protected Process data in a separate allocation

The previous architecture had a huge flaw: the pointer to the protected
data was itself unprotected, allowing you to overwrite it at any time.

This patch reorganizes the protected data so it's part of the Process
class itself. (Actually, it's a new ProcessBase helper class.)

We use the first 4 KB of Process objects themselves as the new storage
location for protected data. Then we make Process objects page-aligned
using MAKE_ALIGNED_ALLOCATED.

This allows us to easily turn on/off write-protection for everything in
the ProcessBase portion of Process. :^)

Thanks to @bugaevc for pointing out the flaw! This is still not perfect
but it's an improvement.
This commit is contained in:
Andreas Kling 2021-03-11 13:13:05 +01:00
parent 4fcc637e29
commit 90c0f9664e
10 changed files with 125 additions and 125 deletions

View file

@ -37,7 +37,10 @@ KResultOr<int> Process::sys$seteuid(uid_t new_euid)
if (euid() != new_euid)
set_dumpable(false);
MutableProtectedData(*this)->euid = new_euid;
ProtectedDataMutationScope scope { *this };
m_euid = new_euid;
return 0;
}
@ -51,7 +54,8 @@ KResultOr<int> Process::sys$setegid(gid_t new_egid)
if (egid() != new_egid)
set_dumpable(false);
MutableProtectedData(*this)->egid = new_egid;
ProtectedDataMutationScope scope { *this };
m_egid = new_egid;
return 0;
}
@ -65,10 +69,10 @@ KResultOr<int> Process::sys$setuid(uid_t new_uid)
if (euid() != new_uid)
set_dumpable(false);
MutableProtectedData protected_data { *this };
protected_data->uid = new_uid;
protected_data->euid = new_uid;
protected_data->suid = new_uid;
ProtectedDataMutationScope scope { *this };
m_uid = new_uid;
m_euid = new_uid;
m_suid = new_uid;
return 0;
}
@ -82,10 +86,10 @@ KResultOr<int> Process::sys$setgid(gid_t new_gid)
if (egid() != new_gid)
set_dumpable(false);
MutableProtectedData protected_data { *this };
protected_data->gid = new_gid;
protected_data->egid = new_gid;
protected_data->sgid = new_gid;
ProtectedDataMutationScope scope { *this };
m_gid = new_gid;
m_egid = new_gid;
m_sgid = new_gid;
return 0;
}
@ -107,10 +111,10 @@ KResultOr<int> Process::sys$setresuid(uid_t new_ruid, uid_t new_euid, uid_t new_
if (euid() != new_euid)
set_dumpable(false);
MutableProtectedData protected_data { *this };
protected_data->uid = new_ruid;
protected_data->euid = new_euid;
protected_data->suid = new_suid;
ProtectedDataMutationScope scope { *this };
m_uid = new_ruid;
m_euid = new_euid;
m_suid = new_suid;
return 0;
}
@ -132,10 +136,10 @@ KResultOr<int> Process::sys$setresgid(gid_t new_rgid, gid_t new_egid, gid_t new_
if (egid() != new_egid)
set_dumpable(false);
MutableProtectedData protected_data { *this };
protected_data->gid = new_rgid;
protected_data->egid = new_egid;
protected_data->sgid = new_sgid;
ProtectedDataMutationScope scope { *this };
m_gid = new_rgid;
m_egid = new_egid;
m_sgid = new_sgid;
return 0;
}
@ -148,7 +152,8 @@ KResultOr<int> Process::sys$setgroups(ssize_t count, Userspace<const gid_t*> use
return EPERM;
if (!count) {
MutableProtectedData(*this)->extra_gids.clear();
ProtectedDataMutationScope scope { *this };
m_extra_gids.clear();
return 0;
}
@ -163,13 +168,13 @@ KResultOr<int> Process::sys$setgroups(ssize_t count, Userspace<const gid_t*> use
unique_extra_gids.set(extra_gid);
}
MutableProtectedData protected_data { *this };
protected_data->extra_gids.resize(unique_extra_gids.size());
ProtectedDataMutationScope scope { *this };
m_extra_gids.resize(unique_extra_gids.size());
size_t i = 0;
for (auto& extra_gid : unique_extra_gids) {
if (extra_gid == gid())
continue;
protected_data->extra_gids[i++] = extra_gid;
m_extra_gids[i++] = extra_gid;
}
return 0;
}