Skip to contentSkip to conversation

Is getpass(3) really obsolete?

PATCH New Alejandro Colomar 11 participants 22 messages Oct 29, 2021 – Sep 27, 2022 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

Hi,

As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all. The manual page goes further and says "This function is obsolete. Do not use it." in its first lines.

But, glibc doesn't seem to have deprecated this function at all. And it seems to be the most portable way to get a password, even if it's not in POSIX.

BSDs have readpassphrase(3), but glibc doesn't, so unless you recommend using readpassphrase(3) from libbsd, or plan to add it to glibc, I think getpass(3) should be the recommended function in Linux, and therefore we should remove the hard words against it.

As a real example, git(1) uses getpass(3).
<https://github.com/git/git/blob/master/compat/terminal.c>

What are your thoughts?

Thanks,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#1 lore raw

[Add a few CCs, since I mentioned them.]

Quoted 7 lines · Alejandro Colomar

Hi,

As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all.  The manual page goes further and says "This function is obsolete. Do not use it." in its first lines.

But, glibc doesn't seem to have deprecated this function at all.  And it seems to be the most portable way to get a password, even if it's not in POSIX.

BSDs have readpassphrase(3), but glibc doesn't, so unless you recommend

OpenBSD also marks getpass(3) as obsolete and recommends readpassphrase(3):
<https://man.openbsd.org/getpass>

Quoted 12 lines

using readpassphrase(3) from libbsd, or plan to add it to glibc, I think getpass(3) should be the recommended function in Linux, and therefore we should remove the hard words against it.

As a real example, git(1) uses getpass(3).
<https://github.com/git/git/blob/master/compat/terminal.c>

What are your thoughts?

Thanks,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#2 lore raw
Quoted 1 line · Alejandro Colomar (man-pages)

[Add a few CCs, since I mentioned them.]

[I'm not sure what the full context of this thread is, but just replying from the POV of git@ being CC'd on this]

Quoted 11 lines · Alejandro Colomar
Quoted 9 lines

Hi,
As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all.  The manual page goes further and says "This function is obsolete. Do not use it." in its first lines. But, glibc doesn't seem to have deprecated this function at all. And it seems to be the most portable way to get a password, even if it's not in POSIX.
BSDs have readpassphrase(3), but glibc doesn't, so unless you recommend

Quoted 2 lines

OpenBSD also marks getpass(3) as obsolete and recommends readpassphrase(3):
<https://man.openbsd.org/getpass>

Simply not being familiar with that case: Is that suggestive of getpass(3) being bad to use in general, or a case where OpenBSD's deprecation of it makes sense holistically on that OS, but not necessarily elsewhere?

Just skimming the linked man pages it looks like OpenBSD might have deprecated it at least partly due to getpass() accepting a password on stdin.

Even within OpenBSD I wonder what that case means for software such as git. I.e. is it better to be portable and accept the same behavior on OpenBSD as elsewhere, or conform more closely to platform-specific conventions.

I haven't looked closely out our getpass() integration, maybe that's a moot point either way.

Quoted 9 lines
Quoted 9 lines

using readpassphrase(3) from libbsd, or plan to add it to glibc, I think getpass(3) should be the recommended function in Linux, and therefore we should remove the hard words against it. As a real example, git(1) uses getpass(3).
<https://github.com/git/git/blob/master/compat/terminal.c>
What are your thoughts?
Thanks,
Alex

Just while we've got some OpenBSD people CC'd (added the devel/git maintainers). I occasionally test git on OpenBSD myself (on the GCC farm), and we've got a few broken tests on the platform.

Looking at the ports source there's at least a couple of OpenBSD portability patches in there that would make sense to upstream.

So if that's easy for you or you're willing to submit them upstream we'd be happy to take them. Usually the only reason we haven't fixed things like that already is because nobody told us, and we're not actively looking into the local patches local packagers apply.

#7 lore raw
In reply to Alejandro Colomar (man-pages)
Quoted 11 lines · Alejandro Colomar
Quoted 10 lines · Alejandro Colomar

Hi,

As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all. The manual page goes further and says "This function is obsolete. Do not use it." in its first lines.

But, glibc doesn't seem to have deprecated this function at all. And it seems to be the most portable way to get a password, even if it's not in POSIX.

Quoted 16 lines
Quoted 2 lines

BSDs have readpassphrase(3), but glibc doesn't, so unless you recommend

OpenBSD also marks getpass(3) as obsolete and recommends readpassphrase(3):
<https://man.openbsd.org/getpass>

Quoted 9 lines

using readpassphrase(3) from libbsd, or plan to add it to glibc, I think
getpass(3) should be the recommended function in Linux, and therefore we should remove the hard words against it.

As a real example, git(1) uses getpass(3).
<https://github.com/git/git/blob/master/compat/terminal.c>

What are your thoughts?

getpass() is obsolete in POSIX.2. However, some platforms still are on POSIX.1, so replacing it instead of providing a configure detection/switch for it might cause issues.

-Randall

#3 lore raw
In reply to Ævar Arnfjörð Bjarmason

Hi Ævar,

Quoted 7 lines · Ævar Arnfjörð Bjarmason
Quoted 1 line · Alejandro Colomar (man-pages)

[Add a few CCs, since I mentioned them.]

[I'm not sure what the full context of this thread is, but just replying from the POV of git@ being CC'd on this]

The first message on this thread was mine from '10/29/21 13:15', so you've read it all.

The broader context is that I was trying to make the deprecation notices more consistent in the Linux manpages, by using the [[deprecated]] attribute where appropriate. While doing that, I found a few cases where the deprecation/obsoletion is not so clear to me, such as this one ([as]ctime[_r](3) is another one, since it is deprecated by POSIX, but not by the C standard, but I'll start a different thread with that; and isascii(3) is another one, since the user of it should know if the character set he's using is compatible with ascii, and in that case it's perfectly valid, it's only a case of garbage in garbage out, IMO).

Quoted 11 lines
Quoted 11 lines · Alejandro Colomar
Quoted 9 lines

Hi,
As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all.  The manual page goes further and says "This function is obsolete. Do not use it." in its first lines. But, glibc doesn't seem to have deprecated this function at all. And it seems to be the most portable way to get a password, even if it's not in POSIX.
BSDs have readpassphrase(3), but glibc doesn't, so unless you recommend

[...]

Cheers,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#8 lore raw
In reply to [email protected]
Quoted 1 line · On Fri, Oct 29, 2021 at 2:40 PM <[email protected]> wrote:

getpass() is obsolete in POSIX.2. However, some platforms still are on POSIX.1, so replacing it instead of providing a configure detection/switch for it might cause issues.

POSIX.2 is not a newer POSIX version, but rather a book (“Shell and utilities”) in pre-2001 standard revisions, and it has nothing to do with the system interfaces (that is POSIX.1).
And the only mention of getpass() in POSIX (at least, since the 2001's edition) indeed seems to be [1], in the list of functions that have not been carried forward from XSH5, the 1997 revision of “System Interfaces and Headers” (that is, SUSv2)[2], where it is inherited from SUSv1[4] from XPG[5] and, as Alejandro already mentioned, marked as obsolete, per XPG3 to XPG4 migration guide[6]; the previous, 1988, version of POSIX[3] does not mention getpass() at all.

[1] https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap01.html
[2] https://pubs.opengroup.org/onlinepubs/7908799/xsh/getpass.html
[3] https://mirror.math.princeton.edu/pub/oldlinux/download/c953.pdf
[4] https://pubs.opengroup.org/onlinepubs/9695969499/toc.pdf
[5] https://bitsavers.computerhistory.org/pdf/xOpen/X_Open_Portability_Guide_1985/xpg_2_xopen_system_v_specification_2.pdf
[6] http://archive.opengroup.org/publications/archive/CDROM/g501.pdf

Signature · 3 lines
Eugene Syromyatnikov
mailto:[email protected]
xmpp:esyr@jabber.{ru|org}
#9 lore raw
In reply to [email protected]
Quoted 24 lines · <[email protected]> wrote:
Quoted 23 lines · Alejandro Colomar
Quoted 6 lines · Alejandro Colomar

Hi,

As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all. The manual page goes further and says "This function is obsolete. Do not use it." in its first lines.

Quoted 6 lines

But, glibc doesn't seem to have deprecated this function at all. And it seems to be the most portable way to get a password, even if it's not in POSIX.

BSDs have readpassphrase(3), but glibc doesn't, so unless you recommend

OpenBSD also marks getpass(3) as obsolete and recommends readpassphrase(3):
<https://man.openbsd.org/getpass>

Quoted 5 lines

using readpassphrase(3) from libbsd, or plan to add it to glibc, I think
getpass(3) should be the recommended function in Linux, and therefore we should remove the hard words against it.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

As a real example, git(1) uses getpass(3).
<https://github.com/git/git/blob/master/compat/terminal.c>

What are your thoughts?

getpass() is obsolete in POSIX.2. However, some platforms still are on POSIX.1, so replacing it instead of providing a configure detection/switch for it might cause issues.

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past, and incorrectly led to readpassphrase(3)

readpassphrase(3) has a few too many features/extensions for my taste, but at least it is harder to abuse.

#10 lore raw
Quoted 33 lines · Theo de Raadt

Subject: Re: Is getpass(3) really obsolete?

Quoted 30 lines · <[email protected]> wrote:
Quoted 11 lines · Alejandro Colomar
Quoted 10 lines · Alejandro Colomar

Hi,

As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all. The manual page goes further and says "This function is obsolete. Do not use it." in its first lines.

But, glibc doesn't seem to have deprecated this function at all. And it seems to be the most portable way to get a password, even if it's not in POSIX.

Quoted 16 lines
Quoted 2 lines

BSDs have readpassphrase(3), but glibc doesn't, so unless you recommend

OpenBSD also marks getpass(3) as obsolete and recommends readpassphrase(3):
<https://man.openbsd.org/getpass>

Quoted 9 lines

using readpassphrase(3) from libbsd, or plan to add it to glibc, I think
getpass(3) should be the recommended function in Linux, and therefore we should remove the hard words against it.

As a real example, git(1) uses getpass(3).
<https://github.com/git/git/blob/master/compat/terminal.c>

What are your thoughts?

getpass() is obsolete in POSIX.2. However, some platforms still are on

POSIX.1,

Quoted 1 line

so replacing it instead of providing a configure detection/switch for it

might

Quoted 7 lines

cause issues.

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past, and incorrectly led to

readpassphrase(3)

Quoted 2 lines

readpassphrase(3) has a few too many features/extensions for my taste, but

at

Quoted 1 line

least it is harder to abuse.

readpassphrase is not generally supported. This will break builds on many platforms.

#11 lore raw
Quoted 11 lines · <[email protected]> wrote:
Quoted 1 line
Quoted 1 line

getpass() is obsolete in POSIX.2. However, some platforms still are on

POSIX.1,

Quoted 1 line

so replacing it instead of providing a configure detection/switch for it

might

Quoted 7 lines

cause issues.

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past, and incorrectly led to

Quoted 8 lines

readpassphrase(3)

Quoted 2 lines

readpassphrase(3) has a few too many features/extensions for my taste, but

at

Quoted 1 line

least it is harder to abuse.

readpassphrase is not generally supported. This will break builds on many platforms.

Of course moving forward takes a long time. If a better API is supplied then there is a choice in 10 years. If a better API is not supplied, then 10 years from now this conversation can get a reply.

#12 lore raw

October 29, 2031 10:21 AM, Theo de Raadt will write:

Quoted 20 lines
Quoted 18 lines · <[email protected]> wrote:
Quoted 2 lines
Quoted 2 lines

getpass() is obsolete in POSIX.2. However, some platforms still are on

POSIX.1,

Quoted 2 lines

so replacing it instead of providing a configure detection/switch for it

might

Quoted 7 lines

cause issues.

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past, and incorrectly led to

readpassphrase(3)

Quoted 3 lines

readpassphrase(3) has a few too many features/extensions for my taste, but

at

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

least it is harder to abuse.

readpassphrase is not generally supported. This will break builds on many platforms.

Of course moving forward takes a long time. If a better API is supplied then there is a choice in 10 years. If a better API is not supplied, then 10 years from now this conversation can get a reply.

I checked the API 10 years from now (check the above date) at it's still not there 😉 In the meantime, compatibility is important. I checked the latest release (last week's) on my platform and readpassphrase() is not available. Let's please put a compatibility layer in.

#13 lore raw

Hi Randall, Theo,

Quoted 17 lines · On 10/29/21 16:33, [email protected] wrote:

October 29, 2031 10:21 AM, Theo de Raadt will write:

Quoted 16 lines
Quoted 14 lines · <[email protected]> wrote:
Quoted 2 lines
Quoted 2 lines

getpass() is obsolete in POSIX.2. However, some platforms still are on

POSIX.1,

Quoted 2 lines

so replacing it instead of providing a configure detection/switch for it

might

Quoted 7 lines

cause issues.

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past, and incorrectly led to

readpassphrase(3)

That seems a good reason to keep the "Do not use it." note in the manual page. I think I'll add a recommendation for readpassphrase(3bsd) for the moment which is the only alternative available in Linux.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

readpassphrase(3) has a few too many features/extensions for my taste, but

at

Quoted 1 line

least it is harder to abuse.

readpassphrase is not generally supported. This will break builds on many platforms.

I found readpassphrase(3) in FreeBSD and OpenBSD.
It is also present in libbsd(7), which is available in most Linux distributions. I also found it on a Mac that I have access.

NetBSD has getpass_r(3) instead. It is not in any other system I have access.

Quoted 7 lines
Quoted 4 lines

Of course moving forward takes a long time. If a better API is supplied then there is a choice in 10 years. If a better API is not supplied, then 10 years from now this conversation can get a reply.

I checked the API 10 years from now (check the above date) at it's still not there 😉 In the meantime, compatibility is important. I checked the latest release (last week's) on my platform and readpassphrase() is not available. Let's please put a compatibility layer in.

libbsd(7) is probably the compatibility layer that you're looking for. What system are you on?

<https://libbsd.freedesktop.org/wiki/>

Cheers,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#15 lore raw
In reply to Theo de Raadt
Quoted 8 lines · Theo de Raadt
Quoted 7 lines · <[email protected]> wrote:
Quoted 6 lines · Alejandro Colomar
Quoted 5 lines · Alejandro Colomar

Hi,

As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all. The manual page goes further and says "This function is obsolete. Do not use it." in its first lines.

...

Quoted 4 lines

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past

I was about to post exactly the same thing. getpass(3) is not deprecated because there's a better replacement, it's deprecated because it's _unsafe_. The glibc implementation wraps getline(3) and therefore doesn't truncate the passphrase or overflow a fixed-size buffer, no matter how long the input is, but portable code cannot rely on that. And come to think of it, using getline(3) means that prefixes of the passphrase may be left lying around in malloc's free lists.

(getpass also cannot be made thread safe, due to recycling of a static buffer, but a program in which multiple threads are racing to prompt the user for passwords would be a UX disaster anyway, so I don't think that's a critical flaw the way it is for e.g. strtok(3).)

The Linux manpage project's documentation is, as I understand it, for Linux with glibc _first_, but not _only_; it should not describe this function as not-deprecated just because glibc has patched its worst problems and doesn't offer any better API.

Quoted 2 lines

readpassphrase(3) has a few too many features/extensions for my taste, but at least it is harder to abuse.

I am inclined to agree that readpassphrase has too many knobs, and I can't think of any legitimate present-day use for several of them, which is not a good property for an API handling security-critical data. Also, it relies on the caller to size the buffer for the passphrase, and therefore risks truncating people's passphrases.

With my libxcrypt hat on I've thought a bit about replacements for getpass. The conclusion I came to is that the easy changes are all putting lipstick on a pig, and if I was going to work on this at all I was going to design a privilege-separated authentication service that could be asked to take over a tty, read a passphrase, check it, and return just success or failure to the caller. Neither the passphrase itself, nor any strings derived from it, would ever be in the caller's address space. But this is obviously well out of scope for the C library.

zw

#14 lore raw
In reply to Alejandro Colomar (man-pages)
Quoted 45 lines · Alejandro Colomar
Quoted 17 lines · On 10/29/21 16:33, [email protected] wrote:

October 29, 2031 10:21 AM, Theo de Raadt will write:

Quoted 16 lines
Quoted 14 lines · <[email protected]> wrote:
Quoted 2 lines
Quoted 2 lines

getpass() is obsolete in POSIX.2. However, some platforms still are on

POSIX.1,

Quoted 2 lines

so replacing it instead of providing a configure detection/switch for it

might

Quoted 7 lines

cause issues.

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past, and incorrectly led to

readpassphrase(3)

That seems a good reason to keep the "Do not use it." note in the manual page. I think I'll add a recommendation for readpassphrase(3bsd) for the moment which is the only alternative available in Linux.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

readpassphrase(3) has a few too many features/extensions for my taste, but

at

Quoted 1 line

least it is harder to abuse.

readpassphrase is not generally supported. This will break builds on many platforms.

I found readpassphrase(3) in FreeBSD and OpenBSD.
It is also present in libbsd(7), which is available in most Linux distributions. I also found it on a Mac that I have access.

NetBSD has getpass_r(3) instead. It is not in any other system I have access.

Quoted 3 lines
Quoted 3 lines

Of course moving forward takes a long time. If a better API is supplied then there is a choice in 10 years. If a better API is not supplied, then 10 years

from

Quoted 3 lines
Quoted 1 line

now this conversation can get a reply.

I checked the API 10 years from now (check the above date) at it's still not

Quoted 8 lines

there 😉 In the meantime, compatibility is important. I checked the latest release (last week's) on my platform and readpassphrase() is not available. Let's please put a compatibility layer in.

Quoted 1 line

libbsd(7) is probably the compatibility layer that you're looking for. What system are you on?

<https://libbsd.freedesktop.org/wiki/>

I am on two variants (x86 and ia64) of HPE NonStop with current operating systems - and I do the build/test for git and OpenSSL. getpass() an alias to getpass2() but the other procs are not present. If this is going into git, I would suggest putting something into compat.c to abstract out the call. If it's there, we can handle it on a platform-by-platform basis.

Thanks,
Randall

#20 lore raw

PATCH 1/1 getpass.3: SYNOPSIS: Mark getpass() as [[deprecated]]

In reply to Alejandro Colomar (man-pages)

Suggest readpassphrase(3bsd) as an alternative.

See the long discussion in the mailing list for more details (link at the bottom of this commit message). I'll quote some relevant parts here:

Eugene Syromyatnikov <[email protected]>:
{
And the only mention of getpass() in POSIX (at least, since the 2001's edition) indeed seems to be [1], in the list of functions that have not been carried forward from XSH5, the 1997 revision of “System Interfaces and Headers” (that is, SUSv2)[2], where it is inherited from SUSv1[4] from XPG[5] and, as Alejandro already mentioned, marked as obsolete, per XPG3 to XPG4 migration guide[6]; the previous, 1988, version of POSIX[3] does not mention getpass() at all.

[1] https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap01.html
[2] https://pubs.opengroup.org/onlinepubs/7908799/xsh/getpass.html
[3] https://mirror.math.princeton.edu/pub/oldlinux/download/c953.pdf
[4] https://pubs.opengroup.org/onlinepubs/9695969499/toc.pdf
[5] https://bitsavers.computerhistory.org/pdf/xOpen/X_Open_Portability_Guide_1985/xpg_2_xopen_system_v_specification_2.pdf
[6] http://archive.opengroup.org/publications/archive/CDROM/g501.pdf
}

Theo de Raadt <[email protected]>:
{
The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past, and incorrectly led to readpassphrase(3).

readpassphrase(3) has a few too many features/extensions for my taste, but at least it is harder to abuse.
}

Alejandro Colomar <[email protected]>:
{
I found readpassphrase(3) in FreeBSD and OpenBSD. It is also present in libbsd(7), which is available in most Linux distributions. I also found it on a Mac that I have access.

NetBSD has getpass_r(3) instead. It is not in any other system I have access.
}

Zack Weinberg <[email protected]>:
{
I was about to post exactly the same thing. getpass(3) is not deprecated because there's a better replacement, it's deprecated because it's _unsafe_. The glibc implementation wraps getline(3) and therefore doesn't truncate the passphrase or overflow a fixed-size buffer, no matter how long the input is, but portable code cannot rely on that. And come to think of it, using getline(3) means that prefixes of the passphrase may be left lying around in malloc's free lists.

(getpass also cannot be made thread safe, due to recycling of a static buffer, but a program in which multiple threads are racing to prompt the user for passwords would be a UX disaster anyway, so I don't think that's a critical flaw the way it is for e.g. strtok(3).)

The Linux manpage project's documentation is, as I understand it, for Linux with glibc _first_, but not _only_; it should not describe this function as not-deprecated just because glibc has patched its worst problems and doesn't offer any better API.
}

List: linux-man <https://lore.kernel.org/linux-man/[email protected]/T/#t>

Cc OpenBSD <[email protected]>
Cc Ævar Arnfjörð Bjarmason <[email protected]>
Cc Benoit Lecocq <[email protected]>
Cc Klemens Nanni <[email protected]>
Cc Randall <[email protected]>
Cc Eugene Syromyatnikov <[email protected]>
Cc Theo de Raadt <[email protected]>
Cc Zack Weinberg <[email protected]>
Cc Florian Weimer <[email protected]>
---
 man3/getpass.3 | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
man3/getpass.3 +7 -2
index fa2031544..7d6da07fa 100644
@@ -28,7 +28,7 @@ getpass \- get a password
 .nf
 .B #include <unistd.h>
 .PP
-.BI "char *getpass(const char *" prompt );
+.BI "[[deprecated]] char *getpass(const char *" prompt );
 .fi
 .PP
 .RS -4
@@ -48,6 +48,7 @@ Feature Test Macro Requirements for glibc (see
 .SH DESCRIPTION
 This function is obsolete.
 Do not use it.
+See NOTES.
 If you want to read input without terminal echoing enabled,
 see the description of the
 .I ECHO
@@ -126,7 +127,11 @@ Removed in POSIX.1-2001.
 .\" are transmitted as part of the password.
 .\" Since libc 5.4.19 also line editing is disabled, so that also
 .\" backspace and the like will be seen as part of the password.
-.
+You should use instead
+.BR readpassphrase (3bsd),
+provided by
+.IR libbsd .
+.PP
 In the GNU C library implementation, if
 .I /dev/tty
 cannot be opened, the prompt is written to
Signature · 1 line
2.33.1
#4 lore raw
In reply to Alejandro Colomar (man-pages)
Quoted 6 lines · Alejandro Colomar (man-pages) via Libc-alpha

The broader context is that I was trying to make the deprecation notices more consistent in the Linux manpages, by using the [[deprecated]] attribute where appropriate. While doing that, I found a few cases where the deprecation/obsoletion is not so clear to me, such as this one ([as]ctime[_r](3) is another one, since it is deprecated by POSIX, but not by the C standard, but I'll start a different thread with that; and isascii(3) is

See the discussion of deprecation starting with
<https://sourceware.org/pipermail/libc-alpha/2021-May/126356.html> (C2X has also deprecated those functions). The comments in that thread supported marking the functions deprecated, but it needs someone to send a patch and I don't know what breakage might result in applications using those functions.

Signature · 2 lines
Joseph S. Myers
[email protected]
#21 lore raw
In reply to Alejandro Colomar (man-pages)
Quoted 2 lines · Alejandro Colomar (man-pages)
Quoted 2 lines

As a real example, git(1) uses getpass(3).
<https://github.com/git/git/blob/master/compat/terminal.c>

Sort of. It is the compile-time fallback of last resort. Most builds would use either termios with /dev/tty or a Windows-native equivalent.

You can see all the reasons we stopped using getpass() in the commit below.

-- >8 --
commit 21aeafceda2382d26bfa73a98ba45a937d65d77a

Author: Jeff King <[email protected]>
Date:   Sat Dec 10 05:41:01 2011 -0500

add generic terminal prompt function

When we need to prompt the user for input interactively, we want to access their terminal directly. We can't rely on stdio because it may be connected to pipes or files, rather than the terminal. Instead, we use "getpass()", because it abstracts the idea of prompting and reading from the terminal. However, it has some problems:

1. It never echoes the typed characters, which makes it OK for passwords but annoying for other input (like usernames).

2. Some implementations of getpass() have an extremely small input buffer (e.g., Solaris 8 is reported to support only 8 characters).

3. Some implementations of getpass() will fall back to reading from stdin (e.g., glibc). We explicitly don't want this, because our stdin may be connected to a pipe speaking a particular protocol, and reading will disrupt the protocol flow (e.g., the remote-curl helper).

4. Some implementations of getpass() turn off signals, so that hitting "^C" on the terminal does not break out of the password prompt. This can be a mild annoyance.

Instead, let's provide an abstract "git_terminal_prompt" function that addresses these concerns. This patch includes an implementation based on /dev/tty, enabled by setting HAVE_DEV_TTY. The fallback is to use getpass() as before.

#5 lore raw
In reply to Joseph Myers

Hi Joseph,

Quoted 9 lines · Joseph Myers
Quoted 6 lines · Alejandro Colomar (man-pages) via Libc-alpha

The broader context is that I was trying to make the deprecation notices more consistent in the Linux manpages, by using the [[deprecated]] attribute where appropriate. While doing that, I found a few cases where the deprecation/obsoletion is not so clear to me, such as this one ([as]ctime[_r](3) is another one, since it is deprecated by POSIX, but not by the C standard, but I'll start a different thread with that; and isascii(3) is

Quoted 7 lines

See the discussion of deprecation starting with
<https://sourceware.org/pipermail/libc-alpha/2021-May/126356.html> (C2X has also deprecated those functions). The comments in that thread supported marking the functions deprecated, but it needs someone to send a patch and I don't know what breakage might result in applications using those functions.

Thanks. The latest draft for C2x that I know of is N2596. Is there any newer draft that I can consult for these things? I see many proposals, but it's difficult to know which have been accepted and which not without an actual recent draft of the standard.

Cheers,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#6 lore raw
Quoted 9 lines · Alejandro Colomar (man-pages) via Libc-alpha
Quoted 6 lines

See the discussion of deprecation starting with
<https://sourceware.org/pipermail/libc-alpha/2021-May/126356.html> (C2X has also deprecated those functions). The comments in that thread supported marking the functions deprecated, but it needs someone to send a patch and I don't know what breakage might result in applications using those functions.

Thanks. The latest draft for C2x that I know of is N2596. Is there any newer draft that I can consult for these things? I see many proposals, but it's

The latest public draft is N2731, but there are still various accepted proposals not included in there, including N2566 (wording version 2) which I believe was accepted in October 2020 (I think issue 68 in the (private) C standard GitLab, for integrating that paper, has been incorrectly closed without integrating it).

Signature · 2 lines
Joseph S. Myers
[email protected]
#16 lore raw

readpassphrase(3) in glibc, and agetpass() (Was: Is getpass(3) really obsolete?)

In reply to Zack Weinberg

Hi Zack,

Quoted 21 lines · Zack Weinberg via Libc-alpha
Quoted 8 lines · Theo de Raadt
Quoted 7 lines · <[email protected]> wrote:
Quoted 6 lines · Alejandro Colomar
Quoted 5 lines · Alejandro Colomar

Hi,

As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all. The manual page goes further and says "This function is obsolete. Do not use it." in its first lines.

...

Quoted 4 lines

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past

I was about to post exactly the same thing. getpass(3) is not deprecated because there's a better replacement, it's deprecated because it's _unsafe_. The glibc implementation wraps getline(3) and therefore doesn't truncate the passphrase or overflow a fixed-size buffer, no matter how long the input is, but portable code cannot rely on that. And come to think of it, using getline(3) means that prefixes of the passphrase may be left lying around in malloc's free lists.

(getpass also cannot be made thread safe, due to recycling of a static buffer, but a program in which multiple threads are racing to prompt the user for passwords would be a UX disaster anyway, so I don't think that's a critical flaw the way it is for e.g. strtok(3).)

The Linux manpage project's documentation is, as I understand it, for Linux with glibc _first_, but not _only_; it should not describe this function as not-deprecated just because glibc has patched its worst problems and doesn't offer any better API.

Quoted 8 lines
Quoted 2 lines

readpassphrase(3) has a few too many features/extensions for my taste, but at least it is harder to abuse.

I am inclined to agree that readpassphrase has too many knobs, and I can't think of any legitimate present-day use for several of them, which is not a good property for an API handling security-critical data. Also, it relies on the caller to size the buffer for the passphrase, and therefore risks truncating people's passphrases.

With my libxcrypt hat on I've thought a bit about replacements for getpass. The conclusion I came to is that the easy changes are all putting lipstick on a pig, and if I was going to work on this at all I was going to design a privilege-separated authentication service that could be asked to take over a tty, read a passphrase, check it, and return just success or failure to the caller. Neither the passphrase itself, nor any strings derived from it, would ever be in the caller's address space. But this is obviously well out of scope for the C library.

zw

I happen to be working on replacing getpass(3) in shadow-utils. As there is no replacement in glibc, I'm making the code depend on libbsd on GNU systems.

I developed a function similar to getpass(3), but which allocates a buffer (similar to asprintf(3)). I only allocate once, and bail out if the password exceeds PASS_MAX, so no leaks in allocated memory (modulo bugs that I may have not noticed).

I also enforce both clearing and freeing the memory, by requiring a specific clean-up function.

The prototypes for the function and the clean-up are:

```
void erase_pass(char *p);
[[gnu::malloc(erase_pass)]] char *shdw_getpass(const char *prompt);

```

And the implementation is:

```
#include "prototypes.h"

#include <limits.h>
#include <readpassphrase.h>
#include <stdio.h>
#include <stdlib.h>


#if !defined(PASS_MAX)
#define PASS_MAX  BUFSIZ
#endif


char *
agetpass(const char *prompt)
{
	char    *p;
	size_t  len;

	p = malloc(PASS_MAX);
	if (p == NULL)
		return NULL;

	if (readpassphrase(prompt, p, PASS_MAX, 0) == NULL)
		goto fail;

	len = strlen(p);

	if (len == 0)
		return p;

	if (p[len - 1] != '\n')
		goto truncated;

	p[len - 1] = '\0';

	return p;

truncated:
	memzero(p, PASS_MAX);
fail:
	free(p);
	return NULL;
}


void
erase_pass(char *p)
{
	if (p != NULL)
		memzero(p, PASS_MAX);
	free(p);
}
```

Would you mind implementing readpassphrase(3) in glibc so that it's easier to use something safe and portable without resorting to compatibility libraries? Also, I'd like some review of this function, if you think the API could be improved. Maybe agetpass() would be a simple almost-drop-in replacement for getpass(3), so if you like it for glibc, let's discuss it.

I chose a predefined buffer size to not have to pass a buffer size all the time, which could be error-prone. I also allocated the buffer internally, to make it easier to replace getpass(3). It may be desirable to use existing buffers, and pass them through a pointer, but for shadow-utils, it was simpler to keep the getpass(3) API.

I don't know what was the practice with PASS_MAX regarding the NUL byte, but to avoid creating a buffer of a power of two plus one, I decided that the NUL byte would be within PASS_MAX. Another solution would be to declare PASS_MAX to be something like BUFSIZ-1, and then use PASS_MAX+1, but I opted for simplicity.

What are your thoughts?

Cheers,

Alex

#17 lore raw

Re: readpassphrase(3) in glibc, and agetpass() (Was: Is getpass(3) really obsolete?)

On 9/27/22 21:19, Alejandro Colomar wrote:
...

Quoted 6 lines

The prototypes for the function and the clean-up are:

```
void erase_pass(char *p);
[[gnu::malloc(erase_pass)]] char *shdw_getpass(const char *prompt);

I edited the function name for the email, and forgot to fix it here :)

s/shdw_/a/

Cheers,

Alex

Signature · 1 line
<http://www.alejandro-colomar.es/>
#18 lore raw

Re: readpassphrase(3) in glibc, and agetpass() (Was: Is getpass(3) really obsolete?)

In reply to Alejandro Colomar
Quoted 32 lines · Alejandro Colomar via Libc-alpha

Hi Zack,

Quoted 14 lines · Zack Weinberg via Libc-alpha
Quoted 8 lines · Theo de Raadt
Quoted 7 lines · <[email protected]> wrote:
Quoted 6 lines · Alejandro Colomar
Quoted 5 lines · Alejandro Colomar

Hi,

As the manual pages says, SUSv2 marked it as LEGACY, and POSIX doesn't have it at all. The manual page goes further and says "This function is obsolete. Do not use it." in its first lines.

...

Quoted 4 lines

The community finally had the balls to get rid of gets(3).

getpass(3) shares the same flaw, that the buffer size isn't passed. This has been an issue in the past

Quoted 8 lines

I was about to post exactly the same thing. getpass(3) is not deprecated because there's a better replacement, it's deprecated because it's _unsafe_. The glibc implementation wraps getline(3) and therefore doesn't truncate the passphrase or overflow a fixed-size buffer, no matter how long the input is, but portable code cannot rely on that. And come to think of it, using getline(3) means that prefixes of the passphrase may be left lying around in malloc's free lists. (getpass also cannot be made thread safe, due to recycling of a static buffer, but a program in which multiple threads are racing to prompt the user for passwords would be a UX disaster anyway, so I don't think that's a critical flaw the way it is for e.g. strtok(3).)
The Linux manpage project's documentation is, as I understand it, for Linux with glibc _first_, but not _only_; it should not describe this function as not-deprecated just because glibc has patched its worst problems and doesn't offer any better API.

Quoted 2 lines

readpassphrase(3) has a few too many features/extensions for my taste, but at least it is harder to abuse.

I am inclined to agree that readpassphrase has too many knobs, and I can't think of any legitimate present-day use for several of them, which is not a good property for an API handling security-critical data. Also, it relies on the caller to size the buffer for the passphrase, and therefore risks truncating people's passphrases.
With my libxcrypt hat on I've thought a bit about replacements for getpass. The conclusion I came to is that the easy changes are all putting lipstick on a pig, and if I was going to work on this at all I was going to design a privilege-separated authentication service that could be asked to take over a tty, read a passphrase, check it, and return just success or failure to the caller. Neither the passphrase itself, nor any strings derived from it, would ever be in the caller's address space. But this is obviously well out of scope for the C library. zw

I happen to be working on replacing getpass(3) in shadow-utils. As there is no replacement in glibc, I'm making the code depend on libbsd on GNU systems.

Quoted 9 lines

I developed a function similar to getpass(3), but which allocates a buffer (similar to asprintf(3)). I only allocate once, and bail out if the password exceeds PASS_MAX, so no leaks in allocated memory (modulo bugs that I may have not noticed).

I also enforce both clearing and freeing the memory, by requiring a specific clean-up function.

The prototypes for the function and the clean-up are:

[snip]
Would you mind implementing readpassphrase(3) in glibc so that it's easier to use something safe and portable without resorting to compatibility libraries? Also, I'd like some review of this function, if you think the API could be improved. Maybe agetpass() would be a simple almost-drop-in replacement for getpass(3), so if you like it for glibc, let's discuss it.

I assume it'd be libxcrypt instead?

Best,
sam

#19 lore raw

Re: readpassphrase(3) in glibc, and agetpass()

In reply to Alejandro Colomar
Quoted 17 lines · Alejandro Colomar

I happen to be working on replacing getpass(3) in shadow-utils. As there is no replacement in glibc, I'm making the code depend on libbsd on GNU systems.
...
Would you mind implementing readpassphrase(3) in glibc so that it's easier to use something safe and portable without resorting to compatibility libraries? Also, I'd like some review of this function, if you think the API could be improved. Maybe agetpass() would be a simple almost-drop-in replacement for getpass(3), so if you like it for glibc, let's discuss it.

I chose a predefined buffer size to not have to pass a buffer size all the time, which could be error-prone. I also allocated the buffer internally, to make it easier to replace getpass(3). It may be desirable to use existing buffers, and pass them through a pointer, but for shadow-utils, it was simpler to keep the getpass(3) API.

Quoted 5 lines

I don't know what was the practice with PASS_MAX regarding the NUL byte, but to avoid creating a buffer of a power of two plus one, I decided that the NUL byte would be within PASS_MAX. Another solution would be to declare PASS_MAX to be something like BUFSIZ-1, and then use PASS_MAX+1, but I opted for simplicity.

I understand that in an old thread last fall we were CC'ed only because we are one of the users of getpass(3), but could you take us out of the CC list, please? We'd migrate, if getpass() starts being unusable for us, away from it and choose an alternative, but until then [email protected] is a good place to discuss this.

Thanks.