summaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2012-06-24 13:24:00 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2012-06-30 19:53:06 +0200
commitf3a6511ae86ffb1afbe442c45932da6dfce1c584 (patch)
tree1413cc10fc2228429747732ad66fb689bb41d02b /fs
parenta6e358b2f5b219fda18a7bc9348cb969043c19d5 (diff)
downloadbarebox-f3a6511ae86ffb1afbe442c45932da6dfce1c584.tar.gz
barebox-f3a6511ae86ffb1afbe442c45932da6dfce1c584.tar.xz
fs: Fix file create bug when parent is not a directory
When creating a file or a directory we have to check if the parent is actually a directory. Otherwise trying it results in a crash. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'fs')
-rw-r--r--fs/fs.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/fs/fs.c b/fs/fs.c
index af73c8c8aa..6d5740bc36 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -33,6 +33,7 @@
#include <libbb.h>
#include <magicvar.h>
#include <environment.h>
+#include <libgen.h>
void *read_file(const char *filename, size_t *size)
{
@@ -427,6 +428,25 @@ out:
return ret;
}
+static int parent_check_directory(const char *path)
+{
+ struct stat s;
+ int ret;
+ char *dir = dirname(xstrdup(path));
+
+ ret = stat(dir, &s);
+
+ free(dir);
+
+ if (ret)
+ return -ENOENT;
+
+ if (!S_ISDIR(s.st_mode))
+ return -ENOTDIR;
+
+ return 0;
+}
+
const char *getcwd(void)
{
return cwd;
@@ -515,6 +535,12 @@ int open(const char *pathname, int flags, ...)
goto out1;
}
+ if (exist_err) {
+ ret = parent_check_directory(path);
+ if (ret)
+ goto out1;
+ }
+
f = get_file();
if (!f) {
ret = -EMFILE;
@@ -1153,6 +1179,10 @@ int mkdir (const char *pathname, mode_t mode)
char *freep = p;
int ret;
+ ret = parent_check_directory(p);
+ if (ret)
+ goto out;
+
ret = path_check_prereq(pathname, S_UB_DOES_NOT_EXIST);
if (ret)
goto out;