[CRIU] [PATCH 1/3] files: expand slice for parasite_drain_fd dinamically

Cyrill Gorcunov gorcunov at gmail.com
Mon Apr 4 10:46:55 PDT 2016


On Mon, Apr 04, 2016 at 07:58:06PM +0300, Andrey Vagin wrote:
> From: Andrew Vagin <avagin at virtuozzo.com>
> 
> We are going to remove the PARASITE_MAX_FDS limit and
> this patch is a preparation for this.
> 
> Signed-off-by: Andrew Vagin <avagin at virtuozzo.com>
> ---
>  criu/cr-dump.c          | 19 +++++++++++++++----
>  criu/include/parasite.h |  2 +-
>  2 files changed, 16 insertions(+), 5 deletions(-)
> 
> diff --git a/criu/cr-dump.c b/criu/cr-dump.c
> index aaa09e3..5163699 100644
> --- a/criu/cr-dump.c
> +++ b/criu/cr-dump.c
> @@ -184,10 +184,11 @@ static int dump_sched_info(int pid, ThreadCoreEntry *tc)
>  
>  struct cr_imgset *glob_imgset;
>  
> -static int collect_fds(pid_t pid, struct parasite_drain_fd *dfds)
> +static int collect_fds(pid_t pid, struct parasite_drain_fd **dfds)
>  {
>  	struct dirent *de;
>  	DIR *fd_dir;
> +	int size = 0;
>  	int n;
>  
>  	pr_info("\n");
> @@ -206,10 +207,20 @@ static int collect_fds(pid_t pid, struct parasite_drain_fd *dfds)
>  		if (n > PARASITE_MAX_FDS - 1)
>  			return -ENOMEM;
>  
> -		dfds->fds[n++] = atoi(de->d_name);
> +		if (sizeof(struct parasite_drain_fd) + sizeof(int) * (n + 1) > size) {
> +			struct parasite_drain_fd *t;
> +
> +			size += PAGE_SIZE;
> +			t = xrealloc(*dfds, size);
> +			if (!t)
> +				return -1;
> +			*dfds = t;
> +		}
> +
> +		(*dfds)->fds[n++] = atoi(de->d_name);
>  	}
>  
> -	dfds->nr_fds = n;
> +	(*dfds)->nr_fds = n;
>  	pr_info("Found %d file descriptors\n", n);
>  	pr_info("----------------------------------------\n");
>  
> @@ -1208,7 +1219,7 @@ static int dump_one_task(struct pstree_item *item)
>  		if (!dfds)
>  			goto err;
>  
> -		ret = collect_fds(pid, dfds);
> +		ret = collect_fds(pid, &dfds);
>  		if (ret) {
>  			pr_err("Collect fds (pid: %d) failed with %d\n", pid, ret);
>  			goto err;
> diff --git a/criu/include/parasite.h b/criu/include/parasite.h
> index 341a8e5..417b324 100644
> --- a/criu/include/parasite.h
> +++ b/criu/include/parasite.h
> @@ -226,7 +226,7 @@ static inline void copy_sas(ThreadSasEntry *dst, const stack_t *src)
>  
>  struct parasite_drain_fd {
>  	int	nr_fds;
> -	int	fds[PARASITE_MAX_FDS];
> +	int	fds[0];
>  };

Wait, if there is no max-fds on the structure then we might have overrun
memory allocated for parasite structures, don't we?


More information about the CRIU mailing list