From: Luca Boccassi <luca.boccassi@gmail.com>
To: Adhemerval Zanella <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 12:54:45 +0100 [thread overview]
Message-ID: <CAMw=ZnTjS69a_xNKKok9jCFwB7_FgMnzdDFQ1p9ogRuKtKknsA@mail.gmail.com> (raw)
In-Reply-To: <20230516114612.159103-4-adhemerval.zanella@linaro.org>
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?
> + struct parse_fdinfo_t *fdinfo = arg;
> + fdinfo->found = true;
> + fdinfo->pid = n;
> +
> + return 1;
> +}
> +
> +pid_t
> +pidfd_getpid (int fd)
> +{
> + if (__glibc_unlikely (fd < 0))
> + {
> + __set_errno (EINVAL);
> + return -1;
> + }
> +
> + char fdinfoname[FDINFO_FILENAME_LEN];
> +
> + char *p = mempcpy (fdinfoname, FDINFO_TO_FILENAME_PREFIX,
> + strlen (FDINFO_TO_FILENAME_PREFIX));
> + *_fitoa_word (fd, p, 10, 0) = '\0';
> +
> + struct parse_fdinfo_t fdinfo = { .found = false, .pid = -1 };
> + if (procutils_read_file (fdinfoname, parse_fdinfo, &fdinfo) == -1)
> + /* The fdinfo contains an invalid 'Pid:' value. */
> + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF);
> +
> + /* The FD does not have a 'Pid:' entry associated. */
> + if (!fdinfo.found)
> + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF);
> +
> + /* The pidfd cannot be resolved because it is in a separate pid
> + namespace. */
> + if (fdinfo.pid == 0)
> + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EREMOTE);
> +
> + /* A negative value means the process is terminated. */
> + if (fdinfo.pid < 0)
> + return INLINE_SYSCALL_ERROR_RETURN_VALUE (ESRCH);
Here it just assumes this is an ESRSCH case, but again it could be
failing to parse it for other corner cases of stroul. It should return
ESRCH _only_ if it really parsed -1 from fdinfo, otherwise we cannot
rely on it.
Kind regards,
Luca Boccassi
next prev parent reply other threads:[~2023-05-16 11:54 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 [this message]
2023-05-16 12:26 ` Adhemerval Zanella Netto
2023-05-16 12:38 ` Luca Boccassi
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=ZnTjS69a_xNKKok9jCFwB7_FgMnzdDFQ1p9ogRuKtKknsA@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).