The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Mike Rapoport <rppt@kernel.org>
To: Jiangshan Yi <yijiangshan@kylinos.cn>
Cc: akpm@linux-foundation.org, david@kernel.org, shuah@kernel.org,
	ljs@kernel.org, liam@infradead.org, vbabka@kernel.org,
	surenb@google.com, mhocko@suse.com, linux-mm@kvack.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	13667453960@163.com
Subject: Re: [PATCH] selftests/mm: cow: fix leaks in child_vmsplice_memcmp_fn()
Date: Thu, 2 Jul 2026 10:37:59 +0300	[thread overview]
Message-ID: <akYVVzhEpG3-D3LF@kernel.org> (raw)
In-Reply-To: <20260702072436.739677-1-yijiangshan@kylinos.cn>

On Thu, Jul 02, 2026 at 03:24:36PM +0800, Jiangshan Yi wrote:
> child_vmsplice_memcmp_fn() allocated two heap buffers (old, new) and
> opened a pipe, but every return path - including the normal one -
> returned directly without freeing the buffers or closing the pipe file
> descriptors. It also used the malloc() results without checking them,
> so memcpy(old, mem, size) would crash on allocation failure (size can
> be as large as a huge page).
> 
> The sibling helper do_test_vmsplice_in_parent() in the same file already
> uses the goto-based cleanup pattern; follow it here.

The difference is that child_vmsplice_memcmp_fn() runs in a forked process
that dies if any error happens, so the allocated resources are anyway
immediately freed.

> Check the allocations up front

This is the only check that is actually required, others are nice to have
at best.

I'd keep the code simple and only add the allocation check that returns
-ENOMEM on failure.

> and introduce unified error handling with goto labels so that old/new are
> always freed and both pipe fds are always closed before returning, on
> every path.
> 
> Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
> ---
>  tools/testing/selftests/mm/cow.c | 44 ++++++++++++++++++++++++--------
>  1 file changed, 33 insertions(+), 11 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c
> index 0c627ea89ff7..f6a1bedfd6e2 100644
> --- a/tools/testing/selftests/mm/cow.c
> +++ b/tools/testing/selftests/mm/cow.c
> @@ -145,26 +145,39 @@ static int child_vmsplice_memcmp_fn(char *mem, size_t size,
>  	char *old, *new;
>  	int fds[2];
>  	char buf;
> +	int ret;
>  
>  	old = malloc(size);
>  	new = malloc(size);
> +	if (!old || !new) {
> +		ret = -ENOMEM;
> +		goto free;
> +	}
>  
>  	/* Backup the original content. */
>  	memcpy(old, mem, size);
>  
> -	if (pipe(fds) < 0)
> -		return -errno;
> +	if (pipe(fds) < 0) {
> +		ret = -errno;
> +		goto free;
> +	}
>  
>  	/* Trigger a read-only pin. */
>  	transferred = vmsplice(fds[1], &iov, 1, 0);
> -	if (transferred < 0)
> -		return -errno;
> -	if (transferred == 0)
> -		return -EINVAL;
> +	if (transferred < 0) {
> +		ret = -errno;
> +		goto close_pipe;
> +	}
> +	if (transferred == 0) {
> +		ret = -EINVAL;
> +		goto close_pipe;
> +	}
>  
>  	/* Unmap it from our page tables. */
> -	if (munmap(mem, size) < 0)
> -		return -errno;
> +	if (munmap(mem, size) < 0) {
> +		ret = -errno;
> +		goto close_pipe;
> +	}
>  
>  	/* Wait until the parent modified it. */
>  	write(comm_pipes->child_ready[1], "0", 1);
> @@ -174,11 +187,20 @@ static int child_vmsplice_memcmp_fn(char *mem, size_t size,
>  	/* See if we still read the old values via the pipe. */
>  	for (total = 0; total < transferred; total += cur) {
>  		cur = read(fds[0], new + total, transferred - total);
> -		if (cur < 0)
> -			return -errno;
> +		if (cur < 0) {
> +			ret = -errno;
> +			goto close_pipe;
> +		}
>  	}
>  
> -	return memcmp(old, new, transferred);
> +	ret = memcmp(old, new, transferred);
> +close_pipe:
> +	close(fds[0]);
> +	close(fds[1]);
> +free:
> +	free(old);
> +	free(new);
> +	return ret;
>  }
>  
>  typedef int (*child_fn)(char *mem, size_t size, struct comm_pipes *comm_pipes);
> -- 
> 2.25.1
> 

-- 
Sincerely yours,
Mike.

  reply	other threads:[~2026-07-02  7:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02  7:24 [PATCH] selftests/mm: cow: fix leaks in child_vmsplice_memcmp_fn() Jiangshan Yi
2026-07-02  7:37 ` Mike Rapoport [this message]
2026-07-02  7:58   ` David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=akYVVzhEpG3-D3LF@kernel.org \
    --to=rppt@kernel.org \
    --cc=13667453960@163.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=yijiangshan@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox