From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x112e.google.com (mail-yw1-x112e.google.com [IPv6:2607:f8b0:4864:20::112e]) by sourceware.org (Postfix) with ESMTPS id 9CEDE3858439 for ; Tue, 16 May 2023 11:54:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9CEDE3858439 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-561a7d96f67so3733467b3.3 for ; Tue, 16 May 2023 04:54:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684238096; x=1686830096; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=zFsqb0lxRziQj0/yiUH0xlRIqpEvK6X5SEhZJFxbPHY=; b=ZptGw7utfNw3F6aNlo4GqK4xVl8fyAnYmODJE640GGtRMtKM7kC2Z8aK35CjHxnLMg nZ80jR+g4nYfHaTM3qj0Ktp014CNNfLsJ8ggTIyFxdWuzQmyD8VQeA6nLE8Un0wzgUzo /JELYIxSQuPMayRtgQifDdpx9lMcYkS4XQiH43ZHq1H7NdCHsGe/6/dpf/4OznzpsF/b nqj4gU3Ct31t7SMh8A2tNzboJkACbjoo39/JCHb3+N2+ZJkGyH4XKaM8LZcwQcbURa3/ yW9ree4rky278rHGgcpXbdtyqb7eqARYv7SztRy8WRSbnNAgYWLYC0UMKDXvNbgdI35Q mU1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684238096; x=1686830096; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zFsqb0lxRziQj0/yiUH0xlRIqpEvK6X5SEhZJFxbPHY=; b=C5E3+wiEvgPGGYh0qb2G5B3zu4MFst+GA1LTryfb+Ynn7c7HVe/GTzv6TUUxxplgJZ Pew0rQ5G2AXhJShW/lC8c8xKEq1JTrJDawLiDKXMK+FlHbRT05XVBmd4su4LMlhY8neR zR1KNAv3p7PnmFVgDn+sGNoTKO2Py741xoSPsePTqRN7pQW3bLf66fjRS1bQri6wzN/P fhvhxz9ETLGeBrPhOXcBokpbcwP6isnb1dRG7uRuUd5yZZm8pseiw1dP8H87JXB0/ohg FZlwa1aVL5fVjqZwup1CtFAPpHn/nR1EoB5E78lvJQjQcLjgpwJslDt5pjldO0hFoHUU AQQg== X-Gm-Message-State: AC+VfDzgNfbqHgtJYBetHjn7nZzMljCG2WrPBoJsPuRV/KU4nXGWAVLz KFKpeiJ6/5ThepIGAhxKGpGpP1BWVTg1LV+kx4E= X-Google-Smtp-Source: ACHHUZ4b85Tx9cKTkQ+GBUFOvh/P9O3nPe9OBZVpVICwqklJYCbhrzObiTLMb7UmEw46uF/QiP4xxYaH7y8OX/ZOh0s= X-Received: by 2002:a0d:df14:0:b0:55a:ad7d:3f38 with SMTP id i20-20020a0ddf14000000b0055aad7d3f38mr35080338ywe.26.1684238095872; Tue, 16 May 2023 04:54:55 -0700 (PDT) MIME-Version: 1.0 References: <20230516114612.159103-1-adhemerval.zanella@linaro.org> <20230516114612.159103-4-adhemerval.zanella@linaro.org> In-Reply-To: <20230516114612.159103-4-adhemerval.zanella@linaro.org> From: Luca Boccassi Date: Tue, 16 May 2023 12:54:45 +0100 Message-ID: Subject: Re: [PATCH v3 3/3] linux: Add pidfd_getpid To: Adhemerval Zanella Cc: libc-alpha@sourceware.org, Florian Weimer , Philip Withnall Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, 16 May 2023 at 12:46, Adhemerval Zanella 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