From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 459213858CDB for ; Thu, 13 Jul 2023 19:02:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 459213858CDB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689274919; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7qT6VDrb9RFKFae6IL4TVmn1C0RIRoL3xlVF9DXATQY=; b=h2R9dSiibRHmcqr6xu05ZjZNXv865/0wrK4xuh77YU7341ydg0wJUAYIhbUyaycJVFifXg ghvsVoBiSKer0xcji/9/EnuoDEhWZg4S3kFv8Ct5f5tJTmpXYRjuqr3kz1JbIuiIPk5q/w D0ExB5HE7G/mNPC07AViKT3yE3UCJJI= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-316-qiURYJJQPuCUz4Dy_VwoRQ-1; Thu, 13 Jul 2023 15:01:58 -0400 X-MC-Unique: qiURYJJQPuCUz4Dy_VwoRQ-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-401df9d2dc4so2430951cf.0 for ; Thu, 13 Jul 2023 12:01:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689274917; x=1691866917; h=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=7qT6VDrb9RFKFae6IL4TVmn1C0RIRoL3xlVF9DXATQY=; b=J+4ynEfe/AUjTmo1BAIoboo6xxQRPPZoiyy2KJwbnwPkdnlO3A5kbKmaj9NJ/EVEo/ j7uZCHLDWQdDAzixaetKNBBZA1NJYdoS+NvwST74Oj4oS241UqkC63tOLiKadSCRDBsi 8gsqxCSW5d1XRQBdXAfOhe3sJgYjToTFWKh75LBuMbY/D3CSYo7ixQvh7TDgEO7OQQnT EpoOVkJp9BvHjNnvAcwR8x7nfBswJvfVuaoj7fFEBjDVnTjQAx7VoIZIt/Zi2p39NkDd y6X71ITWfgXKNharJ/aoLeU0pHIIRXEFgtpjWswEiwfnTUkK2I7MeGScEZiudHzEdGC1 h64Q== X-Gm-Message-State: ABy/qLbjvUeqdHOoIge3oqChrorOkDgFbNqMSoPA9aRMg/B3Ux3AdlFq vg3in2zsYgIYi/XTQM/QRs9v8+4U2TansCptyhVpTuScNwAnq74kyqMp2/Yx4Tmq9RfEQ7vyUKF +myeXxNo6Z/vRuEYReEaSYKmav1ezX1Ey4Rekvxg= X-Received: by 2002:a05:622a:1888:b0:400:8036:6f05 with SMTP id v8-20020a05622a188800b0040080366f05mr3170135qtc.2.1689274917452; Thu, 13 Jul 2023 12:01:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlF98GhiiN9scMdVzLle8IWelDRk7o9UTqmnsW20Q2QmWfArFK3ji3sEG9Vpt6R0YmYUin6WzJNLr9jbxfzaB4s= X-Received: by 2002:a05:622a:1888:b0:400:8036:6f05 with SMTP id v8-20020a05622a188800b0040080366f05mr3170114qtc.2.1689274917089; Thu, 13 Jul 2023 12:01:57 -0700 (PDT) MIME-Version: 1.0 References: <1de3b3ee-7dd8-db16-6e17-365dbd9fde84@fibranet.cat> <74633613-8420-5ad6-2882-6ad14066f781@foss.st.com> <9ad091f5-4ca9-6410-e990-0cd4f1ece86e@foss.st.com> <61101fc8-fc4f-8781-98a4-d84be32c126b@fibranet.cat> In-Reply-To: <61101fc8-fc4f-8781-98a4-d84be32c126b@fibranet.cat> From: Jeff Johnston Date: Thu, 13 Jul 2023 15:01:46 -0400 Message-ID: Subject: Re: [PATCH] Fix getlogin() to check only stdin to get a valid tty To: Jordi Sanfeliu , newlib@sourceware.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="0000000000005c8739060062f701" X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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: --0000000000005c8739060062f701 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I think that when in doubt, going with glibc is the right answer. I'll give Corinna time to pipe in if she wishes, otherwise, I'll check in the glibc-like patch. -- Jeff J. On Thu, Jul 13, 2023 at 12:58=E2=80=AFPM Jordi Sanfeliu via Newlib < newlib@sourceware.org> wrote: > Hello, > > So we have two possible patches to solve this situation: > > My first original patch that only checks if stdin is a tty and seems to > match with glibc comments provided by Torbj=C3=B6rn. The only problem is = that > it won't work in the scenario when someone pipes some data on stdin to > the application: > > $ echo | ./test > $ > > > diff --git a/newlib/libc/unix/getlogin.c b/newlib/libc/unix/getlogin.c > index da4f47a95..e646bcb08 100644 > --- a/newlib/libc/unix/getlogin.c > +++ b/newlib/libc/unix/getlogin.c > @@ -16,9 +16,7 @@ getlogin () > extern char *ttyname (); > char *tty; > > - if (((tty =3D ttyname (0)) =3D=3D 0) > - || ((tty =3D ttyname (1)) =3D=3D 0) > - || ((tty =3D ttyname (2)) =3D=3D 0)) > + if ((tty =3D ttyname (0)) =3D=3D 0) > return 0; > > if ((utmp_fd =3D open (UTMP_FILE, O_RDONLY)) =3D=3D -1) > > > > And a patch based on the comments of Torbj=C3=B6rn that checks all three = fds > before returning NULL: > > diff --git a/newlib/libc/unix/getlogin.c b/newlib/libc/unix/getlogin.c > index da4f47a95..5a3f172e7 100644 > --- a/newlib/libc/unix/getlogin.c > +++ b/newlib/libc/unix/getlogin.c > @@ -16,10 +16,10 @@ getlogin () > extern char *ttyname (); > char *tty; > > - if (((tty =3D ttyname (0)) =3D=3D 0) > - || ((tty =3D ttyname (1)) =3D=3D 0) > - || ((tty =3D ttyname (2)) =3D=3D 0)) > - return 0; > + if ((tty =3D ttyname (0)) =3D=3D 0) > + if ((tty =3D ttyname (1)) =3D=3D 0) > + if ((tty =3D ttyname (2)) =3D=3D 0) > + return 0; > > if ((utmp_fd =3D open (UTMP_FILE, O_RDONLY)) =3D=3D -1) > return 0; > > > Any thoughts? > Thanks. > > > On 7/13/23 18:25, Torbjorn SVENSSON wrote: > > > I took a sneak peak at how glibc does this and there is this comment: > > > > /* Get name of tty connected to fd 0. Return NULL if not a tty or > > if fd 0 isn't open. Note that a lot of documentation says that > > getlogin() is based on the controlling terminal---what they > > really mean is "the terminal connected to standard input". The > > getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all > > return NULL if fd 0 has been closed, so this is the compatible > > thing to do. Note that ttyname(open("/dev/tty")) on those > > systems returns /dev/tty, so that is not a possible solution for > > getlogin(). */ > > > > Based on this comment, I guess it would be sane to drop the check on > > stdout and stderr, but it would have the consequence that you are not > > able to pipe some data on stdin to the application that calls getlogin > > as it would fail in that scenario. > > > > I'm not a maintainer of newlib so I don't really have anything to say > > about what path you decide to go. > > -- > Jordi Sanfeliu > FIBRANET Network Services Provider > https://www.fibranet.cat > > --0000000000005c8739060062f701--