SlideShare a Scribd company logo
Safe Clearing of Private Data
Author: Roman Fomichev
Date: 06.04.2016
We often need to store private data in programs, for example passwords, secret keys, and their
derivatives, and we usually need to clear their traces in the memory after using them so that a potential
intruder can't gain access to these data. In this article we will discuss why you can't clear private data
using memset() function.
memset()
You may have already read the article discussing vulnerabilities in programs where memset() is used to
erase memory. However, that article doesn't fully cover all the possible scenarios of incorrect use of
memset(). You may have problems not only with clearing stack-allocated buffers but with clearing
dynamically allocated buffers as well.
The stack
For a start, let's discuss an example from the above-mentioned article that deals with using a stack-
allocated variable.
Here is a code fragment that handles a password:
#include <string>
#include <functional>
#include <iostream>
//Private data
struct PrivateData
{
size_t m_hash;
char m_pswd[100];
};
//Function performs some operations on password
void doSmth(PrivateData& data)
{
std::string s(data.m_pswd);
std::hash<std::string> hash_fn;
data.m_hash = hash_fn(s);
}
//Function for password entering and processing
int funcPswd()
{
PrivateData data;
std::cin >> data.m_pswd;
doSmth(data);
memset(&data, 0, sizeof(PrivateData));
return 1;
}
int main()
{
funcPswd();
return 0;
}
This example is rather conventional and completely synthetic.
If we build a debug version of that code and run it in the debugger (I was using Visual Studio 2015), we'll
see that it works well: the password and its calculated hash value are erased after they have been used.
Let's take a look at the assembler version of our code in the Visual Studio debugger:
....
doSmth(data);
000000013F3072BF lea rcx,[data]
000000013F3072C3 call doSmth (013F30153Ch)
memset(&data, 0, sizeof(PrivateData));
000000013F3072C8 mov r8d,70h
000000013F3072CE xor edx,edx
000000013F3072D0 lea rcx,[data]
000000013F3072D4 call memset (013F301352h)
return 1;
000000013F3072D9 mov eax,1
....
We see the call of memset() function, that clears the private data after use.
We could stop here, but we'll go on and try to build an optimized release version. Now, this is what we
see in the debugger:
....
000000013F7A1035 call
std::operator>><char,std::char_traits<char> > (013F7A18B0h)
000000013F7A103A lea rcx,[rsp+20h]
000000013F7A103F call doSmth (013F7A1170h)
return 0;
000000013F7A1044 xor eax,eax
....
All the instructions associated with the call to the memset() function have been deleted. The compiler
assumes that there is no need to call a function erasing data since they are no longer in use. It's not an
error; it's a legal choice of the compiler. From the language viewpoint, a memset() call is not needed
since the buffer is not used further in the program, so removing this call cannot affect its behavior. So,
our private data remain uncleared, and it's very bad.
The heap
Now let's dig deeper. Let's see what happens to data when we allocate them in dynamic memory using
the malloc function or the new operator.
Let's modify our previous code to work with malloc:
#include <string>
#include <functional>
#include <iostream>
struct PrivateData
{
size_t m_hash;
char m_pswd[100];
};
void doSmth(PrivateData& data)
{
std::string s(data.m_pswd);
std::hash<std::string> hash_fn;
data.m_hash = hash_fn(s);
}
int funcPswd()
{
PrivateData* data = (PrivateData*)malloc(sizeof(PrivateData));
std::cin >> data->m_pswd;
doSmth(*data);
memset(data, 0, sizeof(PrivateData));
free(data);
return 1;
}
int main()
{
funcPswd();
return 0;
}
We'll be testing a release version since the debug version has all the calls where we want them to be.
After compiling it in Visual Studio 2015, we get the following assembler code:
....
000000013FBB1021 mov rcx,
qword ptr [__imp_std::cin (013FBB30D8h)]
000000013FBB1028 mov rbx,rax
000000013FBB102B lea rdx,[rax+8]
000000013FBB102F call
std::operator>><char,std::char_traits<char> > (013FBB18B0h)
000000013FBB1034 mov rcx,rbx
000000013FBB1037 call doSmth (013FBB1170h)
000000013FBB103C xor edx,edx
000000013FBB103E mov rcx,rbx
000000013FBB1041 lea r8d,[rdx+70h]
000000013FBB1045 call memset (013FBB2A2Eh)
000000013FBB104A mov rcx,rbx
000000013FBB104D call qword ptr [__imp_free (013FBB3170h)]
return 0;
000000013FBB1053 xor eax,eax
....
Visual Studio has done well this time: it erases the data as planned. But what about other compilers?
Let's try gcc, version 5.2.1, and clang, version 3.7.0.
I've modified our code a bit for gcc and clang and added some code to print the contents of the
allocated memory block before and after the cleanup. I print the contents of the block the pointer
points to after the memory is freed, but you shouldn't do it in real programs because you never know
how the application will respond. In this experiment, however, I'm taking the liberty to use this
technique.
....
#include "string.h"
....
size_t len = strlen(data->m_pswd);
for (int i = 0; i < len; ++i)
printf("%c", data->m_pswd[i]);
printf("| %zu n", data->m_hash);
memset(data, 0, sizeof(PrivateData));
free(data);
for (int i = 0; i < len; ++i)
printf("%c", data->m_pswd[i]);
printf("| %zu n", data->m_hash);
....
Now, here's a fragment of the assembler code generated by gcc compiler:
movq (%r12), %rsi
movl $.LC2, %edi
xorl %eax, %eax
call printf
movq %r12, %rdi
call free
The printing function (printf) is followed by a call to the free() function while the call to the memset()
function is gone. If we run the code and enter an arbitrary password (for example "MyTopSecret"), we'll
see the following message printed on the screen:
MyTopSecret| 7882334103340833743
MyTopSecret| 0
The hash has changed. I guess it's a side effect of the memory manager's work. As for our password
"MyTopSecret", it stays intact in the memory.
Let's check how it works with clang:
movq (%r14), %rsi
movl $.L.str.1, %edi
xorl %eax, %eax
callq printf
movq %r14, %rdi
callq free
Just like in the previous case, the compiler decides to remove the call to the memset() function. This is
what the printed output looks like:
MyTopSecret| 7882334103340833743
MyTopSecret| 0
So, both gcc and clang decided to optimize our code. Since the memory is freed after calling the
memset() function, the compilers treat this call as irrelevant and delete it.
As our experiments reveal, compilers tend to delete memset() calls for the sake of optimization working
with both stack and dynamic memory of the application.
Finally, let's see how the compilers will respond when allocating memory using the new operator.
Modifying the code again:
#include <string>
#include <functional>
#include <iostream>
#include "string.h"
struct PrivateData
{
size_t m_hash;
char m_pswd[100];
};
void doSmth(PrivateData& data)
{
std::string s(data.m_pswd);
std::hash<std::string> hash_fn;
data.m_hash = hash_fn(s);
}
int funcPswd()
{
PrivateData* data = new PrivateData();
std::cin >> data->m_pswd;
doSmth(*data);
memset(data, 0, sizeof(PrivateData));
delete data;
return 1;
}
int main()
{
funcPswd();
return 0;
}
Visual Studio clears the memory as expected:
000000013FEB1044 call doSmth (013FEB1180h)
000000013FEB1049 xor edx,edx
000000013FEB104B mov rcx,rbx
000000013FEB104E lea r8d,[rdx+70h]
000000013FEB1052 call memset (013FEB2A3Eh)
000000013FEB1057 mov edx,70h
000000013FEB105C mov rcx,rbx
000000013FEB105F call operator delete (013FEB1BA8h)
return 0;
000000013FEB1064 xor eax,eax
The gcc compiler decided to leave the clearing function, too:
call printf
movq %r13, %rdi
movq %rbp, %rcx
xorl %eax, %eax
andq $-8, %rdi
movq $0, 0(%rbp)
movq $0, 104(%rbp)
subq %rdi, %rcx
addl $112, %ecx
shrl $3, %ecx
rep stosq
movq %rbp, %rdi
call _ZdlPv
The printed output has changed accordingly; the data we have entered are no longer there:
MyTopSecret| 7882334103340833743
| 0
But as for clang, it chose to optimize our code in this case as well and cut out the "unnecessary"
function:
movq (%r14), %rsi
movl $.L.str.1, %edi
xorl %eax, %eax
callq printf
movq %r14, %rdi
callq _ZdlPv
Let's print the memory's contents:
MyTopSecret| 7882334103340833743
MyTopSecret| 0
The password remains, waiting for being stolen.
Let's sum it all up. We have found that an optimizing compiler may remove a call to the memset()
function no matter what type of memory is used - stack or dynamic. Although Visual Studio didn't
remove memset() calls when using dynamic memory in our test, you can't expect it to always behave
that way in real-life code. The harmful effect may reveal itself with other compilation switches. What
follows from our small research is that one cannot rely on the memset() function to clear private data.
So, what is a better way to clear them?
You should use special memory-clearing functions, which can't be deleted by the compiler when it
optimizes the code.
In Visual Studio, for example, you can use RtlSecureZeroMemory. Starting with C11, function memset_s
is also available. In addition, you can implement a safe function of your own, if necessary; a lot of
examples and guides can be found around the web. Here are some of them.
Solution No. 1.
errno_t memset_s(void *v, rsize_t smax, int c, rsize_t n) {
if (v == NULL) return EINVAL;
if (smax > RSIZE_MAX) return EINVAL;
if (n > smax) return EINVAL;
volatile unsigned char *p = v;
while (smax-- && n--) {
*p++ = c;
}
return 0;
}
Solution No. 2.
void secure_zero(void *s, size_t n)
{
volatile char *p = s;
while (n--) *p++ = 0;
}
Some programmers go even further and create functions that fill the array with pseudo-random values
and have different running time to hinder attacks based on time measuring. Implementations of these
can be found on the web, too.
Conclusion
PVS-Studio static analyzer can detect data-clearing errors we have discussed here, and uses diagnostic
V597 to signal about the problem. This article was written as an extended explanation of why this
diagnostic is important. Unfortunately, many programmers tend to think that the analyzer "picks on"
their code and there is actually nothing to worry about. Well, it's because they see their memset() calls
intact when viewing the code in the debugger, forgetting that what they see is still just a debug version.

More Related Content

What's hot

Checking Intel IPP Samples for Windows - Continuation
Checking Intel IPP Samples for Windows - ContinuationChecking Intel IPP Samples for Windows - Continuation
Checking Intel IPP Samples for Windows - Continuation
PVS-Studio
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
Programming Homework Help
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++
Seok-joon Yun
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
Seok-joon Yun
 
Computer Science Homework Help
Computer Science Homework HelpComputer Science Homework Help
Computer Science Homework Help
Programming Homework Help
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
Dillon Lee
 
Show innodb status
Show innodb statusShow innodb status
Show innodb status
justlooks
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Andrey Karpov
 
A few words about OpenSSL
A few words about OpenSSLA few words about OpenSSL
A few words about OpenSSL
PVS-Studio
 
Tutorial4 Threads
Tutorial4  ThreadsTutorial4  Threads
Tutorial4 Threads
tech2click
 
RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019
Gebreigziabher Ab
 
Operating System Assignment Help
Operating System Assignment HelpOperating System Assignment Help
Operating System Assignment Help
Programming Homework Help
 
Adding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMPAdding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMP
Jacques Rioux
 
Implementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphoresImplementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphores
Gowtham Reddy
 
14 thread
14 thread14 thread
14 thread
Bayarkhuu
 
Linux on System z debugging with Valgrind
Linux on System z debugging with ValgrindLinux on System z debugging with Valgrind
Linux on System z debugging with Valgrind
IBM India Smarter Computing
 
Bluespec @waseda
Bluespec @wasedaBluespec @waseda
Bluespec @waseda
Takefumi MIYOSHI
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
PVS-Studio
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
timotheeg
 
Npc13
Npc13Npc13

What's hot (20)

Checking Intel IPP Samples for Windows - Continuation
Checking Intel IPP Samples for Windows - ContinuationChecking Intel IPP Samples for Windows - Continuation
Checking Intel IPP Samples for Windows - Continuation
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
Computer Science Homework Help
Computer Science Homework HelpComputer Science Homework Help
Computer Science Homework Help
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 
Show innodb status
Show innodb statusShow innodb status
Show innodb status
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...
 
A few words about OpenSSL
A few words about OpenSSLA few words about OpenSSL
A few words about OpenSSL
 
Tutorial4 Threads
Tutorial4  ThreadsTutorial4  Threads
Tutorial4 Threads
 
RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019RMI Java Programming Lab Manual 2019
RMI Java Programming Lab Manual 2019
 
Operating System Assignment Help
Operating System Assignment HelpOperating System Assignment Help
Operating System Assignment Help
 
Adding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMPAdding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMP
 
Implementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphoresImplementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphores
 
14 thread
14 thread14 thread
14 thread
 
Linux on System z debugging with Valgrind
Linux on System z debugging with ValgrindLinux on System z debugging with Valgrind
Linux on System z debugging with Valgrind
 
Bluespec @waseda
Bluespec @wasedaBluespec @waseda
Bluespec @waseda
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
 
Npc13
Npc13Npc13
Npc13
 

Viewers also liked

Seguridad social 2
Seguridad social 2Seguridad social 2
Seguridad social 2
LilianaCordoba
 
Business Plan
Business PlanBusiness Plan
Business Plan
Adhitya Maulana Yusuf
 
ознайомлення з дробами
ознайомлення з дробамиознайомлення з дробами
ознайомлення з дробами
Ирина Слуцкая
 
Best5 홍보마케팅
Best5 홍보마케팅Best5 홍보마케팅
Best5 홍보마케팅수민 김
 
Transitivity AND THEME z& RHEME
Transitivity AND THEME z& RHEMETransitivity AND THEME z& RHEME
Transitivity AND THEME z& RHEME
Ahmed Qadoury Abed
 
Tarea 2 creación índices y constraints en bases de datos de sql server
Tarea 2 creación índices y constraints en bases de datos de sql serverTarea 2 creación índices y constraints en bases de datos de sql server
Tarea 2 creación índices y constraints en bases de datos de sql server
Gil Pérez
 
2014.02.20 Үндэсний компаниудын төлөвшил, өрсөлдөх чадвар, Хишиг арвин индуст...
2014.02.20 Үндэсний компаниудын төлөвшил, өрсөлдөх чадвар, Хишиг арвин индуст...2014.02.20 Үндэсний компаниудын төлөвшил, өрсөлдөх чадвар, Хишиг арвин индуст...
2014.02.20 Үндэсний компаниудын төлөвшил, өрсөлдөх чадвар, Хишиг арвин индуст...
The Business Council of Mongolia
 
Узкие грядки
Узкие грядкиУзкие грядки
Níquel cerro matoso
Níquel cerro matosoNíquel cerro matoso
Níquel cerro matoso
Uniambiental
 
Evaluación trabajo final_40%
Evaluación trabajo final_40%Evaluación trabajo final_40%
Evaluación trabajo final_40%
andressabe
 
Actividad 2.1 lourdes_valeriano
Actividad 2.1 lourdes_valerianoActividad 2.1 lourdes_valeriano
Actividad 2.1 lourdes_valeriano
Univarsidad Tecnológica Indoamérica
 
Osciloscópio online
Osciloscópio onlineOsciloscópio online
Osciloscópio online
José Ronaldo Souza Junior
 
Business Plan Cookies Hias
Business Plan Cookies HiasBusiness Plan Cookies Hias
Business Plan Cookies Hias
Pipit Pitrianingsih Suryana
 
130609
130609130609
Error analysis
Error analysis Error analysis
Error analysis
Ahmed Qadoury Abed
 
Classicismo
ClassicismoClassicismo
Classicismo
Adeildo Júnior
 
Inovasi Pelayanan Publik
Inovasi Pelayanan PublikInovasi Pelayanan Publik
Inovasi Pelayanan Publik
Tri Widodo W. UTOMO
 
β΄ παγκόσμιος πόλεμος
β΄ παγκόσμιος πόλεμοςβ΄ παγκόσμιος πόλεμος
β΄ παγκόσμιος πόλεμοςakisvfb
 

Viewers also liked (20)

Seguridad social 2
Seguridad social 2Seguridad social 2
Seguridad social 2
 
Business Plan
Business PlanBusiness Plan
Business Plan
 
ознайомлення з дробами
ознайомлення з дробамиознайомлення з дробами
ознайомлення з дробами
 
Best5 홍보마케팅
Best5 홍보마케팅Best5 홍보마케팅
Best5 홍보마케팅
 
Transitivity AND THEME z& RHEME
Transitivity AND THEME z& RHEMETransitivity AND THEME z& RHEME
Transitivity AND THEME z& RHEME
 
Tarea 2 creación índices y constraints en bases de datos de sql server
Tarea 2 creación índices y constraints en bases de datos de sql serverTarea 2 creación índices y constraints en bases de datos de sql server
Tarea 2 creación índices y constraints en bases de datos de sql server
 
2014.02.20 Үндэсний компаниудын төлөвшил, өрсөлдөх чадвар, Хишиг арвин индуст...
2014.02.20 Үндэсний компаниудын төлөвшил, өрсөлдөх чадвар, Хишиг арвин индуст...2014.02.20 Үндэсний компаниудын төлөвшил, өрсөлдөх чадвар, Хишиг арвин индуст...
2014.02.20 Үндэсний компаниудын төлөвшил, өрсөлдөх чадвар, Хишиг арвин индуст...
 
Узкие грядки
Узкие грядкиУзкие грядки
Узкие грядки
 
Níquel cerro matoso
Níquel cerro matosoNíquel cerro matoso
Níquel cerro matoso
 
Sectores económicos
Sectores económicosSectores económicos
Sectores económicos
 
Evaluación trabajo final_40%
Evaluación trabajo final_40%Evaluación trabajo final_40%
Evaluación trabajo final_40%
 
Actividad 2.1 lourdes_valeriano
Actividad 2.1 lourdes_valerianoActividad 2.1 lourdes_valeriano
Actividad 2.1 lourdes_valeriano
 
Osciloscópio online
Osciloscópio onlineOsciloscópio online
Osciloscópio online
 
Business Plan Cookies Hias
Business Plan Cookies HiasBusiness Plan Cookies Hias
Business Plan Cookies Hias
 
130609
130609130609
130609
 
تست
تستتست
تست
 
Error analysis
Error analysis Error analysis
Error analysis
 
Classicismo
ClassicismoClassicismo
Classicismo
 
Inovasi Pelayanan Publik
Inovasi Pelayanan PublikInovasi Pelayanan Publik
Inovasi Pelayanan Publik
 
β΄ παγκόσμιος πόλεμος
β΄ παγκόσμιος πόλεμοςβ΄ παγκόσμιος πόλεμος
β΄ παγκόσμιος πόλεμος
 

Similar to Safe Clearing of Private Data

Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Vincenzo Iozzo
 
Checking the Open-Source Multi Theft Auto Game
Checking the Open-Source Multi Theft Auto GameChecking the Open-Source Multi Theft Auto Game
Checking the Open-Source Multi Theft Auto Game
Andrey Karpov
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
PVS-Studio
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
Andrey Karpov
 
Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor
skbansal222
 
How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.
PVS-Studio
 
How to make fewer errors at the stage of code writing. Part N1
How to make fewer errors at the stage of code writing. Part N1How to make fewer errors at the stage of code writing. Part N1
How to make fewer errors at the stage of code writing. Part N1
Andrey Karpov
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
PVS-Studio
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
Andrey Karpov
 
How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.
PVS-Studio
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
acsmadurai
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorial
hughpearse
 
Software diseases: memset
Software diseases: memsetSoftware diseases: memset
Software diseases: memset
PVS-Studio
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
Vivek chan
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
Max Kleiner
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
karmuhtam
 
Linux Kernel, tested by the Linux-version of PVS-Studio
Linux Kernel, tested by the Linux-version of PVS-StudioLinux Kernel, tested by the Linux-version of PVS-Studio
Linux Kernel, tested by the Linux-version of PVS-Studio
PVS-Studio
 
C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.
Mohamed Fawzy
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
PVS-Studio
 
PVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd CheckPVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd Check
Andrey Karpov
 

Similar to Safe Clearing of Private Data (20)

Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
 
Checking the Open-Source Multi Theft Auto Game
Checking the Open-Source Multi Theft Auto GameChecking the Open-Source Multi Theft Auto Game
Checking the Open-Source Multi Theft Auto Game
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor
 
How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.
 
How to make fewer errors at the stage of code writing. Part N1
How to make fewer errors at the stage of code writing. Part N1How to make fewer errors at the stage of code writing. Part N1
How to make fewer errors at the stage of code writing. Part N1
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
 
How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorial
 
Software diseases: memset
Software diseases: memsetSoftware diseases: memset
Software diseases: memset
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
Linux Kernel, tested by the Linux-version of PVS-Studio
Linux Kernel, tested by the Linux-version of PVS-StudioLinux Kernel, tested by the Linux-version of PVS-Studio
Linux Kernel, tested by the Linux-version of PVS-Studio
 
C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
 
PVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd CheckPVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd Check
 

Recently uploaded

Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 

Recently uploaded (20)

Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 

Safe Clearing of Private Data

  • 1. Safe Clearing of Private Data Author: Roman Fomichev Date: 06.04.2016 We often need to store private data in programs, for example passwords, secret keys, and their derivatives, and we usually need to clear their traces in the memory after using them so that a potential intruder can't gain access to these data. In this article we will discuss why you can't clear private data using memset() function. memset() You may have already read the article discussing vulnerabilities in programs where memset() is used to erase memory. However, that article doesn't fully cover all the possible scenarios of incorrect use of memset(). You may have problems not only with clearing stack-allocated buffers but with clearing dynamically allocated buffers as well. The stack For a start, let's discuss an example from the above-mentioned article that deals with using a stack- allocated variable. Here is a code fragment that handles a password: #include <string> #include <functional> #include <iostream> //Private data struct PrivateData { size_t m_hash; char m_pswd[100];
  • 2. }; //Function performs some operations on password void doSmth(PrivateData& data) { std::string s(data.m_pswd); std::hash<std::string> hash_fn; data.m_hash = hash_fn(s); } //Function for password entering and processing int funcPswd() { PrivateData data; std::cin >> data.m_pswd; doSmth(data); memset(&data, 0, sizeof(PrivateData)); return 1; } int main() { funcPswd(); return 0; } This example is rather conventional and completely synthetic. If we build a debug version of that code and run it in the debugger (I was using Visual Studio 2015), we'll see that it works well: the password and its calculated hash value are erased after they have been used. Let's take a look at the assembler version of our code in the Visual Studio debugger: .... doSmth(data); 000000013F3072BF lea rcx,[data] 000000013F3072C3 call doSmth (013F30153Ch) memset(&data, 0, sizeof(PrivateData)); 000000013F3072C8 mov r8d,70h
  • 3. 000000013F3072CE xor edx,edx 000000013F3072D0 lea rcx,[data] 000000013F3072D4 call memset (013F301352h) return 1; 000000013F3072D9 mov eax,1 .... We see the call of memset() function, that clears the private data after use. We could stop here, but we'll go on and try to build an optimized release version. Now, this is what we see in the debugger: .... 000000013F7A1035 call std::operator>><char,std::char_traits<char> > (013F7A18B0h) 000000013F7A103A lea rcx,[rsp+20h] 000000013F7A103F call doSmth (013F7A1170h) return 0; 000000013F7A1044 xor eax,eax .... All the instructions associated with the call to the memset() function have been deleted. The compiler assumes that there is no need to call a function erasing data since they are no longer in use. It's not an error; it's a legal choice of the compiler. From the language viewpoint, a memset() call is not needed since the buffer is not used further in the program, so removing this call cannot affect its behavior. So, our private data remain uncleared, and it's very bad. The heap Now let's dig deeper. Let's see what happens to data when we allocate them in dynamic memory using the malloc function or the new operator. Let's modify our previous code to work with malloc: #include <string> #include <functional> #include <iostream> struct PrivateData { size_t m_hash; char m_pswd[100]; }; void doSmth(PrivateData& data)
  • 4. { std::string s(data.m_pswd); std::hash<std::string> hash_fn; data.m_hash = hash_fn(s); } int funcPswd() { PrivateData* data = (PrivateData*)malloc(sizeof(PrivateData)); std::cin >> data->m_pswd; doSmth(*data); memset(data, 0, sizeof(PrivateData)); free(data); return 1; } int main() { funcPswd(); return 0; } We'll be testing a release version since the debug version has all the calls where we want them to be. After compiling it in Visual Studio 2015, we get the following assembler code: .... 000000013FBB1021 mov rcx, qword ptr [__imp_std::cin (013FBB30D8h)] 000000013FBB1028 mov rbx,rax 000000013FBB102B lea rdx,[rax+8] 000000013FBB102F call std::operator>><char,std::char_traits<char> > (013FBB18B0h) 000000013FBB1034 mov rcx,rbx 000000013FBB1037 call doSmth (013FBB1170h) 000000013FBB103C xor edx,edx 000000013FBB103E mov rcx,rbx 000000013FBB1041 lea r8d,[rdx+70h]
  • 5. 000000013FBB1045 call memset (013FBB2A2Eh) 000000013FBB104A mov rcx,rbx 000000013FBB104D call qword ptr [__imp_free (013FBB3170h)] return 0; 000000013FBB1053 xor eax,eax .... Visual Studio has done well this time: it erases the data as planned. But what about other compilers? Let's try gcc, version 5.2.1, and clang, version 3.7.0. I've modified our code a bit for gcc and clang and added some code to print the contents of the allocated memory block before and after the cleanup. I print the contents of the block the pointer points to after the memory is freed, but you shouldn't do it in real programs because you never know how the application will respond. In this experiment, however, I'm taking the liberty to use this technique. .... #include "string.h" .... size_t len = strlen(data->m_pswd); for (int i = 0; i < len; ++i) printf("%c", data->m_pswd[i]); printf("| %zu n", data->m_hash); memset(data, 0, sizeof(PrivateData)); free(data); for (int i = 0; i < len; ++i) printf("%c", data->m_pswd[i]); printf("| %zu n", data->m_hash); .... Now, here's a fragment of the assembler code generated by gcc compiler: movq (%r12), %rsi movl $.LC2, %edi xorl %eax, %eax call printf movq %r12, %rdi call free The printing function (printf) is followed by a call to the free() function while the call to the memset() function is gone. If we run the code and enter an arbitrary password (for example "MyTopSecret"), we'll see the following message printed on the screen: MyTopSecret| 7882334103340833743 MyTopSecret| 0 The hash has changed. I guess it's a side effect of the memory manager's work. As for our password "MyTopSecret", it stays intact in the memory. Let's check how it works with clang: movq (%r14), %rsi movl $.L.str.1, %edi xorl %eax, %eax callq printf
  • 6. movq %r14, %rdi callq free Just like in the previous case, the compiler decides to remove the call to the memset() function. This is what the printed output looks like: MyTopSecret| 7882334103340833743 MyTopSecret| 0 So, both gcc and clang decided to optimize our code. Since the memory is freed after calling the memset() function, the compilers treat this call as irrelevant and delete it. As our experiments reveal, compilers tend to delete memset() calls for the sake of optimization working with both stack and dynamic memory of the application. Finally, let's see how the compilers will respond when allocating memory using the new operator. Modifying the code again: #include <string> #include <functional> #include <iostream> #include "string.h" struct PrivateData { size_t m_hash; char m_pswd[100]; }; void doSmth(PrivateData& data) { std::string s(data.m_pswd); std::hash<std::string> hash_fn; data.m_hash = hash_fn(s); } int funcPswd() { PrivateData* data = new PrivateData(); std::cin >> data->m_pswd; doSmth(*data); memset(data, 0, sizeof(PrivateData));
  • 7. delete data; return 1; } int main() { funcPswd(); return 0; } Visual Studio clears the memory as expected: 000000013FEB1044 call doSmth (013FEB1180h) 000000013FEB1049 xor edx,edx 000000013FEB104B mov rcx,rbx 000000013FEB104E lea r8d,[rdx+70h] 000000013FEB1052 call memset (013FEB2A3Eh) 000000013FEB1057 mov edx,70h 000000013FEB105C mov rcx,rbx 000000013FEB105F call operator delete (013FEB1BA8h) return 0; 000000013FEB1064 xor eax,eax The gcc compiler decided to leave the clearing function, too: call printf movq %r13, %rdi movq %rbp, %rcx xorl %eax, %eax andq $-8, %rdi movq $0, 0(%rbp) movq $0, 104(%rbp) subq %rdi, %rcx addl $112, %ecx shrl $3, %ecx rep stosq movq %rbp, %rdi call _ZdlPv The printed output has changed accordingly; the data we have entered are no longer there:
  • 8. MyTopSecret| 7882334103340833743 | 0 But as for clang, it chose to optimize our code in this case as well and cut out the "unnecessary" function: movq (%r14), %rsi movl $.L.str.1, %edi xorl %eax, %eax callq printf movq %r14, %rdi callq _ZdlPv Let's print the memory's contents: MyTopSecret| 7882334103340833743 MyTopSecret| 0 The password remains, waiting for being stolen. Let's sum it all up. We have found that an optimizing compiler may remove a call to the memset() function no matter what type of memory is used - stack or dynamic. Although Visual Studio didn't remove memset() calls when using dynamic memory in our test, you can't expect it to always behave that way in real-life code. The harmful effect may reveal itself with other compilation switches. What follows from our small research is that one cannot rely on the memset() function to clear private data. So, what is a better way to clear them? You should use special memory-clearing functions, which can't be deleted by the compiler when it optimizes the code. In Visual Studio, for example, you can use RtlSecureZeroMemory. Starting with C11, function memset_s is also available. In addition, you can implement a safe function of your own, if necessary; a lot of examples and guides can be found around the web. Here are some of them. Solution No. 1. errno_t memset_s(void *v, rsize_t smax, int c, rsize_t n) { if (v == NULL) return EINVAL; if (smax > RSIZE_MAX) return EINVAL; if (n > smax) return EINVAL; volatile unsigned char *p = v; while (smax-- && n--) { *p++ = c; } return 0; } Solution No. 2.
  • 9. void secure_zero(void *s, size_t n) { volatile char *p = s; while (n--) *p++ = 0; } Some programmers go even further and create functions that fill the array with pseudo-random values and have different running time to hinder attacks based on time measuring. Implementations of these can be found on the web, too. Conclusion PVS-Studio static analyzer can detect data-clearing errors we have discussed here, and uses diagnostic V597 to signal about the problem. This article was written as an extended explanation of why this diagnostic is important. Unfortunately, many programmers tend to think that the analyzer "picks on" their code and there is actually nothing to worry about. Well, it's because they see their memset() calls intact when viewing the code in the debugger, forgetting that what they see is still just a debug version.