public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Luca Boccassi <luca.boccassi@gmail.com>
To: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org, Florian Weimer <fweimer@redhat.com>,
	 Philip Withnall <bugzilla@tecnocode.co.uk>
Subject: Re: [PATCH v3 3/3] linux: Add pidfd_getpid
Date: Tue, 16 May 2023 13:38:22 +0100	[thread overview]
Message-ID: <CAMw=ZnSnrAsmJwYO+-EwcLy-ON9FSR5kMgpAU5rFDRrjRwe65w@mail.gmail.com> (raw)
In-Reply-To: <bcea08f2-403e-f307-5c9b-0e8dda471d5d@linaro.org>

On Tue, 16 May 2023 at 13:26, Adhemerval Zanella Netto
<adhemerval.zanella@linaro.org> wrote:
> On 16/05/23 08:54, Luca Boccassi wrote:
> > On Tue, 16 May 2023 at 12:46, Adhemerval Zanella
> > <adhemerval.zanella@linaro.org> wrote:
> >>
> >> This interface allows to obtain the associated pid ID from the
> >> process file descriptor.  It is done by parsing the procps fdinfo
> >> information.  Its prototype is:
> >>
> >>    pid_t pidfd_getpid (int fd)
> >>
> >> It returns the associated pid or -1 in case of an error and set the
> >> errno accordingly.  The possible errno values are the smae from
> >> open, read, and close (used on procps parsing), along with:
> >>
> >>    - EINVAL if the FP is negative (similar to fexecve).
> >>
> >>    - EBADF if the FD does not have a PID associated of if the fdinfo
> >>      fields contains a value larger than pid_t.
> >>
> >>    - EREMOTE if the PID is in a separate namespace.
> >>
> >>    - ESRCH if the process is already terminated.
> >>
> >> Checked on x86_64-linux-gnu on Linux 4.15 (no CLONE_PID or waitid
> >> support), Linux 5.15 (only clone support), and Linux 5.19 (full
> >> support including clone3).
> >> ---
> > <..>
> >> +#define FDINFO_TO_FILENAME_PREFIX "/proc/self/fdinfo/"
> >> +
> >> +#define FDINFO_FILENAME_LEN \
> >> +  (sizeof (FDINFO_TO_FILENAME_PREFIX) + INT_STRLEN_BOUND (int))
> >> +
> >> +struct parse_fdinfo_t
> >> +{
> >> +  bool found;
> >> +  pid_t pid;
> >> +};
> >> +
> >> +static int
> >> +parse_fdinfo (const char *l, void *arg)
> >> +{
> >> +  enum { fieldlen = sizeof ("Pid:") - 1 };
> >> +  if (strncmp (l, "Pid:", fieldlen) != 0)
> >> +    return 0;
> >> +
> >> +  l += fieldlen;
> >> +
> >> +  char *endp;
> >> +  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?

Kind regards,
Luca Boccassi

  reply	other threads:[~2023-05-16 12:38 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 [this message]
2023-05-16 12:55         ` Zack Weinberg
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='CAMw=ZnSnrAsmJwYO+-EwcLy-ON9FSR5kMgpAU5rFDRrjRwe65w@mail.gmail.com' \
    --to=luca.boccassi@gmail.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=bugzilla@tecnocode.co.uk \
    --cc=fweimer@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).