google / google/libnss-cache

nss_cache getpwent loop runs forever if getpwnam in body

Open
#85 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
63
Forks
29
Avg merge
3m
Merged PRs (30d)
4

Description

(Note: This is a copy of a Google-internal issue written by Stan Shebs, who's now retired; unfortunately I don't have additional context.)

Calling `getpwnam()` inside of a `getpwent()` loop causes it to run forever, if `nss_cache` is in use:

```
while ((pw = getpwent()) != NULL)
{
pw2 = getpwnam("anyname");
}
```

The problem is that `getpwnam` closes `/etc/passwd.cache`, so `getpwent()` then has to open and start reading from the
beginning again. The `nss_files` module has a bit of special code to prevent this, `nss_cache` will need something like it.

----

The attached file takes an argument, so you can see "bad files" work, and "bad cache" hang:
```
#include
#include

int
main(int argc, char **argv)
{
char *serviceline = "cache";
struct passwd *pw, *pw2;

if (argc > 1)
serviceline = argv[1];

__nss_configure_lookup("passwd", serviceline);

setpwent();

while ((pw = getpwent()) != NULL)
{
pw2 = getpwnam("anyname");
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.