Switch to C23's memset_explicit().

Test: treehugger
Change-Id: Ib6ef45cedaf95fa251d0b03de0f14701f910d063
This commit is contained in:
Elliott Hughes 2022-08-11 23:43:36 +00:00
parent a5d927ba6a
commit 78c33f3f5e
2 changed files with 3 additions and 17 deletions

View file

@ -17,32 +17,18 @@
#ifndef ANDROID_VOLD_KEYBUFFER_H #ifndef ANDROID_VOLD_KEYBUFFER_H
#define ANDROID_VOLD_KEYBUFFER_H #define ANDROID_VOLD_KEYBUFFER_H
#include <cstring> #include <string.h>
#include <memory> #include <memory>
#include <vector> #include <vector>
namespace android { namespace android {
namespace vold { namespace vold {
/**
* Variant of memset() that should never be optimized away. Borrowed from keymaster code.
*/
#ifdef __clang__
#define OPTNONE __attribute__((optnone))
#else // not __clang__
#define OPTNONE __attribute__((optimize("O0")))
#endif // not __clang__
inline OPTNONE void* memset_s(void* s, int c, size_t n) {
if (!s) return s;
return memset(s, c, n);
}
#undef OPTNONE
// Allocator that delegates useful work to standard one but zeroes data before deallocating. // Allocator that delegates useful work to standard one but zeroes data before deallocating.
class ZeroingAllocator : public std::allocator<char> { class ZeroingAllocator : public std::allocator<char> {
public: public:
void deallocate(pointer p, size_type n) { void deallocate(pointer p, size_type n) {
memset_s(p, 0, n); memset_explicit(p, 0, n);
std::allocator<char>::deallocate(p, n); std::allocator<char>::deallocate(p, n);
} }
}; };

View file

@ -48,7 +48,7 @@ KeystoreOperation::~KeystoreOperation() {
} }
static void zeroize_vector(std::vector<uint8_t>& vec) { static void zeroize_vector(std::vector<uint8_t>& vec) {
memset_s(vec.data(), 0, vec.size()); memset_explicit(vec.data(), 0, vec.size());
} }
static bool logKeystore2ExceptionIfPresent(::ndk::ScopedAStatus& rc, const std::string& func_name) { static bool logKeystore2ExceptionIfPresent(::ndk::ScopedAStatus& rc, const std::string& func_name) {