LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 07-17-2012, 07:01 PM   #1
sarmads1
LQ Newbie
 
Registered: Jul 2012
Location: ca
Posts: 4

Rep: Reputation: Disabled
Exclamation Embedded Linux Hard Link Suddenly Created on a Root Directory?


Hello
I work on an embedded linux, 2.6.10, and my storage media is flash NAND memory.

I am logged as usr1 over a machine that has NO ROOT log in.
I have a daemon that runs normally each 5 minutes, opens a config file, copies some parameters, Fopen-create's config.tmp file, pastes the parameters into it then then atomically calls
rename (the config.tmp file, into config file);

I am using the uni-std functions of GNU C and compile it as a live module owned by usr1.
rename() in C just renames whatever comes, and keeps NO trace of the original file.

Both config and config.tmp file are usr owned, created in a root directory, and the embedded system FORBIDS totally getting write permissions to that directory.


The code ran successfully over 3-4 years, over 100 machines.
Suddenly I found this on 1 machine only!
ls -l
drwxr-xr-x 4 root root 0 Jul 18 00:49 .
drwxr-xr-x 11 root root 0 Jan 1 1970 ..
-rw------- 2 usr1 users 442 Jul 18 03:44 config
-rw------- 2 usr1 users 442 Jul 18 03:44 config.tmp
other files here ....

As it is obvious, it is a root owned directory, and we CANNOT WRITE TO IT
The number 2 stands for a hard link between config and config.tmp files - which I NEVER did, and NOONE can create manually

This is Linux Security Compromising - creating a file in a read only directory.
Could it be a race condition with another daemon?

Could anyone tell me the internals of rename() function? does it at any moment create a hardlink then delete the old file in GNU?

Or could the Linux storage daemon over the flash memory be the root cause of security problem?

After 3-4 years experience, I am facing this security breach for the 1st time!
Thanks
 
Old 07-17-2012, 10:27 PM   #2
ceyx
Member
 
Registered: May 2009
Location: Fort Langley BC
Distribution: Kubuntu,Free BSD,OSX,Windows
Posts: 342

Rep: Reputation: 59
How old is the flash Nand memory ?
 
Old 07-17-2012, 10:50 PM   #3
sarmads1
LQ Newbie
 
Registered: Jul 2012
Location: ca
Posts: 4

Original Poster
Rep: Reputation: Disabled
ceyx - thanks

Thanks for the reply
The unit I got this problem on is heavily functional for 6-7 months starting(from September 2011 till June 2012)
I am not sure about the "factory - shipping" process time - it is out of my knowledge - but it is supposed to be "new fabricated flash"
 
Old 07-17-2012, 11:06 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Could you add more information. From your description, the rename function should always fail, but you stated otherwise.
Please provide full path information in the ls command you gave. Where are the two files? In the root (/) directoy? Using a relative address in your description removes information. It sounds like the file config and config.tmp are created in the root directory (/) but you shouldn't be able to do that as usr1 ever.

Are you renaming from a root owned directory without usr1 write access, to a directory with usr1 write access, always producing an error?

From the rename 2 manpage:
Code:
If newpath already exists it will be atomically replaced (subject to a few conditions; see ERRORS  below),
       so that there is no point at which another process attempting to access newpath will find it missing.
One of the errors is "ENOMEM Insufficient kernel memory was available." If it is the kernel which provides the atomicity via a system call, and the kernel itself runs out of memory, you may have a file where the path portion of the name wasn't changed due to kernel failure.

The generic description for the rename system call has an additional line:
Code:
If newpath already exists it will be atomically replaced (subject to a few conditions; see ERRORS below), so that there is no point at which another process attempting to access newpath will find it missing.

If newpath exists but the operation fails for some reason rename() guarantees to leave an instance of newpath in place.

However, when overwriting there will probably be a window in which both oldpath and newpath refer to the file being renamed.

Last edited by jschiwal; 07-17-2012 at 11:14 PM.
 
Old 07-18-2012, 12:09 AM   #5
sarmads1
LQ Newbie
 
Registered: Jul 2012
Location: ca
Posts: 4

Original Poster
Rep: Reputation: Disabled
Arrow Jschiwal thanks

1- I wonder why you suppose rename fails always?
Please on your linux try

touch oldname.txt
ln oldname.txt newname.txt (this is hard link not symlink)
Then compile and run the file on
http://www.cplusplus.com/reference/c...cstdio/rename/
You will see that the same files will remain as they are and no perror - I was surprised by this fact - if both file names are hardlinked, rename doesnt fail

2- Full path
IT IS NOT /
is on a mount drive actually - since my embedded system is on flash, we mount it as /mnt/sram/......
Both files are on the same directory /mnt/sram


ls /mnt/sram/ -l
drwxr-xr-x 4 root root 0 Jul 18 00:49 .
drwxr-xr-x 11 root root 0 Jan 1 1970 ..
-rw------- 2 usr1 users 442 Jul 18 03:44 config
-rw------- 2 usr1 users 442 Jul 18 03:44 config.tmp


3- My rename usage is childish from cplusplus.com
char *name ="config";
char *newname =strcat (name, ".tmp");
/* I do parsing and processing here*/
result= rename( name , newname );
if ( result != 0 ) perror (return -error from www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html);

About memory, normally I have plenty of memory onf my /mnt/sram/ - besides I checked the resources of the system, 10% cpu time, 33% memory utilization normal operation

BUT:
Could you refer to that maybe?:
ONLY at the time when my code failed - the hardlink was created - that time rename had NOMEM Linux Error??
This is only duplicate-able when I:
while (p) {p = (*char) malloc(10);} And this will leave only 10 bytes data memory for rename
rename here!

Actually I tried duplicating the rename failure, by infinite looping and renaming, but still I couldn't create the hard link on normal system resources

Please advise
thanks

Last edited by sarmads1; 07-18-2012 at 12:16 AM.
 
Old 07-19-2012, 06:21 PM   #6
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Where does your filesystem image reside? By that, I mean what manufacturing uses to stamp the NAND. If the master image has stray config and config.tmp files, I wouldn't be surprised by your results.
 
  


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
[SOLVED] Hard Link Directory podollb Linux - Server 5 01-30-2012 08:29 AM
[SOLVED] zcat command created link in home directory EDDY1 Linux - Newbie 3 04-24-2011 05:37 AM
Apache2 root server showing directory contents of a symbolic link in the /var/www dir rtoney5 Linux - Server 7 09-03-2010 07:50 PM
My embedded linux suddenly restarts, how to troubleshoot henrik9 Linux - General 1 10-30-2009 01:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 04:14 AM.

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