Recent

Author Topic: SOLVED: Truly cleanup a string  (Read 4616 times)

ezlage

  • Guest
SOLVED: Truly cleanup a string
« on: May 22, 2018, 10:07:32 pm »
Friends,

I need to find a way of truly erase a string. I chose a type with no reference count, the ShortString. When a ShortString variable gets out of scope, I can't find it in a memory dump, that's nice. So, I want to find a way to erase the text from memory without waiting for it to come out of scope. How can I do this?

=== In Brazilian Portuguese ===

Amigos,

Eu preciso encontrar uma maneira de realmente apagar uma string. Eu escolhi um tipo sem contagem de referência, o ShortString. Quando uma variável ShortString fica fora do escopo, não consigo encontrá-la em um despejo de memória, isso é bom. Então, quero encontrar uma forma de apagar o texto da memória sem esperar que ele saia do escopo. Como eu posso fazer isso?

Code: Pascal  [Select][+][-]
  1. procedure ZeroFree(var str: shortstring);
  2. begin
  3.   while Length(str)>%0 do begin
  4.     str[High(str)]:=#0;
  5.     SetLength(str,Length(str)-%1);
  6.   end;
  7. end;
  8.  
  9. function ReverseShortStr(AText: shortstring): shortstring;
  10. var
  11.   i,j: byte;
  12. begin
  13.   SetLength(Result,Length(AText));
  14.   i:=%1; j:=Length(AText);
  15.   while (i<=j) do begin
  16.     Result[i]:=AText[j-i+%1];
  17.     Inc(i);
  18.   end;
  19.   ZeroFree(AText);
  20. end;
  21.  
  22. procedure TForm1.Button1Click(Sender: TObject);
  23. var
  24.   test: shortstring='';
  25. begin
  26.   test:=ReverseShortStr('egalleiuqeze');
  27.   ZeroFree(test);
  28.   ShowMessage('Do a memory dump now and try: type file.dump | find /i "ezequiellage"');
  29. end;
  30.  
« Last Edit: May 24, 2018, 09:07:47 pm by ezlage »

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Truly cleanup a string
« Reply #1 on: May 22, 2018, 10:14:02 pm »
Not sure why you (think) you need that, but FillChar(AShortString, #0, SizeOf(AShortString)) will fill the entire memory layout of the string with zero's.

Bart

ezlage

  • Guest
Re: Truly cleanup a string
« Reply #2 on: May 22, 2018, 10:21:20 pm »
Not sure why you (think) you need that, but FillChar(AShortString, #0, SizeOf(AShortString)) will fill the entire memory layout of the string with zero's.

Bart

Unfortunately it did not work. See the attachment.

Infelizmente não funcionou. Veja o anexo.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Truly cleanup a string
« Reply #3 on: May 22, 2018, 10:36:20 pm »
Parameters for FillChar are (variable, count, value). Bart changed order of value and count.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

ezlage

  • Guest
Re: Truly cleanup a string
« Reply #4 on: May 22, 2018, 10:38:53 pm »
Parameters for FillChar are (variable, count, value). Bart changed order of value and count.

Yes, I had noticed. Thank you!

Sim, eu havia percebido. Obrigado!
« Last Edit: May 22, 2018, 10:46:36 pm by ezlage »

ezlage

  • Guest
Re: Truly cleanup a string
« Reply #5 on: May 22, 2018, 10:43:54 pm »
Not sure why you (think) you need that...

Bart

Sorry.
I've noticed that user entries like username and password remain in memory, so I need a way to manually delete it. But before I do anything, I need to understand.

Desculpe.
Eu percebi que entradas do usuário como nome de usuário e senha permanecem na memória, então preciso de uma forma para apagar manualmente. Mas antes de executar alguma coisa, preciso entender.

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Truly cleanup a string
« Reply #6 on: May 23, 2018, 01:15:44 pm »
[…] When a ShortString variable gets out of scope, I can't find it in a memory dump, that's nice.
You're possibly not seeing the text anywhere, because it's already been overwritten. AFAIK FPC does not insert code wiping out memory (i.e. zeroing), since it's generally deemed as unnecessary.
Yours Sincerely
Kai Burghardt

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Truly cleanup a string
« Reply #7 on: May 23, 2018, 03:56:04 pm »
[…] When a ShortString variable gets out of scope, I can't find it in a memory dump, that's nice.
You're possibly not seeing the text anywhere, because it's already been overwritten. AFAIK FPC does not insert code wiping out memory (i.e. zeroing), since it's generally deemed as unnecessary.
No, it re-uses the address space...It is also highly dependent on the memory manager. This is not an FPC issue only. C++ is worse...
Specialize a type, not a var.

piGrimm

  • Guest
Re: Truly cleanup a string
« Reply #8 on: May 23, 2018, 04:19:59 pm »
C++ is worse...
maracas are better with yer diapers

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Truly cleanup a string
« Reply #9 on: May 23, 2018, 04:41:41 pm »
[…] AFAIK FPC does not insert code wiping out memory (i.e. zeroing), since it's generally deemed as unnecessary.
No, it re-uses the address space...It is also highly dependent on the memory manager. This is not an FPC issue only. […]
Uhm, I thought variables that are located on the stack aren't managed by the memory manager. Quote “The heap”,
Quote
The heap is used to store all dynamic variables, and to store class instances.
Since his shortstring is declared in the var-block of the function's declaration, which is virtually just a chunk of declaredLength+1 bytes, I assumed it's on the stack. Am I wrong?

But my idea, that the text might've already been overwritten is plausible, is it not?
Yours Sincerely
Kai Burghardt

ezlage

  • Guest
Re: Truly cleanup a string
« Reply #10 on: May 23, 2018, 07:09:00 pm »
[…] When a ShortString variable gets out of scope, I can't find it in a memory dump, that's nice.
You're possibly not seeing the text anywhere, because it's already been overwritten. AFAIK FPC does not insert code wiping out memory (i.e. zeroing), since it's generally deemed as unnecessary.

I honestly do not know if there is overwriting. I notice that in the application memory dump, the value in question does not appear after it exits the scope. It seems possible to me that the value remain in the memory of the computer, so I wrote the code below. Anyway, I'm still not sure if the value is actually erased or not. But I think if I could erase it while it was in scope, I would not have to worry.

Sinceramente não sei se há sobreposição. Percebo que no dump de memória do aplicativo, o valor em questão não aparece depois que sai do escopo. Parece possível para mim que o valor permaneça na memória do computador, por isso escrevi o código abaixo. De qualquer forma, continuo sem ter certeza se o valor é de fato apagado ou não. Mas penso que se eu conseguisse apagá-lo enquanto ele estivesse no escopo, eu não precisaria me preocupar.

Based on SecureString.pas, created by Stefan van As. Baseado na SecureString.pas, criada por Stefan van As.
Code: Pascal  [Select][+][-]
  1. unit Strongs;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. type
  8.  
  9.   IStrong = interface
  10.     function Data: shortstring;
  11.     function Length: byte;
  12.   end;
  13.  
  14.   Strong = IStrong;
  15.  
  16. procedure ZeroFree(var str: shortstring);
  17. function NewStrong(str: shortstring): strong;
  18. function ReverseShortStr(AText: shortstring): shortstring;
  19.  
  20. implementation
  21.  
  22. type
  23.  
  24.   { TStrong }
  25.  
  26.   TStrong = class(TInterfacedObject,IStrong)
  27.   strict private
  28.     fData: shortstring;
  29.   public
  30.     constructor Create(const AValue: shortstring);
  31.     destructor Destroy; override;
  32.     function Data: shortstring;
  33.     function Length: byte;
  34.   end;
  35.  
  36.   { TStrong }
  37.  
  38. procedure ZeroFree(var str: shortstring);
  39. begin
  40.   while Length(str)>%0 do begin
  41.     str[High(str)]:=#0;
  42.     SetLength(str,Length(str)-%1);
  43.   end;
  44. end;
  45.  
  46. function ReverseShortStr(AText: shortstring): shortstring;
  47. var
  48.   i,j: byte;
  49. begin
  50.   SetLength(Result,Length(AText));
  51.   i:=%1; j:=Length(AText);
  52.   while (i<=j) do begin
  53.     Result[i]:=AText[j-i+%1];
  54.     Inc(i);
  55.   end;
  56.   ZeroFree(AText);
  57. end;
  58.  
  59. constructor TStrong.Create(const AValue: shortstring);
  60. begin
  61.   inherited Create;
  62.   fData:=AValue;
  63. end;
  64.  
  65. destructor TStrong.Destroy;
  66. begin
  67.   if System.Length(fData)>%0
  68.     then ZeroFree(fData);
  69.   inherited Destroy;
  70. end;
  71.  
  72. function TStrong.Data: shortstring;
  73. begin
  74.   Result:=ReverseShortStr(fData);
  75. end;
  76.  
  77. function TStrong.Length: byte;
  78. begin
  79.   Result:=System.Length(fData);
  80. end;
  81.  
  82. function NewStrong(str: shortstring): strong;
  83. begin
  84.   Result:=TStrong.Create(ReverseShortStr(str));
  85.   if Length(str)>%0
  86.     then ZeroFree(str);
  87. end;
  88.  
  89. end.

The use of string reversal is merely testing, just so I know when the value found in memory is what is stored by my interface and when it is not. Finding a way to truly erase the contents of a shortstring would change the string's reversion to something else.

O uso de reversão de string é mero teste, apenas para eu saber quando o valor encontrado na memória é o que está armazenado pela minha interface e quando não é. Encontrando uma forma de verdadeiramente apagar o conteúdo de uma shortstring, trocaria a reversão da string por qualquer outra coisa.
« Last Edit: May 23, 2018, 07:17:28 pm by ezlage »

piGrimm

  • Guest
Re: Truly cleanup a string
« Reply #11 on: May 23, 2018, 09:30:10 pm »
according to this

http://lazarus.lazarus.freepascal.narkive.com/QDRSMELW/zeromemory-functionality

you do not have to create a procedure putting zeros char by char into a memory allocated location, because
"FillChar Compiler RTL procedure is optimized for each processor.
If it's large amounts of memory, use FillWord."
as told Michael on a comment to the above link
optimized means FillChar/FillWord procedure use specific processors targets loop instructions at assembler level when possible, so this ZeroMemory replacement procedure will always be faster than any procedure you write in pascal

piGrimm

  • Guest
Re: Truly cleanup a string
« Reply #12 on: May 23, 2018, 09:46:14 pm »
try something like this

 :P
Code: Pascal  [Select][+][-]
  1. procedure ZeroStr(var s : shortstring);
  2. var p: PChar;
  3. begin
  4.   p:=PChar(@s[1]);
  5.  FillChar(p,Integer(s[0]),#0);
  6. end;  

ZeroStr(mystr);
SetLength(mystr,0):
instead of your zerofree procedure which makes multiple copies of truncated strings (on the lfly) inside ungarbed mem AND IS BIG PROBLEM
« Last Edit: May 23, 2018, 09:55:03 pm by piGrimm »

piGrimm

  • Guest
Re: Truly cleanup a string
« Reply #13 on: May 23, 2018, 09:58:43 pm »
my boss and Friend sam707 says "hello HAHAHAH"

piGrimm

  • Guest
Re: Truly cleanup a string
« Reply #14 on: May 23, 2018, 11:05:38 pm »
correction :
please use Word(s[0]) instead of Integer(s[0]) because if the length goes below #127, Integer transtyping would give negative values

 

TinyPortal © 2005-2018