Bug 3544 - getgroups() makes a useless system call
Summary: getgroups() makes a useless system call
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.3.6
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-11-20 16:44 UTC by Bruno Haible
Modified: 2016-05-08 14:07 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Last reconfirmed:
fweimer: security-


Attachments
fix (579 bytes, patch)
2006-11-20 16:46 UTC, Bruno Haible
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Bruno Haible 2006-11-20 16:44:59 UTC
glibc-2.3.6 on Linux-2.4.x/i386 makes a useless system call when the
getgroups() function is called.

Test program:

===================================================================
#include <sys/types.h>
#include <unistd.h>
int main ()
{
  gid_t buf[10];
  getgroups (10, buf);
  return 0;
}
===================================================================

strace shows:

open("/proc/sys/kernel/ngroups_max", O_RDONLY) = -1 ENOENT (No such file or 
directory)
getgroups32(0xa, 0xbfffe4c0)            = 3

This open() call, coming from a __sysconf() call, is useless, because the
getgroups32 system call exists; there is no need to allocate a kernel_groups[]
array since there is no translation to do.

Proposed fix (untested):


2006-11-18  Bruno Haible  <bruno@clisp.org>

	* sysdeps/unix/sysv/linux/i386/getgroups.c (__getgroups): Invoke
	__sysconf only after having tried to call getgroups32.

--- sysdeps/unix/sysv/linux/i386/getgroups.c.bak	2003-09-03 
05:21:27.000000000 +0200
+++ sysdeps/unix/sysv/linux/i386/getgroups.c	2006-11-18 15:35:59.000000000 
+0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
+/* Copyright (C) 1997, 1998, 2000, 2001, 2003, 2006 Free Software Foundation, 
Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -52,8 +52,6 @@
     }
   else
     {
-      int i, ngids;
-      __kernel_gid_t kernel_groups[n = MIN (n, __sysconf (_SC_NGROUPS_MAX))];
 # ifdef __NR_getgroups32
       if (__libc_missing_32bit_uids <= 0)
 	{
@@ -69,6 +67,9 @@
 	}
 # endif /* __NR_getgroups32 */
 
+      int i, ngids;
+      __kernel_gid_t kernel_groups[n = MIN (n, __sysconf (_SC_NGROUPS_MAX))];
+
       ngids = INLINE_SYSCALL (getgroups, 2, n, CHECK_N (kernel_groups, n));
       if (n != 0 && ngids > 0)
 	for (i = 0; i < ngids; i++)
Comment 1 Bruno Haible 2006-11-20 16:46:18 UTC
Created attachment 1428 [details]
fix
Comment 2 Ulrich Drepper 2006-11-20 22:47:36 UTC
Applied to trunk.