From: "Marc-André Lureau" <marcandre.lureau@gmail.com>
To: Michael Tokarev <mjt@tls.msk.ru>
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
"Daniel P . Berrangé" <berrange@redhat.com>,
QEMU <qemu-devel@nongnu.org>,
qemu-stable <qemu-stable@nongnu.org>
Subject: Re: [PATCH v3] qemu-sockets: fix unix socket path copy (again)
Date: Fri, 3 Sep 2021 20:04:03 +0400 [thread overview]
Message-ID: <CAJ+F1CLcSZ_zE8oMZz3k_WCSOvf50hapGCu-dfSd9RxzzVhumA@mail.gmail.com> (raw)
In-Reply-To: <20210901131624.46171-1-mjt@msgid.tls.msk.ru>
[-- Attachment #1: Type: text/plain, Size: 3188 bytes --]
Hi
On Wed, Sep 1, 2021 at 5:22 PM Michael Tokarev <mjt@tls.msk.ru> wrote:
> Commit 4cfd970ec188558daa6214f26203fe553fb1e01f added an
> assert which ensures the path within an address of a unix
> socket returned from the kernel is at least one byte and
> does not exceed sun_path buffer. Both of this constraints
> are wrong:
>
> A unix socket can be unnamed, in this case the path is
> completely empty (not even \0)
>
> And some implementations (notable linux) can add extra
> trailing byte (\0) _after_ the sun_path buffer if we
> passed buffer larger than it (and we do).
>
> So remove the assertion (since it causes real-life breakage)
> but at the same time fix the usage of sun_path. Namely,
> we should not access sun_path[0] if kernel did not return
> it at all (this is the case for unnamed sockets),
> and use the returned salen when copyig actual path as an
> upper constraint for the amount of bytes to copy - this
> will ensure we wont exceed the information provided by
> the kernel, regardless whenever there is a trailing \0
> or not. This also helps with unnamed sockets.
>
> Note the case of abstract socket, the sun_path is actually
> a blob and can contain \0 characters, - it should not be
> passed to g_strndup and the like, it should be accessed by
> memcpy-like functions.
>
> Fixes: 4cfd970ec188558daa6214f26203fe553fb1e01f
> Fixes: http://bugs.debian.org/993145
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
> CC: qemu-stable@nongnu.org
Daniel or Michael, or someone else queued this already?
thanks
> ---
> util/qemu-sockets.c | 13 +++++--------
> 1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
> index f2f3676d1f..c5043999e9 100644
> --- a/util/qemu-sockets.c
> +++ b/util/qemu-sockets.c
> @@ -1345,25 +1345,22 @@ socket_sockaddr_to_address_unix(struct
> sockaddr_storage *sa,
> SocketAddress *addr;
> struct sockaddr_un *su = (struct sockaddr_un *)sa;
>
> - assert(salen >= sizeof(su->sun_family) + 1 &&
> - salen <= sizeof(struct sockaddr_un));
> -
> addr = g_new0(SocketAddress, 1);
> addr->type = SOCKET_ADDRESS_TYPE_UNIX;
> + salen -= offsetof(struct sockaddr_un, sun_path);
> #ifdef CONFIG_LINUX
> - if (!su->sun_path[0]) {
> + if (salen > 0 && !su->sun_path[0]) {
> /* Linux abstract socket */
> - addr->u.q_unix.path = g_strndup(su->sun_path + 1,
> - salen - sizeof(su->sun_family) -
> 1);
> + addr->u.q_unix.path = g_strndup(su->sun_path + 1, salen - 1);
> addr->u.q_unix.has_abstract = true;
> addr->u.q_unix.abstract = true;
> addr->u.q_unix.has_tight = true;
> - addr->u.q_unix.tight = salen < sizeof(*su);
> + addr->u.q_unix.tight = salen < sizeof(su->sun_path);
> return addr;
> }
> #endif
>
> - addr->u.q_unix.path = g_strndup(su->sun_path, sizeof(su->sun_path));
> + addr->u.q_unix.path = g_strndup(su->sun_path, salen);
> return addr;
> }
> #endif /* WIN32 */
> --
> 2.30.2
>
>
>
--
Marc-André Lureau
[-- Attachment #2: Type: text/html, Size: 4278 bytes --]
next prev parent reply other threads:[~2021-09-03 16:17 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-01 13:16 [PATCH v3] qemu-sockets: fix unix socket path copy (again) Michael Tokarev
2021-09-01 13:21 ` Daniel P. Berrangé
2021-09-01 15:59 ` Marc-André Lureau
2021-09-03 16:04 ` Marc-André Lureau [this message]
2021-09-06 11:25 ` Michael Tokarev
2021-09-06 11:34 ` Philippe Mathieu-Daudé
2021-09-06 11:39 ` Michael Tokarev
2021-09-07 6:17 ` Philippe Mathieu-Daudé
2021-09-07 13:19 ` Stefan Reiter
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=CAJ+F1CLcSZ_zE8oMZz3k_WCSOvf50hapGCu-dfSd9RxzzVhumA@mail.gmail.com \
--to=marcandre.lureau@gmail.com \
--cc=berrange@redhat.com \
--cc=mjt@tls.msk.ru \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-stable@nongnu.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).