diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-09-04 09:39:25 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-09-04 09:41:26 +0100 |
commit | 1030e8aba0993b48cc36c4aa78cd6665914ed99c (patch) | |
tree | be425599353c0620cef1db40df2a8452f358b043 /src | |
parent | 26732678c6f23730f28a37694be932c96e3c4e50 (diff) | |
download | libguestfs-1030e8aba0993b48cc36c4aa78cd6665914ed99c.tar.gz libguestfs-1030e8aba0993b48cc36c4aa78cd6665914ed99c.tar.xz libguestfs-1030e8aba0993b48cc36c4aa78cd6665914ed99c.zip |
Fix guestfs_disk_image API to work with relative paths.
guestfs_disk_image makes a symbolic link to the real filename in order
to sanitize the filename. However this fails if the filename is a
relative path. Call realpath(3) to make the filename canonical.
This fixes commit 20902e7ce02fa375d5d336e6b984f615472ad1b1.
Diffstat (limited to 'src')
-rw-r--r-- | src/info.c | 13 |
1 files changed, 12 insertions, 1 deletions
@@ -20,6 +20,7 @@ #include <stdio.h> #include <stdlib.h> +#include <limits.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> @@ -44,6 +45,7 @@ char * guestfs__disk_format (guestfs_h *g, const char *filename) { + char *abs_filename = NULL; char *safe_filename = NULL; pid_t pid = 0; int fd[2] = { -1, -1 }; @@ -60,7 +62,14 @@ guestfs__disk_format (guestfs_h *g, const char *filename) safe_filename = safe_asprintf (g, "%s/format.%d", g->tmpdir, ++g->unique); - if (symlink (filename, safe_filename) == -1) { + /* 'filename' must be an absolute path so we can link to it. */ + abs_filename = realpath (filename, NULL); + if (abs_filename == NULL) { + perrorf (g, "realpath"); + goto error; + } + + if (symlink (abs_filename, safe_filename) == -1) { perrorf (g, "symlink"); goto error; } @@ -140,6 +149,7 @@ guestfs__disk_format (guestfs_h *g, const char *filename) ret = safe_strdup (g, "unknown"); free (safe_filename); + free (abs_filename); free (line); return ret; /* caller frees */ @@ -154,6 +164,7 @@ guestfs__disk_format (guestfs_h *g, const char *filename) waitpid (pid, NULL, 0); free (safe_filename); + free (abs_filename); free (line); free (ret); |