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 D7F983858D1E for ; Mon, 17 Jul 2023 07:54:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D7F983858D1E 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=1689580464; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKfqNm4jvYmkl7K7zf4lPRwZ4GF7Ij7zIEwdv75rkOE=; b=XeHvRI0ovkVkcGYXZDlwWfEsIsluJxB1QCWz4W5S9k2Std1HDZ21Yv3uGRpagKnJkrUs/O LDpECnV5RbDDvACyfsrE2rwkvVTL8nvehChxMWzay13MXPsqhJ9m5r0jdkC622FqHQYJYH ayCD4rg+2wffMB0zLZgqiQWCn63klPk= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-572-qiyq8tcDPm2rLJTj2ZHlVw-1; Mon, 17 Jul 2023 03:54:22 -0400 X-MC-Unique: qiyq8tcDPm2rLJTj2ZHlVw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9682A1C172A1 for ; Mon, 17 Jul 2023 07:54:22 +0000 (UTC) Received: from calimero.vinschen.de (unknown [10.39.194.53]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7419A207B2DF for ; Mon, 17 Jul 2023 07:54:22 +0000 (UTC) Received: by calimero.vinschen.de (Postfix, from userid 500) id 1BD3FA80D3F; Mon, 17 Jul 2023 09:54:21 +0200 (CEST) Date: Mon, 17 Jul 2023 09:54:21 +0200 From: Corinna Vinschen To: newlib@sourceware.org Subject: Re: [PATCH] Fix getlogin() to check only stdin to get a valid tty Message-ID: Reply-To: newlib@sourceware.org Mail-Followup-To: newlib@sourceware.org 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> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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: On Jul 13 15:01, Jeff Johnston wrote: > 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. Sounds right to me. Corinna > > -- Jeff J. > > On Thu, Jul 13, 2023 at 12:58 PM 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örn. 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 = ttyname (0)) == 0) > > - || ((tty = ttyname (1)) == 0) > > - || ((tty = ttyname (2)) == 0)) > > + if ((tty = ttyname (0)) == 0) > > return 0; > > > > if ((utmp_fd = open (UTMP_FILE, O_RDONLY)) == -1) > > > > > > > > And a patch based on the comments of Torbjörn 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 = ttyname (0)) == 0) > > - || ((tty = ttyname (1)) == 0) > > - || ((tty = ttyname (2)) == 0)) > > - return 0; > > + if ((tty = ttyname (0)) == 0) > > + if ((tty = ttyname (1)) == 0) > > + if ((tty = ttyname (2)) == 0) > > + return 0; > > > > if ((utmp_fd = open (UTMP_FILE, O_RDONLY)) == -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 > > > >