From: ebiederm@xmission.com (Eric W. Biederman)
To: linux-hotplug@vger.kernel.org
Subject: [PATCH] fix util_lookup_group to handle large groups
Date: Sat, 01 Aug 2009 05:26:10 +0000 [thread overview]
Message-ID: <m1skgcjea5.fsf@fess.ebiederm.org> (raw)
I have recently been getting the above message on fc11 and
I have traced it down to a bug in util_lookup_group.
As of 145 util_lookup_group reads:
gid_t util_lookup_group(struct udev *udev, const char *group)
{
char *endptr;
int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
char buf[buflen];
struct group grbuf;
struct group *gr;
gid_t gid = 0;
if (strcmp(group, "root") = 0)
return 0;
gid = strtoul(group, &endptr, 10);
if (endptr[0] = '\0')
return gid;
errno = 0;
getgrnam_r(group, &grbuf, buf, buflen, &gr);
if (gr != NULL)
return gr->gr_gid;
if (errno = 0 || errno = ENOENT || errno = ESRCH)
err(udev, "specified group '%s' unknown\n", group);
else
err(udev, "error resolving group '%s': %m\n", group);
return 0;
}
The errno value from getgrnam_r here is ERANGE which is documented as
"Insufficient buffer space supplied".
When I call get getgrnam_r with a large enough buffer everything
works. Indicating that the problem is that sysconf is returning
a value too small.
A quick google search tells me that sysconf(_S_GETGR_R_SIZE_MAX)
is documented as:
> sysconf(_S_GETGR_R_SIZE_MAX) returns either -1 or a good
> suggested starting value for buflen. It does not return the
> worst case possible for buflen.
In my case I have a group with about 50 users in /etc/group
and that is what triggered the problem in udev and caused
all of the udevs group lookups to fail.
The following patch which dynamically allocates the group member buffer
should fix this problem.
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
---
--- udev-145/libudev/libudev-util-private.c-orig 2009-07-31 22:03:54.000000000 -0700
+++ udev-145/libudev/libudev-util-private.c 2009-07-31 22:23:21.000000000 -0700
@@ -149,8 +149,8 @@
gid_t util_lookup_group(struct udev *udev, const char *group)
{
char *endptr;
- int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
- char buf[buflen];
+ int buflen;
+ char *buf;
struct group grbuf;
struct group *gr;
gid_t gid = 0;
@@ -161,15 +161,31 @@
if (endptr[0] = '\0')
return gid;
- errno = 0;
- getgrnam_r(group, &grbuf, buf, buflen, &gr);
- if (gr != NULL)
- return gr->gr_gid;
- if (errno = 0 || errno = ENOENT || errno = ESRCH)
- err(udev, "specified group '%s' unknown\n", group);
- else
- err(udev, "error resolving group '%s': %m\n", group);
- return 0;
+ buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (buflen < 0)
+ buflen = 1000;
+ buf = NULL;
+ gid = 0;
+ for (;;) {
+ buf = realloc(buf, buflen);
+ if (!buf)
+ break;
+ errno = 0;
+ getgrnam_r(group, &grbuf, buf, buflen, &gr);
+ if (gr != NULL)
+ gid = gr->gr_gid;
+ else if (errno = ERANGE) {
+ buflen *= 2;
+ continue;
+ }
+ else if (errno = 0 || errno = ENOENT || errno = ESRCH)
+ err(udev, "specified group '%s' unknown\n", group);
+ else
+ err(udev, "error resolving group '%s': %m\n", group);
+ break;
+ }
+ free(buf);
+ return gid;
}
/* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
next reply other threads:[~2009-08-01 5:26 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-01 5:26 Eric W. Biederman [this message]
2009-08-01 12:36 ` [PATCH] fix util_lookup_group to handle large groups Alan Jenkins
2009-08-01 13:43 ` Kay Sievers
2009-08-24 16:50 ` Ansgar Johannes Pflipsen
2009-08-24 17:50 ` Lennart Poettering
2009-08-25 12:52 ` Ansgar Johannes Pflipsen
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=m1skgcjea5.fsf@fess.ebiederm.org \
--to=ebiederm@xmission.com \
--cc=linux-hotplug@vger.kernel.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 NNTP newsgroup(s).