fs: take into account that cr_system overrides standard descriptors

Submitted by Andrey Vagin on May 12, 2018, 7:24 a.m.

Details

Message ID 20180512072420.4725-1-avagin@virtuozzo.com
State Accepted
Series "fs: take into account that cr_system overrides standard descriptors"
Headers show

Commit Message

Andrey Vagin May 12, 2018, 7:24 a.m.
(00.027596) mnt: Dumping mountpoints
(00.027600) mnt: 	90: 2f:/ @ ./dev/pts
(00.027614) mnt: 	130: 2e:/ @ ./run
tar: /proc/self/fd/1: Cannot open: Not a directory
tar: Error is not recoverable: exiting now

Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
---
 criu/filesystems.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/filesystems.c b/criu/filesystems.c
index fa6d96ecf..59f7c99fe 100644
--- a/criu/filesystems.c
+++ b/criu/filesystems.c
@@ -384,15 +384,27 @@  static int tmpfs_dump(struct mount_info *pm)
 	int ret = -1, fd = -1, userns_pid = -1;
 	char tmpfs_path[PSFDS];
 	struct cr_img *img;
+	int tmp_fds[3], ntmp_fds = 0, i;
 
 	fd = open_mountpoint(pm);
 	if (fd < 0)
 		return MNT_UNREACHABLE;
 
-	/* if fd happens to be 0 here, we need to move it to something
-	 * non-zero, because cr_system_userns closes STDIN_FILENO as we are not
-	 * interested in passing stdin to tar.
+	/*
+	 * fd should not be one of standard descriptors, because
+	 * cr_system_userns will override them.
 	 */
+	for (i = 0; i < 3; i++) {
+		if (fd > 2)
+			break;
+		tmp_fds[ntmp_fds++] = fd;
+		fd = dup(fd);
+		if (fd < 0) {
+			pr_perror("Unable to duplicate a file descriptor");
+			goto out;
+		}
+	}
+
 	if (move_fd_from(&fd, STDIN_FILENO) < 0)
 		goto out;
 
@@ -427,6 +439,8 @@  static int tmpfs_dump(struct mount_info *pm)
 
 	close_image(img);
 out:
+	for (i = 0; i < ntmp_fds; i++)
+		close(tmp_fds[i]);
 	close_safe(&fd);
 	return ret;
 }

Comments

Andrey Vagin May 15, 2018, 12:06 a.m.
Applied
On Sat, May 12, 2018 at 10:24:20AM +0300, Andrei Vagin wrote:
> (00.027596) mnt: Dumping mountpoints
> (00.027600) mnt: 	90: 2f:/ @ ./dev/pts
> (00.027614) mnt: 	130: 2e:/ @ ./run
> tar: /proc/self/fd/1: Cannot open: Not a directory
> tar: Error is not recoverable: exiting now
> 
> Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
> ---
>  criu/filesystems.c | 20 +++++++++++++++++---
>  1 file changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/criu/filesystems.c b/criu/filesystems.c
> index fa6d96ecf..59f7c99fe 100644
> --- a/criu/filesystems.c
> +++ b/criu/filesystems.c
> @@ -384,15 +384,27 @@ static int tmpfs_dump(struct mount_info *pm)
>  	int ret = -1, fd = -1, userns_pid = -1;
>  	char tmpfs_path[PSFDS];
>  	struct cr_img *img;
> +	int tmp_fds[3], ntmp_fds = 0, i;
>  
>  	fd = open_mountpoint(pm);
>  	if (fd < 0)
>  		return MNT_UNREACHABLE;
>  
> -	/* if fd happens to be 0 here, we need to move it to something
> -	 * non-zero, because cr_system_userns closes STDIN_FILENO as we are not
> -	 * interested in passing stdin to tar.
> +	/*
> +	 * fd should not be one of standard descriptors, because
> +	 * cr_system_userns will override them.
>  	 */
> +	for (i = 0; i < 3; i++) {
> +		if (fd > 2)
> +			break;
> +		tmp_fds[ntmp_fds++] = fd;
> +		fd = dup(fd);
> +		if (fd < 0) {
> +			pr_perror("Unable to duplicate a file descriptor");
> +			goto out;
> +		}
> +	}
> +
>  	if (move_fd_from(&fd, STDIN_FILENO) < 0)
>  		goto out;
>  
> @@ -427,6 +439,8 @@ static int tmpfs_dump(struct mount_info *pm)
>  
>  	close_image(img);
>  out:
> +	for (i = 0; i < ntmp_fds; i++)
> +		close(tmp_fds[i]);
>  	close_safe(&fd);
>  	return ret;
>  }
> -- 
> 2.14.3
>