[v4] grantpt: Get rid of alloca

Message ID 20230613191631.1080455-1-josimmon@redhat.com
State Committed
Commit 01dd2875f85213b26beefb66caad3564da89d1d1
Headers
Series [v4] grantpt: Get rid of alloca |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed

Commit Message

Joe Simmons-Talbott June 13, 2023, 7:16 p.m. UTC
  Replace alloca with a scratch_buffer to avoid potential stack overflows.
---
Changes since v3:
  * Explicitly set the retval for scratch_buffer_set_array_size failure.

 sysdeps/unix/grantpt.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
  

Comments

Samuel Thibault June 17, 2023, 11:08 p.m. UTC | #1
Applied, thanks!

Joe Simmons-Talbott, le mar. 13 juin 2023 15:16:31 -0400, a ecrit:
> Replace alloca with a scratch_buffer to avoid potential stack overflows.
> ---
> Changes since v3:
>   * Explicitly set the retval for scratch_buffer_set_array_size failure.
> 
>  sysdeps/unix/grantpt.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/sysdeps/unix/grantpt.c b/sysdeps/unix/grantpt.c
> index 38fce52576..226e7adb75 100644
> --- a/sysdeps/unix/grantpt.c
> +++ b/sysdeps/unix/grantpt.c
> @@ -20,6 +20,7 @@
>  #include <fcntl.h>
>  #include <grp.h>
>  #include <limits.h>
> +#include <scratch_buffer.h>
>  #include <stdlib.h>
>  #include <string.h>
>  #include <sys/resource.h>
> @@ -147,10 +148,19 @@ grantpt (int fd)
>  	/* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
>  	   Try a moderate value.  */
>  	grbuflen = 1024;
> -      grtmpbuf = (char *) __alloca (grbuflen);
> +      struct scratch_buffer sbuf;
> +      scratch_buffer_init (&sbuf);
> +      if (!scratch_buffer_set_array_size (&sbuf, 1, grbuflen))
> +	{
> +	  retval = -1;
> +	  goto cleanup;
> +	}
> +      grtmpbuf = sbuf.data;
>        __getgrnam_r (TTY_GROUP, &grbuf, grtmpbuf, grbuflen, &p);
>        if (p != NULL)
>  	tty_gid = p->gr_gid;
> +
> +      scratch_buffer_free(&sbuf);
>      }
>    gid_t gid = tty_gid == -1 ? __getgid () : tty_gid;
>  
> -- 
> 2.39.2
>
  

Patch

diff --git a/sysdeps/unix/grantpt.c b/sysdeps/unix/grantpt.c
index 38fce52576..226e7adb75 100644
--- a/sysdeps/unix/grantpt.c
+++ b/sysdeps/unix/grantpt.c
@@ -20,6 +20,7 @@ 
 #include <fcntl.h>
 #include <grp.h>
 #include <limits.h>
+#include <scratch_buffer.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/resource.h>
@@ -147,10 +148,19 @@  grantpt (int fd)
 	/* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
 	   Try a moderate value.  */
 	grbuflen = 1024;
-      grtmpbuf = (char *) __alloca (grbuflen);
+      struct scratch_buffer sbuf;
+      scratch_buffer_init (&sbuf);
+      if (!scratch_buffer_set_array_size (&sbuf, 1, grbuflen))
+	{
+	  retval = -1;
+	  goto cleanup;
+	}
+      grtmpbuf = sbuf.data;
       __getgrnam_r (TTY_GROUP, &grbuf, grtmpbuf, grbuflen, &p);
       if (p != NULL)
 	tty_gid = p->gr_gid;
+
+      scratch_buffer_free(&sbuf);
     }
   gid_t gid = tty_gid == -1 ? __getgid () : tty_gid;