LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-14-2017, 11:13 AM   #1
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Rep: Reputation: 174Reputation: 174
Overwriting a variable in memory


Simply put, if I change the contents of a variable from within a bash script will that action overwrite the ACTUAL memory location where the variable was initially stored?

The script prompts me for a passphrase then uses the passphrase to unlock and mount two LUKS partitions. If I then execute PASS="a long string of rubbish..." will the value of the original passphrase still be in memory? Is there a better way to "wipe" the value from memory?

TIA,

Ken
 
Old 01-14-2017, 11:22 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
It depends on the internals of bash, it will probably allocate space on the heap when creating the variable, large enough to hold the data and any internal data it needs, when the variable changes it may reuse that memory if the new value uses less data than the old one, more probably it will release the memory back to the heap then allocate more memory for the new data, this will leave the old data in memory until the memory is re-allocated to some other process or bash itself and it would then be overwritten, but how long it will stay intact in memory is un-knowable.
 
Old 01-14-2017, 12:40 PM   #3
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Thanks Keith Hedger,

That is what I suspected.

Ken
 
Old 01-14-2017, 04:39 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by Keith Hedger View Post
It depends on the internals of bash, it will probably allocate space on the heap when creating the variable, large enough to hold the data and any internal data it needs, when the variable changes it may reuse that memory if the new value uses less data than the old one, more probably it will release the memory back to the heap then allocate more memory for the new data, this will leave the old data in memory until the memory is re-allocated to some other process or bash itself and it would then be overwritten, but how long it will stay intact in memory is un-knowable.
Well, it won't be reallocated to another process. Any memory released to the kernel (unlikely) is zeroed before allocation to the alternate process.

But environment variables will remain within bash - but are retrievable by the debugger that may connect to the bash process.

This is one of the limitations of shell programming - they are NOT designed for security operations.
 
Old 01-20-2017, 11:55 AM   #5
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Thanks jpollard and pardon my delay in responding to your reply. When I ran the script I received a message scolding me about unsecure memory and asking me if I was root. It appears that I need to run the script as root. Perhaps root's use of memory is different?

Ken
 
Old 01-20-2017, 05:08 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by taylorkh View Post
Thanks jpollard and pardon my delay in responding to your reply. When I ran the script I received a message scolding me about unsecure memory and asking me if I was root. It appears that I need to run the script as root. Perhaps root's use of memory is different?

Ken
Nope. The insecurity is inherent in shell programming.

It takes special effort to write applications that are secure - and none of them are done in a shell script. Possibly the best (IMO) is Perl - you can turn on the security option that will then track variables and signal when known insecurity occurs (a "taint" mode to detect insecure usage). This doesn't prevent security issues, but it does try to identify when they occur.
 
Old 01-20-2017, 05:50 PM   #7
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
OK. Suppose I manually enter at the command line
Quote:
cryptsetup luksOpen /dev/disk/by-uuid/e3581441-2eb3...
and enter the passphrase when prompted. Is the passphrase left floating in RAM somewhere? I believe I have read that the actual key used to decrypt the partition remains in memory so I wonder if the passphrase is a bigger vulnerability?
 
Old 01-21-2017, 06:40 AM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
I believe the setup protects the passphrase by bypassing the shell when the prompt is presented (going through /dev/tty directly, the same way that passwd protects changing passwords), then once the passphrase is used to decrypt the decryption key the phrase is discarded by clearing the memory within the cryptsetup tool.

The decrypted key is protected by the kernel so that access to the data is possible.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass password to cryptsetup from a memory variable? taylorkh Linux - Security 13 02-08-2018 07:42 AM
[SOLVED] Overwriting free space or overwriting single files restored by photorec fcrok Linux - Security 22 09-15-2012 12:53 PM
stringstream overwriting memory ineya Programming 5 10-14-2009 12:43 PM
Memory for local variable in C on stack shivaligupta Programming 10 01-20-2007 01:22 AM
Variable at random location in memory? Ephracis Linux - Software 2 08-27-2006 05:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration