mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-11-10 07:06:34 +00:00
fs.mitm: Implement basic boot0 protection against writes/pubk writes.
This commit is contained in:
parent
878ac59aae
commit
05187502b3
4 changed files with 101 additions and 35 deletions
|
@ -61,7 +61,6 @@ class IStorageInterface : public IServiceObject {
|
||||||
};
|
};
|
||||||
virtual Result Write(InBuffer<u8, BufferType_Type1> buffer, u64 offset, u64 size) final {
|
virtual Result Write(InBuffer<u8, BufferType_Type1> buffer, u64 offset, u64 size) final {
|
||||||
return this->base_storage->Write(buffer.buffer, std::min(buffer.num_elements, size), offset);
|
return this->base_storage->Write(buffer.buffer, std::min(buffer.num_elements, size), offset);
|
||||||
|
|
||||||
};
|
};
|
||||||
virtual Result Flush() final {
|
virtual Result Flush() final {
|
||||||
return this->base_storage->Flush();
|
return this->base_storage->Flush();
|
||||||
|
|
78
stratosphere/fs_mitm/source/fsmitm_boot0storage.cpp
Normal file
78
stratosphere/fs_mitm/source/fsmitm_boot0storage.cpp
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2018 Atmosphère-NX
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms and conditions of the GNU General Public License,
|
||||||
|
* version 2, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <switch.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
#include "fsmitm_boot0storage.hpp"
|
||||||
|
|
||||||
|
static HosMutex g_boot0_mutex;
|
||||||
|
static u8 g_boot0_bct_buffer[Boot0Storage::BctEndOffset];
|
||||||
|
|
||||||
|
bool Boot0Storage::AllowWrites() {
|
||||||
|
return this->title_id < 0x0100000000001000ULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Boot0Storage::CanModifyBctPubks() {
|
||||||
|
return this->title_id != 0x010000000000001FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result Boot0Storage::Read(void *_buffer, size_t size, u64 offset) {
|
||||||
|
std::scoped_lock<HosMutex> lk{g_boot0_mutex};
|
||||||
|
|
||||||
|
return Base::Read(_buffer, size, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
|
||||||
|
std::scoped_lock<HosMutex> lk{g_boot0_mutex};
|
||||||
|
|
||||||
|
if (!AllowWrites()) {
|
||||||
|
return 0x313802;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We care about protecting autorcm from NS. */
|
||||||
|
if (CanModifyBctPubks() || offset >= BctEndOffset || (offset + BctSize >= BctEndOffset && offset % BctSize >= BctPubkEnd)) {
|
||||||
|
return Base::Write(_buffer, size, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result rc = 0;
|
||||||
|
u8 *buffer = static_cast<u8 *>(_buffer);
|
||||||
|
|
||||||
|
/* First, let's deal with the data past the end. */
|
||||||
|
if (offset + size >= BctEndOffset) {
|
||||||
|
const u64 diff = BctEndOffset - offset;
|
||||||
|
if (R_FAILED((rc = ProxyStorage::Write(buffer + diff, size - diff, BctEndOffset)))) {
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
size -= diff;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read in the current BCT region. */
|
||||||
|
if (R_FAILED((rc = ProxyStorage::Read(g_boot0_bct_buffer, BctEndOffset, 0)))) {
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update the bct buffer. */
|
||||||
|
for (u64 cur_ofs = offset; cur_ofs < BctEndOffset && cur_ofs < offset + size; cur_ofs++) {
|
||||||
|
const u64 cur_bct_rel_ofs = cur_ofs % BctSize;
|
||||||
|
if (cur_bct_rel_ofs < BctPubkStart || BctPubkEnd <= cur_bct_rel_ofs) {
|
||||||
|
g_boot0_bct_buffer[cur_ofs] = buffer[cur_ofs - offset];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ProxyStorage::Write(g_boot0_bct_buffer, BctEndOffset, 0);
|
||||||
|
}
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
|
#include <cstring>
|
||||||
#include <stratosphere.hpp>
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
#include "fs_istorage.hpp"
|
#include "fs_istorage.hpp"
|
||||||
|
@ -42,6 +43,11 @@ class SectoredProxyStorage : public ProxyStorage {
|
||||||
u8 *buffer = static_cast<u8 *>(_buffer);
|
u8 *buffer = static_cast<u8 *>(_buffer);
|
||||||
this->Seek(offset);
|
this->Seek(offset);
|
||||||
|
|
||||||
|
if (this->cur_sector_ofs == 0 && size % SectorSize == 0) {
|
||||||
|
/* Fast case. */
|
||||||
|
return ProxyStorage::Read(buffer, size, offset);
|
||||||
|
}
|
||||||
|
|
||||||
if (R_FAILED((rc = ProxyStorage::Read(this->sector_buf, SectorSize, this->cur_seek)))) {
|
if (R_FAILED((rc = ProxyStorage::Read(this->sector_buf, SectorSize, this->cur_seek)))) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
@ -80,6 +86,11 @@ class SectoredProxyStorage : public ProxyStorage {
|
||||||
u8 *buffer = static_cast<u8 *>(_buffer);
|
u8 *buffer = static_cast<u8 *>(_buffer);
|
||||||
this->Seek(offset);
|
this->Seek(offset);
|
||||||
|
|
||||||
|
if (this->cur_sector_ofs == 0 && size % SectorSize == 0) {
|
||||||
|
/* Fast case. */
|
||||||
|
return ProxyStorage::Write(buffer, size, offset);
|
||||||
|
}
|
||||||
|
|
||||||
if (R_FAILED((rc = ProxyStorage::Read(this->sector_buf, SectorSize, this->cur_seek)))) {
|
if (R_FAILED((rc = ProxyStorage::Read(this->sector_buf, SectorSize, this->cur_seek)))) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
@ -124,44 +135,22 @@ class SectoredProxyStorage : public ProxyStorage {
|
||||||
/* Represents an RCM-preserving BOOT0 partition. */
|
/* Represents an RCM-preserving BOOT0 partition. */
|
||||||
class Boot0Storage : public SectoredProxyStorage<0x200> {
|
class Boot0Storage : public SectoredProxyStorage<0x200> {
|
||||||
using Base = SectoredProxyStorage<0x200>;
|
using Base = SectoredProxyStorage<0x200>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static constexpr u64 BctEndOffset = 0xFC000;
|
||||||
|
static constexpr u64 BctSize = 0x4000;
|
||||||
|
static constexpr u64 BctPubkStart = 0x210;
|
||||||
|
static constexpr u64 BctPubkSize = 0x100;
|
||||||
|
static constexpr u64 BctPubkEnd = BctPubkStart + BctPubkSize;
|
||||||
private:
|
private:
|
||||||
u64 title_id;
|
u64 title_id;
|
||||||
private:
|
private:
|
||||||
HosMutex *GetMutex() {
|
bool AllowWrites();
|
||||||
static HosMutex s_boot0_mutex;
|
bool CanModifyBctPubks();
|
||||||
return &s_boot0_mutex;
|
|
||||||
}
|
|
||||||
bool AllowWrites() {
|
|
||||||
return title_id < 0x0100000000001000ULL;
|
|
||||||
}
|
|
||||||
bool CanModifyBctPubks() {
|
|
||||||
return title_id != 0x010000000000001FULL;
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
Boot0Storage(FsStorage *s, u64 t) : Base(s), title_id(t) { }
|
Boot0Storage(FsStorage *s, u64 t) : Base(s), title_id(t) { }
|
||||||
Boot0Storage(FsStorage s, u64 t) : Base(s), title_id(t) { }
|
Boot0Storage(FsStorage s, u64 t) : Base(s), title_id(t) { }
|
||||||
public:
|
public:
|
||||||
virtual Result Read(void *_buffer, size_t size, u64 offset) override {
|
virtual Result Read(void *_buffer, size_t size, u64 offset) override;
|
||||||
GetMutex()->Lock();
|
virtual Result Write(void *_buffer, size_t size, u64 offset) override;
|
||||||
ON_SCOPE_EXIT { GetMutex()->Unlock(); };
|
|
||||||
|
|
||||||
return Base::Read(_buffer, size, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual Result Write(void *_buffer, size_t size, u64 offset) override {
|
|
||||||
GetMutex()->Lock();
|
|
||||||
ON_SCOPE_EXIT { GetMutex()->Unlock(); };
|
|
||||||
|
|
||||||
if (!AllowWrites()) {
|
|
||||||
return 0x313802;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We care about protecting autorcm from NS. */
|
|
||||||
if (CanModifyBctPubks()) {
|
|
||||||
return Base::Write(_buffer, size, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO */
|
|
||||||
return 0x313802;
|
|
||||||
}
|
|
||||||
};
|
};
|
|
@ -101,7 +101,7 @@ Result FsMitmService::OpenBisStorage(Out<std::shared_ptr<IStorageInterface>> out
|
||||||
if (R_SUCCEEDED(rc)) {
|
if (R_SUCCEEDED(rc)) {
|
||||||
const bool allow_writes = this->title_id < 0x0100000000001000;
|
const bool allow_writes = this->title_id < 0x0100000000001000;
|
||||||
if (bis_partition_id == BisStorageId_Boot0) {
|
if (bis_partition_id == BisStorageId_Boot0) {
|
||||||
storage = std::make_shared<IStorageInterface>(new Boot0Storage(bis_storage, allow_writes));
|
storage = std::make_shared<IStorageInterface>(new Boot0Storage(bis_storage, this->title_id));
|
||||||
} else {
|
} else {
|
||||||
if (allow_writes) {
|
if (allow_writes) {
|
||||||
storage = std::make_shared<IStorageInterface>(new ROProxyStorage(bis_storage));
|
storage = std::make_shared<IStorageInterface>(new ROProxyStorage(bis_storage));
|
||||||
|
|
Loading…
Reference in a new issue