util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Karel Zak <kzak@redhat.com>
To: Sean Anderson <seanga2@gmail.com>
Cc: util-linux@vger.kernel.org, Daniel Gerber <dg@atufi.org>
Subject: Re: [PATCH 1/2] unshare: Fix parsing of id maps
Date: Mon, 17 Jan 2022 09:50:12 +0100	[thread overview]
Message-ID: <20220117084842.2a56c2wbcgx46rek@ws.net.home> (raw)
In-Reply-To: <20220115162926.546843-2-seanga2@gmail.com>

On Sat, Jan 15, 2022 at 11:29:25AM -0500, Sean Anderson wrote:
> For whatever reason, mem2strcpy places the nul-terminator at the end of
> the buffer

Yes, it was originally designed for utmp-like strings where rest of
the buffer is filled by nul-terminators. I think we can fix it to make
it usable for normal strings too.

> instead of at the end of the string it copies. This makes it
> completely useless for our purposes, since one would have to add a
> terminator manually to avoid getting garbage. Just use memcpy instead.
> 
> Fixes: ff5dc96eb ("unshare: Add options to map blocks of user/group IDs")
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> Reported-by: Daniel Gerber <dg@atufi.org>
> ---
> 
>  sys-utils/unshare.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/sys-utils/unshare.c b/sys-utils/unshare.c
> index 443358952..889c561ca 100644
> --- a/sys-utils/unshare.c
> +++ b/sys-utils/unshare.c
> @@ -387,8 +387,9 @@ static int uint_to_id(const char *name, size_t sz)
>  {
>  	char buf[UID_BUFSIZ];
>  
> -	mem2strcpy(buf, name, sz, sizeof(buf));
> -	return strtoul_or_err(name, _("could not parse ID"));
> +	memcpy(buf, name, min(sz, sizeof(buf) - 1));
> +	buf[sz] = '\0';
        ^^
What about sz > sizeof(buf)?

Maybe it would be enough to improve mem2strcpy() in include/strutils.h 

 - dest[nmax-1] = '\0';
 + dest[n] = '\0';

 Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


  reply	other threads:[~2022-01-17  8:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-15 16:29 [PATCH 0/2] unshare: some idmap fixes Sean Anderson
2022-01-15 16:29 ` [PATCH 1/2] unshare: Fix parsing of id maps Sean Anderson
2022-01-17  8:50   ` Karel Zak [this message]
2022-01-15 16:29 ` [PATCH 2/2] unshare: Fix doc comments Sean Anderson
2022-01-18 11:35 ` [PATCH 0/2] unshare: some idmap fixes Karel Zak

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=20220117084842.2a56c2wbcgx46rek@ws.net.home \
    --to=kzak@redhat.com \
    --cc=dg@atufi.org \
    --cc=seanga2@gmail.com \
    --cc=util-linux@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).