From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Luca Boccassi <luca.boccassi@gmail.com>
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 09:26:25 -0300 [thread overview]
Message-ID: <bcea08f2-403e-f307-5c9b-0e8dda471d5d@linaro.org> (raw)
In-Reply-To: <CAMw=ZnTjS69a_xNKKok9jCFwB7_FgMnzdDFQ1p9ogRuKtKknsA@mail.gmail.com>
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.
>
>> + 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.
It is already handled by the 'found' check above, where for values not in
PID range or error in strtoul, EBADF will be returned. I might improve
the comments to make it clear.
next prev parent reply other threads:[~2023-05-16 12:26 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 [this message]
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=bcea08f2-403e-f307-5c9b-0e8dda471d5d@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=bugzilla@tecnocode.co.uk \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=luca.boccassi@gmail.com \
/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).