radiantmatrix has asked for the wisdom of the Perl Monks concerning the following question:

I've poked around a bit in SuperSearch and on Google, but I either I can't seem to hit on the right terms, or no one is discussing this.

I'm thinking about writing a cryptographic GUI (probably Tk) application in Perl; the plaintext will obviously have to be in memory at some point so that it can be displayed. However, once the user no longer needs to see the plaintext, I'd like to ensure that it gets entirely cleared from memory so that it cannot be examined later without decrypting it again.

Additionally, I'm concerned about the plaintext ending up in swap.

Is there anything I can do in Perl to ensure this kind of memory security? Or will I have to dust the rust off my C skills to get this to work?

Am I barking up the wrong tree by doing something like:

my $plaintext = decrypt($ciphertext, $key); # .. display plain text, wait for user to close .. $key = '0' x length($key); $plaintext = '0' x length($plaintext);
?

Thanks in advance to anyone who can point me in the right direction!

<radiant.matrix>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet
  • Comment on Can I do secure memory management in Perl scripts for cryptographic applications?
  • Download Code

Replies are listed 'Best First'.
Re: Can I do secure memory management in Perl scripts for cryptographic applications?
by sgifford (Prior) on Jan 19, 2007 at 20:54 UTC
    mmap might do most of what you want, if you're very careful. It will create a scalar tied to a specific region of memory allocated by the OS. If you are very careful with how you access it (see the docs), it will stay in that region. If OS permissions permit, you might be able to write a small module in XS or Inline::C which would use mlock to prevent the pages from being swapped out.

    The biggest problem with all this is that it's very hard to tell if everything's working like it's supposed to. How do you write a test to make sure your data hasn't been exposed to the possibility of being written to swap?

Re: Can I do secure memory management in Perl scripts for cryptographic applications?
by shmem (Chancellor) on Jan 19, 2007 at 21:29 UTC
    Heh... I've done just that some time ago; we have a CSV file with customer data / emergency passwords that gets mailed around to the response team, gpg encrypted. I wrote a Tk app to retrieve that information and display it, since there was no other tool at hand but the shell, gpg, tar etc. It's an ugly quick hack meant to be fired up only in case of urgency. It spread through the company, and there was much discussion concerning safety, memory latency, swap impregnation and so on...

    My take on that is: Thou shalt not utter passwords but inside thyself. At the moment sensitive information is on display, that display becomes a location inside thyself which thou shalt not reveal. Since that information propagates through your computer memory via X cut buffers and what not, your whole box is to be treated as being inside thyself, once a security token has been accessed. So you must keep it from utterance, and the only safe way to detach your responsibility from that box is turning it off (provided it's disks are encrypted and the swapspace is disposed of properly at shutdown).

    In short, it's not a matter of memory destruction but of perception. Security is about awareness, not about a particular device, much the same as firewalls aint software or appliances, but concepts.

    Even if I provide for my colleague to encrypt proper all their data with unbreakable ciphers, I cannot prevent them from shouting in the mall.

    That said, your approach seems safe to me (for some value of safe ;-) which doesn't mean it could not be improved...

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Can I do secure memory management in Perl scripts for cryptographic applications?
by Joost (Canon) on Jan 19, 2007 at 20:25 UTC
    Maybe there is some CPAN module for this, but I don't think you can do this securely in standard perl. In your own C/XS code you can at least be certain to override overwrite the memory locations.

    Tk itself might also be problematic - this is probably true for all user interface code - say if you print to STDOUT you have no way of knowing all traces will be erased after your program is done with it.

    In the end I think (on UNIX at least) you just can't protect yourself from the super user, but you'll be reasonably safe from other users by default as long as you don't write your data to accessible files / open X clients etc.

Re: Can I do secure memory management in Perl scripts for cryptographic applications?
by ikegami (Patriarch) on Jan 20, 2007 at 03:40 UTC

    Good luck, but you have a very tough problem ahead of you. Consider the situation where the memory page containing the plain text is swapped to disk. The disk block to which the memory page was written could lay unchanged for a while. Even worse, it could be claimed by a file which only uses the start of the block, preserving all or a part of the plaintext at least until the file is deleted.

    Bruce Schneier touched on this in recent blog entry and Wired article Choosing Secure Passwords. (Look near the bottom for "Forensic Toolkit".)

Re: Can I do secure memory management in Perl scripts for cryptographic applications?
by CountZero (Bishop) on Jan 19, 2007 at 21:49 UTC
    I think that if you are really that paranoid about the content of the data you decoded (and you may have very well good reasons for it) then you should program this in the lowest level code possible (assembler anyone?) and even forego the use of all OS-calls since you can never be sure if and where they get intercepted, sidetracked or otherwise (ab)used in a way contrary to your security needs.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Well, I'm not that paranoid. I understand (as will my customers) that while the text is displayed, any rouge app/etc. that might be on the machine could access it -- if nothing else, by capturing the screen buffer.

      All I'm really looking for in terms of safety is that when the cleartext display is destroyed, being reasonably certain that it's not still lurking about in some easy-to-retrieve place. I'm willing to live with it remaining in swap (encrypting swap is so damned easy these days...), but would not want it hanging about in RAM.

      <radiant.matrix>
      Ramblings and references
      The Code that can be seen is not the true Code
      I haven't found a problem yet that can't be solved by a well-placed trebuchet
        (encrypting swap is so damned easy these days...)

        and then you have that encryption key somewhere in RAM...

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Can I do secure memory management in Perl scripts for cryptographic applications?
by diotalevi (Canon) on Jan 19, 2007 at 20:27 UTC

    Even if you switch to C, can you count on Tk to treat your data safely?

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Can I do secure memory management in Perl scripts for cryptographic applications?
by derby (Abbot) on Jan 19, 2007 at 20:34 UTC

    Methinks you need a copy of this and this.

    -derby

      What is "this" and how is it different from the other "this"? Can you give a little context for those hyperlinks? Titles would be wonderful.

        Geez ... why does my snarky parade always get rained on. The *links* are to Secure Programming in C/C++ and Embedding and Extending Perl.

        -derby
Encrypting swap (OS-dependent)
by rkrieger (Friar) on Jan 21, 2007 at 17:01 UTC
    If you're worried about what ends up in swap, you may want to check whether your OS offers functionality like the following:

    In OpenBSD, the sysctl(8) command allows you to instruct the OS to encrypt the pages to go to swap. See the man pages for more information on that feature.
    Resulting in the (shell) command of:
    $ sudo sysctl -w vm.swapencrypt.enable=1 vm.swapencrypt.enable: 0 -> 1
    Granted, it relies on your OS' feature set and it's not done in Perl. Still, it may alleviate your concerns.

    Cheers,

    Rogier
    --
    If you don't know where you're going, any road will get you there.
Re: Can I do secure memory management in Perl scripts for cryptographic applications?
by Anonymous Monk on Jan 20, 2007 at 11:34 UTC