public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Zack Weinberg" <zack@owlfolio.org>
To: "GNU libc development" <libc-alpha@sourceware.org>
Subject: Re: [PATCH v3 3/3] linux: Add pidfd_getpid
Date: Tue, 16 May 2023 08:55:51 -0400	[thread overview]
Message-ID: <a8daaf31-02c0-497a-b72c-459c8dfc390c@app.fastmail.com> (raw)
In-Reply-To: <CAMw=ZnSnrAsmJwYO+-EwcLy-ON9FSR5kMgpAU5rFDRrjRwe65w@mail.gmail.com>

On Tue, May 16, 2023, at 8:38 AM, Luca Boccassi via Libc-alpha wrote:
> On Tue, 16 May 2023 at 13:26, Adhemerval Zanella Netto
> <adhemerval.zanella@linaro.org> wrote:
>> >> +  unsigned long n = strtoul (l, &endp, 10);
>> >> +  if (l == endp || (n > INT_MAX && n != ULONG_MAX))
>> >> +    return 0;
>> >
>> > How can this tell the difference between '-1' and garbage input? It
>> > seems to me this will confuse mangled input here with ESRCH, given the
>> > pid in fdinfo is initialized to -1, no?
>>
>> Because -1 will be parsed as ULONG_MAX.  For instance, with the inputs:
>>
>> Input:             | Function result |         parse_fdinfo_t
>> -------------------|-----------------|-----------------------
>> "Pid: 0"           |               1 |        {1,          0}
>> "Pid: 1"           |               1 |        {1,          1}
>> "Pid: 2147483647"  |               1 |        {1, 2147483647}
>> "Pid: 2147483648"  |               0 |        {0,         -1}
>> "Pid: -1"          |               1 |        {1,         -1}
>> "Pid: -3"          |               0 |        {0,         -1}
>> "Pid: -24x"        |               0 |        {0,         -1}
>>
>> So only if the PID if positive less than INT_MAX or -1 the function
>> will set that the PID as found.
>
> This seems excessively convoluted and fragile to me, relying on
> overflows and so on. I cannot stress this enough, in order to use this
> from systemd et al I need to be absolutely certain that garbage input
> is not treated the same as ESCHR. Why not just use strtoll or so, to
> get an exact result out of it?

Also, all of the strto* functions are sensitive to the locale.  It shouldn't be too hard to make this function async-signal-safe and I think we really ought to.

(Actually, why isn't this operation exposed as an ioctl on the pidfd?  I thought the kernel people had come around to accepting that parsing text files in /proc is fragile, inconvenient, and slow.)

zw

  reply	other threads:[~2023-05-16 12:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-16 11:46 [PATCH v3 0/3] Add pidfd_spawn, pidfd_spawnp, pidfd_fork, and pidfd_getpid Adhemerval Zanella
2023-05-16 11:46 ` [PATCH v3 1/3] posix: Add pidfd_spawn and pidfd_spawnp (BZ# 30349) Adhemerval Zanella
2023-05-16 11:58   ` Andreas Schwab
2023-05-16 11:46 ` [PATCH v3 2/3] posix: Add pidfd_fork Adhemerval Zanella
2023-05-16 11:46 ` [PATCH v3 3/3] linux: Add pidfd_getpid Adhemerval Zanella
2023-05-16 11:54   ` Luca Boccassi
2023-05-16 12:26     ` Adhemerval Zanella Netto
2023-05-16 12:38       ` Luca Boccassi
2023-05-16 12:55         ` Zack Weinberg [this message]
2023-05-16 13:05           ` Adhemerval Zanella Netto
2023-05-16 12:10   ` Andreas Schwab

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=a8daaf31-02c0-497a-b72c-459c8dfc390c@app.fastmail.com \
    --to=zack@owlfolio.org \
    --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).