-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API for direct access to a tasks' memory through the mem file
- Loading branch information
Showing
8 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef PFS_MEM_HPP | ||
#define PFS_MEM_HPP | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "types.hpp" | ||
|
||
namespace pfs { | ||
|
||
class mem final | ||
{ | ||
public: | ||
mem(const mem&) = default; | ||
mem(mem&&) = default; | ||
|
||
mem& operator=(const mem&) = delete; | ||
mem& operator=(mem&&) = delete; | ||
|
||
~mem(); | ||
|
||
public: // API | ||
std::vector<uint8_t> read(const mem_region& region); | ||
std::vector<uint8_t> read(off_t offset, size_t len); | ||
|
||
private: | ||
friend class task; | ||
mem(const std::string& path); | ||
|
||
private: | ||
const std::string _path; | ||
int _fd; | ||
}; | ||
|
||
} // namespace pfs | ||
|
||
#endif // PFS_MEM_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <fcntl.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <sys/uio.h> | ||
#include <unistd.h> | ||
|
||
#include <system_error> | ||
|
||
#include "pfs/mem.hpp" | ||
|
||
namespace pfs { | ||
|
||
mem::mem(const std::string& path) | ||
: _path(path), _fd(open(path.c_str(), O_RDONLY)) | ||
{ | ||
if (_fd < 0) | ||
{ | ||
throw std::system_error(errno, std::system_category(), | ||
"Couldn't open file"); | ||
} | ||
} | ||
|
||
mem::~mem() | ||
{ | ||
close(_fd); | ||
} | ||
|
||
std::vector<uint8_t> mem::read(const mem_region& region) | ||
{ | ||
return read(region.start_address, | ||
region.end_address - region.start_address); | ||
} | ||
|
||
std::vector<uint8_t> mem::read(off_t offset, size_t bytes) | ||
{ | ||
std::vector<uint8_t> buffer(bytes); | ||
|
||
struct iovec iov = {&buffer[0], buffer.size()}; | ||
|
||
ssize_t bytes_read = preadv(_fd, &iov, 1, offset); | ||
if (bytes_read == -1) | ||
{ | ||
throw std::system_error(errno, std::system_category(), | ||
"Couldn't read from memory"); | ||
} | ||
buffer.resize(bytes_read); | ||
|
||
return buffer; | ||
} | ||
|
||
} // namespace pfs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "catch.hpp" | ||
|
||
#include "pfs/procfs.hpp" | ||
|
||
TEST_CASE("Read mem", "[mem][read]") | ||
{ | ||
std::vector<uint8_t> secret = {'s', 'e', 'c', 'r', 'e', 't'}; | ||
off_t secret_offset = reinterpret_cast<off_t>(&secret[0]); | ||
|
||
auto self = pfs::procfs().get_task(); | ||
auto mem = self.get_mem(); | ||
auto extracted = mem.read(secret_offset, secret.size()); | ||
|
||
REQUIRE(secret == extracted); | ||
} |