2020-02-09 19:00:00 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-09 19:00:00 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
Kernel: Fix restrictions in is_allowed_to_mmap_to_userspace function
This small change simplifies the function a bit but also fixes a problem
with it.
Let's take an example to see this:
Let's say we have a reserved range between 0xe0000 to 0xfffff (EBDA),
then we want to map from the memory device (/dev/mem) the entire
EBDA to a program. If a program tries to map more than 131072 bytes,
the current logic will work - the start address is 0xe0000, and ofcourse
it's below the limit, hence it passes the first two restrictions.
Then, the third if statement will fail if we try to mmap more than
the said allowed bytes.
However, let's take another scenario, where we try to mmap from
0xf0000 - but we try to mmap less than 131072 - but more than 65536.
In such case, we again pass the first two if statements, but the third
one is passed two, because it doesn't take into account the offseted
address from the start of the reserved range (0xe0000). In such case,
a user can easily mmap 65535 bytes above 0x100000. This might
seem negligible. However, it's still a severe bug that can theoretically
be exploited into a info leak or tampering with important kernel
structures.
2021-10-22 11:59:31 +03:00
|
|
|
#include <AK/Checked.h>
|
2021-03-12 19:29:37 +03:00
|
|
|
#include <AK/Format.h>
|
2020-02-09 19:00:00 +03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2021-07-07 06:35:15 +03:00
|
|
|
typedef u64 PhysicalPtr;
|
|
|
|
typedef u64 PhysicalSize;
|
|
|
|
|
2020-02-09 19:00:00 +03:00
|
|
|
class PhysicalAddress {
|
|
|
|
public:
|
2021-07-07 06:35:15 +03:00
|
|
|
ALWAYS_INLINE static PhysicalPtr physical_page_base(PhysicalPtr page_address) { return page_address & ~(PhysicalPtr)0xfff; }
|
2021-07-08 04:50:05 +03:00
|
|
|
ALWAYS_INLINE static size_t physical_page_index(PhysicalPtr page_address)
|
|
|
|
{
|
|
|
|
auto page_index = page_address >> 12;
|
|
|
|
if constexpr (sizeof(size_t) < sizeof(PhysicalPtr))
|
|
|
|
VERIFY(!(page_index & ~(PhysicalPtr)((size_t)-1)));
|
|
|
|
return (size_t)(page_index);
|
|
|
|
}
|
2021-07-07 06:35:15 +03:00
|
|
|
|
2021-02-28 16:42:08 +03:00
|
|
|
PhysicalAddress() = default;
|
2021-07-07 06:35:15 +03:00
|
|
|
explicit PhysicalAddress(PhysicalPtr address)
|
2020-02-09 19:00:00 +03:00
|
|
|
: m_address(address)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-07 06:35:15 +03:00
|
|
|
[[nodiscard]] PhysicalAddress offset(PhysicalPtr o) const { return PhysicalAddress(m_address + o); }
|
Kernel: Fix restrictions in is_allowed_to_mmap_to_userspace function
This small change simplifies the function a bit but also fixes a problem
with it.
Let's take an example to see this:
Let's say we have a reserved range between 0xe0000 to 0xfffff (EBDA),
then we want to map from the memory device (/dev/mem) the entire
EBDA to a program. If a program tries to map more than 131072 bytes,
the current logic will work - the start address is 0xe0000, and ofcourse
it's below the limit, hence it passes the first two restrictions.
Then, the third if statement will fail if we try to mmap more than
the said allowed bytes.
However, let's take another scenario, where we try to mmap from
0xf0000 - but we try to mmap less than 131072 - but more than 65536.
In such case, we again pass the first two if statements, but the third
one is passed two, because it doesn't take into account the offseted
address from the start of the reserved range (0xe0000). In such case,
a user can easily mmap 65535 bytes above 0x100000. This might
seem negligible. However, it's still a severe bug that can theoretically
be exploited into a info leak or tampering with important kernel
structures.
2021-10-22 11:59:31 +03:00
|
|
|
[[nodiscard]] bool offset_addition_would_overflow(PhysicalPtr o) const { return Checked<PhysicalPtr>::addition_would_overflow(m_address, o); }
|
2021-07-07 06:35:15 +03:00
|
|
|
[[nodiscard]] PhysicalPtr get() const { return m_address; }
|
|
|
|
void set(PhysicalPtr address) { m_address = address; }
|
|
|
|
void mask(PhysicalPtr m) { m_address &= m; }
|
2020-02-09 19:00:00 +03:00
|
|
|
|
2021-02-15 02:18:35 +03:00
|
|
|
[[nodiscard]] bool is_null() const { return m_address == 0; }
|
2020-02-09 19:00:00 +03:00
|
|
|
|
2021-11-01 00:54:39 +03:00
|
|
|
// NOLINTNEXTLINE(readability-make-member-function-const) const PhysicalAddress shouldn't be allowed to modify the underlying memory
|
2021-02-15 02:18:35 +03:00
|
|
|
[[nodiscard]] u8* as_ptr() { return reinterpret_cast<u8*>(m_address); }
|
2022-04-01 20:58:27 +03:00
|
|
|
[[nodiscard]] u8 const* as_ptr() const { return reinterpret_cast<u8 const*>(m_address); }
|
2020-02-09 19:00:00 +03:00
|
|
|
|
2021-07-07 06:35:15 +03:00
|
|
|
[[nodiscard]] PhysicalAddress page_base() const { return PhysicalAddress(physical_page_base(m_address)); }
|
|
|
|
[[nodiscard]] PhysicalPtr offset_in_page() const { return PhysicalAddress(m_address & 0xfff).get(); }
|
2020-02-09 19:00:00 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(PhysicalAddress const& other) const { return m_address == other.m_address; }
|
|
|
|
bool operator!=(PhysicalAddress const& other) const { return m_address != other.m_address; }
|
|
|
|
bool operator>(PhysicalAddress const& other) const { return m_address > other.m_address; }
|
|
|
|
bool operator>=(PhysicalAddress const& other) const { return m_address >= other.m_address; }
|
|
|
|
bool operator<(PhysicalAddress const& other) const { return m_address < other.m_address; }
|
|
|
|
bool operator<=(PhysicalAddress const& other) const { return m_address <= other.m_address; }
|
2020-02-09 19:00:00 +03:00
|
|
|
|
|
|
|
private:
|
2021-07-07 06:35:15 +03:00
|
|
|
PhysicalPtr m_address { 0 };
|
2020-02-09 19:00:00 +03:00
|
|
|
};
|
|
|
|
|
2021-01-09 02:42:44 +03:00
|
|
|
template<>
|
|
|
|
struct AK::Formatter<PhysicalAddress> : AK::Formatter<FormatString> {
|
2021-11-16 03:15:21 +03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, PhysicalAddress value)
|
2021-01-09 02:42:44 +03:00
|
|
|
{
|
2021-07-07 06:35:15 +03:00
|
|
|
if constexpr (sizeof(PhysicalPtr) == sizeof(u64))
|
2022-07-11 20:32:29 +03:00
|
|
|
return AK::Formatter<FormatString>::format(builder, "P{:016x}"sv, value.get());
|
2021-07-07 06:35:15 +03:00
|
|
|
else
|
2022-07-11 20:32:29 +03:00
|
|
|
return AK::Formatter<FormatString>::format(builder, "P{}"sv, value.as_ptr());
|
2021-01-09 02:42:44 +03:00
|
|
|
}
|
|
|
|
};
|