[PATCH 0/3] Fix resolving absolute symlinks (RHBZ#579608).
This patchset just fixes the 'hexdump' command as an example. The important part of the patch is #2 since that shows the approach I want to take to fix this. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://et.redhat.com/~rjones/virt-top
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v
On 06/05/10 22:03, Richard W.M. Jones wrote:
From fd62c985a26bfd0cac27735a47ce7fd7b068fdb4 Mon Sep 17 00:00:00 2001 From: Richard Jones <rjones@redhat.com> Date: Thu, 6 May 2010 20:32:09 +0100 Subject: [PATCH 1/3] daemon: Change command to abort() on resource problems.
The comment in the code describes it thus:
/* Note: abort is used in a few places along the error paths early * in this function. This is because (a) cleaning up correctly is * very complex at these places and (b) abort is used when a * resource problems is indicated which would be due to much more * serious issues - eg. memory or file descriptor leaks. We * wouldn't expect fork(2) or pipe(2) to fail in normal * circumstances. */
ACK. Matt -- Matthew Booth, RHCA, RHCSS Red Hat Engineering, Virtualisation Team M: +44 (0)7977 267231 GPG ID: D33C3490 GPG FPR: 3733 612D 2D05 5458 8A8A 1600 3441 EA19 D33C 3490
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v
On Thu, May 06, 2010 at 10:04:09PM +0100, Richard W.M. Jones wrote:
+ if (xwrite (1, buffer, n) == -1) + _exit (EXIT_FAILURE);
To implement 'head' I had to change this to: if (xwrite (1, buffer, n) == -1) /* EPIPE error indicates the command process has exited * early. If the command process fails that will be caught * by the daemon, and if not, then it's not an error. */ _exit (errno == EPIPE ? EXIT_SUCCESS : EXIT_FAILURE); This is of course because the head process exits early, so causing this process to get EPIPE. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
Richard W.M. Jones wrote:
Subject: [PATCH 2/3] daemon: Fix for commands working on absolute symbolic links (RHBZ#579608).
The original idea (suggested by Al Viro) was to fork and chroot into the sysroot and read the file from there. Because of the separate process being chrooted, absolute links would be resolved correctly. The slightly modified idea is to open the file in the daemon process (but temporarily chrooted, so symlinks resolve correctly), fork, and have the subprocess just be responsible for copying the file. (Strictly speaking we don't need to fork, but this implementation is simpler).
This commit just includes the changes needed to the command*() functions in daemon/guestfsd.c and adds an absolute symlink to the test ISO for testing it. Later commits will fix the broken daemon commands themselves. --- .gitignore | 1 + daemon/daemon.h | 4 ++- daemon/guestfsd.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++-- images/Makefile.am | 4 ++ 4 files changed, 103 insertions(+), 5 deletions(-)
diff --git a/.gitignore b/.gitignore index 382cbcf..a79edca 100644 --- a/.gitignore +++ b/.gitignore @@ -103,6 +103,7 @@ images/100kallspaces images/100kallzeroes images/100krandom images/10klines +images/abssymlink images/hello.b64 images/initrd images/initrd-x86_64.img diff --git a/daemon/daemon.h b/daemon/daemon.h index 81a9f36..977dfdc 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -53,7 +53,9 @@ extern void free_stringslen (char **argv, int len); #define commandv(out,err,argv) commandvf((out),(err),0,(argv)) #define commandrv(out,err,argv) commandrvf((out),(err),0,(argv))
-#define COMMAND_FLAG_FOLD_STDOUT_ON_STDERR 1 +#define COMMAND_FLAG_FD_MASK (1024-1) +#define COMMAND_FLAG_FOLD_STDOUT_ON_STDERR 1024 +#define COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN 2048
This looks fine. A couple of stylistic nits below. The 1024 seems like it's related to the setting of _POSIX_OPEN_MAX (1024) on Linux-based systems these days. It might be good to add something like this, just in case it's ever increased: verify (_POSIX_OPEN_MAX <= COMMAND_FLAG_FOLD_STDOUT_ON_STDERR);
extern int commandf (char **stdoutput, char **stderror, int flags, const char *name, ...); diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index be79300..4832842 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -715,6 +715,14 @@ commandvf (char **stdoutput, char **stderror, int flags, * error messages in the *stderror buffer. If using this flag, * you should pass stdoutput as NULL because nothing could ever be * captured in that buffer. + * + * COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN: For running external + * commands on chrooted files correctly (see RHBZ#579608) specifying + * this flag causes another process to be forked which chroots into + * sysroot and just copies the input file to stdin of the specified + * command. The file descriptor is ORed with the flags, and that file + * descriptor is always closed by this function. See hexdump.c for an + * example of usage. */ int commandrvf (char **stdoutput, char **stderror, int flags, @@ -722,7 +730,9 @@ commandrvf (char **stdoutput, char **stderror, int flags, { int so_size = 0, se_size = 0; int so_fd[2], se_fd[2]; - pid_t pid; + int flag_copy_stdin = flags & COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN; + int stdin_fd[2] = { -1, -1 }; + pid_t pid, stdin_pid = -1; int r, quit, i; fd_set rset, rset2; char buf[256]; @@ -752,15 +762,28 @@ commandrvf (char **stdoutput, char **stderror, int flags, abort (); }
+ if (flag_copy_stdin) { + if (pipe (stdin_fd) == -1) { + perror ("pipe");
No big deal, but something to keep in mind... It's slightly better to use a function like glibc's error here, since it flushes stderr. Especially when the next thing that happens is an abort or kill, it can happen that the desired output is never produced.
+ abort (); + } + } + pid = fork (); if (pid == -1) { perror ("fork"); abort (); }
- if (pid == 0) { /* Child process. */ + if (pid == 0) { /* Child process running the command. */ close (0); - open ("/dev/null", O_RDONLY); /* Set stdin to /dev/null (ignore failure) */ + if (flag_copy_stdin) { + dup2 (stdin_fd[0], 0); + close (stdin_fd[0]); + close (stdin_fd[1]); + } else + /* Set stdin to /dev/null (ignore failure) */ + open ("/dev/null", O_RDONLY);
IMHO, it's safer to put curly braces around multi-line blocks like this "else" one, even though it's obviously a single stmt. Here's some justification: http://libvirt.org/git/?p=libvirt.git;a=blob;f=HACKING#l108 I'm a little sensitive about this, since I introduced a bug into libvirt recently due to a similar situation. My penance was to write that addition to HACKING ;-)
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
On Thu, May 06, 2010 at 10:04:54PM +0100, Richard W.M. Jones wrote:
+ CHROOT_IN; + fd = open (path, O_RDONLY); + CHROOT_OUT; + + if (fd == -1) { + perror (path); ^^^ This should be:
reply_with_perror ("%s", path); Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
Richard W.M. Jones wrote: ...
Subject: [PATCH 3/3] daemon: Fix hexdump to work on absolute symbolic links (RHBZ#579608). ...
This looks fine. Glad you caught the s/perror/reply_with_error/ problem you mentioned in the follow-up comment.
char * do_hexdump (const char *path) { - char *buf; - int r; + int fd, flags, r;
I have to say I would find it easier to read/review if each of these three declarations were moved down to its point of first use.
char *out, *err;
- buf = sysroot_path (path); - if (!buf) { - reply_with_perror ("malloc"); + CHROOT_IN; + fd = open (path, O_RDONLY); + CHROOT_OUT; + + if (fd == -1) { + perror (path); return NULL; }
- r = command (&out, &err, "hexdump", "-C", buf, NULL); - free (buf); + flags = COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN | fd; + r = commandf (&out, &err, flags, "hexdump", "-C", NULL); if (r == -1) { reply_with_error ("%s: %s", path, err); free (err);
participants (3)
-
Jim Meyering -
Matthew Booth -
Richard W.M. Jones