Merge pull request #354 from ikerexxe/useradd_resource_leak

useradd.c: fix covscan RESOURCE_LEAK
This commit is contained in:
Serge Hallyn 2021-06-22 09:22:09 -05:00 committed by GitHub
commit a026ea6bbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1964,16 +1964,26 @@ static void faillog_reset (uid_t uid)
memzero (&fl, sizeof (fl)); memzero (&fl, sizeof (fl));
fd = open (FAILLOG_FILE, O_RDWR); fd = open (FAILLOG_FILE, O_RDWR);
if ( (-1 == fd) if (-1 == fd) {
|| (lseek (fd, offset_uid, SEEK_SET) != offset_uid) fprintf (stderr,
_("%s: failed to open the faillog file for UID %lu: %s\n"),
Prog, (unsigned long) uid, strerror (errno));
SYSLOG ((LOG_WARN, "failed to open the faillog file for UID %lu", (unsigned long) uid));
return;
}
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (write (fd, &fl, sizeof (fl)) != (ssize_t) sizeof (fl)) || (write (fd, &fl, sizeof (fl)) != (ssize_t) sizeof (fl))
|| (fsync (fd) != 0) || (fsync (fd) != 0)) {
|| (close (fd) != 0)) {
fprintf (stderr, fprintf (stderr,
_("%s: failed to reset the faillog entry of UID %lu: %s\n"), _("%s: failed to reset the faillog entry of UID %lu: %s\n"),
Prog, (unsigned long) uid, strerror (errno)); Prog, (unsigned long) uid, strerror (errno));
SYSLOG ((LOG_WARN, "failed to reset the faillog entry of UID %lu", (unsigned long) uid)); SYSLOG ((LOG_WARN, "failed to reset the faillog entry of UID %lu", (unsigned long) uid));
/* continue */ }
if (close (fd) != 0) {
fprintf (stderr,
_("%s: failed to close the faillog file for UID %lu: %s\n"),
Prog, (unsigned long) uid, strerror (errno));
SYSLOG ((LOG_WARN, "failed to close the faillog file for UID %lu", (unsigned long) uid));
} }
} }
@ -1997,17 +2007,29 @@ static void lastlog_reset (uid_t uid)
memzero (&ll, sizeof (ll)); memzero (&ll, sizeof (ll));
fd = open (LASTLOG_FILE, O_RDWR); fd = open (LASTLOG_FILE, O_RDWR);
if ( (-1 == fd) if (-1 == fd) {
|| (lseek (fd, offset_uid, SEEK_SET) != offset_uid) fprintf (stderr,
_("%s: failed to open the lastlog file for UID %lu: %s\n"),
Prog, (unsigned long) uid, strerror (errno));
SYSLOG ((LOG_WARN, "failed to open the lastlog file for UID %lu", (unsigned long) uid));
return;
}
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|| (write (fd, &ll, sizeof (ll)) != (ssize_t) sizeof (ll)) || (write (fd, &ll, sizeof (ll)) != (ssize_t) sizeof (ll))
|| (fsync (fd) != 0) || (fsync (fd) != 0)) {
|| (close (fd) != 0)) {
fprintf (stderr, fprintf (stderr,
_("%s: failed to reset the lastlog entry of UID %lu: %s\n"), _("%s: failed to reset the lastlog entry of UID %lu: %s\n"),
Prog, (unsigned long) uid, strerror (errno)); Prog, (unsigned long) uid, strerror (errno));
SYSLOG ((LOG_WARN, "failed to reset the lastlog entry of UID %lu", (unsigned long) uid)); SYSLOG ((LOG_WARN, "failed to reset the lastlog entry of UID %lu", (unsigned long) uid));
/* continue */ /* continue */
} }
if (close (fd) != 0) {
fprintf (stderr,
_("%s: failed to close the lastlog file for UID %lu: %s\n"),
Prog, (unsigned long) uid, strerror (errno));
SYSLOG ((LOG_WARN, "failed to close the lastlog file for UID %lu", (unsigned long) uid));
/* continue */
}
} }
static void tallylog_reset (const char *user_name) static void tallylog_reset (const char *user_name)
@ -2254,6 +2276,7 @@ static void create_home (void)
} }
cp = strtok (NULL, "/"); cp = strtok (NULL, "/");
} }
free (bhome);
(void) chown (prefix_user_home, user_id, user_gid); (void) chown (prefix_user_home, user_id, user_gid);
mode_t mode = getdef_num ("HOME_MODE", mode_t mode = getdef_num ("HOME_MODE",