public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: Carlos O'Donell <carlos@redhat.com>,
	GNU C Library <libc-alpha@sourceware.org>
Subject: Re: nonstrings in Glibc
Date: Mon, 27 Nov 2017 16:25:00 -0000	[thread overview]
Message-ID: <891f8f0b-75e4-8273-4d6b-eed70dd2d616@gmail.com> (raw)
In-Reply-To: <20171122001425.GA5120@altlinux.org>

On 11/21/2017 05:14 PM, Dmitry V. Levin wrote:
> On Tue, Nov 21, 2017 at 04:41:27PM -0700, Martin Sebor wrote:
>> On 11/20/2017 11:20 AM, Carlos O'Donell wrote:
>>> On 11/20/2017 08:54 AM, Martin Sebor wrote:
>>>> I'm done testing my update to the -Wstringop-truncation GCC patch
>>>> to find misuses of non-string arrays.  With the very limited use
>>>> of attribute nonstring it only found one potential bug (22447).
>>>> I've been looking at other uses of strncpy in Glibc to see if there
>>>> are other arrays that would benefit from the attribute.  I'm not
>>>> sufficiently familiar with Glibc data structures so it's a very
>>>> slow going.  Could someone help suggests data structures with
>>>> array members that might be candidates?
>>>
>>> struct sockaddr's sun_path?
>>>
>>> http://thread.gmane.org/gmane.comp.standards.posix.austin.general/5735
>>>
>>> Is that what you need help finding?
>>
>> Yes, that's what I'm looking for, thanks!
>>
>>  From the referenced thread it sounds like POSIX doesn't require
>> sun_path to be nul-terminated and BSD UNIX doesn't terminate it.
>> But I'm not sure what happens on Linux.  According to Michael
>> Kerrisk's response it sounds like it is nul-terminated, but
>> then according to the longer discussion on linux.kernel.api
>> it sounds like it isn't.  Which is it?
>
> When struct sockaddr_un is passed to linux kernel, the kernel doesn't
> treat sun_path as nul-terminated, see net/unix/af_unix.c:unix_mkname
> for implementation details.

Thank you.  This is good to know (and expected).  The Glibc
attribute is meant to help with the user side of things so
it's your note below that's especially relevant.  (There
are a few warnings in the Linux kernel suggesting it too
might benefit from the new attribute, if only to suppress
them.)

> However, when linux kernel returns struct sockaddr_un, sun_path is
> nul-terminated if there is enough room provided by userspace, e.g.
> it may need sizeof(struct sockaddr_un) + 1 bytes to write that NUL
> beyond struct sockaddr_un.sun_path.

So if I understand correctly, sun_path will contain a nul-
terminated string if there is enough room for the terminating
nul but not otherwise.  If that's so, that would suggest that
adding attribute nonstring to sum_path might be appropriate.
At the same time, based on existing usage it seems that it
would be prone to false positives.  Annotating sun_path with
it turns up only one warning in a Glibc build:

clnt_unix.c:137:13: warning: ‘strlen’ argument 1 declared attribute 
‘nonstring’ [-Wstringop-overflow=]
        len = strlen (raddr->sun_path) + sizeof (raddr->sun_family) + 1;

AFAICS, the function is only called from clnt_create() where
it's passed a nul-terminated string, so unless it can be called
from user code with a non-nul-terminated array this would be
an example of such a false positive.

Carlos, do you think the attribute would be helpful despite
the false positives?

Martin

  reply	other threads:[~2017-11-27 16:25 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-20 16:54 Martin Sebor
2017-11-20 17:59 ` Paul Eggert
2017-11-20 18:44   ` Martin Sebor
2017-11-21  0:45     ` Paul Eggert
2017-11-20 18:20 ` Carlos O'Donell
2017-11-20 18:41   ` Florian Weimer
2017-11-21 21:21     ` Martin Sebor
2017-11-21 21:34       ` Florian Weimer
2017-11-21 21:37       ` Zack Weinberg
2017-11-21 22:31         ` Andreas Schwab
2017-11-21 22:39           ` Florian Weimer
2017-11-21 22:47         ` Joseph Myers
2017-11-21 23:41   ` Martin Sebor
2017-11-22  0:14     ` Dmitry V. Levin
2017-11-27 16:25       ` Martin Sebor [this message]
2017-11-27 16:40         ` Carlos O'Donell
2017-11-27 16:25       ` Martin Sebor

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=891f8f0b-75e4-8273-4d6b-eed70dd2d616@gmail.com \
    --to=msebor@gmail.com \
    --cc=carlos@redhat.com \
    --cc=libc-alpha@sourceware.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 read-only IMAP folder(s) and NNTP newsgroup(s).