From b299f9d368773f1bbfd69494efc77196c7c4e1f6 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Thu, 25 Aug 2011 18:41:27 +0200 Subject: [PATCH] Uses the NOFOLLOW option in open() to ensure to do not follow symbolic links. --- handle.c | 10 ++++++---- handle.h | 5 +++++ mingw.h | 4 ++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/handle.c b/handle.c index 072d5005..0d1e5f42 100644 --- a/handle.c +++ b/handle.c @@ -54,7 +54,7 @@ int handle_close_if_different(struct snapraid_handle* handle, struct snapraid_fi return 0; } -static int handle_ancestor(const char* file) +int handle_ancestor(const char* file) { char dir[PATH_MAX]; char* c; @@ -111,12 +111,13 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file) } /* opening in sequential mode in Windows */ - handle->f = open(handle->path, O_RDWR | O_CREAT | O_BINARY | O_SEQUENTIAL, 0600); + /* do not follow links to ensure to open the real file */ + handle->f = open(handle->path, O_RDWR | O_CREAT | O_BINARY | O_SEQUENTIAL | O_NOFOLLOW, 0600); /* if failed for missing permission */ if (handle->f == -1 && errno == EACCES) { /* retry without requesting write permission */ - handle->f = open(handle->path, O_RDONLY | O_BINARY | O_SEQUENTIAL); + handle->f = open(handle->path, O_RDONLY | O_BINARY | O_SEQUENTIAL | O_NOFOLLOW); } if (handle->f == -1) { @@ -195,8 +196,9 @@ int handle_open(struct snapraid_handle* handle, struct snapraid_file* file) } /* opening in sequential mode in Windows */ + /* do not follow links to ensure to open the real file */ pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub); - handle->f = open(handle->path, O_RDONLY | O_BINARY | O_SEQUENTIAL); + handle->f = open(handle->path, O_RDONLY | O_BINARY | O_SEQUENTIAL | O_NOFOLLOW); if (handle->f == -1) { /* invalidate for error */ diff --git a/handle.h b/handle.h index 2d99d3dd..b3e6d41c 100644 --- a/handle.h +++ b/handle.h @@ -29,6 +29,11 @@ struct snapraid_handle { struct stat st; /**< Stat info of the opened file. */ }; +/** + * Creates all the required directories if missing. + */ +int handle_ancestor(const char* file); + /** * Closes a file, if it's different from the specified one. */ diff --git a/mingw.h b/mingw.h index 8457ba59..5b1a63c9 100644 --- a/mingw.h +++ b/mingw.h @@ -48,6 +48,10 @@ #define opendir windows_opendir #define readdir windows_readdir #define closedir windows_closedir +#define O_NOFOLLOW 0 +#define S_ISLNK(mode) 0 +#define readlink(a,b,c) -1 +#define symlink(a,b) -1 /** * Generic stat information.