security: fix missing fdopen() NULL check in conf.c

Memory Safety: Medium

In pw_conf_save_state(), the return value of fdopen() was not checked
for NULL. If fdopen() fails, subsequent fprintf() and fclose() calls
would operate on a NULL FILE pointer, causing a crash. Additionally,
the file descriptor would be leaked since fclose() would not be called.

Added a NULL check after fdopen() that closes the raw fd and returns
an error on failure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Wim Taymans 2026-04-23 17:15:04 +02:00
parent 6798f591bd
commit 05bcfa7a2a

View file

@ -368,6 +368,11 @@ int pw_conf_save_state(const char *prefix, const char *name, const struct pw_pro
} }
f = fdopen(fd, "w"); f = fdopen(fd, "w");
if (f == NULL) {
res = -errno;
close(fd);
return res;
}
fprintf(f, "{"); fprintf(f, "{");
count += pw_properties_serialize_dict(f, &conf->dict, PW_PROPERTIES_FLAG_NL); count += pw_properties_serialize_dict(f, &conf->dict, PW_PROPERTIES_FLAG_NL);
fprintf(f, "%s}", count == 0 ? " " : "\n"); fprintf(f, "%s}", count == 0 ? " " : "\n");