From: Joe Simmons-Talbott <josimmon@redhat.com>
To: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] getpw: Get rid of alloca
Date: Mon, 28 Aug 2023 16:37:56 -0400 [thread overview]
Message-ID: <20230828203756.GA3849957@oak> (raw)
In-Reply-To: <0dfcb736-7948-55bc-a6d4-e715b39d5fee@linaro.org>
On Mon, Aug 28, 2023 at 02:01:54PM -0300, Adhemerval Zanella Netto wrote:
>
>
> On 07/07/23 17:04, Joe Simmons-Talbott via Libc-alpha wrote:
> > Use a scratch_buffer rather than alloca to avoid potential stack
> > overflow.
> > ---
> > pwd/getpw.c | 34 +++++++++++++++++++++++++++-------
> > 1 file changed, 27 insertions(+), 7 deletions(-)
> >
> > diff --git a/pwd/getpw.c b/pwd/getpw.c
> > index cf747374b8..7a27d79910 100644
> > --- a/pwd/getpw.c
> > +++ b/pwd/getpw.c
> > @@ -15,8 +15,8 @@
> > License along with the GNU C Library; if not, see
> > <https://www.gnu.org/licenses/>. */
> >
> > -#include <alloca.h>
> > #include <errno.h>
> > +#include <scratch_buffer.h>
> > #include <stdio.h>
> > #include <unistd.h>
> > #include <pwd.h>
> > @@ -34,28 +34,48 @@ __getpw (__uid_t uid, char *buf)
> > size_t buflen;
> > char *tmpbuf;
> > struct passwd resbuf, *p;
> > + int retval = 0;
> > + struct scratch_buffer sbuf;
> > + scratch_buffer_init (&sbuf);
> >
> > if (buf == NULL)
> > {
> > __set_errno (EINVAL);
> > - return -1;
> > + retval = -1;
> > + goto error_out;
> > }
> >
>
> There is no need to call scratch_buffer_free here. You can move the
> scratch_buffer initialization later.
>
> > buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
> > - tmpbuf = alloca (buflen);
> > + if (!scratch_buffer_set_array_size (&sbuf, 1, buflen))
>
> The _SC_GETPW_R_SIZE_MAX will be always NSS_BUFLEN_PASSWD so there is no need to
> a scratch_buffer here (similar to sysdeps/posix/cuserid.c assumption). Since the
> functions is historical tricky to be used correctly, I think it should continue to
> fail with passwords larger than _SC_GETPW_R_SIZE_MAX.
I removed the scratch_buffer in v2 and used a fixed sized array.
Thanks,
Joe
>
> > + {
> > + retval = -1;
> > + goto error_out;
> > + }
> > + tmpbuf = sbuf.data;
> >
> > if (__getpwuid_r (uid, &resbuf, tmpbuf, buflen, &p) != 0)
> > - return -1;
> > + {
> > + retval = -1;
> > + goto error_out;
> > + }
> >
> > if (p == NULL)
> > - return -1;
> > + {
> > + retval = -1;
> > + goto error_out;
> > + }
> >
> > if (sprintf (buf, "%s:%s:%lu:%lu:%s:%s:%s", p->pw_name, p->pw_passwd,
> > (unsigned long int) p->pw_uid, (unsigned long int) p->pw_gid,
> > p->pw_gecos, p->pw_dir, p->pw_shell) < 0)
> > - return -1;
> > + {
> > + retval = -1;
> > + goto error_out;
> > + }
> >
> > - return 0;
> > +error_out:
> > + scratch_buffer_free (&sbuf);
> > + return retval;
> > }
> > weak_alias (__getpw, getpw)
> >
>
prev parent reply other threads:[~2023-08-28 20:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-07 20:04 Joe Simmons-Talbott
2023-08-10 13:45 ` Joe Simmons-Talbott
2023-08-28 13:21 ` Joe Simmons-Talbott
2023-08-28 17:01 ` Adhemerval Zanella Netto
2023-08-28 20:37 ` Joe Simmons-Talbott [this message]
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=20230828203756.GA3849957@oak \
--to=josimmon@redhat.com \
--cc=adhemerval.zanella@linaro.org \
--cc=libc-alpha@sourceware.org \
/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).