1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-05 19:51:45 +00:00

fs.mitm: Implement basic boot0 protection against writes/pubk writes.

This commit is contained in:
Michael Scire 2018-11-15 03:57:55 -08:00
parent 878ac59aae
commit 05187502b3
4 changed files with 101 additions and 35 deletions

View file

@ -61,7 +61,6 @@ class IStorageInterface : public IServiceObject {
};
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);
};
virtual Result Flush() final {
return this->base_storage->Flush();

View 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);
}

View file

@ -16,6 +16,7 @@
#pragma once
#include <switch.h>
#include <cstring>
#include <stratosphere.hpp>
#include "fs_istorage.hpp"
@ -42,6 +43,11 @@ class SectoredProxyStorage : public ProxyStorage {
u8 *buffer = static_cast<u8 *>(_buffer);
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)))) {
return rc;
}
@ -80,6 +86,11 @@ class SectoredProxyStorage : public ProxyStorage {
u8 *buffer = static_cast<u8 *>(_buffer);
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)))) {
return rc;
}
@ -124,44 +135,22 @@ class SectoredProxyStorage : public ProxyStorage {
/* Represents an RCM-preserving BOOT0 partition. */
class Boot0Storage : public 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:
u64 title_id;
private:
HosMutex *GetMutex() {
static HosMutex s_boot0_mutex;
return &s_boot0_mutex;
}
bool AllowWrites() {
return title_id < 0x0100000000001000ULL;
}
bool CanModifyBctPubks() {
return title_id != 0x010000000000001FULL;
}
bool AllowWrites();
bool CanModifyBctPubks();
public:
Boot0Storage(FsStorage *s, u64 t) : Base(s), title_id(t) { }
Boot0Storage(FsStorage s, u64 t) : Base(s), title_id(t) { }
public:
virtual Result Read(void *_buffer, size_t size, u64 offset) override {
GetMutex()->Lock();
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;
}
virtual Result Read(void *_buffer, size_t size, u64 offset) override;
virtual Result Write(void *_buffer, size_t size, u64 offset) override;
};

View file

@ -101,7 +101,7 @@ Result FsMitmService::OpenBisStorage(Out<std::shared_ptr<IStorageInterface>> out
if (R_SUCCEEDED(rc)) {
const bool allow_writes = this->title_id < 0x0100000000001000;
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 {
if (allow_writes) {
storage = std::make_shared<IStorageInterface>(new ROProxyStorage(bis_storage));