From d27f4a4928f493f9e7a2987ad93e6be918b70f6c Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Thu, 18 Oct 2018 12:55:27 -0400
Subject: [PATCH 1/3] common: Move Is4KBAligned() to alignment.h

Aligning on 4KB pages isn't a Switch-specific thing, so this can be
moved to common so it can be used with other things as well.
---
 src/common/alignment.h      |  6 ++++++
 src/core/hle/kernel/svc.cpp | 16 +++++++---------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/common/alignment.h b/src/common/alignment.h
index 225770fab..b3f103c07 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -19,4 +19,10 @@ constexpr T AlignDown(T value, std::size_t size) {
     return static_cast<T>(value - value % size);
 }
 
+template <typename T>
+constexpr bool Is4KBAligned(T value) {
+    static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
+    return (value & 0xFFF) == 0;
+}
+
 } // namespace Common
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index d08b84bde..b0bdd822e 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -8,6 +8,7 @@
 #include <mutex>
 #include <vector>
 
+#include "common/alignment.h"
 #include "common/assert.h"
 #include "common/logging/log.h"
 #include "common/microprofile.h"
@@ -36,9 +37,6 @@
 
 namespace Kernel {
 namespace {
-constexpr bool Is4KBAligned(VAddr address) {
-    return (address & 0xFFF) == 0;
-}
 
 // Checks if address + size is greater than the given address
 // This can return false if the size causes an overflow of a 64-bit type
@@ -69,11 +67,11 @@ bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) {
 // in the same order.
 ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_addr, VAddr src_addr,
                                       u64 size) {
-    if (!Is4KBAligned(dst_addr) || !Is4KBAligned(src_addr)) {
+    if (!Common::Is4KBAligned(dst_addr) || !Common::Is4KBAligned(src_addr)) {
         return ERR_INVALID_ADDRESS;
     }
 
-    if (size == 0 || !Is4KBAligned(size)) {
+    if (size == 0 || !Common::Is4KBAligned(size)) {
         return ERR_INVALID_SIZE;
     }
 
@@ -570,11 +568,11 @@ static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 s
               "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}",
               shared_memory_handle, addr, size, permissions);
 
-    if (!Is4KBAligned(addr)) {
+    if (!Common::Is4KBAligned(addr)) {
         return ERR_INVALID_ADDRESS;
     }
 
-    if (size == 0 || !Is4KBAligned(size)) {
+    if (size == 0 || !Common::Is4KBAligned(size)) {
         return ERR_INVALID_SIZE;
     }
 
@@ -599,11 +597,11 @@ static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64
     LOG_WARNING(Kernel_SVC, "called, shared_memory_handle=0x{:08X}, addr=0x{:X}, size=0x{:X}",
                 shared_memory_handle, addr, size);
 
-    if (!Is4KBAligned(addr)) {
+    if (!Common::Is4KBAligned(addr)) {
         return ERR_INVALID_ADDRESS;
     }
 
-    if (size == 0 || !Is4KBAligned(size)) {
+    if (size == 0 || !Common::Is4KBAligned(size)) {
         return ERR_INVALID_SIZE;
     }
 

From 541e9624eb69bb79f9d2f50c98a05445c54a220c Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Thu, 18 Oct 2018 12:58:23 -0400
Subject: [PATCH 2/3] common: Add function for checking word alignment to
 alignment.h

This will be used in a following change to svcArbitrateLock() and
svcArbitrateUnlock()
---
 src/common/alignment.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/common/alignment.h b/src/common/alignment.h
index b3f103c07..d94a2291f 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -25,4 +25,10 @@ constexpr bool Is4KBAligned(T value) {
     return (value & 0xFFF) == 0;
 }
 
+template <typename T>
+constexpr bool IsWordAligned(T value) {
+    static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
+    return (value & 0b11) == 0;
+}
+
 } // namespace Common

From 4b5ae8dbaa267d0f269ded8e621b43065fb5ae20 Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Thu, 18 Oct 2018 13:01:26 -0400
Subject: [PATCH 3/3] svc: Check for word alignment of addresses within
 svcArbitrateLock/svcArbitrateUnlock

The kernel itself checks whether or not the provided addresses are word
aligned before continuing, so we should be doing the same.
---
 src/core/hle/kernel/svc.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index b0bdd822e..d3c9d50b5 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -350,6 +350,10 @@ static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr,
         return ERR_INVALID_ADDRESS_STATE;
     }
 
+    if (!Common::IsWordAligned(mutex_addr)) {
+        return ERR_INVALID_ADDRESS;
+    }
+
     auto& handle_table = Core::System::GetInstance().Kernel().HandleTable();
     return Mutex::TryAcquire(handle_table, mutex_addr, holding_thread_handle,
                              requesting_thread_handle);
@@ -363,6 +367,10 @@ static ResultCode ArbitrateUnlock(VAddr mutex_addr) {
         return ERR_INVALID_ADDRESS_STATE;
     }
 
+    if (!Common::IsWordAligned(mutex_addr)) {
+        return ERR_INVALID_ADDRESS;
+    }
+
     return Mutex::Release(mutex_addr);
 }