[ELDK] [PATCH 4/5] Avoid overwriting random files when adding directories

Wolfgang Denk wd at denx.de
Thu Apr 29 12:35:05 CEST 2010


The code that added any misisng (but needed) standard directories to
the root file system was built on the assumption that none of the used
names existed.  However, some of these names may get installed as part
of copying the "custom" files to the root file system.  Eventually,
these might not even be directories, but symbolic links (thisis
actually needed when building a read-only root file system which
symlinks writable files to some other, writable file system).  To
avoid potentially dangerous actions by creating directories through
symlinks that make no sense (at least not the intended one) in the
host file system, a new script "mkdir_safe" is added which bails out
when it finds any symlinks in the path.

Signed-off-by: Detlev Zundel <dzu at denx.de>
Signed-off-by: Wolfgang Denk <wd at denx.de>
---
 Makefile           |    2 +-
 scripts/mkdir_safe |   31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletions(-)
 create mode 100755 scripts/mkdir_safe

diff --git a/Makefile b/Makefile
index 67af08c..9786e27 100644
--- a/Makefile
+++ b/Makefile
@@ -164,7 +164,7 @@ $(STAMP_ADD_DIRS):
            rootfs/{bin,dev,etc,ftp,home,lib,proc,sbin,tmp} \
            rootfs/{usr,usr/sbin,usr/bin,var,var/log,var/run} ; \
 	do \
-	    mkdir -p $(BUILD)/$$dir ;\
+	    scripts/mkdir_safe $(BUILD)/$$dir ;\
 	done
 	@touch $(STAMP_ADD_DIRS)
 
diff --git a/scripts/mkdir_safe b/scripts/mkdir_safe
new file mode 100755
index 0000000..e58d993
--- /dev/null
+++ b/scripts/mkdir_safe
@@ -0,0 +1,31 @@
+#!/bin/bash
+#
+# Safe version of "mkdir -p" command. "safe" means here that we 
+# "safe" means here that we allow only real directories in the path
+# used for "mkdir" and bail out on symbolic links.
+#
+# (C) Copyright 2010 Detlev Zundel <dzu at denx.de>, DENX Software Engineering
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+is_linkfree_path() {
+    [ "/" = "$1" -o "." = "$1" ] && return 0
+    [ -L "$1" -o "" = "$1" ] && return 1
+    is_linkfree_path `dirname "$1"`
+}
+
+if is_linkfree_path "$1"; then
+    mkdir -p "$1"
+fi
-- 
1.6.2.5



More information about the eldk mailing list