Skip to content

Commit 3d474d3

Browse files
committed
lib: Clear out LiveUpdate area after consuming all partitions, add checks
1 parent e3aad09 commit 3d474d3

5 files changed

Lines changed: 25 additions & 5 deletions

File tree

lib/LiveUpdate/partition.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
**/
2121
#include "storage.hpp"
2222
#include <util/crc32.hpp>
23+
#include <cassert>
2324

2425
inline uint32_t liu_crc32(const void* buf, size_t len)
2526
{
@@ -48,6 +49,9 @@ int storage_header::find_partition(const char* key)
4849
auto& part = ptable.at(p);
4950
if (strncmp(part.name, key, sizeof(part.name)) == 0)
5051
{
52+
// the partition must have a valid name
53+
assert(part.name[0] != 0);
54+
// the partition should be fully consistent
5155
uint32_t chsum = part.generate_checksum(this->vla);
5256
if (part.crc == chsum) {
5357
return p;

lib/LiveUpdate/resume.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ void resume_begin(storage_header& storage, std::string key, LiveUpdate::resume_f
101101
// call next manually only when no one called go_next
102102
if (oldptr == ptr) ptr = storage.next(ptr);
103103
}
104-
/// wake all the slumbering IP stacks
104+
// wake all the slumbering IP stacks
105105
serialized_tcp::wakeup_ip_networks();
106-
/// zero out the partition for security reasons
106+
// clear registered resume callbacks
107+
resume_funcs.clear();
108+
// zero out the partition for security reasons
107109
storage.zero_partition(p);
110+
// if there are no more partitions, clear everything
111+
storage.try_zero();
108112
}
109113

110114
void LiveUpdate::on_resume(uint16_t id, resume_func func)

lib/LiveUpdate/storage.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ uint32_t storage_header::generate_checksum() noexcept
162162
return checksum;
163163
}
164164

165+
void storage_header::try_zero() noexcept
166+
{
167+
for (int p = 0; p < partitions; p++) {
168+
auto& part = ptable.at(p);
169+
if (part.length != 0 && part.name[0] != 0) return;
170+
}
171+
// zero everything
172+
this->zero();
173+
}
165174
void storage_header::zero()
166175
{
167176
memset(this, 0, sizeof(storage_header) + this->length);

lib/LiveUpdate/storage.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,13 @@ struct storage_header
141141
}
142142
void finalize();
143143
bool validate() noexcept;
144-
145-
// zero out the entire header and its data, for extra security
146-
void zero();
144+
// zero out everything if all partitions consumed
145+
void try_zero() noexcept;
147146

148147
private:
149148
uint32_t generate_checksum() noexcept;
149+
// zero out the entire header and its data, for extra security
150+
void zero();
150151

151152
uint64_t magic;
152153
uint32_t crc;

src/kernel/os.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ void OS::shutdown()
102102

103103
void OS::post_start()
104104
{
105+
// if the LiveUpdate storage area is not yet determined,
106+
// we can assume its a fresh boot, so calculate new one based on ...
105107
if (OS::liveupdate_loc_ == 0)
106108
{
107109
// default size is 1/4 of heap from the end of memory

0 commit comments

Comments
 (0)