[BusyBox 0001412]: "cp -a" allows copy of a directory into itself

bugs at busybox.net bugs at busybox.net
Tue Aug 21 11:58:35 UTC 2007


A NOTE has been added to this issue. 
====================================================================== 
http://busybox.net/bugs/view.php?id=1412 
====================================================================== 
Reported By:                kiltedknight
Assigned To:                BusyBox
====================================================================== 
Project:                    BusyBox
Issue ID:                   1412
Category:                   Other
Reproducibility:            always
Severity:                   minor
Priority:                   normal
Status:                     assigned
====================================================================== 
Date Submitted:             06-29-2007 09:16 PDT
Last Modified:              08-21-2007 04:58 PDT
====================================================================== 
Summary:                    "cp -a" allows copy of a directory into itself
Description: 
execute the following series of commands on GNU linux:

mkdir /tmp/dir1
touch /tmp/dir1/stuff
cp -a /tmp/dir1 /tmp/dir1

The following error is returned: cp: cannot copy a directory, `/tmp/dir1',
into itself, `/tmp/dir1/dir1'

Now run these:

mkdir /tmp/dir1
touch /tmp/dir1/stuff
busybox cp -a /tmp/dir1 /tmp/dir1

Now you get the following:
cp: cannot stat
'/tmp/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1':
File name too long

====================================================================== 

---------------------------------------------------------------------- 
 vapier - 06-29-07 09:22  
---------------------------------------------------------------------- 
i wouldnt really call this a bug ... i'd say "dont do it" 

---------------------------------------------------------------------- 
 kiltedknight - 06-29-07 09:28  
---------------------------------------------------------------------- 
Yeah, but you never know what a script will attempt at some point. 

---------------------------------------------------------------------- 
 vapier - 06-29-07 09:36  
---------------------------------------------------------------------- 
regardless of what a script does, you're going to end up with errors that
someone needs to go in and clean/fix ... what exact form that error takes
i think is irrelevant 

---------------------------------------------------------------------- 
 kiltedknight - 06-29-07 09:46  
---------------------------------------------------------------------- 
Except that this isn't just an error.  It actually recursively copies the
directories.  GNU Linux cp does not.  It does it one time and exits with
the error. 

---------------------------------------------------------------------- 
 vapier - 06-29-07 10:36  
---------------------------------------------------------------------- 
... which is still an error that needs to be cleaned up 

---------------------------------------------------------------------- 
 vda - 06-30-07 10:21  
---------------------------------------------------------------------- 
I know about this behaviour. It's not strictly a bug, more like "user
definitely made a typo in dir name, warn him and stop instead of filling
entire filesystem with recursive copy".

I think it makes sense for busybox cp to have such code too, just make it
conditional on some CONFIG_FEATURE_CP_xxx. 

---------------------------------------------------------------------- 
 kiltedknight - 08-08-07 11:05  
---------------------------------------------------------------------- 
Why should it be conditional?  A recursive copy of a directory into itself
may have started as a typo, but it should never continue to execute until
hitting the longest path name allowed or a disk full error, whichever
comes first. 

---------------------------------------------------------------------- 
 sh4d0wstr1f3 - 08-21-07 04:58  
---------------------------------------------------------------------- 
coreutils cp handles: copy of dir to dir, copy of dir into dir/sub1/ ...
subN/dir, and copy of dir into dir/ ... /link->dir ... the same code is
also used for move, so it detects a move of dir into
dir/sub1/.../subN/dir.

This is actually a battle that y'all fought long ago in a code base far
far away...

http://www.busybox.net/cgi-bin/viewcvs.cgi?rev=8516&view=rev
http://www.busybox.net/cgi-bin/viewcvs.cgi?rev=8517&view=rev
http://www.busybox.net/cgi-bin/viewcvs.cgi?rev=8547&view=rev

What situation the code in 8517 didn't handle that led Erik to revert it?
Maybe it wasn't pretty -- but it was miles cleaner than coreutils cp. 

Issue History 
Date Modified   Username       Field                    Change               
====================================================================== 
06-29-07 09:16  kiltedknight   New Issue                                    
06-29-07 09:16  kiltedknight   Status                   new => assigned     
06-29-07 09:16  kiltedknight   Assigned To               => BusyBox         
06-29-07 09:22  vapier         Note Added: 0002534                          
06-29-07 09:28  kiltedknight   Note Added: 0002535                          
06-29-07 09:28  kiltedknight   Issue Monitored: kiltedknight                    
06-29-07 09:36  vapier         Note Added: 0002536                          
06-29-07 09:46  kiltedknight   Note Added: 0002537                          
06-29-07 10:36  vapier         Note Added: 0002538                          
06-30-07 10:21  vda            Note Added: 0002540                          
08-08-07 11:05  kiltedknight   Note Added: 0002651                          
08-21-07 04:58  sh4d0wstr1f3   Note Added: 0002674                          
======================================================================




More information about the busybox-cvs mailing list